// $date_ is display date (non-sql)
function add_bank_trans($type, $trans_no, $bank_act, $ref, $date_,
- $amount, $person_type_id, $person_id, $currency="", $err_msg="", $rate=0)
+ $amount, $charge=0, $person_type_id=0, $person_id=0, $currency="", $rate=0)
{
$sqlDate = date2sql($date_);
}
else
$amount_bank = $amount;
-
-
- // Also store the rate to the home
- //$BankToHomeCurrencyRate = get_exchange_rate_to_home_currency($bank_account_currency, $date_);
+ $amount_bank = round2($amount_bank, user_price_dec());
$sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, bank_act, ref,
- trans_date, amount, person_type_id, person_id) ";
+ trans_date, amount, charge, person_type_id, person_id) ";
$sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate',
- ".db_escape($amount_bank).", ".db_escape($person_type_id)
+ ".db_escape($amount_bank).", ".db_escape($charge).", ".db_escape($person_type_id)
.", ". db_escape($person_id).")";
- if ($err_msg == "")
- $err_msg = "The bank transaction could not be inserted";
-
- db_query($sql, $err_msg);
+ db_query($sql, "The bank transaction could not be inserted");
}
//----------------------------------------------------------------------------------------
function exists_bank_trans($type, $type_no)
{
- $sql = "SELECT trans_no FROM ".TB_PREF."bank_trans WHERE type=".db_escape($type)
- ." AND trans_no=".db_escape($type_no);
+ $sql = "SELECT trans_no
+ FROM ".TB_PREF."bank_trans
+ WHERE type=".db_escape($type)
+ ." AND trans_no=".db_escape($type_no)
+ ." AND (amount!=0 OR charge!=0)";
$result = db_query($sql, "Cannot retreive a bank transaction");
return (db_num_rows($result) > 0);
function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id=null)
{
- $sql = "SELECT *, bank_account_name, account_code, bank_curr_code
- 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=".db_escape($type);
- if ($trans_no != null)
- $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 = ".db_escape($person_type_id);
- if ($person_id != null)
- $sql .= " AND ".TB_PREF."bank_trans.person_id = ".db_escape($person_id);
- $sql .= " ORDER BY trans_date, ".TB_PREF."bank_trans.id";
+ $sql = "SELECT bt.*, act.*,
+ IFNULL(abs(dt.ov_amount), IFNULL(ABS(st.ov_amount), bt.amount)) settled_amount,
+ IFNULL(abs(dt.ov_amount/bt.amount), IFNULL(ABS(st.ov_amount/bt.amount), 1)) settle_rate,
+ IFNULL(debtor.curr_code, IFNULL(supplier.curr_code, act.bank_curr_code)) settle_curr
+
+ FROM ".TB_PREF."bank_trans bt
+ LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=bt.type AND dt.trans_no=bt.trans_no
+ LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = dt.debtor_no
+ LEFT JOIN ".TB_PREF."supp_trans st ON st.type=bt.type AND st.trans_no=bt.trans_no
+ LEFT JOIN ".TB_PREF."suppliers supplier ON supplier.supplier_id = st.supplier_id,
+ ".TB_PREF."bank_accounts act
+ WHERE act.id=bt.bank_act ";
+ if (isset($type))
+ $sql .= " AND bt.type=".db_escape($type);
+ if (isset($trans_no))
+ $sql .= " AND bt.trans_no = ".db_escape($trans_no);
+ if (isset($person_type_id))
+ $sql .= " AND bt.person_type_id = ".db_escape($person_type_id);
+ if (isset($person_id))
+ $sql .= " AND bt.person_id = ".db_escape($person_id);
+ $sql .= " ORDER BY trans_date, bt.id";
return db_query($sql, "query for bank transaction");
}
{
$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) . "
- AND trans_date >= '$from'
- AND trans_date <= '$to'
- ORDER BY trans_date,".TB_PREF."bank_trans.id";
+ $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'
+ AND (amount != 0 OR charge != 0)
+ 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'";
+ $sql = "SELECT SUM(amount+charge)
+ 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="
- .db_escape($account)." AND type=".db_escape($type)
- ." AND type_no=".db_escape($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");
function void_bank_trans($type, $type_no, $nested=false)
{
+
if (!$nested)
- begin_transaction();
+ begin_transaction(__FUNCTION__, func_get_args());
- $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0
- WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
+ $sql = "UPDATE ".TB_PREF."bank_trans
+ SET amount=0, charge=0
+ 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");
+ db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
void_gl_trans($type, $type_no, true);
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=null, $user=null, $balance_offset = 0)
{
- global $Refs;
-
- if (!$nested)
- begin_transaction();
+ if ($delta_amount >= 0 && isset($date))
+ return null; // amount increase is always safe
- $sql = "DELETE FROM ".TB_PREF."bank_trans
- WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
+ $balance = $date ? get_bank_account_limit($bank_account, $date, $user) : 0;
- $result = db_query($sql, "could not clear bank transactions for type=$type and trans_no=$type_no");
+ if (!isset($balance) && isset($date))
+ return null; // unlimited account
- clear_gl_trans($type, $type_no, true);
+ $balance += $balance_offset;
+ if (floatcmp($balance, -$delta_amount) < 0)
+ return array('amount' => $balance + $delta_amount, 'trans_date'=> date2sql($date));
- // in case it's a customer trans - probably better to check first
- void_cust_allocations($type, $type_no);
- clear_customer_trans($type, $type_no);
+ $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+charge) as amount, trans_date, trans_no, type
+ 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";
- 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 (round2($balance, user_price_dec()) < 0)
+ {
+ $trans['amount'] = $balance;
+ 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['charge'], $trans['bank_act'], sql2date($trans['trans_date'])) == null;
+ }
+ }
+ return true;
+}
-?>
\ No newline at end of file
+function update_reconcile_date($type, $trans_no, $date=NULL)
+{
+ $sql = "UPDATE ".TB_PREF."bank_trans SET reconciled=".($date ? "'".date2sql($date)."'" : 'NULL')
+ ." WHERE type=" . db_escape($type). " AND trans_no=".db_escape($trans_no);
+
+ db_query($sql, "Can't change reconciliation status");
+}