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

$page_security = 'SA_PRINT_CHEQUES';
$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/ui.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 . "/includes/banking.inc");
include_once($path_to_root . "/gl/includes/db/print_cheques_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_bank_accounts.inc"); 
include_once($path_to_root . "/gl/includes/db/gl_db_bank_trans.inc"); 

$js = '';
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

page(_($help_context = "Print Cheques"), false, false, "", $js);

simple_page_mode();

check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));

//-----------------------------------------------------------------------------------
// Ajax updates
//

global $Ajax;
	$Ajax->activate('trans_tbl');

//------------------------------------------------------------------------------------------------
if ($selected_id > 0) {
    $msg = save_online_payment($selected_id);
        if ($msg['success'] === true) {
            display_notification($msg['message']);
        } else {
            display_error($msg['message']);
        }
}

start_form();
start_table(TABLESTYLE_NOBORDER);
start_row();

if(!isset($_POST['bank_account'])){
    if(get_global_bank_account() > 0){
        $_POST['bank_account'] = get_global_bank_account();
    }
}

if(isset($_POST['bank_account'])){
    bank_accounts_list_cells(_("Account:"), 'bank_account', $_POST['bank_account'], true, 'Select a Bank Account');
}
else{
    bank_accounts_list_cells(_("Account:"), 'bank_account', null, true, 'Select a Bank Account');
}

set_global_bank_account($_POST['bank_account']) ;

if(isset($_POST['TransAfterDate'])){
    $TransAfterDate = $_POST['TransAfterDate'];
}
else{
    $TransAfterDate = add_days(Today(), -30);
}


if(isset($_POST['TransToDate'])){
    $TransToDate = $_POST['TransToDate'];
}
else{
    $TransToDate = add_days(Today(), 1);
}

if(isset($_POST['printed'])){
    $printed = $_POST['printed'];
}
else{
    $printed = 0;
}


//$values_array[ST_BANKPAYMENT] = $systypes_array[ST_BANKPAYMENT];
//$values_array[ST_BANKTRANSFER] = $systypes_array[ST_BANKTRANSFER];
//$values_array[ST_SUPPAYMENT] = $systypes_array[ST_SUPPAYMENT];

//systypes_list_cells("Type:", 'type_id', null, false, array(), 'Select a Transaction Type');
//custom_values_list_cells("Type:", 'type_id', $values_array, null, 'Select a Transaction Type', true);
cheque_printing_trans_types_list_cells("Type:", 'type_id', null, 'Select a Transaction Type', true);
date_cells(_("From:"), 'TransAfterDate');
date_cells(_("To:"), 'TransToDate');
yesno_list_cells("Printed", "printed", null, "All", "Not Printed", true);
//submit_cells('Search',_("Search"),'','', 'default');
end_row();
end_table();

submit_center_first("Search", 'search');
end_form();

br(2);
//------------------------------------------------------------------------------------------------

//if (!isset($_POST['bank_account']))
//	$_POST['bank_account'] = "";

if(($_POST['bank_account'] > 0 || $_POST['type_id'] > 0)){
    
}else{
      $_POST['bank_account'] = 9999999999;
      $_POST['type_id'] = 99999999;
    $selection_note= ("Please select either a bank account or a transaction type");
    
    display_note($selection_note);
    br(2);
}

$bank_account = $_POST['bank_account'];
$result = get_bank_trans_for_cheque_printing($bank_account, $_POST['TransAfterDate'], $_POST['TransToDate'],
                                            $_POST['printed'], $_POST['type_id']);	

div_start('trans_tbl');
//$act = get_bank_account($_POST["bank_account"]);
//display_heading($act['bank_account_name']." - ".$act['bank_curr_code']);

start_table(TABLESTYLE, "width=98%");


$th = array(_("Bank Account"),_("Type"), _("#"), _("Reference"), _("Date"), _("Amount"), _("Person"), _("Memo"), _("Payment Type"), _("Crossing"), 
            _("Cheque / <br>Transaction Date"), _("Cheque / <br>Transaction No"), "");
table_header($th);
        


end_row();

