<?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/nopay_references_db.inc");

$selected_id = -1;
$Mode = "";

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

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

function get_selected_id(&$selected_id, &$Mode)
{

    foreach( $_POST as $key => $post_var ) {
        if(!is_array( $post_var ) ) {
            $e = substr($key,0,11);
            if($e == "EditLabour_"){
                $selected_id = $key;
                $Mode = "Edit";
                return ;
            }
            if(isset($_POST['Add_WO_Material'])){
                $selected_id = -1;
                $Mode = "Add_WO_Material";
                return ;
            }
            $d = substr($key,0,13);
            if($d == 'DeleteLabour_'){
                $selected_id = $key;
                $Mode = "Delete";
                return ;
            }
        } 
    }
    
    if(isset($_POST['selected_id'])){
        $e = substr($_POST['selected_id'],0,11);
        if($e == "EditLabour_"){
            $selected_id = $_POST['selected_id'];
            $Mode = "Edit";
            return ;
        }
    }
    
    return -1;
    //$Mode = 'ADD_ITEM';
}

get_selected_id($selected_id, $Mode);

function display_wo_process_name($wo_process)
{
    start_row();
    $name = $wo_process["sequence"]." - ".$wo_process["workcentre_name"]." - ".$wo_process["description"];
    label_cell("<b>".$name."</b>", "colspan=7 align=center");
    end_row();
}



function display_wo_process_labour($workorder_id, $process_id)
{
    $result = get_wo_labour($workorder_id, $process_id);
	
	$k = 0;
	while ($myrow = db_fetch($result))
	{

		alt_table_row_color($k);

		label_cell($myrow["job_category_name"]);
		label_cell($myrow["rate_category_name"]);
        label_cell(get_yes_no_name($myrow["setup"]), "align=center");
        label_cell(get_yes_no_name($myrow["processing"]), "align=center");
        label_cell(get_yes_no_name($myrow["cleaning"]), "align=center");
        label_cell($myrow["comment"]);
        
 		edit_button_cell("Edit"."Labour_".$myrow['id'], _("Edit"));
 		delete_button_cell("Delete"."Labour_".$myrow['id'], _("Delete"));
        end_row();

	} //END WHILE LIST LOOP

}

function display_wo_labour($workorder_id)
{
    $result = get_wo_processes($workorder_id);
    div_start('bom');
	start_table(TABLESTYLE, "width=60%");
	$th = array(_("Job Category"), _("Rate Category"), _("Setup"),  _("Processing"),  _("Cleaning"),  _("Comment"), '','');
	table_header($th);

	$k = 0;
	while ($myrow = db_fetch($result))
	{
        display_wo_process_name($myrow);
    
        display_wo_process_labour($workorder_id, $myrow['id']);
        
	} 
    end_table();
	div_end();
}

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

function display_wo_labour_edit_controls($workorder_id, &$Mode, $selected_id)
{
    global $Ajax;
    
    div_start('wo_labour_controls');
    start_table(TABLESTYLE2);
    
    $id = substr($selected_id,11);
            
    if ($id != -1)
	{
 		if ($Mode == 'Edit') {
			$myrow = get_wo_labour_detail($id);

			$_POST['wo_labour_process_id'] = $myrow["process_id"];
            $_POST['wo_labour_job_category_id'] = $myrow["job_category_id"];
			$_POST['wo_labour_rate_category_id'] = $myrow["rate_category_id"]; // by Tom Moulton
            $_POST['wo_labour_setup']  = $myrow['setup'];
            $_POST['wo_labour_processing']  = $myrow['processing'];
            $_POST['wo_labour_cleaning']  = $myrow['cleaning'];
            $_POST['wo_labour_comment']  = $myrow['comment'];

		}
		hidden('selected_id', $selected_id);
	}
	
    br(2);
    
    workorder_processes_list_row('Process', 'wo_labour_process_id', $workorder_id);

    value_list_row("Job Category:", "wo_labour_job_category_id", "JOB_CATEGORY", null, '');
	
	//hidden('stock_id', $workorder_id);

	rate_category_list_row("Rate Category", "wo_labour_rate_category_id", null, '');

    //time_row("Setup Time (hhh:mm) :", "wo_labour_duration", null, 10);
	//qty_row(_("Duration:"), 'wo_labour_duration', null, null, null, 0);
    
    yesno_list_row("Setup :", "wo_labour_setup");
    
    yesno_list_row("Processing :", "wo_labour_processing");
    
    yesno_list_row("Cleaning :", "wo_labour_cleaning");
    
    textarea_row(_("Comment:"), "wo_labour_comment", null, 31, 3);
    
	end_table(1);
	div_end();
    
    submit_add_or_update_center($selected_id == -1, '', 'both');
    
//    if (strlen($selected_id) > 5) {
//        submit_center_first('Update_BOM_Item', _("Update"), '', 'default');
//        submit_center_last('Delete_BOM_Item', _("Delete"), _("Cancel Edition"), 'cancel');
//    }
//    else
//    {
//        submit_center_first('Add_BOM_Item', _("Save"), '', 'default');
//    }

    
    
    }

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

