<?php
/**********************************************************************
***********************************************************************/
include_once($path_to_root . "/includes/ui.inc");

//--------------------------------------------------------------------------------------

function generate_manufacturing_schedule($id)
{
    $user_id = $updated_by = $_SESSION['wa_current_user']->user;
    
    start_generating_production_schedule($id, $user_id);
    
    delete_all_production_schedule_unconfirmed_records();
    
    delete_all_unconfirmed_workorders();
    
    $prod_schedule = get_production_schedule_run($id);
    
    generate_manufacturing_schedule_workorders($prod_schedule, $user_id);
    
    generate_manufacturing_schedule_purchase_requisitions($prod_schedule, $user_id);
    
    generate_manufacturing_schedule_secondary_workorders($prod_schedule, 1, $user_id);
        
    generate_manufacturing_schedule_secondary_purchase_requisitions($prod_schedule, $user_id);
    
    end_generating_production_schedule($id, $user_id);
}

function generate_manufacturing_schedule_workorders($prod_schedule, $user_id)
{
    
    $result = get_scheduling_manufacturing_requirements($prod_schedule['period_start_date'], $prod_schedule['period_end_date'], ST_WORKORDER);
    while ($mrequirement = db_fetch($result)){
        $comment = "";
        
//        if(already_scheduled(ST_MANUF_REQUIREMENT, $mrequirement['id'], $mrequirement['id'])){
//            continue;
//        }
        
        $item = get_stock_item_edit_info($mrequirement['stock_id']);

        $wo_no = get_next_schedule_wo_number();

        $schedule_method = WOSM_BACKWARD_SCHEDULAR;
        build_scheduling_wo_array($wo, $wo_no, $mrequirement['stock_id'], 
                                    $schedule_method, $mrequirement['manufacturing_unit'], $mrequirement['manufacturing_unit_qty'], 
                                    $mrequirement['required_by'], $mrequirement['project_id'], $mrequirement['task_id']);
        
        $swo = schedule_workorder($wo);
        if($swo['success'] !== true){
            $message = "";
            add_production_schedule_detail($prod_schedule['id'], ST_MANUF_REQUIREMENT,
                                            $mrequirement['id'], $mrequirement['id'], $mrequirement['project_id'],
                                            $mrequirement['task_id'], $mrequirement['stock_id'],
                                            ST_WORKORDER, -99,
                                            0,$mrequirement['loc_code'], $mrequirement['manufacturing_quantity'],
                                            $mrequirement['manufacturing_unit'], $mrequirement['manufacturing_unit_qty'],
                                            $mrequirement['manufacturing_unit_cf'], $mrequirement['required_by'],
                                            $swo['message'], 1,
                                            $user_id);
        }
        else{
            add_production_schedule_detail($prod_schedule['id'], ST_MANUF_REQUIREMENT,
                                            $mrequirement['id'], $mrequirement['id'], $mrequirement['project_id'],
                                            $mrequirement['task_id'], $mrequirement['stock_id'],
                                            ST_WORKORDER, $wo_no,
                                            0,$mrequirement['loc_code'], $mrequirement['manufacturing_quantity'],
                                            $mrequirement['manufacturing_unit'], $mrequirement['manufacturing_unit_qty'],
                                            $mrequirement['manufacturing_unit_cf'], $mrequirement['required_by'],
                                            $comment, 1,
                                            $user_id);
            update_manufacturing_requirement_order_no($mrequirement['id'], ST_WORKORDER, $wo_no, 0, $mrequirement['row_state']);
        } 
    }
}


function generate_manufacturing_schedule_secondary_workorders($prod_schedule, $source_cycle_no, $user_id)
{
    $result = get_scheduling_manufacturing_secondary_requirements($prod_schedule['id'], ST_WORKORDER, "M", $source_cycle_no);
    while ($mitem = db_fetch($result)) {
        if(already_scheduled(ST_WORKORDER, $mitem['wo_id'], $mitem['id'])){
            continue;
        }
        $comment = "";
        $wo_no = get_next_schedule_wo_number();
        
        $required_by = substr($mitem['wo_start_date_time'],0,10);
        
        $schedule_method = WOSM_BACKWARD_SCHEDULAR;
        build_scheduling_wo_array($wo, $wo_no, $mitem['stock_id'], 
                                    $schedule_method, $mitem['req_unit'], $mitem['req_unit_qty'], 
                                    $required_by,  $mitem['project_id'], $mitem['task_id']);
        
        $swo = schedule_workorder($wo);
        if($swo['success'] !== true){
            $message = "";
        }
        else{
            add_production_schedule_detail($prod_schedule['id'], ST_WORKORDER,
                                            $mitem['wo_id'], $mitem['id'], $mitem['project_id'],
                                            $mitem['task_id'], $mitem['stock_id'],
                                            ST_WORKORDER, $wo_no,
                                            0,$mitem['loc_code'], $mitem['units_req'],
                                            $mitem['req_unit'], $mitem['req_unit_qty'],
                                            $mitem['req_unit_conversion_factor'], $required_by,
                                            $comment, $source_cycle_no + 1,
                                            $user_id);

            //update_manufacturing_requirement_order_no($mrequirement['id'], ST_WORKORDER, $wo_no, 0, $mrequirement['row_state']);
        }
        
    }
    
}


