<?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");

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

if (isset($_GET['bom_parent']) && $_GET['bom_parent'] > 0) 
{
    $bom_parent = $_GET['bom_parent'];
}


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



function display_bom_process_labour($parent, $process_id)
{
    $result = get_bom_labour($parent, $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_bom_labour($parent)
{
    $result = get_bom_processes($parent);
    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_bom_process_name($myrow);
    
        display_bom_process_labour($parent, $myrow['id']);
        
	} 
    end_table();
	div_end();
}

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

function display_bom_labour_edit_controls($parent, &$Mode, $selected_id)
{
    global $Ajax;
    
    div_start('bom_labour_controls');
    start_table(TABLESTYLE2);
    
    if ($selected_id != -1)
	{
 		if ($Mode == 'Edit') {
			//editing a selected component from the link to the line item
            $id = substr($selected_id,7);
			$myrow = get_bom_labour_detail($id);

			$_POST['bom_labour_process_id'] = $myrow["process_id"];
            $_POST['bom_labour_job_category_id'] = $myrow["job_category_id"];
			$_POST['bom_labour_rate_category_id'] = $myrow["rate_category_id"]; // by Tom Moulton
            $_POST['bom_labour_setup']  = $myrow["setup"];
            $_POST['bom_labour_processing']  = $myrow["processing"];
            $_POST['bom_labour_cleaning']  = $myrow["cleaning"];

		}
		hidden('selected_id', $selected_id);
	}
	
    br(2);
    
    bom_processes_list_row('Process', 'bom_labour_process_id', $parent);

    value_list_row("Job Category:", "bom_labour_job_category_id", "JOB_CATEGORY", null, '');
	
	hidden('stock_id', $parent);

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

    yesno_list_row("Setup", "bom_labour_setup");
    yesno_list_row("Setup", "bom_labour_processing");
    yesno_list_row("Setup", "bom_labour_cleaning");

	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 can_commit_bom_labour($bom_process)
{
    if($_POST['bom_labour_job_category_id'] == -1){
        display_error("Please select a Job Category");
        set_focus('bom_labour_job_category_id');
        return false;
    }
    
    if($_POST['bom_labour_rate_category_id'] == -1){
        display_error("Please select a Rate Category");
        set_focus('bom_labour_rate_category_id');
        return false;
    }
    
    if(!$bom_process){
        display_error("No details found for the selected BOM Process.");
        set_focus('bom_material_process_id');
        return false;
    }
    
    return true;
}


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


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

function save_bom_labour($selected_id, $new = true){
    
    $bom_process = get_bom_process($_POST['bom_labour_process_id']);
    
    if(can_commit_bom_labour($bom_process)){
        if($new){
            add_bom_labour($_POST['bom_parent'], get_post('bom_labour_process_id'), 
                                $_POST['bom_labour_job_category_id'],  $_POST['bom_labour_rate_category_id'], 
                                $_POST['bom_labour_setup'], $_POST['bom_labour_processing'], $_POST['bom_labour_cleaning']);
            
        }
        else{
            $id = substr($selected_id,7);
            if($id > 0){
                update_bom_labour($id, get_post('bom_labour_process_id'), 
                                $_POST['bom_labour_job_category_id'],  $_POST['bom_labour_rate_category_id'], 
                                $_POST['bom_labour_setup'], $_POST['bom_labour_processing'], $_POST['bom_labour_cleaning']);
            }
        }
        
        return array("success" => true); 
    }
    
    return array("success" => false); 
}

//--------------------------------------------------------------------------------------------------
if ($Mode=='ADD_ITEM') 
{
    $sbp_msg = save_bom_labour(-1);   
    if($sbp_msg['success'] === true){
        $Mode = '';
        $selected_id = -1;
    }
}

if ($Mode=='UPDATE_ITEM') 
{
    $sbp_msg = save_bom_labour($selected_id, false);  
    if($sbp_msg['success'] === true){
        $Mode = '';
        $selected_id = -1;
    }
}
//--------------------------------------------------------------------------------------------------

//if(isset($_POST['Add_BOM_Process'])){
//    $sbp_msg = save_bom_process();   
//    if($sbp_msg['success'] === true){
//        $Mode = '';
//        $selected_id = -1;
//    }
//    
//}
//
//if(isset($_POST['Update_BOM_Process'])){
//    $sbp_msg = save_bom_process(false);  
//    if($sbp_msg['success'] === true){
//        $Mode = '';
//        $selected_id = -1;
//    }
//}

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

if ($Mode == 'Delete')
{
    $id = substr($selected_id,7);
    delete_bom_labour($id);
    display_notification(_('Selected labour detail has been deleted'));
    
	$Mode = 'RESET';
}

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

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

global $Ajax;

start_form();

display_bom_labour($bom_parent);

$Ajax->Activate('bom_labour_controls');

if(substr($selected_id,0,6) == "Labour"){
    display_bom_labour_edit_controls($bom_parent, $Mode, $selected_id);
}
else{
    display_bom_labour_edit_controls($bom_parent, $Mode, -1);
}
    


end_form();

//end_page();

?>
