<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PRINT_CHEQUES';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Tax Report
// ----------------------------------------------------------------
$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 . "/gl/includes/gl_db.inc"); 
    
include_once($path_to_root . "/reporting/includes/pdf_report.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_bank_accounts.inc");
include_once($path_to_root . "/admin/db/cheque_printing_templates_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/gl/includes/db/print_cheques_db.inc");

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

print_cheque_leaf();



function get_cheque_payment_details($type, $trans_no)
{
	$sql = "SELECT	IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
		gl.tran_date,
		gl.type,
		gl.type_no,
		refs.reference,
		gl.amount *-1 amount,
		com.memo_ 
		FROM ".TB_PREF."gl_trans as gl
                LEFT JOIN ".TB_PREF."audit_trail as a ON
                        (gl.type=a.type AND gl.type_no=a.trans_no)
                LEFT JOIN ".TB_PREF."comments as com ON
                        (gl.type=com.type AND gl.type_no=com.id)
                LEFT JOIN ".TB_PREF."refs as refs ON
                        (gl.type=refs.type AND gl.type_no=refs.id) 
		WHERE gl.amount < 0 
                AND gl.type =". db_escape($type)." 
                AND gl.type_no =". db_escape($trans_no);
	
	$result = db_query($sql,"Could not get_cheque_payment_details");
    return db_fetch($result);
}


function getTaxInfo($id)
{
	$sql = "SELECT * FROM ".TB_PREF."tax_types WHERE id=$id";
    $result = db_query($sql,"No transactions were returned");
    return db_fetch($result);
}


function display_cheque_printing_error_message(&$rep, $message)
{
    $rep->row = 75;
    $rep->Text(20, $message);
    $rep->End();
}
//----------------------------------------------------------------------------------------------------

