Fixed bank transaction dition problem (bank transaction balance check should be perfo...
authorJanusz Dobrowolski <janusz@frontaccouting.eu>
Sat, 29 Oct 2011 20:37:09 +0000 (22:37 +0200)
committerJanusz Dobrowolski <janusz@frontaccouting.eu>
Sat, 29 Oct 2011 20:37:09 +0000 (22:37 +0200)
gl/includes/db/gl_db_bank_trans.inc
gl/includes/db/gl_db_banking.inc

index f11a56987358dead57b1803f65fb1fa8ff4bea71..59dbaa2322d178fa520dab093636449ef5f26f62 100644 (file)
@@ -155,7 +155,7 @@ function void_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.
+*      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)
@@ -171,7 +171,8 @@ function check_bank_account_history($delta_amount, $bank_account, $date, $user=n
        $date = date2sql($date);
        $balance+= $delta_amount;
 
-       $sql = "SELECT * FROM ".TB_PREF."bank_trans WHERE bank_act=".db_escape($bank_account)." AND trans_date > '$date' ORDER BY trans_date ASC, id ASC";
+       $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";
 
        $history = db_query($sql, "cannot retrieve cash account history");
 
index 6f08087b45d467fc14ba0bd61a8527af577e7a47..4a88583a24a73138d080699f128edbbfa5f934ae 100644 (file)
@@ -308,7 +308,7 @@ function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $
        hook_db_prewrite($args, $trans_type);
        $aid = 0;
        if ($trans_no) {
-               void_transaction($trans_type, $trans_no, Today(), _("Document reentered."));
+               $old_trans = $trans_no;
                $Refs->restore_last($trans_type, $trans_no);
                $aid = has_attachment($trans_type, $trans_no);
        }
@@ -422,6 +422,11 @@ function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $
        $Refs->save($trans_type, $trans_no, $ref);
        add_audit_trail($trans_type, $trans_no, $date_);
 
+       // old transaction can be voided only after new transaction is entered,
+       //  otherwise the operation could fail for cash accounts due to temporary negative balance
+       if (@$old_trans) 
+                       void_transaction($trans_type, $old_trans, Today(), _("Document reentered."));
+
        $args->trans_no = $trans_no;
        hook_db_postwrite($args, $trans_type);
        if ($use_transaction)