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

$page_security = 'SA_REP_PAY_VOUCHER';
// ----------------------------------------------------------------
// $ 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 . "/gl/includes/ui/print_receipts_ui.inc"); 
include_once($path_to_root . "/gl/includes/db/print_receipts_db.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc");  
include_once($path_to_root . "/sales/includes/db/customers_db.inc");  

//----------------------------------------------------------------------------------------------------

print_receipts();

function get_bank_trans_receipt($trans_no, $type, $person_id, $person_type_id) {
    $sql = "SELECT bt.*, bk.bank_account_name 
            FROM ".TB_PREF."bank_trans bt 
                JOIN ".TB_PREF."bank_accounts bk 
                    ON bt.bank_act = bk.id 
            WHERE bt.trans_no = ".db_escape($trans_no)." 
                AND bt.type = ".db_escape($type)." 
                AND bt.person_id = ".db_escape($person_id)." 
                AND bt.person_type_id = ".db_escape($person_type_id);
        
   	$result = db_query($sql, "get_bank_trans_receipt");
        
        return db_fetch($result);
}

function get_customer_address($customer_id, $branch_code) {
        

        $cust_branch = get_cust_branch($customer_id, $branch_code);
        $address = $cust_branch['br_address'];
        
                        
        return $address;               
}

function get_customer_allocations($trans_type, $trans_no) {
    $sql = "SELECT ca.*, trans_type_to type, date_alloc tran_date, amt alloc, 
                    bt.trans_no, ABS(bt.amount) Total, '' reference 
            FROM ".TB_PREF."cust_allocations ca 
                JOIN ".TB_PREF."bank_trans bt  
                    ON ca.trans_no_to = bt.trans_no  
                        AND ca.trans_type_to = bt.type 
            WHERE bt.trans_no = ".db_escape($trans_no)." 
                AND bt.type = ".db_escape($trans_type);

        return db_query($sql, "get_customer_allocations");
}

function get_employee_by_id($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."employee_master 
            WHERE employee_id = ".db_escape($id);
    
        $result = db_query($sql, "get_employee_by_id");

        return db_fetch($result);
}

function get_customer_payments($trans_no, $type) {
    $sql = "SELECT trn.*, 
                (trn.ov_amount + trn.ov_gst) AS amount,
                dm.address, 
                cur.hundreds_name, 
                '' bank_act, 
                trn.reference ref, 
                trn.tran_date trans_date,
                ".PT_CUSTOMER." person_type_id,
                trn.debtor_no person_id,
                trn.branch_code person_detail_id,
                rec.receipt_date, rec.receipt_code, rec.receipt_reference,
                rec.id receipt_id 

            FROM ".TB_PREF."debtor_trans trn 
                JOIN ".TB_PREF."debtors_master dm 
                    ON trn.debtor_no = dm.debtor_no 
                JOIN ".TB_PREF."currencies cur  
                    ON dm.curr_code = cur.curr_abrev  
                LEFT JOIN ".TB_PREF."payment_receipts rec   
                    ON trn.type = rec.type    
                        AND trn.trans_no = rec.trans_no   
            WHERE trn.trans_no = ".db_escape($trans_no)." 
                AND trn.type = ".db_escape($type)."  
                AND trn.ov_amount > 0";
        
   	return db_query($sql, "get_customer_payments");
}

function get_supplier_allocations($type, $trans_no) {
    $sql = "SELECT *
		FROM ".TB_PREF."supp_allocations sa
		WHERE (sa.trans_type_from = ".db_escape($type)." AND sa.trans_no_from =".db_escape($trans_no).") 
                    OR (sa.trans_type_to = ".db_escape($type)." AND sa.trans_no_to =".db_escape($trans_no).")";
    
    $result = db_query($sql, "The payment receipt cannot be retrieved");
    if (db_num_rows($result) == 0) {
        return false;
    } else {
        return true;
    }
}

