<?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 . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/db/crm_contacts_db.inc");
include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc"); 
include_once($path_to_root . "/sales/includes/db/customers_db.inc"); 
include_once($path_to_root . "/payroll/includes/db/employees_db.inc"); 
include_once($path_to_root . "/inventory/includes/ui/create_gate_pass_ui.inc"); 
include_once($path_to_root . "/inventory/includes/db/create_gate_pass_db.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc");  
include_once($path_to_root . "/inventory/includes/db/material_transfer_requisitions_db.inc");  
//----------------------------------------------------------------------------------------------------

print_gate_pass();

function get_material_requisition_transfer($mrt_no) {
    $sql = "SELECT mrt.mrt_no trans_no,
                mrt.mrt_no,
                mrt.reference, 
                rl.location_name to_location,
                il.location_name from_location,
                mrt.project_id, 
                pm.description project_description,
                mrt.comments, 
                u.real_name requestor_name,
                mrt.created_by,
                mrt.created_on, 
                mrt.row_state,
                mrt.source_mtr_no,
                il.location_name issuing_location_name,  
                rl.location_name receiving_location_name,
                mrt.approval_status, 
                mrt.source_mtr_no,
                gp.* ,
                vh.plate_number 
                
            FROM ".TB_PREF."material_requisition_transfers mrt 
                LEFT JOIN ".TB_PREF."projects_master pm 
                    ON mrt.project_id = pm.project_id 
                INNER JOIN ".TB_PREF."users u 
                    ON mrt.created_by = u.id 
                INNER JOIN ".TB_PREF."locations il 
                    ON mrt.issuing_location = il.loc_code 
                INNER JOIN ".TB_PREF."locations rl 
                    ON mrt.receiving_location = rl.loc_code 
                INNER JOIN ".TB_PREF."gate_passes gp  
                    ON mrt.type = gp.type  
                        AND mrt.mrt_no = gp.trans_no   
                LEFT JOIN ".TB_PREF."vehicles vh 
                    ON gp.vehicle_id = vh.id 
            WHERE mrt.mrt_no = ".db_escape($mrt_no);
    
    $result = db_query($sql, "get_material_requisition_transfer");
    return db_fetch($result);
}

function get_mrt_details($mrt_no) {
    $sql = "SELECT mrtd.trans_id id,
                mrtd.trans_no mrt_no,
                mrtd.stock_id,
                mrtd.stock_id item_code,
                mrtd.tran_date,
                mrtd.tran_unit,
                mrtd.tran_qty,
                mrtd.convertion_factor,
                mrtd.qty,
                mrtd.issued_qty,
                mrtd.task_id,
                mrtd.project_site_id,
                mrtd.ref_line_id,
                mrtd.price,
                CONCAT('[', sm.stock_id, '] ', sm.description) item_description,
                sm.units,
                sm.high_value_item,
                sm.sample_item,
                sm.reference_cost, 
                sm.material_cost + sm.labour_cost + sm.overhead_cost AS standard_cost, 
                sm.stock_control_method,
                pt.description task_description,
                ps.description project_site_description 
                
            FROM ".TB_PREF."material_requisition_transfer_details mrtd 
                INNER JOIN ".TB_PREF."stock_master sm 
                    ON mrtd.stock_id = sm.stock_id 
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON mrtd.project_site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON mrtd.task_id = pt.task_id 
            WHERE mrtd.trans_no = ".db_escape($mrt_no)." 
            ORDER BY mrtd.trans_id";
    
        return db_query($sql, "get_mrt_details");
}

function get_pri_header($pri_no) {
    $sql = "SELECT pri.pri_no trans_no, pri.*, 
                    rl.location_name issuing_location_name,
                    'N/A' receiving_location_name, 
                    rl.location_name from_location,
                    u.real_name created_user_name,
                    pm.description project_description,
                    gp.*,
                    vh.plate_number  
            FROM ".TB_PREF."purchase_requisition_issues pri,
                    ".TB_PREF."locations rl,
                    ".TB_PREF."users u,
                    ".TB_PREF."projects_master pm,
                    ".TB_PREF."gate_passes gp 
                    LEFT JOIN ".TB_PREF."vehicles vh 
                        ON gp.vehicle_id = vh.id 
            WHERE pri.issuing_location = rl.loc_code AND 
                    pri.created_by = u.id AND  
                    pri.project_id = pm.project_id AND 
                    gp.type = ".db_escape(ST_PR_ISSUE)." AND 
                    pri.pri_no = gp.trans_no AND 
                    pri_no = ".db_escape($pri_no);
    
    $result = db_query($sql, 'get_pri_header');
    return db_fetch($result);
}

