<?php

function display_job_order_cost_view($jo_id) {
    global $systypes_array;
    $tp_name = $systypes_array[ST_JOB_ORDER];
    
        display_heading("$tp_name #$jo_id");        br();
    
        view_job_order_header($jo_id);        br();
    
        start_outer_table(false, "width=100%");

            view_job_order_cost($jo_id);

        end_outer_table(2);
}

function view_job_order_materials_cost($th, $jo_id, $types_array, $approval_array, $order_by) {
    global $systypes_array;
    
        $x = $k = $total = 0;
        $result = get_job_material_issue_details_by_filters($jo_id, types_array: $types_array, approval_array: $approval_array, order: $order_by);      
            while ($row = db_fetch($result)) {
                if ($x >= 20) {
                    table_header($th);
                    $x = 0;
                }

                alt_table_row_color($k);

                    label_cell($systypes_array[$row["type"]]);
                    label_cell(get_trans_view_str($row['type'], $row['trans_no']), 'align=center');
                    if (! is_null($row["stock_id"]) > 0) {
                        label_cell(get_stock_item_formatted_name($row["stock_id"]));
                    } else {
                        label_cell($row["item_description"]);
                    }
                    label_cell(sql2date($row["tran_date"]), 'align=center');
                    label_cell($row['location_name']);
                    label_cell($row['tran_unit_name'], 'align=center');

                    $plu_min = 1;
                    if (in_array($row['type'], array(ST_JOB_MATERIAL_RETURN, ST_PERSONAL_MATERIAL_RETURN, ST_JOB_MANUAL_COST_RETURN))) {
                        $plu_min *= -1;
                    }
                    qty_cell($row["tran_qty"] * $plu_min);
                    $base_qty = $row["qty"] * $plu_min;
                    qty_cell($base_qty);
                    
                    if (! in_array(ST_SALVAGE_RETURN, $types_array)) {
                        $standard_cost = $row["standard_cost"];
                        qty_cell($standard_cost);

                        $line_total = $base_qty * $standard_cost;
                        qty_cell($line_total);
                        
                        $total += $line_total;
                    } else {
                        label_cell(''); label_cell('');
                    }

                end_row();    

                $x++;
            }
            
            return $total;
}

function view_job_order_cost($jo_id) {
    global $systypes_array;
    
    start_table(TABLESTYLE, "width=88%");

        $th = array(_("Type"), _("Trans #"), _("Item"), _("Date"), _("Location"), _("Unit"), _("Tran Qty"), _("Base Qty"), _("Standard Cost"), _("Amount"));  
        table_header($th);

        $k = $x = $material_issue_return_total = $job_manual_cost_return_total = $labour_total = 0; 
        
        start_row("style=background-color:#0ec9ba");
            label_cell(text_bold($systypes_array[ST_JOB_MATERIAL_ISSUE]), "align=center colspan=10");
        end_row();
        
        $types_array = array(ST_JOB_MATERIAL_ISSUE, ST_JOB_MATERIAL_RETURN, ST_PERSONAL_MATERIAL_ISSUE, ST_PERSONAL_MATERIAL_RETURN);
        $approval_array = array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR, AS_APPROVED, AS_COMPLETED, AS_CLOSED);
        $order_by = 'ard.tran_date, ard.type, ard.trans_no';
        
            $material_issue_return_total = view_job_order_materials_cost($th, $jo_id, $types_array, $approval_array, $order_by);

            start_row("style=background-color:#8ce6ab");
                label_cell(text_bold('Materials Total'), "align=right colspan=9");
                qty_cell($material_issue_return_total, true);
            end_row();
        

        // job manual costs and returns
        table_header($th);

        $x = 0;
        vertical_space('height=18px  colspan=10');
        start_row("style=background-color:#0ec9ba");
            label_cell(text_bold($systypes_array[ST_JOB_MANUAL_COST]), "align=center colspan=10");
        end_row();
            
            $job_manual_cost_return_total = view_job_order_materials_cost($th, $jo_id, array(ST_JOB_MANUAL_COST, ST_JOB_MANUAL_COST_RETURN), $approval_array, $order_by);

        start_row("style=background-color:#8ce6ab");
            label_cell(text_bold('Manual Cost Total'), "align=right colspan=9");
            qty_cell($job_manual_cost_return_total, true);
        end_row();
        
        
        // job manual costs and returns
        table_header($th);

        $x = 0;
        vertical_space('height=18px  colspan=10');
        start_row("style=background-color:#0ec9ba");
            label_cell(text_bold($systypes_array[ST_SALVAGE_RETURN]), "align=center colspan=10");
        end_row();
            
            view_job_order_materials_cost($th, $jo_id, array(ST_SALVAGE_RETURN), $approval_array, $order_by);

        
        // job_order_labours_details
        
        $x = 0;
        vertical_space('height=18px  colspan=10');
        start_row("style=background-color:#0ec9ba");
            label_cell(text_bold('Labour'), "align=center colspan=10");
        end_row();
        
        $th_2 = array(_("Trans #"), _("Type"), _("Employee"), _("From Date"), _("To Date"), _("Rate Category"), _("No. Days Worked"), _(""), _("Rate"), _("Amount") ); 
        table_header($th_2);
        
            $result_2 = get_job_order_labours_details($jo_id);
                while ($row = db_fetch($result_2)) {
                    if ($x >= 20) {
                        table_header($th_2);
                        $x = 0;
                    }
                    
                    alt_table_row_color($k);

			label_cell($systypes_array[ST_JOB_LABOUR]);
			label_cell($row['id'], 'align=center');
                        $labour_name = get_employee_formatted_long_name($row['employee_code']);
			label_cell($labour_name);
			label_cell(sql2date($row['from_date']), 'align=center');
			label_cell(sql2date($row['to_date']), 'align=center');
			label_cell($row['rate_category_name']);
                        $num_days_worked = $row['num_days_worked'];
			label_cell($num_days_worked, 'align=center');
			label_cell('', 'align=center');
                        $rate = $row['rate'];
			qty_cell($rate);
                        $line_total = $num_days_worked * $rate;
			qty_cell($line_total);
                        
                    end_row();
                    
                $labour_total += $line_total;
                $x++;
            }
            
        start_row("style=background-color:#8ce6ab");
            label_cell(text_bold('Labour Total'), "align=right colspan=9");
            qty_cell($labour_total, true);
        end_row();
            
        vertical_space('height=18px  colspan=10');
        start_row("style=background-color:#e6de8c");
            label_cell(text_bold('Total Cost'), "align=right colspan=9");
            qty_cell($material_issue_return_total + $job_manual_cost_return_total + $labour_total, true);
        end_row();
        
    end_table(2);
}

?>