<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_UOM';
$path_to_root = "..";
//include($path_to_root . "/includes/session.inc");

//page(_($help_context = "Email Template"));

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/payroll/includes/db/shifts_db.inc");


simple_page_mode(false);
//----------------------------------------------------------------------------------

$work_order_id = 0;

if (isset($_GET['work_order_id']) && $_GET['work_order_id'] != -1) 
{
    $work_order_id = $_GET['work_order_id'];
}

if (isset($_POST['ADD_ITEM'])){
    $Mode = 'ADD_ITEM';
}
elseif (isset($_POST['UPDATE_ITEM'])){
    $Mode = 'UPDATE_ITEM';
}
elseif (isset($_POST['Delete_WO_Process'])){
    $Mode = 'Delete';
}
elseif(!isset($Mode)){
    $Mode = '';
}

if (isset($_POST['ADD_ITEM'])){
    $Mode = 'ADD_ITEM';
}
elseif (isset($_POST['UPDATE_ITEM'])){
    $Mode = 'UPDATE_ITEM';
}
elseif (isset($_POST['Delete'])){
    $Mode = 'Delete';
}
elseif(!isset($Mode)){
    $Mode = '';
}

$selected_id = -1;

function get_selected_id(&$selected_id, &$Mode)
{
    foreach( $_POST as $key => $post_var ) {
        if(!is_array( $post_var ) ) {
            $e = substr($key,0,15);
            if($e == 'EditWO_Process_'){
                $selected_id = substr($key,15);
                $Mode = "Edit";
                return ;
            }
            $d = substr($key,0,17);
            if($d == 'DeleteWO_Process_'){
                $selected_id = substr($key,17);
                $Mode = "Delete";
                return ;
            }
        } 
    }
    
    return -1;
}

get_selected_id($selected_id, $Mode);

function display_wo_processes($work_order_id)
{

	div_start('wo_processes');
	start_table(TABLESTYLE, "width=90%");
	$th = array(_("Sequence"), _("Description"), _("Workcentre"), _("Process Type"), 
                _("Item Description"), _("Item Qty."), _("Start Date"), 
                _("Setup Time"), _("Processing Time"), _("Cleaning Time"), 
                _("End Date"), _("QC"), _("Comment"),"", "");
	table_header($th);

    $result = get_wo_processes($work_order_id);
    
    $k = 0;
	while ($wo_process = db_fetch($result))
	{

		alt_table_row_color($k);

		label_cell($wo_process["sequence"]);
        label_cell($wo_process["description"]);
        label_cell($wo_process["workcentre_name"]);
        label_cell($wo_process["process_type_name"]);
        label_cell($wo_process["item_description"]);
        label_cell($wo_process["item_quantity"]);
        label_cell(substr($wo_process["start_date_time"],0,16));
        label_cell(seconds2time($wo_process["setup_time"]), "align=right");
        label_cell(seconds2time($wo_process["processing_time"]), "align=right");
        label_cell(seconds2time($wo_process["cleaning_time"]), "align=right");
        label_cell(substr($wo_process["end_date_time"],0,16));
        label_cell(get_yes_no_name($wo_process["qc"]));
        label_cell($wo_process["comment"]);

        edit_button_cell("Edit"."WO_Process_".$wo_process['id'], _("Edit"));
 		delete_button_cell("Delete"."WO_Process_".$wo_process['id'], _("Delete"));
        
        end_row();

    }
	end_table();
	div_end();
}


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

function build_workorder_process_array($work_order_id, $process_id, &$wop)
{
    $wop['wo_id'] = $work_order_id;
    $wop['wo_process_id'] = $process_id;
    $wop['wo_process_sequence'] = $_POST['wo_process_sequence'];
    $wop['wo_process_description'] = $_POST['wo_process_description'];
    $wop['wo_process_workcentre_id']= $_POST['wo_process_workcentre_id']; 
    
    $wop['wo_process_item_description'] = $_POST['wo_process_item_description'];
    
    if(input_num('wo_process_item_quantity') == 0){
        $woh = get_work_order_header($work_order_id);
        $item_quantity = $woh['requested_unit_qty'];
    }
    else{
        $item_quantity = input_num('wo_process_item_quantity');
    }
    $wop['wo_process_item_quantity'] = $item_quantity;
    
    $wop['wo_process_start_date_time'] = date2sql($_POST['wo_process_start_date'])." ".$_POST['wo_process_start_time'];
    
    $wop['wo_process_setup_time']  = $_POST['wo_process_setup_time'];
    $wop['wo_process_processing_time']  = $_POST['wo_process_processing_time'];
    $wop['wo_process_cleaning_time']  = $_POST['wo_process_cleaning_time'];
    
    $wop['wo_process_end_date_time'] = date2sql($_POST['wo_process_end_date'])." ".$_POST['wo_process_end_time'];
    $wop['wo_process_qc']  = $_POST['wo_process_qc'];
    
    $wop['wo_process_comment']  = $_POST['wo_process_comment'];
    
    if(!isset($wop['wo_process_schedule_method'])){
        $wop['wo_process_schedule_method']  = WOSM_MANUAL;
    }
    
    $wop['wo_process_type_id'] = $_POST['wo_process_type_id'];
    $wop['wo_process_scheduling_days_limit']  = get_workcentre_scheduling_days_limit();
}