function get_pr_issue_details($pri_no) {
    $sql = "SELECT prid.*,
                pri.issuing_location,
                pri.project_id,
                grni.grn_batch_id grn_no, 
                grn.loc_code location,
                pri.source_pr_no pr_no,
                prd.issued_qty,
                prd.returned_qty,
                CONCAT('[', sm.stock_id, '] ', sm.description) item_description 
            FROM ".TB_PREF."purchase_requistion_issue_details prid   
                JOIN ".TB_PREF."purchase_requisition_issues pri 
                    ON pri.pri_no = prid.pri_no 
                JOIN ".TB_PREF."purchase_requisition_details prd 
                    ON prd.id = prid.pr_line_id 
                LEFT JOIN ".TB_PREF."grn_items grni  
                    ON grni.id = prid.grn_item_id  
                LEFT JOIN ".TB_PREF."grn_batch grn   
                    ON grn.id = grni.grn_batch_id 
                JOIN ".TB_PREF."stock_master sm 
                    ON prid.item_code = sm.stock_id 
            WHERE prid.pri_no = ".db_escape($pri_no);
    
    return db_query($sql, "get_pr_issue_details");
}

function get_material_issue_header($im_no) {
    $sql = "SELECT im.im_no trans_no, im.*, 
                    il.location_name issuing_location_name,
                    'N/A' receiving_location_name,
                    il.location_name from_location,
                    pm.description project_description,
                    u.real_name created_user_name,
                    gc.company_name 
            FROM ".TB_PREF."issue_materials im
                    LEFT JOIN ".TB_PREF."locations il ON
                            im.issuing_location = il.loc_code 
                    LEFT JOIN ".TB_PREF."projects_master pm ON
                            pm.project_id = im.project_id
                    LEFT JOIN ".TB_PREF."users u ON
                            im.created_by = u.id 
                    LEFT JOIN group_companies gc ON
                            im.company_code = gc.company_code 
            WHERE im_no = '".$im_no."'";
    
    $result = db_query($sql, 'get_material_issue_header');
    return db_fetch($result);
    
}

function get_issue_material_details($im_no) {
    $sql = "SELECT  imd.*,
                    CONCAT('[', sm.stock_id, '] ', sm.description) item_description,
                    pt.description task_description,
                    ps.description site_description 
            FROM ".TB_PREF."issue_material_details imd 
                    LEFT JOIN ".TB_PREF."stock_master sm ON  
                            imd.item_code = sm.stock_id 
                    LEFT JOIN ".TB_PREF."project_tasks pt ON  
                            imd.task_id = pt.task_id  
                    LEFT JOIN ".TB_PREF."project_sites ps ON  
                            imd.site_id = ps.id 
            WHERE imd.im_no = '".$im_no."'";
    
    return db_query($sql, 'get_issue_material_details');
}

function get_location_transfer_header($id) {
    $sql = "SELECT st.id trans_no, st.*, 
                il.location_name issuing_location_name,
                il.location_name from_location,
                rl.location_name receiving_location_name,
                rl.location_name to_location,
                pm.description project_description,
                u.real_name created_user_name 
            FROM ".TB_PREF."stock_transfers st
                JOIN ".TB_PREF."locations il 
                    ON st.location_from = il.loc_code 
                JOIN ".TB_PREF."locations rl 
                    ON st.location_to = rl.loc_code 
                JOIN ".TB_PREF."projects_master pm 
                    ON pm.project_id = st.project_id
                JOIN ".TB_PREF."users u 
                    ON st.created_by = u.id 
            WHERE st.id = ".db_escape($id);
    
    $result = db_query($sql, 'get_location_transfer_header');
    return db_fetch($result);
}

function get_stock_moves_details($trans_no, $type) {
    $sql = "SELECT sm.*,
                CONCAT('[', sma.stock_id, '] ', sma.description) item_description 
            FROM ".TB_PREF."stock_moves sm 
                LEFT JOIN ".TB_PREF."stock_master sma 
                    ON sma.stock_id = sm.stock_id 
            WHERE sm.trans_no = ".db_escape($trans_no)." 
                AND sm.type = ".db_escape($type)." 
                AND sm.tran_qty > 0";
    
    return db_query($sql, 'get_stock_moves_details');
}

