<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_BANK_CUST_PAYMENTS';
if(!@$_GET['popup'])
    $path_to_root = "..";
else	
    $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/bank_transfer_validations.inc");

$js = "";
if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}
if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "Bank Customer Payments"));

simple_page_mode(true);

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

function fill_bank_transfer_array(&$data, $myrow){
    global $Refs;
    
    $data['trans_no'] = $myrow['drawer_payment_id'];
    $data['DatePaid'] = $_POST['date_'.$myrow['trans_no']];
//    $data['DatePaid'] = sql2date($myrow['tran_date']);
    $data['amount'] = $myrow['ov_amount'];
    $data['FromBankAccount'] = $myrow['drawer_payment_bank_act'];
    $data['charge'] = 0;
    $data['bank_charge_act'] = get_company_pref('bank_charge_act');
    $data['ref'] = $Refs->get_next(ST_BANKTRANSFER); 
    $data['ToBankAccount'] = $_POST['new_banck_account_id_'.$myrow["drawer_payment_id"]];
    $data['target_amount'] = $myrow['ov_amount'];
    $data['comments'] = "";
    
    return $data;
}

function transfer_drawer_payments($customer_id, $from_date, $to_date){
    $count = 0;
    
    $result = get_drawer_payments($customer_id, $from_date, $to_date);
    while($myrow = db_fetch($result)){
        if(!(isset($_POST['new_banck_account_id_'.$myrow["drawer_payment_id"]]) && $_POST['new_banck_account_id_'.$myrow["drawer_payment_id"]] > 0)){
            continue;
        }
        
        $data = array();
        $msg = array();
        
        fill_bank_transfer_array($data, $myrow);
        $msg = can_process_bank_transfer($data);
        if($msg['success'] !== true){
            return $msg;
        }
        
        begin_transaction();
            $msg = save_bank_transfer($data);
            if($msg['success'] !== true){
                cancel_transaction();
                return $msg;
            }
            
            $n = delete_drawer_payment($myrow["drawer_payment_id"]);
            if($n !== 1){
                cancel_transaction();
                return array("success" => false, "set_focus" => '', "message" => "Could not delete processed drawer payment ".$myrow["drawer_payment_id"]);
            }
        
        commit_transaction();
        
        $count++;
    }
    
    return array("success" => true, "set_focus" => '', "message" => "$count bank account transfers created.");
}

function display_drawer_payments($customer_id, $from_date, $to_date){
    global $Ajax;
    $Ajax->activate('leaves');
    div_start('leaves');
        start_table(TABLESTYLE, "width=95%");

        $th = array(_("Pymnt No."),"Reference", _("Debtor"), "Branch", "Current Bank Account", _("Date"), _("Amount"), "New Bank Account", _("Date"));
        table_header($th);

        $j = 1;
        $k=0; //row colour counter

        $result = get_drawer_payments($customer_id, $from_date, $to_date);
        while ($myrow = db_fetch($result)){
            alt_table_row_color($k);
                label_cell($myrow["trans_no"], "align=center");
                label_cell($myrow["reference"], "align=center");
                label_cell($myrow["customer_name"]);
                label_cell($myrow["customer_branch"]);
                label_cell($myrow["bank_account_name"]);
                label_cell(sql2date($myrow["tran_date"]), "align=center");
                qty_cell($myrow["ov_amount"], false, 2);        

                bank_accounts_list_cells(null, 'new_banck_account_id_'.$myrow["drawer_payment_id"], null, false, '');
                date_cells("", "date_".$myrow["trans_no"]);

            end_row();
            $j++;
            If($j == 12){
                $j = 1;
                table_header($th);
            }
        }

        end_table(1);
    div_end();
}

if(isset($_POST['Process_Bank_Transfers'])){
    $tdp_msg = transfer_drawer_payments($_POST['customer_id'], $_POST['from_date'], $_POST['to_date']);
    if($tdp_msg['success'] === true){
        display_notification($tdp_msg['message']);
    }else{
        display_error($tdp_msg['message']);
    }
}

start_form();
    
    start_table();
        customer_list_cells(_("Customer:"), 'customer_id', null, "Select a customer", true);
        date_cells(_("From Date:"), 'from_date', '', null, 0, -1, 0, null, true);
        date_cells(_("To Date:"), 'to_date', '', null, 0, 1, 0, null, true);
        
        submit_cells('', _("Search"),'',_('Search'), 'default');
    end_table();

    br();
display_drawer_payments($_POST['customer_id'], $_POST['from_date'], $_POST['to_date']);
br();

submit_center('Process_Bank_Transfers', _("Post"), true, false, 'default');

br();

if(!@$_GET['popup']){
    end_form();
    end_page(@$_GET['popup'], false, false);
}

?>