function display_wo_process_edit_controls($parent, &$Mode, $selected_id)
{
    div_start('wo_process_controls');
    
    start_table(TABLESTYLE2);
    
    $_POST['wo_process_id'] = 0;
    
    $_POST['wo_process_setup_time'] = "00:00";
    $_POST['wo_process_processing_time'] = "00:00";
    $_POST['wo_process_cleaning_time'] = "00:00";
    $_POST['wo_process_workcentre_id'] = 0;
    $_POST['wo_process_type_id'] = 0;
    $_POST['wo_process_item_quantity'] = 0;
    
    $_POST['wo_process_start_date'] = ''; 
    $_POST['wo_process_start_time'] = "09:00"; 
    
    $_POST['wo_process_end_date'] = ''; 
    $_POST['wo_process_end_time'] = "09:00"; 
            
            
    if ($selected_id > 1)
	{
        sql2time("");
			//editing a selected component from the link to the line item
            
			$myrow = get_wo_process($selected_id);

            $start_date = sql2date($myrow["start_date_time"]);
            $start_time = sql2time($myrow["start_date_time"]);
            
            $end_date = sql2date($myrow["end_date_time"]);
            $end_time = sql2time($myrow["end_date_time"]);    
    
			$_POST['wo_process_id'] = $myrow["id"];
            $_POST['wo_process_sequence'] = $myrow["sequence"];
            $_POST['wo_process_description'] = $myrow["description"];
			$_POST['wo_process_workcentre_id'] = $myrow["workcentre_id"]; 
            $_POST['wo_process_workcentre_name'] = $myrow["workcentre_name"];
            $_POST['wo_process_type_id'] = $myrow["type_id"]; 
            $_POST['wo_process_item_description'] = $myrow["item_description"];
            $_POST['wo_process_item_quantity'] = $myrow["item_quantity"];
            $_POST['wo_process_start_date'] = $start_date; 
            $_POST['wo_process_start_time'] = $start_time; 
			$_POST['wo_process_setup_time']  = seconds2time($myrow["setup_time"]);
            $_POST['wo_process_processing_time']  = seconds2time($myrow["processing_time"]);
            $_POST['wo_process_cleaning_time']  = seconds2time($myrow["cleaning_time"]);
            $_POST['wo_process_end_date'] = $end_date; 
            $_POST['wo_process_end_time'] = $end_time;
            $_POST['wo_process_qc'] = $myrow["qc"];
            $_POST['wo_process_comment']  = $myrow["comment"];
		
		hidden('selected_id', $selected_id);
	}
	else
	{

	}
    
    
    
    br();
	hidden('stock_id', $parent);
    hidden('wo_process_id', $_POST['wo_process_id']);
    
    qty_row("Sequence", 'wo_process_sequence', null, null, null, 0);
    text_row("Process Description:", 'wo_process_description', null, 50, 200);
    
    if ($selected_id > 0){
        label_row("Workcentre:", $_POST['wo_process_workcentre_name']);
        hidden("wo_process_workcentre_id", $_POST['wo_process_workcentre_id']);
    }
    else{
        workcenter_list_row("Workcentre:", 'wo_process_workcentre_id');
    }

//    echo $_POST['wo_process_type_id'];
    value_list_row("Process Type", "wo_process_type_id", "WO_PROCESS_TYPES", null, "Select a Type");
    
    text_row("Item Description:", 'wo_process_item_description', null, 50, 200);
    qty_row(_("Quantity:"), 'wo_process_item_quantity', null, null, null, 0);
    
//	qty_row(_("Setup Time:"), 'wo_process_setup_time', null, null, null, 0);
//    qty_row(_("Processing Time:"), 'wo_process_processing_time', null, null, null, 0);
//    qty_row(_("Cleaning Time:"), 'wo_process_cleaning_time', null, null, null, 0);
    
    date_row("Start Date:", 'wo_process_start_date');
    time_row("Start Time (24 hr): ", "wo_process_start_time", null, 5);
                
    
    time_row("Setup Time (hhh:mm) :", "wo_process_setup_time", null, 10);
    time_row("Processing Time (hhh:mm) :", "wo_process_processing_time", null, 10);
    time_row("Cleaning Time (hhh:mm) :", "wo_process_cleaning_time", null, 10);
    
    date_row("End Date:", 'wo_process_end_date');
    time_row("End Time (24 hr): ", "wo_process_end_time", null, 5);
    
    yesno_list_row("QC:", 'wo_process_qc');
    textarea_row(_("Comment:"), "wo_process_comment", null, 31, 2);
    
	end_table(1);
    

    
    if ($selected_id > 0) {
        br();
        submit_center_first('Update_WO_Process_Manual', _("Manual Update (No Scheduling)"), '', 'default');
        br(2);
        submit_center_first('Update_WO_Process_Forward', _("Update - Forward"), '', 'default');
        submit('Update_WO_Process_Forward_Inline', _("Update - Forward Inline"), true, '', 'default');
        submit_center_last('Update_WO_Process_Forward_Priority', _("Update - Forward Priority"), '', 'default');
        br();
        submit_center_first('Update_WO_Process_Backward', _("Update - Backward"), '', 'default');
        submit_center_last('Delete_WO_Process', _("Delete"), _("Delete"), 'cancel');
    }
    else
    {
        br();
        submit_center_first('Add_WO_Process_Manual', _("Manual Add (No Scheduling)"), '', 'default');
        br(2);
        submit_center_first('Add_WO_Process_Forward', _("Add - Schedule Forward"), '', 'default');
        submit('Add_WO_Process_Forward_Inline', _("Add - Forward Inline"), true, '', 'default');
        submit_center_last('Add_WO_Process_Forward_Priority', _("Add - Forward Priority"), '', 'default');
        br();
        submit_center_last('Add_WO_Process_Backward',_("Add - Schedule Backwards"), '', 'default');
    }
    
    div_end();
    
	//submit_add_or_update_center($selected_id == -1, '', 'both');
    
//    if (strlen($selected_id) > 5) {
//        submit_center_first('Update_BOM_Process', _("Update"), '', 'default');
//        submit_center_last('Delete_BOM_Process', _("Delete"), _("Cancel Edition"), 'cancel');
//    }
//    else
//    {
//        submit_center_first('Add_BOM_Process', _("Save"), '', 'default');
//    }
    
}




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