function get_stock_adjustment_header_details($trans_no) {
    $sql = "SELECT sa.id trans_no, 
                sa.*,
                l.location_name issuing_location_name,
                l.location_name receiving_location_name,
                pm.description project_description 
            FROM ".TB_PREF."stock_adjustments sa 
                INNER JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                INNER JOIN ".TB_PREF."locations l 
                    ON sa.location = l.loc_code 
            WHERE sa.id = ".db_escape($trans_no);
    
    $result = db_query($sql, "get_stock_adjustment_header_details");
    return db_fetch($result);
}

function get_daily_item_issue_header($trans_no) {
    $sql = "SELECT sa.*,
                l.location_name issuing_location_name,
                l.location_name receiving_location_name,
                pm.description project_description 
               
            FROM ".TB_PREF."daily_item_issues sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.type = ".db_escape(ST_DAILY_ITEM_ISSUE)." 
                AND sa.trans_no = ".db_escape($trans_no)." 
                AND sa.approval_status = ".db_escape(AS_APPROVED);
    
        $result = db_query($sql, "get_daily_item_issue_header_type_trans_no");
        return db_fetch($result);
}

function get_daily_item_issue_details($trans_no, $type) {
    $sql = "SELECT am.editable, ard.*,
                am.description stock_description,
                am.stock_id,
                un.name unit_name,
                ps.description site_description, 
                IF (am.editable = 1, CONCAT('[', ard.stock_id, '] ', ard.item_description), 
                    IF (LENGTH(ard.stock_id) > 0, CONCAT('[', am.stock_id, '] ', am.description), ard.item_description)) item_description 
            FROM ".TB_PREF."daily_item_issue_details ard 
                JOIN ".TB_PREF."locations l 
                    ON ard.loc_code = l.loc_code 
                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
            WHERE ard.trans_no = ".db_escape($trans_no)." 
                AND ard.type = ".db_escape($type);
    
        return db_query($sql, "get_daily_item_issue_details");
}

function get_daily_tool_issue_header($trans_no) {
    $sql = "SELECT sa.*,
                l.location_name issuing_location_name,
                l.location_name receiving_location_name,
                pm.description project_description,
                u.real_name requestor_name 
               
            FROM ".TB_PREF."daily_tool_issues sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."asset_locations l 
                    ON sa.loc_code = l.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.type = ".db_escape(ST_DAILY_TOOL_ISSUE)." 
                AND sa.trans_no = ".db_escape($trans_no)." 
                AND sa.approval_status = ".db_escape(AS_APPROVED);
    
        $result = db_query($sql, "get_daily_tool_issue_header");
        return db_fetch($result);
}

function get_daily_tool_issue_details($trans_no, $type, $order_by='ard.id') {
    $sql = "SELECT ard.*, 'N/A' tran_unit, ard.quantity tran_qty,
                am.description item_description,
                am.asset_id,
                ps.description site_description, 
                ar.description rate_description, 
                pt.description task_description 
            FROM ".TB_PREF."daily_tool_issue_details ard 
                JOIN ".TB_PREF."asset_locations l 
                    ON ard.loc_code = l.loc_code 
                JOIN ".TB_PREF."asset_master am 
                    ON ard.asset_id = am.asset_id 
                LEFT JOIN ".TB_PREF."tool_rates ar 
                    ON am.rate_category_id = ar.id 
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
            WHERE ard.trans_no = ".db_escape($trans_no)." 
                AND ard.type = ".db_escape($type);
                    
        if (strlen($order_by) > 0) {
            $sql .= " ORDER BY $order_by ";
        }
        
        return db_query($sql, "get_daily_tool_issue_details");
}

function get_raw_material_issue_header_type_trans_no($trans_no) {
    $sql = "SELECT sa.*,
                l.location_name issuing_location_name,
                pm.description project_description,
                u.real_name requestor_name 
               
            FROM ".TB_PREF."raw_material_issues sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.type = ".db_escape(ST_RAW_MATERIAL_ISSUE)." 
                AND sa.trans_no = ".db_escape($trans_no)." 
                AND sa.approval_status = ".db_escape(AS_APPROVED);
    
        $result = db_query($sql, "get_raw_material_issue_header_type_trans_no");
        return db_fetch($result);
}

