<?php

function job_order_manual_costs(&$jo_id) {
    global $systypes_array, $path_to_root, $Ajax;
    
    $tp_name = $systypes_array[ST_JOB_MANUAL_COST];
    
    $Ajax->activate('job_order_manual_cost');
    div_start('job_order_manual_cost');
        display_job_order_manual_costs($jo_id);

        start_table();
            hyperlink_params_separate($path_to_root . "/inventory/job_manual_costs.php", "Create a New $tp_name", "jo_id=$jo_id");
        end_table();
    div_end();
}

function display_job_order_manual_costs($jo_id) {
    global $approval_statuses, $path_to_root, $systypes_array;
    
    display_heading($systypes_array[ST_JOB_MANUAL_COST]." Headers");    
    
	start_table(TABLESTYLE, "width=70%");
        
            $th = array(_("Id"), _("Trans #"), _("Reference"), _("Location"), _("Tran Date"), _("Approval Status"), _("Total"), _("")); 
            table_header($th);
            
            $k = $total_cost = 0;  
            $result = get_job_material_issue_headers_by_filters($jo_id, type: ST_JOB_MANUAL_COST);
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);

                        $id = $row['id'];
			label_cell($id, 'align=center');
			label_cell(get_inventory_trans_view_str($row['type'], $row['trans_no']), 'align=center');
			label_cell($row['reference']);
			label_cell($row['location_name']);
			label_cell(sql2date($row['tran_date']), 'align=center');
			label_cell($approval_statuses[$row['approval_status']], 'align=center');
                        
                        $jmc_total = $row['total'];
                        if ($jmc_total > 0) {
                            qty_cell($jmc_total, false, 2);
                        } else {
                            label_cell('');
                        }
                        
                        if (in_array($row['approval_status'], array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR))) {
                            $link = "<a target='_blank' href='$path_to_root/inventory/job_manual_costs.php?ModifyJMC_No=$id'>Edit</a>";
                            label_cell($link, 'align=center');
                        }  elseif (in_array($row['approval_status'], array(AS_APPROVED))) {
                            $link = "<a target='_blank' href='$path_to_root/inventory/job_manual_cost_returns.php?jmc_no=$id'>Return</a>";
                            label_cell($link, 'align=center');
                        } else {
                            label_cell('');
                        }
                        
                    end_row();
                        
                    if ($row['approval_status'] != AS_REJECTED) {
                        $total_cost += $jmc_total;
                    }
                }
                
                start_row();
                    label_cell(text_bold('Manual Cost Total'), 'align=right colspan=6');
                    if ($total_cost > 0) {
                        qty_cell($total_cost, true, 2);
                    } else {
                        label_cell('');
                    }
                    label_cell('', 'colspan=1');
                end_row();

    end_table(2);
}

?>