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

//$path_to_root = "..";
//
//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_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;
}

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




function display_costed_wo($wo_id)
{
    $wo_header= get_work_order_header($wo_id);
    
    $project_id = $wo_header['project_id'];    
    $user_id = $_SESSION['wa_current_user']->user;
    $cap = can_access_project($project_id, $user_id);
    if($cap['success'] !== true) 
    {
        display_error($cap['message']);
        set_focus('project_id');
        exit;
    }
    
    display_heading2("Workorder :".$wo_id.": ".$wo_header['stock_id']." - ".$wo_header["StockItemName"]);
    br();
    
    start_table(TABLESTYLE, "width=60%");
    $th = array(_("Project"),_("Budget"),_("Unit"),_("Quantity"), _("Start Date"),_("End Date"), "Status");        
    table_header($th);


    $des = get_stock_item_edit_info($wo_header['stock_id']);

    label_cell($wo_header['project_description']);
    label_cell($wo_header["task_description"]);
    label_cell($wo_header["requested_unit"]);
    $qty_wo=$wo_header["requested_unit_qty"]*$wo_header["req_unit_conv_factor"];
    qty_cell($qty_wo,false,null);
    label_cell(sql2date($wo_header["date_"]), "align=center");
    label_cell(sql2date($wo_header["required_by"]), "align=center");
    
    $status_text = get_workorder_display_status($wo_header);
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $wo_id."&source_type_id=".ST_WORKORDER, $status_text);
    label_cell($s);
//    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $workorder_id."&source_type_id=".ST_WORKORDER, "Approval Status");
//    label_row($s,$approval_statuses[$_POST["approval_status"]]);

    end_row();
    End_table();
        
    br();


    $wo_processes = get_wo_processes($wo_id);

    $total_cost = 0;
    $k = 0;
    while($wo_process = db_fetch($wo_processes))
    {
        display_wo_process_name($wo_process);
        display_wo_components_heading();

        $total_cost += display_wo_process_material($wo_id, $wo_process['id']);

        display_wo_labour_name();

        $total_cost += display_wo_process_labour($wo_id, $wo_process['id']);

    }
    display_total_wo_cost($total_cost);
    br(2);
    
    return $total_cost;
    
}



?>