function get_raw_material_issue_details($trans_no, $type, $order_by='ard.id') {
    $sql = "SELECT ard.*,
                CONCAT('[', ard.stock_id, '] ', am.description) item_description,
                am.stock_id,
                un.name unit_name,
                ps.description site_description, 
                pt.description task_description 
            FROM ".TB_PREF."raw_material_issue_details ard 
                JOIN ".TB_PREF."locations l 
                    ON ard.loc_code = l.loc_code 
                JOIN ".TB_PREF."stock_master am 
                    ON ard.stock_id = am.stock_id 
                JOIN ".TB_PREF."item_units un  
                    ON ard.tran_unit = un.abbr  
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
            WHERE ard.trans_no = ".db_escape($trans_no)." 
                AND ard.type = ".db_escape($type);
                    
        if (strlen($order_by) > 0) {
            $sql .= " ORDER BY $order_by ";
        }
        
        return db_query($sql, "get_raw_material_issue_details");
}

function get_asset_transfer_header_trans_no($trans_no) {
    $sql = "SELECT at.*, at.trans_no,
                fl.location_name issuing_location_name, tl.location_name receiving_location_name,
                fp.description from_project, tp.description project_description, tp.project_id, 
                u.real_name requestor_name 
               
            FROM ".TB_PREF."asset_transfers at  
                JOIN ".TB_PREF."projects_master fp   
                    ON at.from_project_id = fp.project_id 
                JOIN ".TB_PREF."projects_master tp   
                    ON at.to_project_id = tp.project_id 
                JOIN ".TB_PREF."asset_locations fl 
                    ON at.from_asset_location = fl.loc_code 
                JOIN ".TB_PREF."asset_locations tl 
                    ON at.to_asset_location = tl.loc_code 
                JOIN ".TB_PREF."users u 
                    ON at.created_by = u.id 
            WHERE at.trans_no = ".db_escape($trans_no)." 
                AND at.type = ".db_escape(ST_ASSET_TRANSFER)." 
                AND at.approval_status = ".db_escape(AS_APPROVED);
    
        $result = db_query($sql, "get_asset_transfer_header_trans_no");
        return db_fetch($result);
}

function get_asset_transfer_details($trans_no, $type, $order_by='atd.id') {
    $sql = "SELECT atd.*,
                CONCAT('[', atd.asset_id, '] ', am.asset_code, '-', am.description, ' (', am.asset_id, ')') item_description,
                atd.quantity tran_qty, 'N/A' tran_unit 
            FROM ".TB_PREF."asset_transfer_details atd 
                JOIN ".TB_PREF."asset_master am 
                    ON atd.asset_id = am.asset_id 
            WHERE atd.trans_no = ".db_escape($trans_no)." 
                AND atd.type = ".db_escape($type);
                    
        if (strlen($order_by) > 0) {
            $sql .= " ORDER BY $order_by ";
        }
        
        return db_query($sql, "get_asset_transfer_details");
}

function get_asset_requisition_transfer_header_type_trans_no($trans_no) {
    $sql = "SELECT at.*, at.trans_no,
                fl.location_name issuing_location_name, tl.location_name receiving_location_name,
                fp.description from_project, tp.description project_description, tp.project_id, 
                u.real_name requestor_name 
               
            FROM ".TB_PREF."asset_transfer_requests atr  
                JOIN ".TB_PREF."asset_transfers at  
                    ON atr.id = at.atr_id  
                        AND at.type = ".db_escape(ST_ASSET_REQUISITION_TRANSFER)." 
                JOIN ".TB_PREF."projects_master fp   
                    ON atr.from_project_id = fp.project_id 
                JOIN ".TB_PREF."projects_master tp   
                    ON atr.to_project_id = tp.project_id 
                JOIN ".TB_PREF."asset_locations fl 
                    ON atr.from_asset_location = fl.loc_code 
                JOIN ".TB_PREF."asset_locations tl 
                    ON atr.to_asset_location = tl.loc_code 
                JOIN ".TB_PREF."users u 
                    ON at.created_by = u.id 
            WHERE at.trans_no = ".db_escape($trans_no)." 
                AND at.type = ".db_escape(ST_ASSET_REQUISITION_TRANSFER);
    
        $result = db_query($sql, "get_asset_requisition_transfer_header_type_trans_no");
        return db_fetch($result);
}

