<?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=7 align=center");
    end_row();
}



function display_bom_process_material($parent, $process_id)
{
    $result = get_bom_items($parent, $process_id);
	
	$k = 0;
	while ($myrow = db_fetch($result))
	{

		alt_table_row_color($k);

		label_cell($myrow["component"]);
		label_cell($myrow["description"]);
        label_cell($myrow["location_name"]);
        label_cell($myrow["units"]);
        qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
        
 		edit_button_cell("Edit"."Material_".$myrow['id'], _("Edit"));
 		delete_button_cell("Delete"."Material_".$myrow['id'], _("Delete"));
        end_row();

	} //END WHILE LIST LOOP

}

function display_bom_material($parent)
{
    $result = get_bom_processes($parent);
    div_start('bom');
	start_table(TABLESTYLE, "width=60%");
	$th = array(_("Process"), _("Component"), _("Location"),
		_("Unit"), _("Quantity"),'','');
	table_header($th);

	$k = 0;
	while ($myrow = db_fetch($result))
	{
        display_bom_process_name($myrow);
    
        display_bom_process_material($parent, $myrow['id']);
        
	} 
    end_table();
	div_end();
}

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

function display_bom_material_edit_controls($parent, &$Mode, $selected_id)
{
    global $Ajax;
    
    div_start('bom_material_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,9);
			$myrow = get_component_from_bom($id);

			$_POST['bom_material_process_id'] = $myrow["process_id"];
            $_POST['bom_material_loc_code'] = $myrow["loc_code"];
			$_POST['bom_material_component'] = $myrow["component"]; // by Tom Moulton
			//$_POST['bom_material_workcentre_id']  = $myrow["workcentre_added"];
            $_POST['bom_material_tran_unit']  = $myrow["tran_qty"];
			$_POST['bom_material_tran_qty'] = number_format2($myrow["tran_qty"], get_qty_dec($myrow["component"]));
		}
		hidden('selected_id', $selected_id);
	}
	
    br(2);
    
    bom_processes_list_row('Process', 'bom_material_process_id', $parent);

    start_row();
    label_cell(_("Component:"), "class='label'");

    echo "<td>";
    if(isset($_POST['bom_material_component'])){
        echo stock_component_items_list('bom_material_component', $parent, $_POST['bom_material_component'], false, true);
    }
    else{
        echo stock_component_items_list('bom_material_component', $parent, null, false, true);
    }
    if (list_updated('bom_material_component')) 
    {
        $Ajax->activate('bom_material_tran_qty');
        $Ajax->activate('bom_material_tran_unit');
    }
    echo "</td>";
    end_row();
	
	hidden('stock_id', $parent);

	locations_list_row(_("Location to Draw From:"), 'bom_material_loc_code', null);
    if(isset($_POST['bom_material_component'])){
        units_list_row("Unit", 'bom_material_tran_unit', $_POST['bom_material_component']);
    }
    else{
        units_list_row("Unit", 'bom_material_tran_unit', '');
    }
	//workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
	$dec = get_qty_dec(get_post('bom_material_component'));
	$_POST['bom_material_tran_qty'] = number_format2(input_num('bom_material_tran_qty',1), $dec);
	qty_row(_("Quantity:"), 'bom_material_tran_qty', null, null, null, $dec);
    
    
	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_material($alt_unit_info, $bom_process)
{
    if($_POST['bom_material_tran_unit'] == ''){
        display_error("Please select units");
        set_focus('bom_material_tran_unit');
        return false;
    }
    
    if(input_num('bom_material_tran_qty') <= 0){
        display_error("Quantity entered can not be zero or negative.");
        set_focus('bom_material_tran_qty');
        return false;
    }
    
    if(!$alt_unit_info){
        display_error("Alternative unit conversion factor is not defined for the selected unit.");
        set_focus('bom_material_tran_unit');
        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_material($selected_id, $new = true){
    
    $alt_unit_info = get_alternate_unit_info($_POST['bom_parent'], $_POST['bom_material_tran_unit']);
    $bom_process = get_bom_process($_POST['bom_material_process_id']);
    
    if(can_commit_bom_material($alt_unit_info, $bom_process)){
        if($new){
            add_bom_material($_POST['bom_parent'], $_POST['bom_material_process_id'], 
                            $_POST['bom_material_component'], $bom_process['workcentre_id'], 
                            $_POST['bom_material_loc_code'], input_num('bom_material_tran_qty') * $alt_unit_info['convertion_factor'], 
                            $_POST['bom_material_tran_unit'], input_num('bom_material_tran_qty'), 
                            $alt_unit_info['convertion_factor']);
            
        }
        else{
            $id = substr($selected_id,9);
            if($id > 0){
                update_bom_material($id, get_post('bom_material_process_id'), 
                                $_POST['bom_material_component'], $bom_process['workcentre_id'], 
                                $_POST['bom_material_loc_code'], input_num('bom_material_tran_qty') * $alt_unit_info['convertion_factor'], 
                                $_POST['bom_material_tran_unit'], input_num('bom_material_tran_qty'), 
                                $alt_unit_info['convertion_factor']);
            }
        }
        
        return array("success" => true); 
    }
    
    return array("success" => false); 
}

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

if ($Mode=='UPDATE_ITEM') 
{
    $sbp_msg = save_bom_material($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,9);
        delete_bom_material($id);
        display_notification(_('Selected item 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_material($bom_parent);

$Ajax->Activate('bom_material_controls');

if(substr($selected_id,0,8) == "Material"){
    display_bom_material_edit_controls($bom_parent, $Mode, $selected_id);
}
else{
    display_bom_material_edit_controls($bom_parent, $Mode, -1);
}
    


end_form();

//end_page();

?>
