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

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

function get_scheduling_manufacturing_requirements($period_start_date, $period_end_date, $order_type)
{
    $sql = "SELECT * FROM ".TB_PREF."manufacturing_requirements 
            WHERE order_type = ".db_escape($order_type)." 
                    AND order_no <= 0 
                    AND required_by >= ".db_escape($period_start_date)." 
                    AND required_by <= ".db_escape($period_end_date)." 
            ORDER BY required_by DESC, stock_id" ;
    
    return db_query($sql, "Could not get_scheduling_manufacturing_requirements");
    
}

function already_scheduled($source_type, $source_id, $source_line_id)
{
    $sql = "SELECT * FROM ".TB_PREF."production_schedule_details  
            WHERE source_type = ".db_escape($source_type)." 
                    AND source_id = ".db_escape($source_id)." 
                    AND source_line_id = ".db_escape($source_line_id)." 
                    AND trans_no > 0";
    
    $result = db_query($sql, "Could not check already_scheduled");
    $row = db_fetch($result);
    if($row){
        return true;
    }
    else{
        return false;
    }
    
}

function get_scheduling_manufacturing_secondary_requirements($prod_schedule_id, $order_type, $mb_flag, $cycle_no = "ALL")
{
    $sql = "SELECT psd.scheduled_trans_no, psd.cycle_no, psd.project_id, psd.task_id,
                    sm.mb_flag, 
                    wom.*,
                    wo.id wo_id, wo.date_ wo_start_date_time , wo.project_id, wo.task_id  
            FROM ".TB_PREF."production_schedule_details psd, 
                    ".TB_PREF."wo_material wom, 
                    ".TB_PREF."stock_master sm,
                    ".TB_PREF."wo_processes wop,
                    ".TB_PREF."workorders wo  
            WHERE (psd.scheduled_trans_no = wom.workorder_id OR psd.trans_no = wom.workorder_id)
                    AND wom.stock_id = sm.stock_id 
                    AND (psd.scheduled_trans_no = wop.workorder_id OR psd.trans_no = wop.workorder_id )
                    AND wom.process_id = wop.id 
                    AND (psd.scheduled_trans_no = wo.id OR psd.trans_no = wo.id)
                    AND sm.mb_flag = ".db_escape($mb_flag)." 
                    AND psd.production_schedule_id = ".db_escape($prod_schedule_id)." 
                    AND psd.tran_type = ".db_escape($order_type);
    
    if($cycle_no == "ALL"){
        $sql .= "AND psd.cycle_no > 0 ";
    }
    else{
        $sql .= "AND psd.cycle_no = ".db_escape($cycle_no);
    }
                    
    return db_query($sql, "Could not get_scheduling_manufacturing_secondary_requirements");
    
}

function add_production_schedule_detail($production_schedule_id,$source_type,
                                        $source_id, $source_line_id, $project_id,
                                        $task_id, $stock_id,
                                        $tran_type,$scheduled_trans_no,
                                        $trans_no, $loc_code, $quantity,
                                        $tran_unit,$tran_qty,
                                        $conversion_factor, $required_by,
                                        $comment, $cycle_no,
                                        $created_by)
{
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."production_schedule_details(
                    production_schedule_id,source_type,
                    source_id, source_line_id, project_id,
                    task_id, stock_id,
                    tran_type,scheduled_trans_no,
                    trans_no, loc_code, quantity,
                    tran_unit,tran_qty,
                    conversion_factor, required_by,
                    comment, cycle_no,
                    created_by,created_on)
            VALUES(".db_escape($production_schedule_id).", ".db_escape($source_type).", "
                    .db_escape($source_id).", ".db_escape($source_line_id).", ".db_escape($project_id).", "
                    .db_escape($task_id).", ".db_escape($stock_id).", "
                    .db_escape($tran_type).", ".db_escape($scheduled_trans_no).", "
                    .db_escape($trans_no).", ".db_escape($loc_code).", ".db_escape($quantity).", "
                    .db_escape($tran_unit).", ".db_escape($tran_qty).", "
                    .db_escape($conversion_factor).", ".db_escape($required_by).", "
                    .db_escape($comment).", ".db_escape($cycle_no).", "
                    .db_escape($created_by).", ".db_escape($created_on).")";
    
    db_query($sql, "Could not add_production_schedule_detail");
            
}


function update_production_schedule_detail($id, $task_id, $row_state, $user_id)
{
    $updated_on = date('Y-m-d H:i:s');

    $sql = "UPDATE ".TB_PREF."production_schedule_details  
            SET 
                task_id =".db_escape($task_id).",
                row_state =". db_new_row_state($row_state).",
                updated_by=".db_escape($user_id).",
                updated_on=".db_escape($updated_on)." 
            WHERE id=".db_escape($id)." AND row_state = ". db_escape($row_state);

    db_query($sql,"Could not update_production_schedule_detail");

}


function start_generating_production_schedule($id, $user_id)
{
    $time = date('Y-m-d H:i:s');

    $sql = "UPDATE ".TB_PREF."production_schedule   
            SET 
                run_by =".db_escape($user_id).",
                start_time=".db_escape($time)." 
            WHERE id=".db_escape($id);

    db_query($sql,"Could not start_generating_production_schedule");

}