function generate_manufacturing_schedule_purchase_requisitions($prod_schedule, $user_id)
{

    $result = get_scheduling_manufacturing_requirements($prod_schedule['period_start_date'], $prod_schedule['period_end_date'], ST_PURCH_REQ);
    while ($mrequirement = db_fetch($result)) {
        $comment = "";
        $item = get_stock_item_edit_info($mrequirement['stock_id']);
        add_production_schedule_detail($prod_schedule['id'], ST_MANUF_REQUIREMENT,
                                        $mrequirement['id'], $mrequirement['id'], $mrequirement['project_id'],
                                        $mrequirement['task_id'], $mrequirement['stock_id'],
                                        ST_PURCH_REQ, 0,
                                        0, $mrequirement['loc_code'], $mrequirement['manufacturing_quantity'],
                                        $mrequirement['manufacturing_unit'], $mrequirement['manufacturing_unit_qty'],
                                        $mrequirement['manufacturing_unit_cf'], $mrequirement['required_by'],
                                        $comment, 1,
                                        $user_id);
                
        //update_manufacturing_requirement_order_no($mrequirement['id'], ST_PURCH_REQ, 0, 0, $mrequirement['row_state']);
    }
        
}


function generate_manufacturing_schedule_secondary_purchase_requisitions($prod_schedule, $user_id)
{
    
    $result = get_scheduling_manufacturing_secondary_requirements($prod_schedule['id'], ST_WORKORDER, "B");
    while ($wo_item = db_fetch($result)) {
        if(already_scheduled(ST_WORKORDER, $wo_item['wo_id'], $wo_item['id'])){
            continue;
        }
        $comment = "";

        $required_by = substr($wo_item['wo_start_date_time'],0,10);
        
        add_production_schedule_detail($prod_schedule['id'], ST_WORKORDER,
                                            $wo_item['wo_id'], $wo_item['id'], $wo_item['project_id'],
                                            $wo_item['task_id'], $wo_item['stock_id'],
                                            ST_PURCH_REQ, 0,
                                            0,$wo_item['loc_code'], $wo_item['units_req'],
                                            $wo_item['req_unit'], $wo_item['req_unit_qty'],
                                            $wo_item['req_unit_conversion_factor'], $required_by,
                                            $comment, $wo_item['cycle_no'] + 1,
                                            $user_id);
        
        //update_manufacturing_requirement_order_no($mrequirement['id'], ST_PURCH_REQ, 0, 0, $mrequirement['row_state']);
    }
        
}



function delete_all_production_schedule_unconfirmed_records()
{
    $psd_res = get_all_production_schedule_unconfirmed_records();
    while ($row = db_fetch($psd_res)) {
        if($row['tran_type'] == ST_WORKORDER){
            if($row['trans_no'] == 0){
                begin_transaction();
                delete_production_schedule_workorder($row['scheduled_trans_no']);
                $n = delete_production_schedule_detail($row['id'], $row['row_state']);
                if($n === 1){
                    commit_transaction();
                }
                else{
                    cancel_transaction();
                }
            }
        }
    
        if($row['tran_type'] == ST_PURCH_REQ){
            if($row['trans_no'] == 0){
                delete_production_schedule_detail($row['id'], $row['row_state']);
            }
        }
    }
}

function delete_all_unconfirmed_workorders()
{
    $ucwo_res = get_all_unconfirmed_workorders();
    while ($row = db_fetch($ucwo_res)) {
        begin_transaction();
        delete_production_schedule_workorder($row['id']);
        commit_transaction();
    }
}

function delete_production_schedule_workorder($wo_no)
{
    
    delete_production_schedule_workorder_labour($wo_no);
    delete_production_schedule_workorder_material($wo_no);
    
    $wop_res = get_schedule_workorder_processes($wo_no, true);
    while ($row = db_fetch($wop_res)) {
        delete_production_schedule_workorder_workcentre_schedule($row['id']);
    }
    delete_production_schedule_workorder_processes($wo_no);
    delete_production_schedule_workorder_header($wo_no);
}

function schedule_workorder($wo)
{
    $ccwo = can_commit_workorder($wo);
    if($ccwo['success'] !== true){
        return $ccwo;
    }

    return create_workorder($wo);

}



function build_scheduling_wo_array(&$wo, $work_order_no, $stock_id, 
                                    $schedule_method, $manufacturing_unit, $manufacturing_unit_qty, 
                                    $required_by, $project_id, $task_id)
{   
        $wo['wo_id'] = $work_order_no;
        $wo['wo_schedule_method'] = $schedule_method;
        $wo['wo_ref'] = $work_order_no;
        //$wo['wo_ref'] = date('Y-m-d H:i:s:u');
        $wo['wo_tran_unit'] = $manufacturing_unit;
        $wo['wo_tran_qty'] = $manufacturing_unit_qty;
        $wo['wo_start_date_time'] = $required_by." 00:00:00";
        $wo['wo_end_date_time'] = $required_by." 00:00:00";
        $wo['wo_type'] = 2;
        $wo['wo_stock_id'] = $stock_id;
        $wo['wo_labour'] = 0;
        $wo['wo_costs'] = 0;
        $wo['wo_comment'] = "";
        $wo['wo_row_state'] = 0;
        $wo['wo_StockLocation'] = 'MAIN';

        $tran_unit = $wo['wo_tran_unit'];
        get_convertion_factor2($wo['wo_stock_id'], $tran_unit, $convesion_factor);
        $wo['wo_tran_unit'] = $tran_unit;
        $wo['wo_cf'] = $convesion_factor;

        $wo['wo_project_id'] = $project_id;
        $wo['wo_task_id'] = $task_id;
        
        $wo['wo_cr_acc'] = "";
        $wo['cr_lab_acc'] = "";   

        $wo['bom_factor'] = 1;
        $bom_header = get_bom_header($wo['wo_stock_id']);
        $bom_qty = $bom_header['quantity'];
        $wo_qty = $wo['wo_tran_qty'] * $wo['wo_cf'];
        $wo['bom_factor'] = $wo_qty/$bom_qty;
        
        $wo['wo_scheduling_days_limit'] = get_workcentre_scheduling_days_limit();
        
    
}

?>