<?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 . "/inventory/includes/db/job_material_issues_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");  
include_once($path_to_root . "/purchasing/includes/db/direct_purchase_requisitions_db.inc");  

print_job_order_details();

function rep_get_job_order_labour_details($jo_id) {
    $sql = "SELECT la.*, emp.employee_reference 
            FROM ".TB_PREF."job_order_labours la  
                JOIN ".TB_PREF."employee_master emp   
                    ON la.employee_code = emp.employee_code   
            WHERE la.jo_id = ".db_escape($jo_id)."
            ORDER BY la.from_date";
        
	return db_query($sql, "rep_get_job_order_labour_details");
}

function print_job_order_details() {
    global $path_to_root, $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;
        }
    
        $x = 0;
        $cols = array($x, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50, $x+=50);
        $headers = array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
        $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left', 'left');

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

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

                $rep->NewPage();
                
                    $current_row = $rep->row;
                    
                    $rep->row -= ($current_row - $rep->lineHeight);
                    $rep->fontSize -= 4;
                    $rep->Text(700, _("CMC - Workshop Management System - NG3"));
                    $rep->fontSize += 4;
                    $rep->row += ($current_row - $rep->lineHeight);
                    
                    draw_print_job_card_header_section_one($rep, $jo_row);
                    
                    draw_print_job_card_header_section_two($rep, $jo_row);
                    
                    draw_print_job_card_header_section_three($rep, $jo_row);
                    
                    $rep->row = 316; 
                    
                    $rep->Text(50, _("........................................................."));
                    $rep->Text(350, _("........................................................."));
                    $rep->Text(650, _("........................................................."));
                    
                    $rep->row -= 15; 
                    $rep->Text(50, _("Issuing Officer (Job Office)"));
                    $rep->Text(350, _("Sectional Engineer"));
                    $rep->Text(650, _("Technical Officer"));
                    
                    $rep->row -= 20; 
                    $rep->Text(50, _("Date: ......................................."));
                    $rep->Text(350, _("Date: ......................................."));
                    $rep->Text(650, _("Date: ......................................."));
                                
                    
                // job material issues and returns
                $new_page = draw_print_job_material_issues_returns_details($rep, $jo_id);
                    
                // sub jobs
                $sub_pos_array = draw_print_sub_jobs_details($rep, $jo_id, $new_page);
                
                $new_page = false; // because sub job detail and direct purchase are starting on same line
                    
                // direct purchase orders
                $dpr_pos_array = draw_print_direct_purchase_orders_details($rep, $jo_id, $sub_pos_array["start_line"], $sub_pos_array["y_position"], $new_page);
                    if ($new_page === true) {
                        $sub_pos_array["end_line"] = $rep->pageHeight - $rep->topMargin;
                    }
                    
                // service orders
                $srv_ord_array = draw_print_service_orders_details($rep, $jo_id, $sub_pos_array["end_line"], $new_page);
                    if ($new_page === true) {
                        $dpr_pos_array["end_line"] = $rep->pageHeight - $rep->topMargin;
                    }
                    
                // way bills
                $wayb_pos_array = draw_way_bills_details($rep, $jo_id, $dpr_pos_array["end_line"], $new_page);
                    if ($new_page === true) {
                        $srv_ord_array["end_line"] = $rep->pageHeight - $rep->topMargin;
                    }
                    
                // recommendations
                $rcmn_pos_array = draw_print_recommendation_details($rep, $srv_ord_array["end_line"], $new_page);
                    if ($new_page === true) {
                        $wayb_pos_array["end_line"] = $rep->pageHeight - $rep->topMargin;
                    }
                    
                // time used
                draw_time_used_details($rep, $jo_id, $wayb_pos_array["end_line"], $new_page);
                    if ($new_page === true) {
                        $rcmn_pos_array["end_line"] = $rep->pageHeight - $rep->topMargin;
                    }
                    
                // job card
                draw_print_job_card_details($rep, $rcmn_pos_array["end_line"], $new_page);
                        
            $rep->NewLine();

    $rep->End();
}

function draw_print_job_card_header_section_one(&$rep, $jo_row) {
    $rep->Rect($x=50, $y=20, $w=740, $h=60);

    $rep->LineTo($from=550, $row=575, $to=550, $row2=515); // 1st vertical line
    $rep->LineTo($from=630, $row=575, $to=630, $row2=515); // 2nd vertical line

    $rep->LineTo($from=550, $row=555, $to=790, $row2=555); // 1st horizontal line
    $rep->LineTo($from=550, $row=535, $to=790, $row2=535); // 1st horizontal line

        $rep->row -= 4;
        $row_line = $rep->row;

        $rep->row -= 4;
        $rep->Font('bold');
        $rep->fontSize += 3;
        $rep->Text(100, "COLOMBO MUNICIPAL COUNCIL (Municipal Engineer's Department)");
        $rep->fontSize -= 3;
        $rep->row -= 35; 

        $rep->fontSize += 16;
        $rep->Text(90, "WORKSHOP JOB CONTROL CARD");
        $rep->fontSize -= 16;
        $rep->Font();
        $rep->row -= 20; 

        $rep->row = $row_line; 

        $rep->Text(555, "JOB NUMBER");
        $rep->Text(635, $jo_row['id']);
        $rep->row -= 20; 

        $rep->Text(555, "MAIN SECTION");
        $rep->Text(635, $jo_row['section_name']);
        $rep->row -= 20; 

        $rep->Text(555, "DATE ISSUED");
        $rep->Text(635, sql2date($jo_row['date_']));
        $rep->row -= 20; 
}

