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

$page_security = 'SA_CREATE_GATE_PASS';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Purchase Remittance/SA_REP_PAY_VOUCHER
// ----------------------------------------------------------------
$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/services/includes/db/job_orders_db.inc"); 
include_once($path_to_root . "/services/includes/db/job_order_repairs_db.inc"); 
include_once($path_to_root . "/payroll/includes/db/employees_db.inc"); 
include_once($path_to_root . "/assets/includes/db/assets_db.inc");  
include_once($path_to_root . "/includes/db/inventory_db.inc");  

print_job_order_details();

function get_rep_job_material_requisition_details($jo_id) {
    $sql = "SELECT ard.*,
                am.description stock_description,
                am.stock_id,
                am.mb_flag,
                un.name unit_name,
                ps.description site_description, 
                pt.description task_description 
            FROM ".TB_PREF."job_material_requisition_details ard 
                JOIN ".TB_PREF."job_material_requisitions hd 
                    ON hd.type = ard.type 
                        AND hd.trans_no = ard.trans_no  
                LEFT JOIN ".TB_PREF."stock_master am 
                    ON ard.stock_id = am.stock_id 
                LEFT JOIN ".TB_PREF."item_units un  
                    ON ard.tran_unit = un.abbr  
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.project_site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
            WHERE hd.jo_id = ".db_escape($jo_id)." 
                AND hd.type = ".db_escape(ST_JOB_MATERIAL_REQ);
    
        return db_query($sql, "get_rep_job_material_requisition_details");
}
    