function get_allocations_for_receipt($supplier_id, $type, $trans_no) {
    $sql = "SELECT trans.type,
                trans.trans_no,
                IF(trans.supp_reference='',trans.reference,trans.supp_reference) as reference,
                trans.tran_date,
                supplier.supp_name, 
                supplier.curr_code, 
                ov_amount+ov_gst+ov_discount AS Total,
                trans.alloc,
                trans.due_date,
                trans.supplier_id,
                supplier.address
            FROM ".TB_PREF."supp_trans as trans
                LEFT JOIN ".TB_PREF."suppliers as supplier 
                    ON trans.supplier_id=supplier.supplier_id
            WHERE trans.trans_no=".db_escape($trans_no)."
                AND trans.type=".db_escape($type)." 
                AND trans.supplier_id=".db_escape($supplier_id);

    $sql .= " ORDER BY trans_no";
    
    return db_query($sql, "Cannot retreive alloc to transactions");
}

function get_bank_trans_new($trans_no, $trans_no_2, $type, $person_type_id=null, $person_id=null) {
    $sql = "SELECT bt.*, act.*,
            lv.value_name bank_name,
            IFNULL(abs(dt.ov_amount), IFNULL(ABS(st.ov_amount), bt.amount)) settled_amount,
            IFNULL(abs(dt.ov_amount/bt.amount), IFNULL(ABS(st.ov_amount/bt.amount), 1)) settle_rate,
            IFNULL(debtor.curr_code, IFNULL(supplier.curr_code, act.bank_curr_code)) settle_curr,
            crr.hundreds_name, st.tran_date, st.reference, st.ov_amount,
            rc.receipt_type_id, rc.receipt_date, rc.receipt_code, rc.receipt_reference,
            rc.id receipt_id  
            
            FROM ".TB_PREF."bank_trans bt
                LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=bt.type AND dt.trans_no=bt.trans_no
                LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = dt.debtor_no
                LEFT JOIN ".TB_PREF."supp_trans st ON st.type=bt.type AND st.trans_no=bt.trans_no
                LEFT JOIN ".TB_PREF."suppliers supplier ON supplier.supplier_id = st.supplier_id
                LEFT JOIN ".TB_PREF."currencies crr ON supplier.curr_code = crr.curr_abrev 
                LEFT JOIN ".TB_PREF."payment_receipts rc ON bt.type = rc.type AND bt.trans_no = rc.trans_no,  
                          ".TB_PREF."bank_accounts act
                LEFT JOIN ".TB_PREF."list_values lv ON act.bank_id = lv.id 

            WHERE act.id = bt.bank_act 
             AND bt.amount > 0";
    
    if ($type != null)
        $sql .= " AND bt.type = ".db_escape($type);
    if ($trans_no != null && $trans_no_2 != null)
        $sql .= " AND bt.trans_no >= ".db_escape($trans_no)." AND bt.trans_no <= ".db_escape($trans_no_2);
    if ($person_type_id != null)
        $sql .= " AND bt.person_type_id = ".db_escape($person_type_id);
    if ($person_id != null)
        $sql .= " AND bt.person_id = ".db_escape($person_id);
    
    $sql .= " ORDER BY trans_date, bt.id";

    return db_query($sql, "query for bank transaction");
}

function get_alloc_supp_sql_report($extra_fields=null, $extra_conditions=null, $extra_tables=null) {
    $sql = "SELECT trans.type,
                trans.trans_no,
                IF(trans.supp_reference='',trans.reference,trans.supp_reference) as reference,
                trans.tran_date,
                supplier.supp_name, 
                supplier.curr_code, 
                ov_amount+ov_gst+ov_discount AS Total,
                trans.alloc,
                trans.due_date,
                trans.supplier_id,
                supplier.address";

    if ($extra_fields)
        $sql .= ", $extra_fields ";

    $sql .= " FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier";
    
    if ($extra_tables)
        $sql .= " ,$extra_tables ";

    $sql .= " WHERE trans.supplier_id=supplier.supplier_id";

    if ($extra_conditions)
        $sql .= " AND $extra_conditions";

    return $sql;
}