function draw_print_job_card_header_section_two(&$rep, $jo_row) {
    // outer rectangle
    $rep->Rect($x=50, $y=84, $w=740, $h=75); 

    // right side inner rectangle
    $rep->Rect($x=60, $y=89, $w=260, $h=65); 
    $rep->LineTo($from=145, $row=506, $to=145, $row2=442); // 1st vertical line
    $rep->LineTo($from=60, $row=474, $to=320, $row2=474); // 1st horizontal line

        $rep->row -= 15; 
        $row_line = $rep->row;
        $rep->Text(65, _("Customer Name"));
        if (isset($jo_row['customer_name']) && strlen($jo_row['customer_name']) > 0) {
            $rep->Text(150, $jo_row['customer_name']);
        } else {
            $rep->Text(150, "---");
        }

        $rep->row -= 32; 
        $rep->Text(65, _("Customer Address"));
        if (isset($jo_row['customer_address']) && strlen($jo_row['customer_address']) > 0) {
            $rep->row += 10; 
            $rep->TextWrapLines(150, 170, $jo_row['customer_address']);
        } else {
            $rep->Text(150, "---");
        }
        
    $rep->row = $row_line; 
                                
    // left side inner rectangle                            
    $rep->Rect($x=350, $y=89, $w=430, $h=65); 
    $rep->LineTo($from=480, $row=506, $to=480, $row2=442); // 1st vertical line
    $rep->LineTo($from=350, $row=474, $to=780, $row2=474); // 1st horizontal line

//        $rep->row -= 35; 
        $rep->Text(355, _("Vehicle Equipment Number"));
        $rep->Text(485, $jo_row['asset_code']);

        $rep->row -= 32; 
        $rep->Text(355, _("Machine Type/ Model"));
        $rep->Text(485, $jo_row['vehicle_name']);
        $rep->row -= 20; 
}

function draw_print_job_card_header_section_three(&$rep, $jo_row) {
    // outer rectangle
    $rep->Rect($x=50, $y=170, $w=740, $h=85); 

    // right side inner rectangle
    $rep->Rect($x=60, $y=175, $w=120, $h=50); 
    $rep->LineTo($from=60, $row=390, $to=180, $row2=390); // 1st horizontal line

        $rep->row -= 32; 
        $row_line = $rep->row;
        $rep->Text(65, _("Request Reference Number"));
        $rep->row -= $rep->lineHeight * 2; 
        $rep->Text(65, $jo_row['jo_ref']);
        $rep->row -= $rep->lineHeight * 2; 
        
        $rep->Font('bold');
        $rep->fontSize += 2;
        $rep->Text(80, "JOB DETAILS");
        $rep->fontSize -= 2;
        $rep->Font();

    $rep->row = $row_line + 2; 
         
    $row = $row2 = 412;
    $row_hgt = $rep->lineHeight; 
    // left side inner rectangle                            
    $rep->Rect($x=188, $y=170, $w=592, $h=85); 
    
    // customer complain draw horizontal lines     
    for ($i=0; $i <= 6; $i++) {
        $rep->LineTo($from=188, $row - ($row_hgt * $i), $to=780, $row - ($row_hgt * $i)); 
    }
    
        $rep->row += 10; 
        $rep->TextColLines(3, 15, $jo_row['description']);
}

