<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MANUFTRANSVIEW';
$path_to_root = "../..";

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

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

include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Work Order"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------
$woid = 0;
if ($_GET['trans_no'] != "")
{
	$woid = $_GET['trans_no'];
}

display_heading($systypes_array[ST_WORKORDER] . " # " . $woid);

br(1);

$myrow = get_work_order($woid);

function display_wo_process_name($wo_process)
{
    br();
    $name = $wo_process["sequence"]." - ".$wo_process["workcentre_name"]." - ".$wo_process["description"];
    display_heading($name);
    
}
function display_wo_components_heading()
{
   
    display_heading2("Component Details");
   
}
function display_wo_labour_name()
{
    br();
    display_heading2("Labour Details");
   
}

function display_wo_process_material($wo_id, $process_id)
{
    $th = array(_("Material"),_("Description"),_("Location"),_("Unit"),  _("Quantity"),_("Unit Cost"), _("Total Cost"));
    
    start_table(TABLESTYLE, "width=60%");
    table_header($th);
   
    $result = get_wo_items($wo_id, $process_id);
	$subtotal=0;
    $total=0;
	$k = 0;
	while ($myrow = db_fetch($result))
	{

            alt_table_row_color($k);
            label_cell($myrow["stock_id"]);
            label_cell($myrow["description"]);
            label_cell($myrow["location_name"]);
            label_cell($myrow["req_unit"]);
            qty_cell($myrow["req_unit_qty"], false, get_qty_dec($myrow["stock_id"]));
            amount_cell($myrow["standard_cost"]*$myrow["req_unit_conversion_factor"]);
            amount_cell($myrow["ComponentCost"]*$myrow["req_unit_conversion_factor"]);

            end_row();
            $subtotal +=$myrow["ComponentCost"]*$myrow["req_unit_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_wo_process_labour($wo_id, $process_id)
{
    $th = array(_("Job Category"),_("Rate Category"),_("Setup"),_("Processing"),  _("Cleaning"),  _("Comments"), _("Total"));
    
    start_table(TABLESTYLE, "width=60%");
    table_header($th);
    
    $result = get_wo_labour($wo_id, $process_id);
	$total=0;
	$k = 0;
    $total_cost=0;
	while ($myrow = db_fetch($result))
	{
            alt_table_row_color($k);
            if(!isset($myrow["rate"])){
                $dd = 3;
            }
            
            $cost_setup_time = 0;
            if($myrow["setup"] == 1){
                $cost_setup_time=($myrow["rate"]/3600)*$myrow["setup_time"];
            }
            
            $cost_processing_time = 0;
            if($myrow["processing"] == 1){
                $cost_processing_time=($myrow["rate"]/3600)*$myrow["processing_time"];
            }
            
            $cost_cleaning_time = 0;
            if($myrow["cleaning"] == 1){
                $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;
}

if ($myrow["type"]  == WO_ADVANCED)
	display_wo_details($woid, true);
else
	display_wo_details_quick($woid, true);

echo "<center>";

// display the WO requirements
br(1);
if ($myrow["released"] == false)
{
    display_heading2(_("BOM for item:") . " " . $myrow["StockItemName"]);
    display_bom($myrow["stock_id"]);
}
else
{
//	display_heading2(_("Work Order Requirements"));
        $wo_processes = get_wo_processes($woid);
        while($wo_process = db_fetch($wo_processes))
        {
            display_wo_process_name($wo_process);
            display_wo_components_heading();

            display_wo_process_material($woid, $wo_process['id']);

            display_wo_labour_name();

            display_wo_process_labour($woid, $wo_process['id']);

        }
//	display_wo_requirements($woid, $myrow["units_reqd"]);
	if ($myrow["type"] == WO_ADVANCED)
	{
    	echo "<br><table cellspacing=7><tr valign=top><td>";
    	display_heading2(_("Issues"));
    	display_wo_issues($woid);
    	echo "</td><td>";
    	display_heading2(_("Productions"));
    	display_wo_productions($woid);
    	echo "</td><td>";
//    	display_heading2(_("Additional Costs"));
//    	display_wo_payments($woid);
    	echo "</td></tr></table>";
	}
//	else
//	{
//    	echo "<br><table cellspacing=7><tr valign=top><td>";
//    	display_heading2(_("Additional Costs"));
//    	display_wo_payments($woid);
//    	echo "</td></tr></table>";
//	}
}

echo "<br></center>";

is_voided_display(ST_WORKORDER, $woid, _("This work order has been voided."));

end_page(true, false, false, ST_WORKORDER, $woid);

?>