function print_job_order_details() {
    global $path_to_root, $approval_statuses, $systypes_array;

    include_once($path_to_root . "/reporting/includes/pdf_report.inc");
    
    $jo_id = trim($_POST['PARAM_0']);
    
    $job_row = get_job_order_record($jo_id);
        if (! is_array($job_row)) {
            display_error("Could not find ".$systypes_array[ST_JOB_ORDER]." #$jo_id.");
            return;
        }
    
        $cols = array(0, 60, 160, 210, 275, 360, 458, 528);
        $headers = array('', '', '', '', '', '', '');
        $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'left');

        $rep = new FrontReport(_('JOB CARD DETAILS'), "JobCardDetails", 'A4', 9, 'P');
    
            $jo_row = get_job_order_details($jo_id);
        
            $params = array();
            $params['gate_pass_no'] = $jo_row['id'];

                $rep->SetHeaderType('form_655');
                $rep->Font();
                $rep->Info($params, $cols, $headers, $aligns);

                    $rep->NewPage();
                        
                        $rep->row -= 55; 
                                
                                $row_line = $rep->row;

                                $rep->Font('bold');
                                $rep->Text(40, "Job Ref :");
                                $rep->Font();
                                $rep->Text(85, $jo_row['jo_ref']);
                                $rep->row -= 20; 
                                
                                $rep->Font('bold');
                                $rep->Text(40, "Job Date :");
                                $rep->Font();
                                $rep->Text(90, sql2date($jo_row['date_']));
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(40, "Vehicle/ Tool/ Location :");
                                $rep->Font();
                                $rep->Text(150, get_asset_formatted_name($jo_row['vehicle_id']));
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(40, "Request Department :");
                                $rep->Font();
                                $rep->Text(135, $jo_row['req_department_name']);
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(40, "Department :");
                                $rep->Font();
                                $rep->Text(100, $jo_row['department_name']);
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(40, "Section :");
                                $rep->Font();
                                $rep->Text(85, $jo_row['section_name']);
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(40, "Issuing Officer :");
                                $rep->Font();
                                $issuing_officer_emp_code = $jo_row['issuing_officer'];
                                if (strlen($jo_row['issuing_officer_emp_code']) > 1) {
                                    $issuing_officer_emp_code = get_employee_formatted_long_name($jo_row['issuing_officer_emp_code']);
                                }
                                $rep->Text(115, $issuing_officer_emp_code);
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(40, "Sectional Engineer :");
                                $rep->Font();
                                $rep->fontSize -= 1;
                                $rep->Text(130, get_employee_formatted_long_name($jo_row['engineer_code']));
                                $rep->fontSize += 1;
                                $rep->row -= 20; 

                                
                                $rep->row = $row_line; 


                                $rep->Font('bold');
                                $rep->Text(340, "Technical Officer :");
                                $rep->Font();
                                $rep->fontSize -= 1;
                                $rep->Text(420, get_employee_formatted_long_name($jo_row['tech_officer_code']));
                                $rep->fontSize += 1;
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(340, _("Customer Name :"));
                                $rep->Font();
                                if (isset($jo_row['customer_name']) && strlen($jo_row['customer_name']) > 0) {
                                    $rep->Text(420, $jo_row['customer_name']);
                                }
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(340, _("Customer Address :"));
                                $rep->Font();
                                if (isset($jo_row['customer_address']) && strlen($jo_row['customer_address']) > 0) {
                                    $rep->Text(430, $jo_row['customer_address']);
                                }
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(340, _("Current Owner :"));
                                $rep->Font();
                                if (isset($jo_row['next_job_owner_id']) && strlen($jo_row['next_job_owner_id']) > 0) {
                                    $rep->Text(410, get_employee_formatted_long_name($jo_row['next_job_owner_id']));
                                }
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(340, _("Instruction :"));
                                $rep->Font();
                                if (isset($jo_row['instruction_name']) && strlen($jo_row['instruction_name']) > 0) {
                                    $rep->Text(400, $jo_row['instruction_name']);   
                                }
                                $rep->row -= 20; 

                                $rep->Font('bold');
                                $rep->Text(340, _("Job Accepted :"));
                                $rep->Font();
                                $rep->Text(410, get_yes_no_name($jo_row['job_accepted']));
                                $rep->row -= 20;

                                $rep->Font('bold');
                                $rep->Text(340, _("Work Completed :"));
                                $rep->Font();
                                $rep->Text(420, get_yes_no_name($jo_row['job_completed']));
                                $rep->row -= 20;

                                $rep->Font('bold');
                                $rep->Text(340, _("Approval Status :"));
                                $rep->Font();
                                $rep->Text(420, $approval_statuses[$jo_row['approval_status']]);
                                $rep->row -= 20;


                                $rep->Font('bold');
                                $rep->Text(40, "Customer Complain/ Request :");
                                $rep->Font();
                                if (isset($jo_row['description']) && strlen($jo_row['description']) > 5) {
                                    $rep->NewLine();
                                    $rep->TextColLines(0, 7, $jo_row['description']);
                                } else {
                                    $rep->NewLine();
                                }
                                
                        $rep->NewLine();
                        
                        if ($rep->row < $rep->bottomMargin + ((5) * $rep->lineHeight)) {
                            $rep->NewPage();
                            $rep->row = $row_line;
                        }
                        
                        $rep->Font('bold');
                        $rep->Text(40, "Job Card Repair Details");
                        $rep->Font();
                        $rep->NewLine();
                        $result = get_job_order_repairs_details($jo_id);
                            while ($row = db_fetch($result)) {
                                $rep->NewLine();
                                if ($rep->row < $rep->bottomMargin + ((5) * $rep->lineHeight)) {
                                    $rep->NewPage();
                                    $rep->row = $row_line;
                                }
                                $rep->Text(40, $row['description']);
                            }
                        
            $rep->NewLine(3);
            // job material requisition details
            print_rep_job_material_requisition_details($rep, $row_line, $jo_id);   
            
            $rep->NewLine(3);
            // job material requisition details
            print_rep_sub_job_orders_details($rep, $row_line, $jo_id);            
            
            $rep->NewLine(3);
            // job material requisition details
            print_rep_direct_purchase_requisition_details($rep, $row_line, $jo_id);            

    $rep->End();
}

function print_rep_job_material_requisition_details(&$rep, $row_line, $jo_id) {
    global $daily_item_types_array;
    
    if ($rep->row < $rep->bottomMargin + ((5) * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $row_line;
    }

    $rep->Font('bold');
    $rep->Text(40, "Job Material Requisition Details");
    $rep->Font();
    $rep->NewLine(2);
    
    // header
    $rep->Line($rep->row + 15);
    
    $rep->Font('bold');
    $rep->Text(40, "Line");
    $rep->Text(80, "Trans #");
    $rep->Text(120, "Item Type");
    $rep->Text(185, "Item");
    $rep->Text(380, "Unit");
    $rep->Text(440, "Req.Qty");
    $rep->Text(480, "Iss.Qty");
    $rep->Text(530, "Ret.Qty");
    $rep->Font();
    $rep->NewLine();
    
    $rep->Line($rep->row + 5);
    
    $rep->fontSize -= 1;
        // line details
        $dec = 2;
        $result = get_rep_job_material_requisition_details($jo_id);
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ((2) * $rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $row_line;
                }
                
                $rep->Text(40, $row['trans_id']);
                $rep->Text(80, $row['trans_no']);
                $rep->Text(120, $daily_item_types_array[$row['item_type']]);
                if (in_array($row['item_type'], array(NSIT_STOCK_ITEM, NSIT_NON_STOCK_ITEM))) {
                    $rep->Text(185, get_stock_item_formatted_name($row['stock_id']));
                    $conversion_factor = $row['conversion_factor'];
                } else {
                    $rep->Text(185, $row['item_description']);
                    $conversion_factor = 1;
                }
                $rep->Text(380, $row['unit_name']);
                
                $rep->Text(440, number_format($row['tran_qty']), $dec);
                $rep->Text(480, number_format($row['issued_quantity'] / $conversion_factor), $dec);
                $rep->Text(530, number_format($row['returned_quantity'] / $conversion_factor), $dec);
            }
            
        $rep->Line($rep->row - 5);
    $rep->fontSize += 1;
}

