<?php
/**********************************************************************
***********************************************************************/


$page_security = 'SA_MANUF_BOM';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/trace.php");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
	
page(_($help_context = "Bill of Material"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");

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

include_once($path_to_root . "/includes/inventory.inc");


$user_comp = user_company();


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

if (isset($_GET['payroll_id']))
{
	$_POST['payroll_id'] = $_GET['payroll_id'];
}

$payroll_id = get_post('bom_parent');

if (list_updated('payroll_id')) {
	$_POST['NewEmployeeCode'] = $payroll_id = get_post('payroll_id');
    //clear_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}

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


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

function display_bom_header($selected_parent, $bom_header)
{
	//$bom_header = get_bom_header($selected_parent);
	div_start('bom');
	start_table(TABLESTYLE2);


    if ($bom_header) {
        $_POST['header_tran_unit'] = $bom_header["tran_unit"]; // by Tom Moulton
        $_POST['header_comments']  = $bom_header["comment"];
        $_POST['header_tran_qty'] = number_format2($bom_header["tran_qty"], get_qty_dec($bom_header["parent"]));
    }
    else{
        $_POST['header_tran_unit'] = '';
        $_POST['header_comments']  = '';
        $_POST['header_tran_qty'] = number_format2(0, get_qty_dec($bom_header["parent"]));
    }

    units_list_row("Unit:", "header_tran_unit", $selected_parent);

	$dec = get_qty_dec($selected_parent);
	$_POST['header_tran_qty'] = number_format2(input_num('header_tran_qty',1), $dec);
	qty_row(_("Quantity:"), 'header_tran_qty', null, null, null, $dec);
    textarea_row(_("Comments:"), "header_comments", null, 31, 2);
    
    	end_table(1);
	div_end();
    
    if ($bom_header) {
        submit_center_first('Update_BOM_Header', _("Update"), '', 'default');
        submit_center_last('Delete_BOM_Header', _("Delete"), _("Cancel Edition"), 'cancel');
    }
    else
    {
        submit_center_first('Add_BOM_Header', _("Save"), '', 'default');
    }

}

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

function can_commit_bom_header($alt_unit_info)
{
    if($_POST['header_tran_unit'] == ''){
        display_error("Please select units");
        set_focus('header_tran_unit');
        return false;
    }
    
    if($_POST['header_tran_qty'] <= 0){
        display_error("Quantity entered can not be zero or negative.");
        set_focus('header_tran_qty');
        return false;
    }
    
    if(!$alt_unit_info){
        display_error("Alternative unit conversion factor is not defined for the selected unit.");
        set_focus('header_tran_unit');
        return false;
    }
    return true;
}


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

function save_bom_header($new = true){
    $alt_unit_info = get_alternate_unit_info($_POST['bom_parent'], $_POST['header_tran_unit']);
    if(can_commit_bom_header($alt_unit_info)){
        if($new){
            add_bom_header($_POST['bom_parent'], $_POST['header_tran_qty'] * $alt_unit_info['convertion_factor'], 
                            $_POST['header_tran_unit'], $_POST['header_tran_qty'], 
                            $alt_unit_info['convertion_factor'], $_POST['header_comments']);
        }
        else{
            update_bom_header($_POST['bom_parent'], $_POST['header_tran_qty'] * $alt_unit_info['convertion_factor'], 
                            $_POST['header_tran_unit'], $_POST['header_tran_qty'], 
                            $alt_unit_info['convertion_factor'], $_POST['header_comments']);
        }
    }
}

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

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

function general_settings($selected_parent)
{
    $bom_header = get_bom_header($selected_parent);
    
    display_bom_header($selected_parent, $bom_header);
}

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

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

if(isset($_POST['Add_BOM_Header'])){
    save_bom_header();    
}

if(isset($_POST['Update_BOM_Header'])){
    save_bom_header(false);    
}


start_form(true);

if (!isset($_POST['payroll_id']))
        $_POST['payroll_id'] = get_global_payroll_id ();



//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
//value_list_row('Select Payroll:', 'payroll_id', 'PAYROLLS', $_POST['payroll_id'],false,true);

start_table(TABLESTYLE_NOBORDER);
start_row();
stock_manufactured_items_list_cells(_("Select a manufacturable item:"), 'bom_parent', null, false, true);
//hidden('bom_parent', $_POST['bom_parent']);
end_row();
//if (list_updated('bom_parent'))
//	$Ajax->activate('_page_body');


//	start_row();
//    stock_items_list_cells(_("Select an item:"), 'bom_parent', null,
//	  _('New item'), true, check_value('show_inactive'));
//    
//	$new_item = get_post('stock_id')=='';
//	check_cells(_("Show inactive:"), 'show_inactive', null, true);
//	end_row();
    
end_table();

if (list_updated('bom_parent')){
	$Ajax->activate('_page_body');
    $Ajax->activate('bom');
    
}
//$result = get_all_table_values($_POST['table_id']);

set_global_payroll_id($_POST['payroll_id']);

$Ajax->activate('details');

div_start('details');

$bom_parent = get_post('bom_parent');

$bom_header = get_bom_header($bom_parent);
if (!$bom_header){
	unset($_POST['_tabs_sel']); // force settings tab for new customer
}

tabbed_content_start('tabs', array(
		'settings' => array(_('&General settings'), $bom_header),
        'bom_processes' => array(_('&Processes'), $bom_header),
        'bom_material' => array(_('&Material'), $bom_header),
		'bom_labour' => array(_('&Labour'), $bom_header),
	));
	
	switch (get_post('_tabs_sel')) {
		default:
         case 'settings':    
            general_settings($_POST['bom_parent']); 
			break;
            
        case 'bom_processes':
            $_GET['bom_parent'] = $bom_parent;
			$_GET['popup'] = 1;
			include_once($path_to_root."/manufacturing/bom_processes.php"); 
			break;
		case 'bom_material':
			$_GET['bom_parent'] = $bom_parent;
			$_GET['popup'] = 1;
			include_once($path_to_root."/manufacturing/bom_material.php");
			break;
		case 'bom_labour':
			$_GET['bom_parent'] = $bom_parent;
			$_GET['popup'] = 1;
			include_once($path_to_root."/manufacturing/bom_labour.php");
			break;
	};
br();
tabbed_content_end();

div_end();


hidden('popup', @$_REQUEST['popup']);

end_form();

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

end_page(@$_REQUEST['popup']);
?>
