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

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

function can_process_bank_transfer($data)
{
	global $Refs, $systypes_array;
	
        $error_prefix = "Transaction Id ".$data['trans_no']." - ";
	if (!is_date($data['DatePaid'])) 
	{
		$message = (_($error_prefix."The entered date is invalid."));
		$set_focus = ('DatePaid');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}
	if (!is_date_in_fiscalyear($data['DatePaid']))
	{
		$message = (_($error_prefix."The entered date is not in fiscal year."));
		$set_focus = ('DatePaid');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}

        $msg = is_accounting_period_open($data['DatePaid']);
        if ($msg['success'] !== true)
        {
            $message = ($msg['message']);
            $set_focus = ('DatePaid');
            return array("success" => false, "set_focus" => $set_focus, "message" => $message);
        }
        
	if (!check_num2($data['amount'], 0)) 
	{
		$message = (_($error_prefix."The entered amount is invalid or less than zero."));
		$set_focus = ('amount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}
	if (input_num2($data['amount']) == 0) {
		$message = (_($error_prefix."The total bank amount cannot be 0."));
		$set_focus = ('amount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}

	$limit = get_bank_account_limit($data['FromBankAccount'], $data['DatePaid']);

	$amnt_tr = input_num2($data['charge']) + input_num2($data['amount']);

	if ($limit !== null && floatcmp($limit, $amnt_tr) < 0)
	{
		$message = (sprintf(_($error_prefix."The total bank amount exceeds allowed limit (%s) for source account."), price_format($limit)));
		$set_focus = ('amount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}
    
    $trans = check_bank_account_history(-$amnt_tr, $data['FromBankAccount'], $data['DatePaid']);
    if($trans != null){
		$message = (sprintf(_($error_prefix."The bank transaction would result in exceed of authorized overdraft limit for transaction: %s #%s on %s."),
                                                    $systypes_array[$trans['type']], $trans['trans_no'], sql2date($trans['trans_date'])));
		$set_focus = ('amount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}

//	if (isset($data['charge']) && !check_num2($data['charge'], 0)) 
    $v = $data['charge'];
//    $v = null;
    if (!check_num2($v, 0)) 
	{
		$message = (_($error_prefix."The entered amount is invalid or less than zero.".$data['charge']));
		$set_focus = ('charge');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}
	if (isset($data['charge']) && input_num2($data['charge']) > 0 && get_company_pref('bank_charge_act') == '') {
		$message = (_($error_prefix."The Bank Charge Account has not been set in System and General GL Setup."));
		$set_focus = ('charge');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}
	if (!$Refs->is_valid($data['ref'])) 
	{
		$message = (_($error_prefix."You must enter a reference."));
		$set_focus = ('ref');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}

	if (!is_new_reference($error_prefix.$data['ref'], ST_BANKTRANSFER)) 
	{
		$message = (_("The entered reference is already in use."));
		$set_focus = ('ref');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}

	if ($data['FromBankAccount'] == $data['ToBankAccount']) 
	{
		$message = (_($error_prefix."The source and destination bank accouts cannot be the same."));
		$set_focus = ('ToBankAccount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}

	if (isset($data['target_amount']) && !check_num2($data['target_amount'], 0)) 
	{
		$message = (_($error_prefix."The entered amount is invalid or less than zero."));
		$set_focus = ('target_amount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}
	if (isset($data['target_amount']) && input_num2($data['target_amount']) == 0) {
		$message = (_($error_prefix."The incomming bank amount cannot be 0."));
		$set_focus = ('target_amount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
	}

    $msg = validate_db_has_currency_rates(get_bank_account_currency($data['FromBankAccount']), $data['DatePaid']);
	if ($msg['success'] !== true){
        $message = $error_prefix.$msg['message'];
        $set_focus = ('FromBankAccount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
    }

	$msg = validate_db_has_currency_rates(get_bank_account_currency($data['ToBankAccount']), $data['DatePaid']);
	if ($msg['success'] !== true){
        $message = $error_prefix.$msg['message'];
        $set_focus = ('ToBankAccount');
		return array("success" => false, "set_focus" => $set_focus, "message" => $message);
    }

    return array("success" => true, "message" => "");	
}

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


function save_bank_transfer($data)
{
	new_doc_date($data['DatePaid']);
	$trans_no = add_bank_transfer($data['FromBankAccount'], $data['ToBankAccount'],
		$data['DatePaid'], input_num2($data['amount']), $data['ref'], $data['comments'], input_num2($data['charge']), input_num2($data['target_amount']));

	 return array("success" => true, "trans_no" => $trans_no, "message" => "");	
}

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



?>