<?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/db/bulk_bank_transfers_db.inc");
include_once($path_to_root . "/gl/includes/bank_transfer_validations.inc");

simple_page_mode(true);

$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 = "Bulk Bank Account Transfers"), isset($_GET['bank_account']), false, "", $js);


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

function fill_bank_transfer_array(&$data, $myrow)
{
    global $Refs;
    
    $data['trans_no'] = $myrow['trans_no'];
    $data['DatePaid'] = sql2date($myrow['trans_date']);
    $data['amount'] = $myrow['amount'];
    $data['FromBankAccount'] = $myrow['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["id"]];
    $data['target_amount'] = $myrow['amount'];
    $data['comments'] = "";
    
    return $data;

}

function create_bulk_bak_transfers()
{
    $count = 0;
    
//    $result = get_drawer_payments();
    
$result = get_bank_trans_for_bulk_bank_transfers($_POST['bank_account_id'], $_POST['from_date'], $_POST['to_date'], $_POST['tran_type'], 1);
//get_bank_trans_for_bulk_bank_transfers($bank_account_id, $from_date, $to_date, $tran_type, $debit);
    while ($myrow = db_fetch($result)){
        if(!(isset($_POST['new_banck_account_id_'.$myrow["id"]]) && $_POST['new_banck_account_id_'.$myrow["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;
        }
        
        commit_transaction();
        
        $count++;
    }
    
    return array("success" => true, "set_focus" => '', "message" => "$count bank account transfers created.");
}

function display_bank_trans_for_bulk_bank_transfers($bank_account_id, $from_date, $to_date, $tran_type, $debit)
{
    global $Ajax;
    
    $Ajax->activate('leaves');

    div_start('leaves');

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

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

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

    $result = get_bank_trans_for_bulk_bank_transfers($bank_account_id, $from_date, $to_date, $tran_type, $debit);
    while ($myrow = db_fetch($result)){
        alt_table_row_color($k);

        //label_cells('',$myrow["id"]);
        $link = get_trans_view_str($tran_type, $myrow["trans_no"]);
        label_cell($link, "align=center");
        
        $link2 = get_trans_view_str($tran_type, $myrow["trans_no"], $myrow["ref"]);
        label_cell($link2, "align=center");
        label_cell($myrow["bank_account_name"]);
        
//        label_cell($myrow["bank_account_name"]);
        label_cell(sql2date($myrow["trans_date"]), "align=center");
        qty_cell($myrow["amount"], false, 2);  
        label_cell($myrow["cheque_no"]);

        bank_accounts_list_cells(null, 'new_banck_account_id_'.$myrow["id"], null, false, '');

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

end_table(1);
div_end();

}


function display_selection_parameters()
{
//    start_form();
    start_table(TABLESTYLE_NOBORDER);
    start_row();
    bank_accounts_list_cells(_("Account:"), 'bank_account_id', null);

    date_cells(_("From:"), 'from_date', '', null, -30);
    date_cells(_("To:"), 'to_date');

    systypes_list_cells("Transaction Type", 'tran_type');
    //yesno_list_cells("Type", 'debit', null, "Credit", "Debit", true);
    $_POST['debit'] = 1;
    submit_cells('Search',_("Search"),'','', 'default');
    end_row();
    end_table();
//    end_form();
}

if (isset($_POST['Process_Bank_Transfers'])){
    $cbbt_msg = create_bulk_bak_transfers();
    if($cbbt_msg['success'] === true){
        display_notification($cbbt_msg['message']);
    }
    else{
        display_error($cbbt_msg['message']);
    }
    
}



start_form();

display_selection_parameters();

br();

display_bank_trans_for_bulk_bank_transfers($_POST['bank_account_id'], $_POST['from_date'], $_POST['to_date'], $_POST['tran_type'], 1);

br();

submit_center('Process_Bank_Transfers', _("Create Bank Account Transfers"), true, false, 'default');

br();


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