function draw_print_job_material_issues_returns_details(&$rep, $jo_id) {
    // outer rectangle
    $rep->Rect($x=50, $y=330, $w=740, $h=218); 

    // draw header horizontal lines
    $rep->LineTo($from=50, $row=240, $to=790, $row2=240); // 1st header horizontal line
    $rep->LineTo($from=50, $row=215, $to=790, $row2=215); // 2nd header horizontal line

    // draw vertical lines
    $rep->LineTo($from=134, $row=265, $to=134, $row2=47);  // 1st vertical line
    $rep->LineTo($from=210, $row=265, $to=210, $row2=47);  // 2nd vertical line
    $rep->LineTo($from=530, $row=240, $to=530, $row2=47);  // 3rd vertical line
    $rep->LineTo($from=570, $row=240, $to=570, $row2=47);  // 4th vertical line
    $rep->LineTo($from=670, $row=240, $to=670, $row2=47);  // 5th vertical line
    $rep->LineTo($from=730, $row=240, $to=730, $row2=47);  // 6th vertical line

        $rep->row -= 32; 
        $rep->Font('bold');
        $rep->fontSize += 1;
        $rep->Text(54, _("DATE STARTED"));
        $rep->Text(430, _("SPARE PARTS / MATERIALS USED"));
        $rep->fontSize -= 1;
        $rep->Font();
        $rep->row -= $rep->lineHeight * 2; 
        $rep->Text(65, "Issued Date");
        $rep->Text(140, "Issued Number");
        $rep->Text(350, "Description");
        $rep->Text(535, "Quantity");
        $rep->Text(610, "Cost");
        $rep->Text(685, "To Sign");
        $rep->Text(740, "Eng. Sign");

    $row = 203;
    $row_hgt = $rep->lineHeight; 
    
    // issues and returns details draw horizontal lines     
    for ($i=0; $i <= 12; $i++) {
        $rep->LineTo($from=50, $row - ($row_hgt * $i), $to=790, $row - ($row_hgt * $i));
    }
        
        $rep->row -= 7; 
        $dec = 2;
        $new_page = false;
        $types_array = array(ST_JOB_MATERIAL_ISSUE, ST_JOB_MATERIAL_RETURN, ST_PERSONAL_MATERIAL_ISSUE, ST_PERSONAL_MATERIAL_RETURN, ST_JOB_MANUAL_COST, 
                             ST_JOB_MANUAL_COST_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';
        
        $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)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ($rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
                    $new_page = true;
                    
                    // issues and returns details draw horizontal lines     
                    $rep->LineTo($from=50, $rep->row + $rep->lineHeight, $to=790, $rep->row + $rep->lineHeight);
                }
                
                if ($new_page === true) {
                    draw_job_material_issues_returns_table_lines($rep);
                }
                
                $rep->Text(70, sql2date($row["tran_date"]));
                $rep->Text(140, $row['trans_no']);
                if (! is_null($row["stock_id"]) > 0) {
                    $rep->Text(220, get_stock_item_formatted_name($row["stock_id"]));
                } else {
                    $rep->Text(220, $row["item_description"]);
                }
                $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;
                }
                $base_qty = $row["qty"] * $plu_min;
                $rep->Text(540, number_format($base_qty, $dec), 565, align: 'right');
                $line_cost = $row["standard_cost"] * $base_qty;
                $rep->Text(580, number_format($line_cost, $dec), 665, align: 'right');
            }
            
            return $new_page;
}

function draw_job_material_issues_returns_table_lines(&$rep) {
    // draw vertical lines
    $rep->LineTo($from=50, $rep->row, $to=50, $rep->row + $rep->lineHeight);  // 1st vertical line
    $rep->LineTo($from=134, $rep->row, $to=134, $rep->row + $rep->lineHeight);  // 1st vertical line
    $rep->LineTo($from=210, $rep->row, $to=210, $rep->row + $rep->lineHeight);  // 2nd vertical line
    $rep->LineTo($from=530, $rep->row, $to=530, $rep->row + $rep->lineHeight);  // 3rd vertical line
    $rep->LineTo($from=570, $rep->row, $to=570, $rep->row + $rep->lineHeight);  // 4th vertical line
    $rep->LineTo($from=670, $rep->row, $to=670, $rep->row + $rep->lineHeight);  // 5th vertical line
    $rep->LineTo($from=730, $rep->row, $to=730, $rep->row + $rep->lineHeight);  // 6th vertical line
    $rep->LineTo($from=790, $rep->row, $to=790, $rep->row + $rep->lineHeight);  // 6th vertical line
    
    $row = $rep->row - 2;
    
    // issues and returns details draw horizontal lines     
    $rep->LineTo($from=50, $row, $to=790, $row);
}