function get_project_name($project_id) {
    $sql = "SELECT CONCAT(project_id, ' ', description) project_name 
            FROM ".TB_PREF."projects_master
            WHERE project_id = ".db_escape($project_id);
            
        $result = db_query($sql, "get_project_name");
        return db_result_value($result, 'project_name');
 }
    
function get_gate_pass_details_by_trans_no_trans_type($trans_type, $trans_no) {
    $sql = "SELECT gp.*,
                    vh.plate_number 
            FROM ".TB_PREF."gate_passes gp 
                LEFT JOIN ".TB_PREF."vehicles vh 
                    ON gp.vehicle_id = vh.id 
            WHERE gp.type = ".db_escape($trans_type)." 
                AND gp.trans_no = ".db_escape($trans_no);
    
    $result = db_query($sql, 'get_gate_pass_details_by_trans_no_trans_type');
    return db_fetch($result);
}



function print_column_headers(&$rep, $start_row) {
    $right = $rep->pageWidth - $rep->rightMargin;
    $rep->row = $start_row;
    $rep->Font('bold');
        $count = count($rep->headers);
        $rep->cols[$count] = $right - 3;
        for ($i = 0; $i < $count; $i++) 
            $rep->TextColCenter($i, $i + 1, $rep->headers[$i], -2);
        $rep->Font();
}

function display_gp_header_details(&$rep, $header, $gp_row) {
        $rep->row += 10;
                        
        $row_line = $rep->row;

        $rep->row = $row_line;
        $rep->Text(25, "From Location :");
        $rep->Font('bold');
        $rep->Text(98, $header['from_location']);
        $rep->Font();
        $rep->row -= 16; 

        $rep->Text(25, "To Location :");
        $rep->Font('bold');
        $rep->Text(98, $header['to_location']);
        $rep->Font();
        $rep->row -= 16; 

        $rep->Text(25, _("Project :"));
        $rep->Font('bold');
        $rep->Text(98, $header['project_id'].' '.$header['project_description']);
        $rep->Font();
        $rep->row -= 16; 

        $rep->Text(25, _("Gate Pass No :"));
        $rep->Font('bold');
        $rep->Text(98, $gp_row['id']);
        $rep->Font();

        $rep->row = $row_line; 

        $rep->Text(340, _("Date :"));
        $rep->Font('bold');
        $rep->Text(420, date("Y/m/d h:i:sa"));
        $rep->Font();
        $rep->row -= 16; 

        $rep->Text(340, _("Vehicle No :"));
        $rep->Font('bold');
        $rep->Text(420, strlen($gp_row['vehicle_no']) > 0 ? $gp_row['vehicle_no'] : $gp_row['plate_number']);
        $rep->Font();
        $rep->row -= 16; 

        $rep->Text(340, _("Driver Name :"));
        $rep->Font('bold');
        $rep->Text(420, $gp_row['driver_name']);
        $rep->Font();
        $rep->row -= 16; 

        $rep->Text(340, _("Transaction No :"));
        $rep->Font('bold');
        $rep->Text(420, $header['trans_no']);
        $rep->Font();
        $rep->row -= 4;
}

function draw_payment_items_table($column_headers_start, &$rep) {
    $right = $rep->pageWidth - $rep->rightMargin;
    
    $rep->row = $column_headers_start;
    $cols = count($rep->cols);
    
    $iline7 = $column_headers_start + 12;
    $iline5 = $column_headers_start - 5;
    
    $increase_height = 185;

    $rep->LineTo(25, $iline7, 570, $iline7);
    $rep->LineTo(25, $iline5, 570, $iline5);
    $rep->LineTo(25, $iline7 - $increase_height, 570, $iline7 - $increase_height);
    
//    $rep->Line($iline7);
//    $rep->Line($iline5);
//    $rep->Line($iline7 - $increase_height);

    $top_line_row = $iline7;

//    $rep->LineTo($rep->leftMargin, $top_line_row ,$rep->leftMargin, $iline7 - $increase_height);
    $rep->LineTo(25, $top_line_row, 25, $iline7 - $increase_height);
    $rep->LineTo(50, $top_line_row, 50, $iline7 - $increase_height);
    $rep->LineTo(390, $top_line_row, 390, $iline7 - $increase_height);
    $rep->LineTo(450, $top_line_row, 450, $iline7 - $increase_height);
//    $rep->LineTo($rep->cols[$cols - 5], $top_line_row, $rep->cols[$cols - 5], $iline7 - $increase_height);
//    $rep->LineTo($rep->cols[$cols - 4], $top_line_row, $rep->cols[$cols - 4], $iline7 - $increase_height);
    $rep->LineTo(500, $top_line_row, 500, $iline7 - $increase_height);
    $rep->LineTo(570, $top_line_row, 570, $iline7 - $increase_height);
//    $rep->LineTo($right, $top_line_row, $right, $iline7 - $increase_height);
}