function end_generating_production_schedule($id, $user_id)
{
    $time = date('Y-m-d H:i:s');

    $sql = "UPDATE ".TB_PREF."production_schedule   
            SET 
                run_by =".db_escape($user_id).",
                end_time=".db_escape($time)." 
            WHERE id=".db_escape($id);

    db_query($sql,"Could not end_generating_production_schedule");

}


function mark_production_schedule_detail($id, $selected, $row_state, $user_id)
{
    $updated_on = date('Y-m-d H:i:s');

    $sql = "UPDATE ".TB_PREF."production_schedule_details  
            SET 
                selected =".db_escape($selected).",
                row_state =". db_new_row_state($row_state)." 
            WHERE id=".db_escape($id)." AND row_state = ". db_escape($row_state);

    db_query($sql,"Could not mark_production_schedule_detail");

}


function insert_log_message($payroll_run_id, $payroll_type, $message_type, $message_details)
{  
	$added_by = $_SESSION['wa_current_user']->user;
    $added_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."payroll_log(
                        payroll_run_id, payroll_type,
                        message_type, message_details,
                        added_by, added_on) 
                VALUES(".$payroll_run_id.",".db_escape($payroll_type).","
                        .db_escape($message_type).",".db_escape($message_details).","
                        .$added_by.",".db_escape($added_on).")";
	
	db_query($sql,"Could not add log message");	
        
}


function update_payroll_run_joq_request_id($payroll_run_id, $jobq_id)
{
    $sql = "UPDATE ".TB_PREF."payroll_runs 
            SET jobq_request_id=".db_escape($jobq_id)." 
            WHERE id = ".db_escape($payroll_run_id);
    
    db_query($sql,"Could not update payroll run jobq id");
}


function get_next_schedule_wo_number()
{
    $sql = "SELECT MIN(scheduled_trans_no) prev_wo_no FROM ".TB_PREF."production_schedule_details" ;
    $result = db_query($sql, "Could not get_next_schedule_wo_number");
    $prev_wo_no = db_fetch($result);
    
    $sql = "SELECT MIN(id) prev_wo_no FROM 0_workorders" ;
    $result = db_query($sql, "Could not get_next_schedule_wo_number");
    $workorders_prev_wo_no = db_fetch($result);
    
    if($workorders_prev_wo_no < $prev_wo_no){
        $prev_wo_no = $workorders_prev_wo_no;
    }
    
    if($prev_wo_no){
        $next_schedule_wo_no = $prev_wo_no['prev_wo_no'] - 1;
        if($next_schedule_wo_no > -200){
            $next_schedule_wo_no = -200;
        }
    }
    else{
        $next_schedule_wo_no = -200;
    }
            
    return $next_schedule_wo_no;
}

function get_schedule_workorder_processes($workorder_id)
{
	$sql = "SELECT id  
            FROM ".TB_PREF."wo_processes 
            WHERE workorder_id < -99  
                    AND workorder_id = ".db_escape($workorder_id)." 
            ORDER BY sequence DESC";
	
	return db_query($sql, "Could not get_schedule_workorder_processes");
}


function delete_production_schedule_workorder_labour($wo_no)
{
     $sql = "DELETE FROM ".TB_PREF."wo_labour  
            WHERE workorder_id = ".db_escape($wo_no)." 
                  AND workorder_id < -99";
    
    db_query($sql, "Could not delete_production_schedule_workorder_labour");
}

function delete_production_schedule_workorder_material($wo_no)
{
     $sql = "DELETE FROM ".TB_PREF."wo_material   
            WHERE workorder_id = ".db_escape($wo_no)." 
                  AND workorder_id < -99";
    
    db_query($sql, "Could not delete_production_schedule_workorder_material");
}


function delete_production_schedule_workorder_workcentre_schedule($process_id)
{
     $sql = "DELETE FROM ".TB_PREF."workcentre_schedule    
            WHERE wo_process_id = ".db_escape($process_id);
    
    db_query($sql, "Could not delete_production_schedule_workorder_workcentre_schedule");
}



function delete_production_schedule_workorder_processes($wo_no)
{
     $sql = "DELETE FROM ".TB_PREF."wo_processes   
            WHERE workorder_id = ".db_escape($wo_no)." 
                  AND workorder_id < -99";
    
    db_query($sql, "Could not delete_production_schedule_workorder_processes");
}


function delete_production_schedule_workorder_header($wo_no)
{
     $sql = "DELETE FROM ".TB_PREF."workorders    
            WHERE id = ".db_escape($wo_no)." 
                  AND id < -99";
    
    db_query($sql, "Could not delete_production_schedule_workorder_processes");
}

function delete_production_schedule_detail($id, $row_state)
{
     $sql = "DELETE FROM ".TB_PREF."production_schedule_details    
            WHERE id = ".db_escape($id)." 
                  AND trans_no = 0 
                  AND row_state = ".db_escape($row_state);
    
    db_query($sql, "Could not delete_production_schedule_detail");
    
    return db_num_affected_rows();
}


?>