function draw_print_sub_jobs_details(&$rep, $jo_id, &$new_page) { 
    if ($rep->row < $rep->bottomMargin + (5 * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
        $y_position = $rep->topMargin;
        $new_page = true;
    } elseif ($new_page === false) { // if material issues displayed only in the 1st page
        $rep->NewPage();
        $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
        $y_position = $rep->topMargin;
        $new_page = true;
    } else {
        $rep->row -= ($rep->lineHeight * 2); // get some space from material issues table
        $y_position = $rep->pageHeight - $rep->row;
    }
    
    $row_line = $end_line = $rep->row;

    // outer rectangle
    $rep->Rect($x=50, $y=$y_position, $w=220, $h=83); 
    
    // draw header horizontal lines
    $defaul_lines_conut = 5;
    $rep->LineTo($from=120, $rep->row, $to=120, $rep->row - $rep->lineHeight * 2); 
    $rep->row -= $rep->lineHeight;
    for ($i=1; $i<=$defaul_lines_conut; $i++) {
        $rep->LineTo($from=50, $rep->row - $rep->lineHeight * $i, $to=270, $rep->row - $rep->lineHeight * $i); // horizontal
        $rep->LineTo($from=120, $rep->row - $rep->lineHeight * $i, $to=120, $rep->row - $rep->lineHeight * $i - $rep->lineHeight); // vertical
        $end_line = $rep->row - $rep->lineHeight * $i - $rep->lineHeight;
    }
    
    $rep->row = $row_line - $rep->lineHeight; // table starting row
    
        $rep->Font('bold');
        $rep->Text(67, _("Sub Job"));
        $rep->row -= 10;
        $rep->Text(60, _("Issued Date"));
        $rep->Text(153, _("Sub Job Section"));
        $rep->Font();
        
        $count_num_rows = 1; $draw_line = 0;
        $result = get_job_order_details_by_filters(parent_jo_id: $jo_id); 
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ($rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
                    $draw_line = $new_page = true;
                    
                    // 1st horizontal line     
                    $rep->LineTo($from=50, $rep->row + $rep->lineHeight, $to=270, $rep->row + $rep->lineHeight);
                } elseif($count_num_rows > $defaul_lines_conut) {
                    $draw_line = true;
                }
                
                if ($draw_line === true) {
                    draw_sub_jobs_table_lines($rep);
                }
                
                $rep->Text(62, sql2date($row["date_"]));
                $rep->Text(125, $row['section_name']);
                
                $count_num_rows++;
            }

            if ($rep->row < $end_line) {
                $end_line = $rep->row;
            }
            
            return array("start_line" => $row_line, "end_line" => $end_line, "y_position" => $y_position);
}

function draw_sub_jobs_table_lines(&$rep, $draw_vertical=true) {
    // draw vertical lines
    if ($draw_vertical) {
        $rep->LineTo($from=50, $rep->row, $to=50, $rep->row + $rep->lineHeight);  // 1st vertical line
        $rep->LineTo($from=120, $rep->row, $to=120, $rep->row + $rep->lineHeight);  // 1st vertical line
        $rep->LineTo($from=270, $rep->row, $to=270, $rep->row + $rep->lineHeight);  // 2nd vertical line
    }
    
    $row = $rep->row - 2;
    
    // draw horizontal lines     
    $rep->LineTo($from=50, $row, $to=270, $row);
}

function draw_print_direct_purchase_orders_details(&$rep, $jo_id, $start_line, $y_position, &$new_page) { 
    $rep->row = $end_line = $start_line;
    
    // outer rectangle
    $rep->Rect($x=280, $y=$y_position, $w=510, $h=95); 

    // draw header horizontal lines
    $defaul_lines_conut = 5;
    $vert_space = 60;
    $rep->LineTo($from=280, $rep->row - $rep->lineHeight, $to=790, $rep->row - $rep->lineHeight); 
    $rep->LineTo($from=280, $rep->row - $rep->lineHeight * 3, $to=790, $rep->row - $rep->lineHeight * 3); 
    $rep->row -= ($rep->lineHeight * 3);
    for ($i=1; $i<$defaul_lines_conut; $i++) { 
        $rep->LineTo($from=280, $row=$rep->row - $rep->lineHeight * $i, $to=790, $rep->row - $rep->lineHeight * $i); // horizontal
    }
    
    // draw vertical lines
    $rep->row += ($rep->lineHeight * 2);
    $horizontal_lines_conut = 8; $vertical_lines_conut = 7;
    for ($i=1; $i<$horizontal_lines_conut; $i++) {
        for ($j=1; $j<=$vertical_lines_conut; $j++) {
            if ($j == 3 || $j == 4 || $j == 5) continue;
            $rep->LineTo($from=$x + $vert_space * $j, $rep->row - $rep->lineHeight * $i, $to=$x + $vert_space * $j, $rep->row - $rep->lineHeight * $i + $rep->lineHeight);
        }
        $end_line = $rep->row - ($rep->lineHeight * $i) - $rep->lineHeight;
    }
    
    $rep->row = $start_line - $rep->lineHeight + 2; // table starting row
    
        $rep->Font('bold');
        $rep->Text(500, _("Purchase Order")); 
        $rep->row -= $rep->lineHeight;
        $rep->Text(300, _("Date"));
        $rep->Text(357, _("Order"));
        $rep->row -= $rep->lineHeight;
        $rep->Text(350, _("Numbers"));
        $rep->row += $rep->lineHeight;
        $rep->Text(515, _("Items"));
        $rep->Text(646, _("Number of"));
        $rep->row -= $rep->lineHeight;
        $rep->Text(640, _("Organizations"));
        $rep->row += $rep->lineHeight;
        $rep->Text(735, _("Cost"));
        $rep->Font();
        $rep->row -= $rep->lineHeight;
        
        $count_num_rows = 1; $draw_line = 0;
        $result = get_direct_purchase_requisition_lines_by_filters($jo_id, type: ST_DIRECT_PURCHASE_REQ);
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ($rep->lineHeight)) { 
                    $rep->NewPage();
                    $rep->row = $end_line = $rep->pageHeight - $rep->topMargin; // reset row
                    $draw_line = $new_page = true;
                    
                    // 1st horizontal line     
                    $rep->LineTo($from=$x, $rep->row + $rep->lineHeight, $to=790, $rep->row + $rep->lineHeight);
                } elseif($count_num_rows > $defaul_lines_conut) {
                    $draw_line = true;
                }
                
                if ($draw_line === true) {
                    draw_direct_purchase_orders_table_lines($rep, $x, $vert_space);
                }
                
                $rep->Text(287, sql2date($row["tran_date"]));
                $rep->Text(350, $row["trans_no"]);
                if (! is_null($row["stock_id"]) > 0) {
                    $rep->Text(405, get_stock_item_formatted_name($row["stock_id"]));
                } else {
                    $rep->Text(405, $row["item_description"]);
                }
                $rep->Text(705, number_format($row['total'], 2), 785, align: 'right');
                
                $count_num_rows++;
            }
            
            if ($draw_line === true) {
                $rep->row -= $rep->lineHeight;
            }
            
            if ($rep->row < $end_line) {
                $end_line = $rep->row;
            }
            
            return array("start_line" => $start_line, "end_line" => $end_line, "y_position" => $y_position);
}