function save_workorder_labour($selected_id){
    $id = substr($selected_id,11);
    build_workorder_labour_array($wol);
    $ccwol = can_commit_wo_labour($wol, $id);
    if($ccwol['success'] !== true){
        return $ccwol;
    }
    else{
        return save_wo_labour($wol, $id);
    }

}

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

function delete_workorder_labour($selected_id){
    $id = substr($selected_id,13);
    $ccwol = can_delete_wo_labour($id);
    if($ccwol['success'] !== true){
        return $ccwol;
    }
    else{
        delete_wo_labour($id);
        return array("success" => true);
    }
}

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


function build_workorder_labour_array(&$wol)
{
    $wol['wo_labour_process_id'] = get_post('wo_labour_process_id');
    $wol['wo_labour_job_category_id'] = get_post('wo_labour_job_category_id');
    $wol['wo_labour_rate_category_id'] = get_post('wo_labour_rate_category_id');
    $wol['wo_labour_setup']  = $_POST['wo_labour_setup'];
    $wol['wo_labour_processing']  = $_POST['wo_labour_processing'];
    $wol['wo_labour_cleaning']  = $_POST['wo_labour_cleaning'];
    $wol['wo_labour_comment']  = $_POST['wo_labour_comment'];
    $wol['user_id']  = $_SESSION['wa_current_user']->user;
    
}

//--------------------------------------------------------------------------------------------------
//if ($Mode=='ADD_ITEM') 
if (isset($_POST['ADD_ITEM']))
{
    $sbp_msg = save_workorder_labour(-1);  
    if($sbp_msg['success'] === true){
        $Mode = '';
        $selected_id = -1;
    }
    else{
        display_error($sbp_msg['message']);
        if(isset($sbp_msg['focus_to'])){
            set_focus($sbp_msg['focus_to']);
        }
    }
}

//if ($Mode=='UPDATE_ITEM') 
if (isset($_POST['UPDATE_ITEM']))
{
    $sbp_msg = save_workorder_labour($selected_id);  
    if($sbp_msg['success'] === true){
        $Mode = '';
        $selected_id = -1;
    }
    else{
        display_error($sbp_msg['message']);
        if(isset($sbp_msg['focus_to'])){
            set_focus($sbp_msg['focus_to']);
        }
    }
}



if ($Mode == 'Delete')
{
    $dwol = delete_workorder_labour($selected_id);
    if($dwol['success'] === true){
        $Mode = '';
        $selected_id = -1;
        display_notification("Selected record has been deleted");
    }
    else{
        display_error($sbp_msg['message']);
    }
    
	$Mode = 'RESET';
}

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

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

global $Ajax;

start_form();

display_wo_labour($work_order_id);

$Ajax->Activate('wo_labour_controls');

if(substr($selected_id,0,11) == "EditLabour_"){
    display_wo_labour_edit_controls($work_order_id, $Mode, $selected_id);
}
else{
    display_wo_labour_edit_controls($work_order_id, $Mode, -1);
}
    


end_form();

//end_page();

?>
