}
else
$amount_bank = $amount;
-
+ $amount_bank = round2($amount_bank, user_price_dec());
// Also store the rate to the home
//$BankToHomeCurrencyRate = get_exchange_rate_to_home_currency($bank_account_currency, $date_);
{
$from = date2sql($from);
$to = date2sql($to);
- $sql = "SELECT ".TB_PREF."bank_trans.* FROM ".TB_PREF."bank_trans
- WHERE ".TB_PREF."bank_trans.bank_act = ".db_escape($bank_account) . "
+ $sql = "SELECT t.* FROM "
+ .TB_PREF."bank_trans t LEFT JOIN ".TB_PREF."voided v ON t.type=v.type AND t.trans_no=v.id
+ WHERE t.bank_act = ".db_escape($bank_account) . "
+ AND ISNULL(v.date_)
AND trans_date >= '$from'
AND trans_date <= '$to'
- ORDER BY trans_date,".TB_PREF."bank_trans.id";
+ ORDER BY trans_date, t.id";
return db_query($sql,"The transactions for '" . $bank_account . "' could not be retrieved");
}
function void_bank_trans($type, $type_no, $nested=false)
{
+
if (!$nested)
begin_transaction();
commit_transaction();
}
-//----------------------------------------------------------------------------------
-
-//----------------------------------------------------------------------------------------
-
-function clear_bank_trans($type, $type_no, $nested=false)
+/**
+* Check account history to find transaction which would exceed authorized overdraft for given account.
+* Returns null or transaction in conflict. Running balance is checked on daily basis only, to enable ID change after edition.
+* $delta_amount - tested change in bank balance at $date.
+**/
+function check_bank_account_history($delta_amount, $bank_account, $date, $user=null)
{
- global $Refs;
-
- if (!$nested)
- begin_transaction();
-
- $sql = "DELETE FROM ".TB_PREF."bank_trans
- WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
+ if ($delta_amount >= 0)
+ return null; // amount increese is always safe
- $result = db_query($sql, "could not clear bank transactions for type=$type and trans_no=$type_no");
+ $balance = get_bank_account_limit($bank_account, $date, $user);
- clear_gl_trans($type, $type_no, true);
+ if (!isset($balance))
+ return null; // unlimited account
- // in case it's a customer trans - probably better to check first
- void_cust_allocations($type, $type_no);
- clear_customer_trans($type, $type_no);
+ $date = date2sql($date);
+ $balance+= $delta_amount;
- // in case it's a supplier trans - probably better to check first
- void_supp_allocations($type, $type_no);
- clear_supp_trans($type, $type_no);
+ $sql = "SELECT sum(amount) as amount FROM ".TB_PREF."bank_trans WHERE bank_act=".db_escape($bank_account)
+ ." AND trans_date >= '$date' GROUP BY trans_date ORDER BY trans_date ASC";
- clear_trans_tax_details($type, $type_no);
+ $history = db_query($sql, "cannot retrieve cash account history");
- //Delete the reference
- $Refs->delete($type, $type_no);
+ while ($trans = db_fetch($history)) {
+ $balance += $trans['amount'];
+ if ($balance < 0)
+ return $trans;
+ }
- delete_comments($type, $type_no);
- if (!$nested)
- commit_transaction();
+ return null;
}
+/**
+* Check bank transfer, deposit or customer deposit before voiding.
+**/
+function check_void_bank_trans($type, $type_no)
+{
+ $moves = get_bank_trans($type, $type_no);
+ while ($trans = db_fetch($moves)) {
+ if ($trans['amount'] > 0) { // skip transfer input part
+ return check_bank_account_history(-$trans['amount'], $trans['bank_act'], sql2date($trans['trans_date'])) == null;
+ }
+ }
+ return true;
+}
-?>
\ No newline at end of file
+?>