<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_BOM_COST_INQ';
$path_to_root = "../..";
include_once($path_to_root . "/includes/session.inc");

page(_($help_context = "Costed Bill Of Material Inquiry"));

include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/includes/data_checks.inc");


function display_bom_process_name($wo_material_process)
{
    br();
    $name = $wo_material_process["sequence"]." - ".$wo_material_process["workcentre_name"]." - ".$wo_material_process["description"];
    display_heading($name);
    
}

function display_bom_component_heading()
{
   
    display_heading2("Component Details");
   
}
function display_bom_labour_name()
{
    br();
    display_heading2("Labour Details");
   
}


function display_bom_process_material($item, $process_id)
{
    $th = array(_("Material"),_("Description"),_("Location"),_("Unit"),  _("Quantity"),_("Unit Cost"), _("Total Cost"));
    
    start_table(TABLESTYLE, "width=60%");
    table_header($th);
   
    $result = get_bom_items($item,$process_id);
	$subtotal=0;
        $total=0;
	$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["tran_unit"]);
            qty_cell($myrow["tran_qty"], false, get_qty_dec($myrow["parent"]));
            amount_cell($myrow["standard_cost"]*$myrow["conversion_factor"]);
            amount_cell($myrow["ComponentCost"]*$myrow["conversion_factor"]);

            end_row();
            $subtotal +=$myrow["ComponentCost"]*$myrow["conversion_factor"];
            $total +=$subtotal;
	} 
        
    start_row();   
    label_cell("","colspan=5");
    label_cell("<b>"."Sub Total"."<b>");
    amount_cell($subtotal, true,"align=right");
    end_row();
    
    end_table();
    
    return $subtotal;
}

function display_bom_process_labour($parent, $process_id)
{
    $th = array(_("Job Category"),_("Rate Category"),_("Setup"),_("Processing"),  _("Cleaning"),  _("Comments"), _("Total"));
    
    start_table(TABLESTYLE, "width=60%");
    table_header($th);
    
    $result = get_bom_labour($parent, $process_id);
	$total=0;
	$k = 0;
        $total_cost=0;
	while ($myrow = db_fetch($result))
	{

            alt_table_row_color($k);
            $cost_setup_time=($myrow["rate"]/3600)*$myrow["setup_time"];
            $cost_processing_time=($myrow["rate"]/3600)*$myrow["processing_time"];
            $cost_cleaning_time=($myrow["rate"]/3600)*$myrow["cleaning_time"];
            
            label_cell($myrow["job_category_name"]);
            label_cell($myrow["rate_category_name"]);
            amount_cell($cost_setup_time);
            amount_cell($cost_processing_time);
            amount_cell($cost_cleaning_time);
            $total = $cost_setup_time+$cost_processing_time+$cost_cleaning_time;
            
            label_cell($myrow["comment"]);
            amount_cell($total);
            end_row();
            $total_cost += $total;
	} 
        
        
        start_row();   
        label_cell("","colspan=5");
        label_cell("<b>"."Sub Total"."<b>");
        amount_cell($total_cost, true,"align=right");
        end_row();
        
        end_table();
        
        return $total_cost;
}

function display_total_bom_cost($cost)
{
    br();
    start_row();   
    label_cell("<b>"."BOM Total"."</b>");
    amount_cell($cost, true,"align=right");
    end_row();
   
    
}




function display_costed_bom($item_check)
{
    

       
        start_table(TABLESTYLE, "width=40%");
        $th = array(_("Manufacturing Item"),_("Description"),_("Unit"),_("Quantity"));        
        table_header($th);
        
        $bom_header=get_bom_header($item_check);
        $des = get_stock_item_edit_info($item_check);
        label_cell($bom_header["parent"]);
        label_cell($des["description"]);
        label_cell($bom_header["tran_unit"]);
        $qty_bom=$bom_header["quantity"]*$bom_header["conversion_factor"];
        qty_cell($qty_bom,false,null);
        end_row();
        End_table();
        
        br();
        
        
        $process=get_bom_processes($item_check);
        

        $total_cost = 0;
        $k = 0;
        while($bom_proces=db_fetch($process))
        {
           
            display_bom_process_name($bom_proces);
            display_bom_component_heading();
            
            $total_cost += display_bom_process_material($item_check, $bom_proces['id']);
            
            display_bom_labour_name();
                        
            $total_cost += display_bom_process_labour($item_check, $bom_proces['id']);
            
        }
        display_total_bom_cost($total_cost);
         br(2);
    
}



check_db_has_bom_stock_items(_("There are no manufactured or kit items defined in the system."));

if (isset($_GET['stock_id']))
{
	$_POST['stock_id'] = $_GET['stock_id'];
} 
if (list_updated('stock_id'))
		$Ajax->activate('_page_body');

start_form(false, true);
start_table(TABLESTYLE_NOBORDER);
stock_manufactured_items_list_row(_("Select a manufacturable item:"), 'stock_id', null, false, true);
end_table();
br();
display_heading(_("BOM Details"));
display_costed_bom($_POST['stock_id']);

end_form();

end_page();
?>
