<?php
/**********************************************************************
***********************************************************************/



function confirm_selected_records($ps_id)
{
    $a_result = get_production_schedule_details_for_confirmation($ps_id);
    $any_selected = false;
    $user_id = $_SESSION['wa_current_user']->user;
    $wos = array();
    $prs = array();
    
    while ($a_row = db_fetch($a_result))
    {
        if((isset($_POST["selected_".$a_row["id"]])) &&
                ($_POST["selected_".$a_row["id"]] == true))
        {
            $any_selected = true;
            mark_production_schedule_detail($a_row["id"], 1, get_post("row_state_".$a_row["id"]), $user_id);
            build_confirm_production_schedule_arrays($a_row, $wos, $prs);
        }
        else{
            mark_production_schedule_detail($a_row["id"], 0, get_post("row_state_".$a_row["id"]), $user_id);
        }       
    }
    
    if($any_selected){        
        confirm_production_schedule_workorders($ps_id);
        confirm_production_schedule_purchase_requisitions($ps_id, $user_id);
        display_notification(_("Selected orders have been confirmed."));
    }
    else{
        display_error(_("No orders have been selected."));
    }
}

function build_confirm_production_schedule_arrays($a_row, &$wos, &$prs)
{
    if($a_row['tran_type'] == ST_WORKORDER){
        if($a_row['trans_no'] == 0){
            $wos[] = $a_row;
        }
    }
    if($a_row['tran_type'] == ST_PURCH_REQ){
        if($a_row['trans_no'] == 0){
            $prs[] = $a_row;
        }
    }
}


function confirm_production_schedule_workorders(&$ps_id)
{
    $wo_msg = array();
    
    $res_psd = get_production_schedule_details_for_workorders($ps_id);
    while ($psd = db_fetch($res_psd)) {
        $next_wo_no = get_next_trans_no(ST_WORKORDER);
        if($next_wo_no <= 0){
            $next_wo_no = 1;
        }
        $psd['trans_no'] = $next_wo_no;
        $psd['user_id'] = $_SESSION['wa_current_user']->user;
        if($psd['source_type'] == ST_MANUF_REQUIREMENT){
            $mreq = get_manufacturing_requirement_by_id($psd['source_id']);
            if($mreq['order_no'] > 0){
                continue;
            }
            else{
                begin_transaction();
                $umr_ono = update_manufacturing_requirement_order_no($mreq['id'], $psd['tran_type'], $psd['trans_no'], $mreq['completed'], $mreq['row_state']);
                if($umr_ono["success"] !== true){
                    cancel_transaction();
                    return $umr_ono;
                }
                $uswno = update_scheduled_workorder_number($psd);
                if($uswno["success"] !== true){
                    cancel_transaction();
                    return $uswno;
                }
                commit_transaction();
            }
        }
        elseif($psd['source_type'] == ST_WORKORDER){
            begin_transaction();
            $uswno = update_scheduled_workorder_number($psd);
            if($uswno["success"] !== true){
                cancel_transaction();
                return $uswno;
            }
            commit_transaction();
        }
    }
}


function update_scheduled_workorder_number($wo)
{
    global $Refs;
    
    $wo_reference = $Refs->get_next(ST_WORKORDER); 
    if($Refs->exists(ST_WORKORDER, $wo_reference)){
        return array("success" => false, "tran_no" => $wo['scheduled_trans_no'], "message" => "Generated next reference for workorder ".$wo['scheduled_trans_no']." already exists");
    }
    $uwo_no = update_scheduled_workorder_wo_no($wo['scheduled_trans_no'], $wo['trans_no'], $wo_reference);
    if($uwo_no["success"] !== true){
        return $uwo_no;
    }
    else{
        $Refs->save(ST_WORKORDER, $wo['trans_no'], $wo_reference); 
        update_scheduled_workorder_process_wo_no($wo['scheduled_trans_no'], $wo['trans_no']);
        update_scheduled_workorder_material_wo_no($wo['scheduled_trans_no'], $wo['trans_no']);
        $upsd_ono = update_production_schedule_detail_order_no($wo['id'], $wo['trans_no'],$wo['row_state']);
        if($upsd_ono["success"] !== true){
            return $upsd_ono;
        }
        update_production_schedule_secondary_wo_no($wo['id'], $wo['scheduled_trans_no'], $wo['trans_no']);
    }
    
    return array("success" => true, "message" => "");
}