function save_workorder_process($work_order_id, $schedule_method, $new = true){
    
    $wop = array();
    $wo_process_id = get_post('wo_process_id') ;
    
    $wop['wo_process_schedule_method'] = $schedule_method;
    
    build_workorder_process_array($work_order_id, $wo_process_id, $wop);
    
    $ccwop = can_commit_wo_process($wop);
    if($ccwop["success"] !== true){
        display_error($ccwop['message']);
        if(isset($ccwop['focus_to'])){
            set_focus($ccwop['focus_to']);
        }
        return $ccwop;
    }
    
    save_wo_process($wop);    
    $swop = schedule_wo_process($wop);
    return $swop; 
}


function delete_workorder_process($work_order_id, $selected_id)
{
    $cdwop = can_delete_wo_proces($work_order_id, $selected_id);
    if($cdwop['success'] !== true){
        display_error($cdwop['message']);
        return $cdwop;
    }
    else{
        delete_wo_process($selected_id);
        display_notification(_('Selected process has been deleted'));
        return array("success" => true, "message" => '');
    }
}
//--------------------------------------------------------------------------------------------------


if(isset($_POST['Add_WO_Process_Manual'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_MANUAL, true);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Add_WO_Process_Forward'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_MANUAL, true);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Add_WO_Process_Forward_Inline'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_FORWARD_INLINE_MANUAL, true);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Add_WO_Process_Forward_Priority'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_FORWARD_PRIORITY_MANUAL, true);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Add_WO_Process_Backward'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_BACKWARD_MANUAL, true);  
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    } 
}

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

if(isset($_POST['Update_WO_Process_Manual'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_MANUAL, false);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Update_WO_Process_Forward'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_FORWARD_MANUAL, false);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Update_WO_Process_Forward_Inline'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_FORWARD_INLINE_MANUAL, false);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Update_WO_Process_Forward_Priority'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_FORWARD_PRIORITY_MANUAL, false);   
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    }
}

if(isset($_POST['Update_WO_Process_Backward'])){
    $sbp_msg = save_workorder_process($work_order_id, WOSM_BACKWARD_MANUAL, false);  
    if($sbp_msg['success'] !== true){
        display_error($sbp_msg["message"]);
    }
    else{
        $Mode = '';
        $selected_id = -1;
    } 
}


if ($Mode == 'Delete')
{
    //$process_id = substr($selected_id,8);
    
    $dwop = delete_workorder_process($work_order_id, $selected_id);
    if($dwop['success'] === true){
        $selected_id = -1;
        $Mode = 'RESET';
    }
}


if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

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

global $Ajax;

start_form();

$Ajax->activate('wo_processes');
$Ajax->activate('wo_process_controls');

display_wo_processes($work_order_id);

if($selected_id > 0 ){
    display_wo_process_edit_controls($work_order_id, $Mode, $selected_id);
}
else{
    display_wo_process_edit_controls($work_order_id, $Mode, -1);
}
    


end_form();

//end_page();

?>
