<?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_processes($selected_parent)
{

	div_start('bom');
	start_table(TABLESTYLE, "width=80%");
	$th = array(_("Sequence"), _("Description"), _("Workcentre"), 
                "Process Type", _("Process Item"), _("Process Qty"),
                _("Setup Time"), _("Processing Time"), _("Cleaning Time"), 
                _("QC"), _("Comment"), "", "");
	table_header($th);

    $result = get_bom_processes($selected_parent);
    
    $k = 0;
	while ($bom_process = db_fetch($result))
	{

		alt_table_row_color($k);

		label_cell($bom_process["sequence"]);
        label_cell($bom_process["description"]);
        label_cell($bom_process["workcentre_name"]);
        label_cell($bom_process["process_type_name"]);
        label_cell($bom_process["item_description"]);
        label_cell($bom_process["item_quantity"]);
        qty_cell($bom_process["setup_time"], false, 0);
        qty_cell($bom_process["processing_time"], false, 0);
        qty_cell($bom_process["cleaning_time"], false, 0);
        label_cell(get_yes_no_name($bom_process["qc"]));
        label_cell($bom_process["comment"]);

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

    }
	end_table();
	div_end();
}


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

function display_bom_process_edit_controls($parent, &$Mode, $selected_id)
{
    
    start_table(TABLESTYLE2);
    
    $_POST['bom_process_id'] = 0;
    
    if ($selected_id != -1)
	{
 		if ($Mode == 'Edit') {
			//editing a selected component from the link to the line item
            
            $id = substr($selected_id,8);
			$myrow = get_bom_process($id);

			$_POST['bom_process_id'] = $myrow["id"];
            $_POST['bom_process_sequence'] = $myrow["sequence"];
            $_POST['bom_process_description'] = $myrow["description"];
			$_POST['bom_process_workcentre_id'] = $myrow["workcentre_id"]; // by Tom Moulton
            $_POST['bom_process_type_id'] = $myrow["type_id"];
			$_POST['bom_process_item_description']  = $myrow["item_description"];
            $_POST['bom_process_quantity']  = $myrow["item_quantity"];
            $_POST['bom_process_setup_time']  = $myrow["setup_time"];
            $_POST['bom_process_processing_time']  = $myrow["processing_time"];
            $_POST['bom_process_cleaning_time']  = $myrow["cleaning_time"];
            $_POST['bom_process_qc']  = $myrow["qc"];
            $_POST['bom_process_comment']  = $myrow["comment"];
		}
		hidden('selected_id', $selected_id);
	}
	else
	{

	}
    
    br();
	hidden('stock_id', $parent);
    hidden('bom_process_id', $_POST['bom_process_id']);
    
    qty_row("Sequence", 'bom_process_sequence', null, null, null, 0);
    text_row("Description:", 'bom_process_description', null, 50, 200);
    workcenter_list_row("Workcentre", 'bom_process_workcentre_id', null, "Select a Workcentre");
    value_list_row("Process Type", "bom_process_type_id", "WO_PROCESS_TYPES", null, "Select a Type");
    text_row("Item Description:", 'bom_process_item_description', null, 50, 200);
    qty_row(_("Quantity:"), 'bom_process_quantity', null, null, null, 0);
    
	qty_row(_("Setup Time:"), 'bom_process_setup_time', null, null, null, 0);
    qty_row(_("Processing Time:"), 'bom_process_processing_time', null, null, null, 0);
    qty_row(_("Cleaning Time:"), 'bom_process_cleaning_time', null, null, null, 0);
    yesno_list_row("QC:", 'bom_process_qc');
    textarea_row(_("Comment:"), "bom_process_comment", null, 31, 2);
    
    
	end_table(1);
	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 can_commit_bom_process()
{
    if(!check_num('bom_process_sequence',0)){
        display_error("The sequence number entered is not valid.");
        set_focus('bom_procees_sequence');
        return false;
    }
    
    if(!(get_post('bom_process_workcentre_id') > 0)){
        display_error("Please select a workcentre.");
        set_focus('bom_process_workcentre_id');
        return false;
    }
    
    if(!(get_post('bom_process_type_id') > 0)){
        display_error("Please select a process type.");
        set_focus('bom_process_type_id');
        return false;
    }
    
    if(!check_num('bom_process_quantity',0)){
        display_error("Item quantity entered should be numeric and positive.");
        set_focus('bom_procees_sequence');
        return false;
    }
    
    return true;
}

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


function can_delete_bom_proces($parent, $process_id)
{
    $bom_items = get_bom_items($parent, $process_id);
    $m = db_num_rows($bom_items);
    if($m > 0){
        display_error("This process can not be deleted. There are items attached to it.");
        set_focus('bom_procees_sequence');
        return false;
    }
    
    
    $bom_labour = get_bom_labour($parent, $process_id);
    $l = db_num_rows($bom_labour);
    if($l > 0){
        display_error("This process can not be deleted. There is labour attached to it.");
        set_focus('bom_procees_sequence');
        return false;
    }
    
    return true;
}


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

function save_bom_process($new = true){
    
    if(can_commit_bom_process()){
        
        if(input_num('bom_process_quantity') == 0){
            $bomh = get_bom_header($_POST['stock_id']);
            $item_quantity = $bomh['tran_qty'];
        }
        else{
            $item_quantity = input_num('bom_process_quantity');
        }
        
        if($new){
            add_bom_process($_POST['stock_id'], input_num('bom_process_sequence'), 
                            $_POST['bom_process_description'], $_POST['bom_process_workcentre_id'], 
                            $_POST['bom_process_item_description'], $item_quantity, 
                            input_num('bom_process_setup_time'), input_num('bom_process_processing_time'), 
                            input_num('bom_process_cleaning_time'), $_POST['bom_process_qc'], 
                            $_POST['bom_process_comment'], $_POST['bom_process_type_id']);
        }
        else{
            $bom_process_id = get_post('bom_process_id') ;
            if($bom_process_id > 0){
                update_bom_process(get_post('bom_process_id'), input_num('bom_process_sequence'), 
                                $_POST['bom_process_description'], 
                                $_POST['bom_process_item_description'], $item_quantity, 
                                input_num('bom_process_setup_time'), input_num('bom_process_processing_time'), 
                                input_num('bom_process_cleaning_time'), $_POST['bom_process_qc'], 
                                $_POST['bom_process_comment'], $_POST['bom_process_type_id']);
            }
        }
        
        return array("success" => true); 
    }
    
    return array("success" => false); 
}

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

if ($Mode=='UPDATE_ITEM') 
{
    $sbp_msg = save_bom_process(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')
{
    $process_id = substr($selected_id,8);
    if(can_delete_bom_proces($bom_parent, $process_id)){
        delete_bom_process($process_id);
        display_notification(_('Selected process has been deleted'));
        $Mode = 'RESET';
    }
}

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

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


start_form();

display_bom_processes($bom_parent);

if(substr($selected_id,0,7) == "Process"){
    display_bom_process_edit_controls($bom_parent, $Mode, $selected_id);
}
else{
    display_bom_process_edit_controls($bom_parent, $Mode, -1);
}
    


end_form();

//end_page();

?>