function print_gate_pass() {
    global $path_to_root;

    include_once($path_to_root . "/reporting/includes/pdf_report.inc");
    
    $trans_type = $_POST['PARAM_0'];
    $trans_no = $_POST['PARAM_1'];
    $vehicle_id = $_POST['PARAM_2'];
    $vehicle_no = $_POST['PARAM_3'];
    $driver_name = $_POST['PARAM_4'];
    $comment = $_POST['PARAM_5'];
    
    $msg = save_gate_pass($trans_type, $trans_no, $vehicle_id, $vehicle_no, $driver_name, $comment);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
            return;
        }
    
    $cols = array(4, 60, 160, 210, 275, 360, 458, 528);

    $aligns = array('center', 'left', 'center', 'right', 'right', 'right', 'right');
    $headers  = array(_("Date"),_(" Type"), _("#"), _(" Reference"), _("Total Amount"), _("Left to Allocate"), _("This Allocation"));

    $cur = get_company_Pref('curr_default');

    $rep = new FrontReport(_('GATE PASS'), "GatePass", 'A5', 9, 'L');
    
        $receiving_paroject_description = 'N/A';
        switch ($trans_type) {
            case ST_REQTRANSFER:
                $header = get_material_requisition_transfer($trans_no);
                $mtr_project = get_mtr_header_record_by_id($header['source_mtr_no']);
                $receiving_paroject_description = get_project_name($mtr_project['project_id']);

                break;
            case ST_PR_ISSUE:
                $header = get_pri_header($trans_no);

                break;
            case ST_MATERIAL_ISSUE:
                $header = get_material_issue_header($trans_no);

                break;
            case ST_LOCTRANSFER:
                $header = get_location_transfer_header($trans_no);
                $receiving_paroject_description = get_project_name($header['project_id']);

                break;
            case ST_INVENTORY_ISSUE:
                $header = get_stock_adjustment_header_details($trans_no);
                $receiving_paroject_description = $header['project_description'];

                break;
            case ST_DAILY_ITEM_ISSUE:
                $header = get_daily_item_issue_header($trans_no);
                $receiving_paroject_description = $header['project_description'];

                break;
            case ST_DAILY_TOOL_ISSUE:
                $header = get_daily_tool_issue_header($trans_no);
                $receiving_paroject_description = $header['project_description'];

                break;
            case ST_RAW_MATERIAL_ISSUE:
                $header = get_raw_material_issue_header_type_trans_no($trans_no);
                $receiving_paroject_description = $header['project_description'];

                break;
            case ST_ASSET_TRANSFER:
                $header = get_asset_transfer_header_trans_no($trans_no);
                $receiving_paroject_description = $header['project_description'];

                break;
            case ST_ASSET_REQUISITION_TRANSFER:
                $header = get_asset_requisition_transfer_header_type_trans_no($trans_no);
                $receiving_paroject_description = $header['project_description'];

                break;
        }
        
        $gp_row = get_gate_pass_details_by_trans_no_trans_type($trans_type, $trans_no);
        
            $params['trans_no'] = $header['trans_no'];
            $params['trans_type'] = $trans_type;
            $params['issuing_location_name'] = $header['issuing_location_name'];
            
            if ($trans_type == ST_REQTRANSFER || $trans_type == ST_LOCTRANSFER || $trans_type == ST_ASSET_TRANSFER 
                || $trans_type == ST_ASSET_REQUISITION_TRANSFER) {
                $params['receiving_location_name'] = $header['receiving_location_name'];
            } else {
                $params['receiving_location_name'] = 'N/A';
            }
            $params['project_description'] = $header['project_id'].' '.$header['project_description'];
            $params['receiving_project_description'] = $receiving_paroject_description;
            $params['gate_pass_no'] = $gp_row['id'];
            $params['vehicle_no'] = strlen($gp_row['vehicle_no']) > 0 ? $gp_row['vehicle_no'] : $gp_row['plate_number'];
            $params['driver_name'] = $gp_row['driver_name'];

                $rep->SetHeaderType('form_652');
                $rep->currency = $cur;
                $rep->Font();
                $rep->Info($params, $cols, $headers, $aligns);
                

                    $rep->NewPage();
                        
                        $brs = 17;
                        $returnable = 'No';
                        $dec = 2;
                        
                        switch ($trans_type) {
                            case ST_REQTRANSFER:
                                $items_result = get_mrt_details($trans_no);

                                break;
                            case ST_PR_ISSUE:
                                $items_result = get_pr_issue_details($trans_no);

                                break;
                            case ST_MATERIAL_ISSUE:
                                $items_result = get_issue_material_details($trans_no);

                                break;
                            case ST_LOCTRANSFER:
                                $items_result = get_stock_moves_details($trans_no, ST_LOCTRANSFER);

                                break;
                            case ST_INVENTORY_ISSUE:
                                $items_result = get_stock_moves_details($trans_no, ST_INVENTORY_ISSUE);

                                break;
                            case ST_DAILY_ITEM_ISSUE:
                                $items_result = get_daily_item_issue_details($trans_no, ST_DAILY_ITEM_ISSUE);

                                break;
                            case ST_DAILY_TOOL_ISSUE:
                                $items_result = get_daily_tool_issue_details($trans_no, ST_DAILY_TOOL_ISSUE);
                                $returnable = 'Yes';

                                break;
                            case ST_RAW_MATERIAL_ISSUE:
                                $items_result = get_raw_material_issue_details($trans_no, ST_RAW_MATERIAL_ISSUE);

                                break;
                            case ST_ASSET_TRANSFER:
                                $items_result = get_asset_transfer_details($trans_no, ST_ASSET_TRANSFER);
                                $returnable = 'Yes';
                                $dec = 0;

                                break;
                            case ST_ASSET_REQUISITION_TRANSFER:
                                $items_result = get_asset_transfer_details($trans_no, ST_ASSET_REQUISITION_TRANSFER);
                                $returnable = '';
                                $dec = 0;

                                break;
                        }
                        
                        $rep->row -= 16; 
                        
                        $seq_no = 1;
                        $add_new_line = false;
                            while ($item = db_fetch($items_result)) {
                                if ($add_new_line) {
                                    $rep->NewLine();
                                }
                                $rep->Text(32, $seq_no < 10 ? "0$seq_no" : $seq_no);
                                $rep->Text(55, $item['item_description']);
                                $rep->Text(412, $item['tran_unit']);
                                if ($trans_type == ST_ASSET_REQUISITION_TRANSFER || $trans_type == ST_ASSET_TRANSFER) {
                                    $rep->TextCol(5, 6, number_format($item['tran_qty'], $dec)."    ");
                                } else {
                                    $rep->TextCol(5, 6, number_format($item['tran_qty'], $dec));
                                }
                                $rep->Text(526, $returnable);
                                
                                $add_new_line = true;
                                
                                $brs--;
                                
                                $seq_no++;
                                
                                    $pg_h = $rep->bottomMargin + (5 * $rep->lineHeight);
                                    
                                    if ($rep->row < $pg_h) {
                                        $rep->NewPage();
                                        
                                        $brs = 17;
                                    }
                            }
                            
                            
                            if ($brs > 0 && $add_new_line === true) {
                                $rep->NewLine($brs);
                            } else {
                                $rep->NewLine(16);
                            }
                        
                        if ($rep->row < $rep->bottomMargin + (1 * $rep->lineHeight)) {
                            $rep->NewPage();
                            $rep->NewLine(2);
                        }
                        
                        $rep->Text(30, "Driver's Signature");
                        $rep->Text(104, _("..........................................."));
                        $rep->Text(220, "Supervisor/ Manager");
                        $rep->Text(305, _("..........................................."));
                        $rep->Text(418, "Received By");
                        $rep->Text(470, _("..........................................."));

    $rep->End();
}

?>