function print_rep_sub_job_orders_details(&$rep, $row_line, $jo_id) {
    global $approval_statuses;
    
    if ($rep->row < $rep->bottomMargin + ((5) * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $row_line;
    }

    $rep->Font('bold');
    $rep->Text(40, "Sub Jobs Details");
    $rep->Font();
    $rep->NewLine(2);
    
    // header
    $rep->Line($rep->row + 15);
    
    $rep->Font('bold');
    $rep->Text(40, "Sub. #");
    $rep->Text(80, "Date");
    $rep->Text(130, "Department");
    $rep->Text(200, "Section");
    $rep->Text(280, "Current Owner");
    $rep->Text(500, "Approval.St");
    $rep->Font();
    $rep->NewLine();
    
    $rep->Line($rep->row + 5);
    
    $rep->fontSize -= 1;
        // line details
        $result = get_job_order_details_by_filters(parent_jo_id: $jo_id);
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ((5) * $rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $row_line;
                }
                
                $rep->Text(40, $row['id']);
                $rep->Text(80, sql2date($row['date_']));
                $rep->Text(130, $row['department_name']);
                $rep->Text(200, $row['section_name']);
                $rep->Text(280, get_employee_formatted_long_name($row['next_job_owner_id']));
                $rep->Text(500, $approval_statuses[$row['approval_status']]);
            }
            
        $rep->Line($rep->row - 5);
        
    $rep->fontSize += 1;
}

function print_rep_direct_purchase_requisition_details(&$rep, $row_line, $jo_id) {
    global $daily_item_types_array;
    
    if ($rep->row < $rep->bottomMargin + ((5) * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $row_line;
    }

    $rep->Font('bold');
    $rep->Text(40, "Job Material Requisition Details");
    $rep->Font();
    $rep->NewLine(2);
    
    // header
    $rep->Line($rep->row + 15);
    
    $rep->Font('bold');
    $rep->Text(40, "Line");
    $rep->Text(80, "Trans #");
    $rep->Text(120, "Item Type");
    $rep->Text(185, "Item");
    $rep->Text(380, "Unit");
    $rep->Text(440, "Req.Qty");
    $rep->Text(480, "Iss.Qty");
    $rep->Text(530, "Ret.Qty");
    $rep->Font();
    $rep->NewLine();
    
    $rep->Line($rep->row + 5);
    
    $rep->fontSize -= 1;
        // line details
        $dec = 2;
        $result = get_rep_job_material_requisition_details($jo_id);
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ((2) * $rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $row_line;
                }
                
                $rep->Text(40, $row['trans_id']);
                $rep->Text(80, $row['trans_no']);
                $rep->Text(120, $daily_item_types_array[$row['item_type']]);
                if (in_array($row['item_type'], array(NSIT_STOCK_ITEM, NSIT_NON_STOCK_ITEM))) {
                    $rep->Text(185, get_stock_item_formatted_name($row['stock_id']));
                    $conversion_factor = $row['conversion_factor'];
                } else {
                    $rep->Text(185, $row['item_description']);
                    $conversion_factor = 1;
                }
                $rep->Text(380, $row['unit_name']);
                
                $rep->Text(440, number_format($row['tran_qty']), $dec);
                $rep->Text(480, number_format($row['issued_quantity'] / $conversion_factor), $dec);
                $rep->Text(530, number_format($row['returned_quantity'] / $conversion_factor), $dec);
            }
            
        $rep->Line($rep->row - 5);
    $rep->fontSize += 1;
}

?>