function get_allocations_for_receipt_allocations($supplier_id, $type, $trans_no) {
    $sql = get_alloc_supp_sql_report("amt, supp_reference, trans.alloc", "trans.trans_no = alloc.trans_no_to
            AND trans.type = alloc.trans_type_to
            AND alloc.trans_no_from=".db_escape($trans_no)."
            AND alloc.trans_type_from=".db_escape($type)."
            AND trans.supplier_id=".db_escape($supplier_id),
            TB_PREF."supp_allocations as alloc");
    $sql .= " ORDER BY trans_no";
    
    return db_query($sql, "Cannot retreive alloc to transactions");
}

function get_bank_payments_for_report($type, $trans_id) {
	$sql = "SELECT gl.*, cm.account_name, IFNULL(refs.reference, '') AS reference, user.real_name, 
			COALESCE(st.tran_date, dt.tran_date, bt.trans_date, grn.delivery_date, gl.tran_date) as doc_date,
			IF(ISNULL(st.supp_reference), '', st.supp_reference) AS supp_reference
	FROM ".TB_PREF."gl_trans as gl
		LEFT JOIN ".TB_PREF."chart_master as cm ON gl.account = cm.account_code
		LEFT JOIN ".TB_PREF."refs as refs ON (gl.type=refs.type AND gl.type_no=refs.id)
		LEFT JOIN ".TB_PREF."audit_trail as audit ON (gl.type=audit.type AND gl.type_no=audit.trans_no AND NOT ISNULL(gl_seq))
		LEFT JOIN ".TB_PREF."users as user ON (audit.user=user.id)
	# all this below just to retrieve doc_date :>
		LEFT JOIN ".TB_PREF."supp_trans st ON gl.type_no=st.trans_no AND st.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=st.supplier_id)
		LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=gl.type_no AND gl.type=".ST_SUPPRECEIVE." AND gl.person_id=grn.supplier_id
		LEFT JOIN ".TB_PREF."debtor_trans dt ON gl.type_no=dt.trans_no AND dt.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=dt.debtor_no)
		LEFT JOIN ".TB_PREF."bank_trans bt ON bt.type=gl.type AND bt.trans_no=gl.type_no AND bt.amount!=0
			 AND bt.person_type_id=gl.person_type_id AND bt.person_id=gl.person_id
		LEFT JOIN ".TB_PREF."journal j ON j.type=gl.type AND j.trans_no=gl.type_no"

		." WHERE gl.type= ".db_escape($type) 
		." AND gl.type_no = ".db_escape($trans_id)
		." AND gl.amount > 0"
		." ORDER BY tran_date, counter";

	return db_query($sql, "get_bank_payments_for_report");
}