function draw_direct_purchase_orders_table_lines(&$rep, $x, $vert_space) {
    // draw vertical lines
    $vertical_lines_conut = 9;
    for ($j=0; $j<$vertical_lines_conut; $j++) {
        if ($j == 3 || $j == 4 || $j == 5) continue;
        
        if ($j != 8) {
            $rep->LineTo($from=$x + $vert_space * $j, $rep->row, $to=$x + $vert_space * $j, $rep->row + $rep->lineHeight);
        } else {
            $rep->LineTo($from=790, $rep->row, $to=790, $rep->row + $rep->lineHeight);
        }
    }
    
    $row = $rep->row - 2;
    
    // draw horizontal lines     
    $rep->LineTo($from=$x, $row, $to=790, $row);
}

function draw_print_service_orders_details(&$rep, $jo_id, $end_line, &$new_page) { 
    $rep->row = $end_line;
    
    if ($rep->row < $rep->bottomMargin + (5 * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
        $y_position = $rep->topMargin;
        $new_page = true;
    } else {
        if ($new_page === false) {
            $rep->row -= ($rep->lineHeight * 2); 
        }
        $y_position = $rep->pageHeight - $rep->row - $rep->lineHeight;
        $new_page = false;
    } 
    
    $row_line = $new_end_line = $rep->row;

    // outer rectangle
    $rep->Rect($x=50, $y=$y_position, $w=220, $h=83); 
    
    // draw header horizontal lines
    $defaul_lines_conut = 5;
    for ($i=0; $i<=$defaul_lines_conut; $i++) {
        $rep->LineTo($from=50, $rep->row - $rep->lineHeight * $i, $to=270, $rep->row - $rep->lineHeight * $i); // horizontal
        $rep->LineTo($from=98, $rep->row - $rep->lineHeight * $i, $to=98, $rep->row - $rep->lineHeight * $i - $rep->lineHeight); // vertical
        $rep->LineTo($from=135, $rep->row - $rep->lineHeight * $i, $to=135, $rep->row - $rep->lineHeight * $i - $rep->lineHeight); // vertical
        $new_end_line = $rep->row - ($rep->lineHeight * $i) - $rep->lineHeight;
    }
    
    $rep->row = $row_line; // table starting row
    
        $rep->Font('bold');
        $rep->row += 2;
        $rep->Text(130, _("Service Order"));
        $rep->row -= $rep->lineHeight;
        $rep->Text(65, _("Date"));
        $rep->Text(105, _("Order"));
        $rep->Text(180, _("Location"));
        $rep->Font();
        
        $count_num_rows = 1; $draw_line = 0;
        $result = get_job_order_details_by_filters(parent_jo_id: 123456789); 
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ($rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $new_end_line = $rep->pageHeight - $rep->topMargin; // reset row
                    $draw_line = $new_page = true;
                    
                    // 1st horizontal line     
                    $rep->LineTo($from=50, $rep->row + $rep->lineHeight, $to=270, $rep->row + $rep->lineHeight);
                } elseif($count_num_rows > $defaul_lines_conut) {
                    $draw_line = true;
                }
                
                if ($draw_line === true) {
                    draw_service_orders_table_lines($rep);
                }
                
                $rep->Text(51, sql2date($row["date_"]));
                $rep->Text(102, $row['id']);
                $rep->Text(137, $row['section_name']);
                
                $count_num_rows++;
            }
            
            if ($rep->row < $new_end_line) {
                $new_end_line = $rep->row;
            }
            
            return array("start_line" => $row_line, "end_line" => $new_end_line, "y_position" => $y_position);
}