$j = 1;
$k = 0; //row colour counter
while ($myrow = db_fetch($result))
{
    alt_table_row_color($k);

        $bank_account_formatted_name = get_bank_account_formatted_name($myrow["bank_act"]);
        label_cell($bank_account_formatted_name);

        $trandate = sql2date($myrow["trans_date"]);
        label_cell($systypes_array[$myrow["type"]]);
        label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"]));
        label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"],$myrow['ref']));
        label_cell($trandate);
//	display_debit_or_credit_cells($myrow["amount"]);
        amount_cell(-$myrow["amount"]);
        label_cell(payment_person_name($myrow["person_type_id"],$myrow["person_id"],true,$myrow["person_detail_id"]));
        label_cell(get_comments_string($myrow["type"], $myrow["trans_no"]));
    //	label_cell(get_gl_view_str($myrow["type"], $myrow["trans_no"]));

        $crossing_id_name = 'crossing_id'."_".$myrow["id"];
        if(!(isset($_POST[$crossing_id_name]) && $_POST[$crossing_id_name]> 0)){
            $_POST[$crossing_id_name] = $myrow["crossing_id"];
        }
    //    value_list_cells(null, $crossing_id_name, 'CHEQUE_CROSSINGS', $_POST[$crossing_id_name], '', true);
    //    hidden($crossing_id_name, $_POST[$crossing_id_name]);

        $payment_type = isset($_POST['payment_type_'.$myrow['id']]) ? $_POST['payment_type_'.$myrow['id']] : '';
        three_value_list_cells('', 'payment_type_'.$myrow['id'], null, '', '', 'Cheque', 'Cheque', 'Online', 'Online', true);

        if (($myrow["person_type_id"] == PT_SUPPLIER) || ($payment_type == 'Online')) {

            if ($payment_type == 'Cheque') {
                label_cell($myrow["crossing_name"]);
            } else {
                label_cell('');
            }

            date_cells(null, 'cheque_date_'.$myrow['id'], null, null, 0, 0, 0, null, true);

            if (isset($_POST['cheque_no_'.$myrow['id']])) {
                $cheque_no = $_POST['cheque_no_'.$myrow['id']];
            } else {
                $cheque_no = '';
            }
            
            text_cells_ex(null, 'cheque_no_'.$myrow['id'], 15, 50, $cheque_no, null, null, null, true);

        } else {
            label_cell('');
            label_cell('');
            label_cell('');
            
            $_POST['cheque_date_'.$myrow['id']] = $trandate;
            hidden('cheque_date_'.$myrow['id'], $trandate);
            $_POST['cheque_no_'.$myrow['id']] = '';
            hidden('cheque_no_'.$myrow['id'], '');
        }

        if ($payment_type == 'Online') {
            if (strlen($_POST['cheque_no_'.$myrow['id']]) > 0) {
                $print_cheque_text = $_POST['cheque_date_'.$myrow['id']].' "'.$_POST['cheque_no_'.$myrow['id']].'"';
            } else {
                $print_cheque_text = $_POST['cheque_date_'.$myrow['id']].' '.$_POST['cheque_no_'.$myrow['id']].'';
            }
            $print_link = button('Edit'.$myrow['id'], $print_cheque_text);

        } elseif ($myrow["person_type_id"] != PT_SUPPLIER) {
            $link_text = "Print Manually";
            $url = "/reporting/reports_main.php?Class=7&REP_ID=755";
            $href = $path_to_root . $url;
            $print_link = "<a href='$href'>" . $link_text . "</a>";

        }
        elseif(isset($_POST['cheque_no_'.$myrow['id']]) && strlen($_POST['cheque_no_'.$myrow['id']]) > 0){
            $order_no = array("type"=>$myrow['type'], 
                                "trans_no"=>$myrow['trans_no'], 
                                "cheque_date"=>$_POST['cheque_date_'.$myrow['id']],
                                "cheque_no"=>$_POST['cheque_no_'.$myrow['id']]);

            if (strlen($_POST['cheque_no_'.$myrow['id']]) > 0) {
                $print_cheque_text = $_POST['cheque_date_'.$myrow['id']].' "'.$_POST['cheque_no_'.$myrow['id']].'"';
            } else {
                $print_cheque_text = $_POST['cheque_date_'.$myrow['id']].' '.$_POST['cheque_no_'.$myrow['id']].'';
            }
            $print_link = print_document_link($order_no, $print_cheque_text, true, ST_PRINT_CHEQUES);
        }
        else{
            $print_link = '';
        }

        if ($payment_type == '') {
            label_cell('');
        } else {
            label_cell($print_link);
        }
    
    end_row();


	if ($j == 12)
	{
            $j = 1;
            table_header($th);
	}
	$j++;
}
//end of while loop

end_table(2);
div_end();

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

end_page();



?>