function get_journal_trans_new($trans_no, $trans_no_2, $type, $person_type_id=null, $person_id=null) {
    $sql = "SELECT bt.*, act.*,
            IFNULL(abs(dt.ov_amount), IFNULL(ABS(st.ov_amount), bt.amount)) settled_amount,
            IFNULL(abs(dt.ov_amount/bt.amount), IFNULL(ABS(st.ov_amount/bt.amount), 1)) settle_rate,
            IFNULL(debtor.curr_code, supplier.curr_code) settle_curr,
            crr.hundreds_name, bt.tran_date, bt.reference, st.ov_amount, 
            '' bank_act, bt.reference ref, bt.tran_date trans_date,
            rc.receipt_type_id, rc.receipt_date, rc.receipt_code, rc.receipt_reference,
            prej.person_detail_id,
            rc.id receipt_id  
            FROM ".TB_PREF."journal bt
                JOIN ".TB_PREF."pre_journal prej ON bt.type = prej.type AND bt.trans_no = prej.trans_no 
                JOIN ".TB_PREF."gl_trans gl ON bt.type=gl.type AND bt.trans_no=gl.type_no 
                LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=bt.type AND dt.trans_no=bt.trans_no
                LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = dt.debtor_no
                LEFT JOIN ".TB_PREF."supp_trans st ON st.type=bt.type AND st.trans_no=bt.trans_no
                LEFT JOIN ".TB_PREF."suppliers supplier ON supplier.supplier_id = st.supplier_id
                LEFT JOIN ".TB_PREF."currencies crr ON supplier.curr_code = crr.curr_abrev 
                LEFT JOIN ".TB_PREF."payment_receipts rc ON bt.type = rc.type AND bt.trans_no = rc.trans_no,   
                          ".TB_PREF."chart_master act

            WHERE act.account_code = gl.account 
                AND bt.amount > 0 ";
    
    if ($type != null)
        $sql .= " AND bt.type = ".db_escape($type);
    if ($trans_no != null && $trans_no_2 != null)
        $sql .= " AND bt.trans_no >= ".db_escape($trans_no)." AND bt.trans_no <= ".db_escape($trans_no_2);
    if ($person_type_id != null)
        $sql .= " AND bt.person_type_id = ".db_escape($person_type_id);
    if ($person_id != null)
        $sql .= " AND bt.person_id = ".db_escape($person_id);
    
    $sql .= " ORDER BY bt.tran_date, gl.counter LIMIT 1";

    return db_query($sql, "get_journal_trans_new");
}

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 draw_allocations_table($column_headers_start, &$rep) {
//    $rep->row = $rep->pageHeight - $rep->topMargin;
    $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 = 150;

    $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($rep->cols[$cols - 7], $top_line_row, $rep->cols[$cols - 7], $iline7 - $increase_height);
    $rep->LineTo($rep->cols[$cols - 6], $top_line_row, $rep->cols[$cols - 6], $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($rep->cols[$cols - 3], $top_line_row, $rep->cols[$cols - 3], $iline7 - $increase_height);
    $rep->LineTo($rep->cols[$cols - 2], $top_line_row, $rep->cols[$cols - 2], $iline7 - $increase_height);
    $rep->LineTo($right, $top_line_row, $right, $iline7 - $increase_height);
}

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 = 150;

    $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(85, $top_line_row, 85, $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 get_latest_cheque_print_report($trans_no, $trans_type) {
    $sql = "SELECT cpg.*,
                ba.bank_account_number,
                 bn.value_name bank_name,
                crsn.value_name crossing_name,
                us.real_name created_user
            FROM ".TB_PREF."cheque_printing_log cpg
                LEFT JOIN ".TB_PREF."bank_accounts ba 
                    ON cpg.bank_account = ba.id
                INNER JOIN ".TB_PREF."list_values bn 
                    ON ba.bank_id = bn.id         
                LEFT JOIN ".TB_PREF."list_values crsn 
                    ON cpg.crossing_id = crsn.id 
                LEFT JOIN ".TB_PREF."users us 
                    ON us.id = cpg.created_by
            WHERE cpg.type =". db_escape($trans_type)." AND cpg.trans_no = ". db_escape($trans_no);

    $sql .= " ORDER BY cpg.id DESC LIMIT 1";
             
    $result = db_query($sql,"Could not cheque_printing details");        
    return db_fetch($result);
}

function print_receipts() {
    global $path_to_root;

    include_once($path_to_root . "/reporting/includes/pdf_report.inc");

    $trans_type = $_POST['PARAM_0'];
    $trans_no = $_POST['PARAM_1'];
    $receipt_type_id = $_POST['PARAM_2'];
    $receipt_date_ = $_POST['PARAM_3'];
    $receipt_reference = $_POST['PARAM_4'];
    
    $msg = save_receipts($trans_type, $trans_no, $receipt_type_id, $receipt_date_, $receipt_reference);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
            return;
        }
    
    $trans_no_2 = $trans_no;

    if (! $trans_no || ! $trans_no_2) return;

    $dec = user_price_dec();

    $cols = array(4, 60, 160, 210, 275, 360, 450, 528);

    // $headers in doctext.inc
    $aligns = array('center', 'left', 'center', 'left', 'right', 'right', 'right');
    $headers  = array(_("Date"),_(" Type"), _("#"), _(" Reference"), _("Total Amount"), _("Left to Allocate"), _("This Allocation"));
    $params = array('receipt_type_id' => $receipt_type_id);

    $cur = get_company_Pref('curr_default');


    $rep = new FrontReport(_('PAYMENT VOUCHER'), "PaymentReceipt", 'A5', 9, 'L');
    
        $myrow = get_receipt_trans_type_record($trans_type, $trans_no);

        $params['bankaccount'] = $myrow['bank_act'];
        $params['tran_no'] = $myrow['trans_no'];
        $params['title'] = "Payment Receipt";

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

        $payeee_name = $payeee_address = '';
        switch ($myrow['person_type_id']) {
            case PT_MISC:
                $payeee_address = $myrow['person_id'];
                break;
            case PT_CUSTOMER:
                if(!($myrow['person_detail_id'] > 0)){
                    $branch = get_default_branch($myrow['person_id']);
                    if(isset($branch['branch_code']) && $branch['branch_code'] > 0){
                        $myrow['person_detail_id'] = $branch['branch_code'];
                    }
                }
                $payeee_address = get_customer_address($myrow['person_id'], $myrow['person_detail_id']);
                
                $payeee_name = get_customer_formatted_name($myrow['person_id']); 

                break;
            case PT_SUPPLIER:
                $supplier = get_supplier($myrow['person_id']);
                if (is_array($supplier)) {
                    $payeee_name = $supplier['supp_name'];
                    $payeee_address = $supplier['supp_address'];
                }
                break;
            case PT_QUICKENTRY:
                break;
            case PT_EMPLOYEE:
                $employee = get_employee_by_id($myrow['person_detail_id']);
                    if (is_array($employee)) {
                        $payeee_name = get_employee_formatted_long_name($employee['employee_code']);
                        $payeee_address = $employee['address'];
                    }
                break;

            default:
                break;
        }
                        
                        $cheque_details = get_latest_cheque_print_report($myrow['trans_no'], $myrow['type']);
			$rep->NewPage();
                        
//                        $rep->row -= 10;
                        
                        $row_line = $rep->row;
                        
                        $rep->RoundedRect(70, 80, 240, 80, 10);

                        $rep->row = $row_line;
                        $rep->Text(40, "Payer");
                        $rep->Font('bold');
//                        $rep->Text(80, $payeee_name);
                        $rep->TextWrapLines(75, 250, $payeee_name);
                        
//                        $rep->NewLine(2);
                        $rep->TextWrapLines(75, 250, $payeee_address);
                        $rep->Font();
                        
                        
                        $rep->row = $row_line; 
                        
                        $rep->Text(340, _("Receipt No :"));
                        $rep->Font('bold');
                        $rep->Text(440, $myrow['receipt_id']);
                        $rep->Font();
                        $rep->row -= 4; 
                        
                        $rep->NewLine();
                        $rep->Text(340, _("Receipt Reference :"));
                        $rep->Font('bold');
                        $rep->Text(440, $myrow['receipt_code']);
                        $rep->Font();
                        $rep->row -= 4; 
                        
                        $rep->NewLine();
                        $rep->Text(340, _("Date :"));
                        $rep->Font('bold');
                        $rep->Text(440, sql2date($myrow['receipt_date']));
                        $rep->Font();
                        $rep->row -= 4; 
                        
                        $rep->NewLine();
                        $rep->Text(340, _("Amount :"));
                        $rep->Font('bold');
                        $rep->Text(440, number_format(abs($myrow['amount']), 2));
                        $rep->Font();
                        $rep->row -= 4; 
                        
                        $rep->NewLine();
                        $rep->Text(340, _("Transaction No :"));
                        $rep->Font('bold');
                        $rep->Text(440, $myrow['trans_no']);
                        $rep->Font();
                        $rep->row -= 4; 
                        
                        if(isset($myrow['cheque_no']) && $myrow['cheque_no'] > 0){
                            $rep->NewLine();
                            $rep->Text(25, _("Cheque No").":");
                            $rep->Font('bold');
                            $rep->Text(80, $myrow['cheque_no']);
                            $rep->Font();
                            $rep->Text(150, _("Cheque Date").":");
                            $rep->Font('bold');
                            $rep->Text(215, (isset($cheque_details['cheque_date'])) ? sql2date($cheque_details['cheque_date']) : '');
                            $rep->Font();
                            $rep->Text(300, _("Bank").":");
                            $rep->Font('bold');
                            $rep->Text(330, $myrow['bank_account_name']);
                            $rep->Font();
                            
                            $rep->row -= 4; 

                        }
                        
                        // Payment Items details
                        
                        $column_headers_start = $rep->row - $rep->lineHeight * 2;
//                        draw_payment_items_table($column_headers_start, $rep);
                        
//                        $rep->Font('bold');
//                        $rep->Text(33, 'Acc. Code');
//                        $rep->Text(270, 'Details');
//                        $rep->Text(518, 'Amount');
                        $rep->Font();
                        $rep->NewLine(2);
                        
                        $brs = 12;
                        
                        switch ($trans_type) {
                            case ST_BANKDEPOSIT:
                                $items_result = get_bank_payments_for_report($trans_type, $trans_no);

                                break;
                            case ST_CASH_RECEIPT:
                                $items_result = get_bank_payments_for_report($trans_type, $trans_no);

                                break;
                            case ST_CUSTPAYMENT:
                                $items_result = get_bank_payments_for_report($trans_type, $trans_no);

                                break;
                        }
                        
                        $add_new_line = false;
//                            while ($item = db_fetch($items_result)) {
//                                if ($add_new_line) {
//                                    $rep->NewLine();
//                                }
//                                $rep->Text(28, $item['account']);
//                                $rep->Text(88, $item['account_name']);
//                                $rep->TextCol(6, 7, number_format($item['amount'], 2));
//                                
//                                if (strlen($item['memo_']) > 0) {
//                                    $rep->NewLine();
//                                    $rep->TextWrapLines(88, 503, $item['memo_']);
//                                    $brs -= 2;
//                                }
//                                
//                                $add_new_line = true;
//                                
//                                $brs--;
//                            }
                            
                        $rep->Font();
                        $words = price_in_words(abs($myrow['amount']), $myrow['type']);
                        if ($words != "") {
                            $rep->Text(40, "Received with thanks", - 2);
                            $rep->Font('bold');
                            $rep->Text(130, "Rupees " . " " . $words." ".$myrow['hundreds_name'], - 2);
                        }
                        $rep->Font();
                        
                        $rep->NewLine();
                        
                        $memos = get_comments_string($trans_type, $myrow['trans_no']);
                        if (strlen($memos) > 0) {
                            $rep->NewLine();
                            $rep->Font();
                            $rep->Text(40, _("As payment for "));
                            $rep->Font('bold');
                            $rep->Font();

                            foreach (explode("\n", $memos) as $key => $memo) {
                                $rep->Text(130, $memo);
                                $rep->NewLine();

                                $brs--;
                            }
                            $rep->Font();
                        }                            
                            
//                        if ($brs > 0) {
//                            $rep->NewLine($brs);
//                        }
                        
                        $rep->row = 150;
                        
//                        if ($rep->row < $rep->bottomMargin + (4 * $rep->lineHeight)) {
//                            $rep->NewPage();
//                            $rep->NewLine(2);
//                        }
                        
                        $rep->Text(80, "Prepared By");
                        $rep->Text(140, _(": ...................................................."));
                        $rep->Text(310, "Received By");
                        $rep->Text(370, _(": ...................................................."));
                        
                        $rep->NewLine(3);
                        
                        $rep->Text(80, _("Authorized By"));                      
                        $rep->Text(140, _(": ...................................................."));
                        
                        $rep->Text(310, "Signature");
                        $rep->Text(370, _(": ...................................................."));
                        
                        
                        $rep->NewLine(3);
                        
                        $rep->Text(80, "Approved By");
                        $rep->Text(140, _(": ...................................................."));
                        $rep->Text(310, "Date");
                        $rep->Text(370, _(": ...................................................."));
                        
            

    $rep->End();
}

?>