function draw_service_orders_table_lines(&$rep, $draw_vertical=true) {
    // draw vertical lines
    if ($draw_vertical) {
        $rep->LineTo($from=50, $rep->row, $to=50, $rep->row + $rep->lineHeight);  
        $rep->LineTo($from=98, $rep->row, $to=98, $rep->row + $rep->lineHeight);  
        $rep->LineTo($from=135, $rep->row, $to=135, $rep->row + $rep->lineHeight);  
        $rep->LineTo($from=270, $rep->row, $to=270, $rep->row + $rep->lineHeight);  
    }
    
    $row = $rep->row - 2;
    
    // draw horizontal lines     
    $rep->LineTo($from=50, $row, $to=270, $row);
}

function draw_way_bills_details(&$rep, $jo_id, $end_line, &$new_page) { 
    $rep->row = $new_end_line = $end_line;
    
    if ($rep->row < $rep->bottomMargin + (5 * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
        $y_position = $rep->topMargin;
        $new_page = true;
    } else {
        if ($new_page === false) {
            $rep->row -= ($rep->lineHeight * 2); 
        }
        $y_position = $rep->pageHeight - $rep->row - $rep->lineHeight * 2;
        $new_page = false;
    }
    
    // outer rectangle
    $rep->Rect($x=280, $y=$y_position, $w=510, $h=84); 

    // draw header horizontal lines
    $rep->row += $rep->lineHeight;
    $defaul_lines_conut = 5;
    $rep->LineTo($from=280, $rep->row, $to=790, $rep->row); 
    $rep->LineTo($from=280, $rep->row - $rep->lineHeight, $to=790, $rep->row - $rep->lineHeight); 
    $rep->row -= ($rep->lineHeight);
    for ($i=1; $i<$defaul_lines_conut; $i++) { 
        $rep->LineTo($from=280, $row=$rep->row - $rep->lineHeight * $i, $to=790, $rep->row - $rep->lineHeight * $i); // horizontal
    }
    
    // draw vertical lines
    $vert_space = 60;
    $rep->row += ($rep->lineHeight * 1);
    $horizontal_lines_conut = 7; $vertical_lines_conut = 7;
    for ($i=1; $i<$horizontal_lines_conut; $i++) {
        for ($j=1; $j<=$vertical_lines_conut; $j++) {
            if ($j == 3 || $j == 4 || $j == 5 || $j == 7) continue;
            $rep->LineTo($from=$x + $vert_space * $j, $rep->row - $rep->lineHeight * $i, $to=$x + $vert_space * $j, $rep->row - $rep->lineHeight * $i + $rep->lineHeight);
        }
        $new_end_line = $rep->row - ($rep->lineHeight * $i);
    }
    
    $rep->row = $end_line - $rep->lineHeight + 2; // table starting row
    
        $rep->Font('bold');
        $rep->Text(500, _("Way Bill Details")); 
        $rep->row -= $rep->lineHeight;
        $rep->Text(300, _("Date"));
        $rep->Text(350, _("Numbers"));
        $rep->Text(515, _("Items"));
        $rep->Text(684, _("Issung Officer"));
        $rep->Font();
        
        $count_num_rows = 1; $draw_line = 0;
        $result = get_direct_purchase_requisition_lines_by_filters($jo_id, type: 99999);
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ($rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $new_end_line = $rep->pageHeight - $rep->topMargin; // reset row
                    $draw_line = $new_page = true;
                    
                    // 1st horizontal line     
                    $rep->LineTo($x, $rep->row + $rep->lineHeight, $to=790, $rep->row + $rep->lineHeight);
                } elseif($count_num_rows > $defaul_lines_conut) {
                    $draw_line = true;
                }
                
                if ($draw_line === true) {
                    draw_way_bills_lines($rep, $x, $vert_space);
                }
                
                $rep->Text(287, sql2date($row["tran_date"]));
                $rep->Text(350, $row["trans_no"]);
                if (! is_null($row["stock_id"]) > 0) {
                    $rep->Text(405, get_stock_item_formatted_name($row["stock_id"]));
                } else {
                    $rep->Text(405, $row["item_description"]);
                }
                $rep->Text(705, $row['total']);
                
                $count_num_rows++;
            }
                        
            if ($rep->row < $new_end_line) {
                $new_end_line = $rep->row;
            }
            
            return array("start_line" => $end_line, "end_line" => $new_end_line, "y_position" => $y_position);
}