function print_cheque_leaf()
{
    global $path_to_root, $systypes_array;
	
    $type = $_POST['PARAM_0'];
    $trans_no = $_POST['PARAM_1'];
    $crossing_id = $_POST['PARAM_2'];
    $account_name = $_POST['PARAM_3'];
    $cheque_date = $_POST['PARAM_4'];
    $cheque_no = $_POST['PARAM_5'];
    $payment_type = 0;
    
    if($crossing_id == "AC_PAYEE_ONLY"){
        $crossing_rec = get_listvalue_by_code("CHEQUE_CROSSINGS", $crossing_id);
        $crossing_id = $crossing_rec['id'];
    }

    
    if(isset($_POST['PARAM_6'])){
        $manual = 0;
    }
    else{
        $manual = 1;
    }

    
    
    $orientation = 'P';
    
    $allowed_types_array = array(ST_BANKPAYMENT, ST_BANKTRANSFER, ST_SUPPAYMENT, ST_PETTY_CASH_FLOAT_REIMBUR);
    
    // validations
    if (!($type > 0)) {
        display_error('Please select a type.');
        return;
    }
    
    if (!($crossing_id > 0)) {
        display_error('Please select a crossing type.');
        return;
    }
    
    if (! in_array($type, $allowed_types_array)) {
        display_error('Transaction type '.$systypes_array[$type].' not supported for cheque printing.');
        return;
    }
    
    $cheque_payment = get_cheque_payment_details($type, $trans_no);   
    $result = get_bank_trans($type, $trans_no);
    $bank_trans = db_fetch($result);
    
    
    if (! is_array($bank_trans)) {
        display_error('Transaction '.$systypes_array[$type]." #$trans_no not found.");
        return;
    }
    
    switch ($type) {
        case ST_BANKPAYMENT:
        case ST_SUPPAYMENT: 
        case ST_PETTY_CASH_FLOAT_REIMBUR:
        default:
            $bank_account = get_bank_account($bank_trans['bank_act']);
            $bank_id = $bank_account['bank_id'];
            
            if(!(strlen($account_name) > 0)){
                if($bank_trans['person_type_id'] == PT_SUPPLIER){
                    $supplier = get_supplier($bank_trans['person_id']);
                    $account_name = $supplier['bank_account'];
                }
            }
            break;

    }
    
    
    $factor = 2.835;
    
    $template = get_cheque_printing_template($bank_id);
    if(!$template){
        $page_height = 250;
        $page_width = 550; 
        $font_size = 9; 
    }
    else{
        $page_height = $factor * $template['page_width'];
        $page_width = $factor * $template['page_length']; 
        $font_size = $template['font_size']; 
        if(!($font_size > 0)){
            $font_size = 9;
        }
    }
    
    if(!$template){
        $bank = get_list_value($bank_id);
        $message = "No cheque printing template defined for ".$bank['value_name'].'.';
        display_error($message);
        return;
    } 
    
    if(!(strlen($account_name) > 0)){
        if($bank_trans['person_type_id'] == PT_SUPPLIER){
            $supplier = get_supplier($bank_trans['person_id']);
            $message = "No bank account name entered for supplier ".$supplier['supp_name'].'.';
        }
        else{
            $message = "No beneficiary bank account name found.";
        }
        display_error($message);
        return;
    }
    
    
    if(!is_date($cheque_date)){
        $message = "Cheque date entered is incorrect.";
        display_error($message);
        return;
    }
    
    if(strlen($cheque_no) != 6){
        $message = "Cheque number entered is incorrect.";
        display_error($message);
        return;
    }
    
    
    $company_name = $bank_account["printing_account_name"];
    
    $margins['top'] = 5;
    $margins['bottom'] = 5;
    $margins['left'] = 5;
    $margins['right'] = 5;

    $size = array($page_width, $page_height);
         
    $rep = new FrontReport(_('Print Cheque'), "PrintCheque", $size, $font_size, $orientation, $margins);

    $rep->SetHeaderType('NoHeader');

//	$rep->Font('bold', 'times');
	$rep->Font('', 'times');
//	$rep->Info($params, $cols, $headers, $aligns);

	$rep->NewPage();

    
    $cheque_date = date2sql($cheque_date);
    $amount_value = $cheque_payment['amount'];
    
    if($manual){
        $date = $cheque_date;
    }
    else{
        //$date = $cheque_payment['tran_date'];
        $date = $cheque_date;
        $crossing_id = $template['crossing_id'];
    }
    
    if(!($crossing_id > 0)){
        $crossing_id = 0;
    }
    
    $rec_id = 0;
    begin_transaction();
    update_cheque_printed($type,$trans_no, $cheque_no);
    $rec_id = add_to_cheque_printing_log($type, $trans_no, $bank_trans['bank_act'], $account_name, $crossing_id, $date, $cheque_no, $manual, $payment_type);
    if(!($rec_id > 0)){
        cancel_transaction();
        $message = "Could not save the cheque number. Please try reprinting.";
        display_cheque_printing_error_message($rep, $message);
        return;
    }
    commit_transaction();
    
    
    /******************** Crossing *****************************/
    if($crossing_id > 0){
        $crossing = get_list_value($crossing_id);
        
        $instruction_x = $factor * $template['crossing_x'];
        $instruction_y = $factor * $template['crossing_y'];    
    
        $instruction = $crossing['value_name'];// arg ergqrg qergq rgqergv qrg";
    
            if ($crossing['value_code'] == 'BLANK') {
                
//                $rep->SetLineStyle(array("width"=>1));
//                $line_length = strlen($instruction)* $font_size * 0.5;
//                $rep->LineTo($instruction_x, $page_height - $instruction_y + $font_size + 1, $instruction_x + $line_length, $page_height - $instruction_y + $font_size + 1);
//                $rep->LineTo($instruction_x, $page_height - $instruction_y - 1, $instruction_x + $line_length, $page_height - $instruction_y - 1);
            }    
            elseif ($crossing['value_code'] == 'CASH') {
                
                $rep->SetLineStyle(array("width"=>1));
                $line_length = strlen($instruction)* $font_size * 0.5;
                $rep->LineTo($instruction_x, $page_height - $instruction_y + $font_size + 1, $instruction_x + $line_length, $page_height - $instruction_y + $font_size + 1);
                $rep->LineTo($instruction_x, $page_height - $instruction_y - 1, $instruction_x + $line_length, $page_height - $instruction_y - 1);
                
            } elseif ($crossing['value_code'] == 'AC_PAYEE_ONLY') {
                
                $rep->row = $page_height - $instruction_y;    
                $rep->Text($instruction_x, $instruction);

                $rep->SetLineStyle(array("width"=>1));
                $line_length = strlen($instruction)* $font_size * 0.5;
                $rep->LineTo($instruction_x, $page_height - $instruction_y + $font_size + 1, $instruction_x + $line_length, $page_height - $instruction_y + $font_size + 1);
                $rep->LineTo($instruction_x, $page_height - $instruction_y - 1, $instruction_x + $line_length, $page_height - $instruction_y - 1);
            }
    }
 
    /******************** Date *****************************/
    $full_date_y = $factor * $template['full_date_y'];
    $rep->row = $page_height - $full_date_y; 
    
	$year_x = $factor * $template['year_x'];
	$year_width = $factor * $template['year_width'];
    
    if($template['year_format'] == "YYYY"){
        $year = substr($date, 0, 1)." ".substr($date, 1, 1)." ".substr($date, 2, 1)." ".substr($date, 3, 1);
    }
    elseif($template['year_format'] == "YY"){
        $year = substr($date, 2, 1)." ".substr($date, 3, 1);
    }
    else{
        $year = "No format";
    }
    $rep->TextWrap($year_x, $rep->row, $year_width, $year, 'justify');
    
    $month_x = $factor * $template['month_x'];
	$month_width = $factor * $template['month_width'];
    $mm = substr($date, 5, 1)." ".substr($date, 6, 1);
    $rep->TextWrap($month_x, $rep->row, $month_width, $mm, 'justify');
    
    $date_x = $factor * $template['date_x'];
	$date_width = $factor * $template['date_width'];
    $dd = substr($date, 8, 1)." ".substr($date, 9, 1);
    $rep->TextWrap($date_x, $rep->row, $date_width, $dd, 'justify');

    /******************** Account Name *****************************/
    $account_name_x = $factor * $template['account_name_x'];
    $account_name_y = $factor * $template['account_name_y'];    
    
    $account_name = "***".$account_name."***";
    
    $rep->row = $page_height - $account_name_y;    
    $rep->Text($account_name_x, $account_name);
    
    /******************** Amount Text *****************************/
    $amount_full_text = price_in_words($amount_value, ST_CHEQUE);
    $amount_full_text .= " Cents Only";    
    
    $amount_full_text = "***".$amount_full_text."***";

    $amount_text_width = $factor * $template['amount_text_width'];
    $amount_text_line_height = $template['amount_text_line_height'];
    $amount_text_offset = $factor * $template['amount_text_offset'];
    
    for ($k = 0; $k < 5; $k++) {
        $amount_text_array = $rep->calcTextWrap($amount_full_text, $amount_text_width, true);
        $amount_text = $amount_text_array[0];
        $amount_text_x = $factor * $template['amount_text_x'];
        $amount_text_y = $factor * $template['amount_text_y'];
    
        $rep->row = $page_height - $amount_text_y - ($amount_text_line_height * $k);    
        if($k ==0){
            $amount_text_x += $amount_text_offset;
        }
        $rep->Text($amount_text_x, $amount_text);
        $amount_full_text = $amount_text_array[1];
    }
    
    /******************** Amount Value *****************************/
    $amount_value_x = $factor * $template['amount_value_x'];
	$amount_value_y = $factor * $template['amount_value_y'];
    
    $amount_value_length = strlen($amount_value);
    
    $asterics = "";
    if($amount_value_length < 10){
        $asterics = substr("**********", 0, (10 - $amount_value_length));
    }
    $rep->row = $page_height - $amount_value_y;    
    $rep->Text($amount_value_x, $asterics.number_format($amount_value,2));

    
    /******************** Company Name *****************************/
    $company_name_x = $factor * $template['company_name_x'];
	$company_name_y = $factor * $template['company_name_y'];
    
    $rep->row = $page_height - $company_name_y;    
    $rep->Text($company_name_x, $company_name);
    
    
    /******************** Signatory1 *****************************/
    if(strlen($template['signatory1_name']) > 0){

        $signatory1_x = $factor * $template['signatory1_x'];
        $signatories_y = $factor * $template['signatories_y'];    
    
        $signatory1_name = $template['signatory1_name'];
        $rep->row = $page_height - $signatories_y;    
        $rep->Text($signatory1_x, $signatory1_name);

        $rep->SetLineStyle(array("width"=>1, "dash"=>"1,2"));
        $signatory1_line_length = strlen($signatory1_name)* $font_size * 0.5;
        $dotted_line_y = $page_height - $signatories_y + $font_size + 2;
        $dotted_line_offset = $factor * 5;
        $rep->LineTo($signatory1_x - $dotted_line_offset, $dotted_line_y, $signatory1_x + $signatory1_line_length + $dotted_line_offset, $dotted_line_y);
    }
    
    /******************** Signatory2 *****************************/
    if(strlen($template['signatory2_name']) > 0){

        $signatory2_x = $factor * $template['signatory2_x'];
        $signatories_y = $factor * $template['signatories_y'];    
    
        $signatory2_name = $template['signatory2_name'];
        $rep->row = $page_height - $signatories_y;    
        $rep->Text($signatory2_x, $signatory2_name);

        $rep->SetLineStyle(array("width"=>1, "dash"=>"1,2"));
        $signatory2_line_length = strlen($signatory2_name)* $font_size * 0.5;
        $dotted_line_y = $page_height - $signatories_y + $font_size + 2;
        $dotted_line_offset = $factor * 5;
        $rep->LineTo($signatory2_x - $dotted_line_offset, $dotted_line_y, $signatory2_x + $signatory2_line_length + $dotted_line_offset, $dotted_line_y);
    }

//    $rep->Rotate(180);
            
	$rep->End();
}

?>