function confirm_production_schedule_purchase_requisitions($ps_id, $user_id)
{
    $res_pr_summary = get_production_schedule_details_summary_for_purch_reqs($ps_id);
    while ($pr_summary = db_fetch($res_pr_summary)) {
        $pr_cart = new items_cart(ST_MATERIAL_ISSUE);
        $cc = create_production_schedule_purch_req_cart($pr_cart, $pr_summary, $user_id);
        if($cc["success"] !== true){
            unset($pr_cart);
            return $cc;
        }
        
        $pr_cart->total = calculate_pr_total($pr_cart);
        begin_transaction();
        $cpspr = create_production_schedule_purch_req($pr_cart);
        if($cpspr["success"] !== true){
            cancel_transaction();
            unset($pr_cart);
            return $cpspr;
        }
        else{
            foreach ($pr_cart->line_items as $spr_line) {
                $upsdon = update_production_schedule_detail_order_no($spr_line->psd_line_id, $cpspr["tran_no"], $spr_line->psd_line_row_state);                
                if($upsdon["success"] !== true){
                    cancel_transaction();
                    unset($pr_cart);
                    return $upsdon;
                }
                if($spr_line->mreq_line_id > 0){
                    $umron = update_manufacturing_requirement_order_no($spr_line->mreq_line_id, ST_PURCH_REQ, $cpspr["tran_no"], 1, $spr_line->mreq_line_row_state);
                    if($umron["success"] !== true){
                        cancel_transaction();
                        unset($pr_cart);
                        return $umron;
                    }
                }
            }
            commit_transaction();
            unset($pr_cart);
        }
    }
}



function create_production_schedule_purch_req_cart(&$spr, $pr_summary, $user_id)
{
    $res_ps_details = get_production_schedule_details_for_purch_req($pr_summary['project_id'], $pr_summary['loc_code']);
    while ($ps_detail = db_fetch($res_ps_details)) {
        if($ps_detail["mreq_order_no"] > 0){
            continue;
        }
        if(count($spr->line_items) == 0){
            create_production_schedule_pr_cart_header($spr, $ps_detail, $user_id);
        }
        $aprl = add_production_schedule_pr_cart_line($spr, $ps_detail);
        if($aprl["success"] !== true){
            return $aprl;
        }
    }  
    
    return array("success" => true, "meaasge" => "");
    
}




function create_production_schedule_purch_req($pr)
{
    
    $vpid = validate_pr_item_data($pr);
    
    if($vpid["success"] !== true){
        return $vpid;
    }
    else{
        $apr_msg = add_pr($pr);
        if($apr_msg['success'] !== true){
            //cancel_transaction();
            cancel_transaction();
            return $apr_msg;
        }
        else{
            return $apr_msg;
        }
    }
    
    $pr->clear_items();
    unset($pr);  
}




function add_production_schedule_pr_cart_line($spr, $ps_detail)
{
    build_pr_line_array($ps_detail, $pr_line);
    $cprl = check_pr_line_data($pr_line);
    if($cprl["success"] !== true){
        return $cprl;
    }
    
    $spr->add_to_cart(count($spr->line_items), $ps_detail["stock_id"], $ps_detail["quantity"], 
                    null, null, null, sql2date($ps_detail["required_by"]), 
                    $ps_detail["comment"], 0, 0, 0,
                    0, $pr_line["estimate_price"], $ps_detail["loc_code"], 0,null, null,null, null, null,
                    $ps_detail["tran_unit"], $ps_detail["tran_qty"], $ps_detail["conversion_factor"]);

    $current_element = (count($spr->line_items)-1);
    
    $spr->line_items[$current_element]->task_id = $ps_detail["task_id"];
    $spr->line_items[$current_element]->task_description = "";
    
    $spr->line_items[$current_element]->project_site_id = 0;
    $spr->line_items[$current_element]->project_site_description = "";
    
    $spr->line_items[$current_element]->psd_line_id = $ps_detail["id"];
    $spr->line_items[$current_element]->psd_line_row_state = $ps_detail["row_state"];
    
    if($ps_detail["source_type"] == ST_MANUF_REQUIREMENT){
        $spr->line_items[$current_element]->mreq_line_id = $ps_detail["mreq_id"];
        $spr->line_items[$current_element]->mreq_line_row_state = $ps_detail["mreq_row_state"];
    }
    else{
        $spr->line_items[$current_element]->mreq_line_id = 0;
        $spr->line_items[$current_element]->mreq_line_row_state = 0;
    }
    
    return array("success" => true, "meaasge" => "");
        
}

function create_production_schedule_pr_cart_header($spr, $pr_detail, $user_id)
{
    $spr->order_id = 0;
    $spr->release = false;
    $spr->pr_reference = get_next_reference(ST_PURCH_REQ);
    $spr->requesting_location = $pr_detail['loc_code'];
    $spr->pr_complete = 0;
    $spr->project_id = $pr_detail['project_id'];
    $spr->pr_date = sql2date($pr_detail['required_by']);
    $spr->next_approval_group_id = 0;
    //$pfm->total = $im_cart->total;
    $spr->approval_status = AS_NEW;

    $spr->created_by  = $user_id;
    $spr->row_state  = 0;
}

function build_pr_line_array($ps_detail, &$pr_line)
{
    $pr_line['tran_qty'] = $ps_detail["tran_qty"];
    $pr_line['estimate_price'] = 0.01;
    $pr_line['required_date'] = sql2date($ps_detail["required_by"]);
    $pr_line['task_id'] = $ps_detail["task_id"];

}



?>