function draw_way_bills_lines(&$rep, $x, $vert_space) {
    // draw vertical lines
    $vertical_lines_conut = 9;
    for ($j=0; $j<$vertical_lines_conut; $j++) {
        if ($j == 3 || $j == 4 || $j == 5 || $j == 7) continue;
        
        if ($j != 8) {
            $rep->LineTo($from=$x + $vert_space * $j, $rep->row, $to=$x + $vert_space * $j, $rep->row + $rep->lineHeight);
        } else {
            $rep->LineTo($from=790, $rep->row, $to=790, $rep->row + $rep->lineHeight);
        }
    }
    
    $row = $rep->row - 2;
    
    // draw horizontal lines     
    $rep->LineTo($from=$x, $row, $to=790, $row);
}

function draw_print_recommendation_details(&$rep, $end_line, &$new_page) { 
    $rep->row = $end_line;
    
    if ($rep->row < $rep->bottomMargin + (6 * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
        $y_position = $rep->topMargin;
        $new_page = true;
    } else {
        if ($new_page === false) {
            $rep->row -= ($rep->lineHeight * 2); 
        }
        $y_position = $rep->pageHeight - $rep->row - $rep->lineHeight;
        $new_page = false;
    } 
    
    $row_line = $rep->row;

    // outer rectangle
    $rep->Rect($x=50, $y=$y_position, $w=220, $h=96); 
    $rep->LineTo($from=50, $rep->row - $rep->lineHeight, $to=270, $rep->row - $rep->lineHeight); // horizontal 
    $rep->LineTo($from=50, $rep->row - $rep->lineHeight * 4, $to=270, $rep->row - $rep->lineHeight * 4); // horizontal 
    $rep->LineTo($from=98, $rep->row + $rep->lineHeight, $to=98, $rep->row - $rep->lineHeight * 7); // vertical
    $rep->LineTo($from=220, $rep->row + $rep->lineHeight, $to=220, $rep->row - $rep->lineHeight * 7); // vertical
    
    $rep->row = $row_line; // table starting row
    
        $rep->Font('bold');
        $rep->row += 2;
        $rep->Text(55, _("Finished"));
        $rep->Text(120, _("Recommendation/"));
        $rep->Text(225, _("Signature"));
        $rep->row -= $rep->lineHeight;
        $rep->Text(65, _("Date"));
        $rep->Text(140, _("Remarks"));
        
        $rep->row -= ($rep->lineHeight * 2);
        $rep->Text(222, _(".................."));
        $rep->row -= ($rep->lineHeight * 1);
        $rep->Text(240, _("TO"));
        $rep->row -= ($rep->lineHeight * 2);
        $rep->Text(222, _(".................."));
        $rep->row -= ($rep->lineHeight * 1);
        $rep->Text(225, _("Engineer"));
        $rep->Font();
        
        $new_end_line = $rep->row;
        $rep->row = $row_line - $rep->lineHeight * 2 + 2;
        
        $rep->Text(51, "");
        $to_recom = "";
        $rep->TextWrapLines(99, 122, $to_recom, spacebreak: false);
        
        $rep->Text(51, "");
        $eng_recom = "";
        $rep->TextWrapLines(99, 122, $eng_recom, spacebreak: false);
        
        if ($rep->row < $new_end_line) {
            $new_end_line = $rep->row;
        }
            
            return array("start_line" => $row_line, "end_line" => $new_end_line, "y_position" => $y_position);
}

function draw_time_used_details(&$rep, $jo_id, $end_line, &$new_page) { 
    $rep->row = $end_line;
    
    if ($rep->row < $rep->bottomMargin + (12 * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
        $y_position = $rep->topMargin;
        $new_page = true;
    } else {
        if ($new_page === false) {
            $rep->row -= ($rep->lineHeight * 1); 
        }
        $y_position = $rep->pageHeight - $rep->row;
        $new_page = false;
    }
    
    // outer rectangle
    $rep->Rect($x=280, $y=$y_position, $w=510, $h=144); 

    // draw header horizontal lines
    $rep->row -= $rep->lineHeight;
    $start_line = $rep->row;
    $defaul_lines_conut = 9;
    $rep->LineTo($from=280, $rep->row, $to=790, $rep->row); 
    $rep->LineTo($from=280, $rep->row - $rep->lineHeight * 2, $to=790, $rep->row - $rep->lineHeight * 2); 
    $rep->row -= ($rep->lineHeight * 2);
    for ($i=1; $i<$defaul_lines_conut; $i++) { 
        $rep->LineTo($from=280, $row=$rep->row - $rep->lineHeight * $i, $to=790, $rep->row - $rep->lineHeight * $i); // horizontal
    }
    
    // draw vertical lines
    $vert_space = 68;
    $rep->row += ($rep->lineHeight * 2);
    $horizontal_lines_conut = 12; $vertical_lines_conut = 6;
    for ($i=1; $i<$horizontal_lines_conut; $i++) {
        for ($j=1; $j<=$vertical_lines_conut; $j++) {
            $rep->LineTo($from=$x + $vert_space * $j, $rep->row - $rep->lineHeight * $i, $to=$x + $vert_space * $j, $rep->row - $rep->lineHeight * $i + $rep->lineHeight);
        }
    }
    
    $rep->row = $start_line + 2; // table starting row
    
        $rep->Font('bold');
        $rep->Text(515, _("Time Used")); 
        $rep->row -= $rep->lineHeight;
        $rep->Text(305, _("Date"));
        $rep->Text(370, _("B.T.No"));
        $rep->Text(435, _("Normal"));
        $rep->Text(510, _("OT"));
        $rep->Text(568, _("NT/ Rate"));
        $rep->Text(635, _("OT/ Rate"));
        $rep->Text(730, _("Cost"));
        $rep->row -= $rep->lineHeight;
        $rep->Text(438, _("Hours"));
        $rep->Text(505, _("Hours"));
        $rep->Font();
        
        $count_num_rows = 1; $draw_line = 0;
        $result = rep_get_job_order_labour_details($jo_id);
            while ($row = db_fetch($result)) {
                $rep->NewLine();
                
                if ($rep->row < $rep->bottomMargin + ($rep->lineHeight)) {
                    $rep->NewPage();
                    $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
                    $draw_line = $new_page = true;
                    
                    // 1st horizontal line     
                    $rep->LineTo($x, $rep->row + $rep->lineHeight, $to=790, $rep->row + $rep->lineHeight);
                } elseif($count_num_rows > $defaul_lines_conut) {
                    $draw_line = true;
                }
                
                if ($draw_line === true) {
                    draw_time_used_lines($rep, $x, $vert_space);
                }
                
                $rep->Text(290, sql2date($row["from_date"]));
                $rep->Text(352, $row["employee_reference"]);
                $rep->Text(420, $row["num_days_worked"]);
                $rep->Text(490, '');
                $rep->Text(565, number_format($row['rate'], 2), 615, align: 'right');
                $rep->Text(630, '');
                $rep->Text(725, number_format($row["num_days_worked"] * $row["rate"], 2), 785, align: 'right');
                
                $count_num_rows++;
            }
}

function draw_time_used_lines(&$rep, $x, $vert_space) {
    // draw vertical lines
    $vertical_lines_conut = 9;
    for ($j=0; $j<$vertical_lines_conut; $j++) {
        if ($j == 3 || $j == 4 || $j == 5 || $j == 7) continue;
        
        if ($j != 8) {
            $rep->LineTo($from=$x + $vert_space * $j, $rep->row, $to=$x + $vert_space * $j, $rep->row + $rep->lineHeight);
        } else {
            $rep->LineTo($from=790, $rep->row, $to=790, $rep->row + $rep->lineHeight);
        }
    }
    
    $row = $rep->row - 2;
    
    // draw horizontal lines     
    $rep->LineTo($from=$x, $row, $to=790, $row);
}

function draw_print_job_card_details(&$rep, $end_line, &$new_page) { 
    $rep->row = $end_line;
    
    if ($rep->row < $rep->bottomMargin + (5 * $rep->lineHeight)) {
        $rep->NewPage();
        $rep->row = $rep->pageHeight - $rep->topMargin; // reset row
        $y_position = $rep->topMargin;
        $new_page = true;
    } else {
        if ($new_page === false) {
            $rep->row -= ($rep->lineHeight * 1); 
        }
        $y_position = $rep->pageHeight - $rep->row;
        $new_page = false;
    } 
    
    $row_line = $rep->row;

    // outer rectangle
    $rep->Rect($x=50, $y=$y_position, $w=220, $h=36); 
    $rep->LineTo($from=50, $rep->row - $rep->lineHeight * 2, $to=270, $rep->row - $rep->lineHeight * 2); // horizontal 
    $rep->LineTo($from=50, $rep->row - $rep->lineHeight * 1, $to=270, $rep->row - $rep->lineHeight * 1); // horizontal 
    $rep->LineTo($from=160, $rep->row - $rep->lineHeight * 1, $to=160, $rep->row - $rep->lineHeight * 3); // vertical
    
    $rep->row = $row_line; // table starting row
    $rep->row -= $rep->lineHeight;
    
        $rep->Font('bold');
        $rep->row += 2;
        $rep->Text(135, _("JOB CARD"));
        $rep->row -= $rep->lineHeight;
        $rep->Text(60, _("Date Released"));
        $rep->row -= ($rep->lineHeight * 1);
        $rep->Text(60, _("Job Clerk's Initials"));
        $rep->Font();
}

?>