X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=gl%2Fincludes%2Fdb%2Fgl_db_bank_trans.inc;h=3bd65e23b974270983b1dcb6e2c6e0c474951434;hb=0c760ea65c8c6f0a45ea8328abab53be649f2105;hp=c9f550c82419aff60614b087b662a0f417447075;hpb=9b413697aa4ca30f7f336dbf5fd04bf8caf2bc43;p=fa-stable.git diff --git a/gl/includes/db/gl_db_bank_trans.inc b/gl/includes/db/gl_db_bank_trans.inc index c9f550c8..3bd65e23 100644 --- a/gl/includes/db/gl_db_bank_trans.inc +++ b/gl/includes/db/gl_db_bank_trans.inc @@ -32,7 +32,7 @@ function add_bank_trans($type, $trans_no, $bank_act, $ref, $date_, } 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_); @@ -41,7 +41,8 @@ function add_bank_trans($type, $trans_no, $bank_act, $ref, $date_, trans_date, amount, person_type_id, person_id) "; $sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate', - $amount_bank, $person_type_id, ". db_escape($person_id).")"; + ".db_escape($amount_bank).", ".db_escape($person_type_id) + .", ". db_escape($person_id).")"; if ($err_msg == "") $err_msg = "The bank transaction could not be inserted"; @@ -53,8 +54,8 @@ function add_bank_trans($type, $trans_no, $bank_act, $ref, $date_, function exists_bank_trans($type, $type_no) { - $sql = "SELECT trans_no FROM ".TB_PREF."bank_trans WHERE type=$type - AND trans_no=$type_no"; + $sql = "SELECT trans_no FROM ".TB_PREF."bank_trans WHERE type=".db_escape($type) + ." AND trans_no=".db_escape($type_no); $result = db_query($sql, "Cannot retreive a bank transaction"); return (db_num_rows($result) > 0); @@ -68,13 +69,13 @@ function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id= FROM ".TB_PREF."bank_trans, ".TB_PREF."bank_accounts WHERE ".TB_PREF."bank_accounts.id=".TB_PREF."bank_trans.bank_act "; if ($type != null) - $sql .= " AND type=$type "; + $sql .= " AND type=".db_escape($type); if ($trans_no != null) - $sql .= " AND ".TB_PREF."bank_trans.trans_no = $trans_no "; + $sql .= " AND ".TB_PREF."bank_trans.trans_no = ".db_escape($trans_no); if ($person_type_id != null) - $sql .= " AND ".TB_PREF."bank_trans.person_type_id = $person_type_id "; + $sql .= " AND ".TB_PREF."bank_trans.person_type_id = ".db_escape($person_type_id); if ($person_id != null) - $sql .= " AND ".TB_PREF."bank_trans.person_id = '$person_id'"; + $sql .= " AND ".TB_PREF."bank_trans.person_id = ".db_escape($person_id); $sql .= " ORDER BY trans_date, ".TB_PREF."bank_trans.id"; return db_query($sql, "query for bank transaction"); @@ -82,9 +83,40 @@ function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id= //---------------------------------------------------------------------------------------- +function get_bank_trans_for_bank_account($bank_account, $from, $to) +{ + $from = date2sql($from); + $to = date2sql($to); + $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, t.id"; + + return db_query($sql,"The transactions for '" . $bank_account . "' could not be retrieved"); +} + +//---------------------------------------------------------------------------------------- + +function get_balance_before_for_bank_account($bank_account, $from) +{ + $from = date2sql($from); + $sql = "SELECT SUM(amount) FROM ".TB_PREF."bank_trans WHERE bank_act=" + .db_escape($bank_account) . " + AND trans_date < '$from'"; + $before_qty = db_query($sql, "The starting balance on hand could not be calculated"); + $bfw_row = db_fetch_row($before_qty); + return $bfw_row[0]; +} +//---------------------------------------------------------------------------------------- + function get_gl_trans_value($account, $type, $trans_no) { - $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE account='$account' AND type=$type AND type_no=$trans_no"; + $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE account=" + .db_escape($account)." AND type=".db_escape($type) + ." AND type_no=".db_escape($trans_no); $result = db_query($sql, "query for gl trans value"); @@ -96,11 +128,12 @@ function get_gl_trans_value($account, $type, $trans_no) function void_bank_trans($type, $type_no, $nested=false) { + if (!$nested) begin_transaction(); $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0 - WHERE type=$type AND trans_no=$type_no"; + WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no); $result = db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no"); @@ -120,6 +153,57 @@ function void_bank_trans($type, $type_no, $nested=false) commit_transaction(); } -//---------------------------------------------------------------------------------- +/** +* 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=null, $user=null) +{ + if ($delta_amount >= 0 && isset($date)) + return null; // amount increese is always safe + + $balance = $date ? get_bank_account_limit($bank_account, $date, $user) : 0; + + if (!isset($balance) && isset($date)) + return null; // unlimited account + + if ($balance < $delta_amount) + return array('amount' => $balance, 'trans_date'=> $date); + + $balance += $delta_amount; + + $sql = "SELECT sum(amount) as amount, trans_date FROM ".TB_PREF."bank_trans WHERE bank_act=".db_escape($bank_account); + if ($date) + { + $date = date2sql($date); + $sql .= " AND trans_date > '$date'"; + } + $sql .= " GROUP BY trans_date ORDER BY trans_date ASC"; + + $history = db_query($sql, "cannot retrieve cash account history"); + + while ($trans = db_fetch($history)) { + $balance += $trans['amount']; + if ($balance < 0) + return $trans; + } + + 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 +?>