Fixed allocation date for customer payments entry, additional fixes for strict sql...
[fa-stable.git] / includes / db / audit_trail_db.inc
index c25fdc5111526228c42f858afa8ed8756123b524..feb85a991f744b6ed3facf54fd4fffaff18d7964 100644 (file)
 
 function add_audit_trail($trans_type, $trans_no, $trans_date, $descr='')
 {
+       begin_transaction();
+
        $date = date2sql($trans_date);
        $sql = "INSERT INTO ".TB_PREF."audit_trail"
-               . " (type, trans_no, user, gl_date, description)
+               . " (type, trans_no, user, gl_date, description, stamp)
                        VALUES(".db_escape($trans_type).", ".db_escape($trans_no).","
                        . $_SESSION["wa_current_user"]->user . ","
-                       . "'$date',". db_escape($descr). ")";
+                       . "'$date',". db_escape($descr). ", CURRENT_TIMESTAMP)";
 
        db_query($sql, "Cannot add audit info");
        // all audit records beside just inserted one should have gl_seq set to NULL
        // to avoid need for subqueries (not existing in MySQL 3) all over the code
        $sql = "UPDATE ".TB_PREF."audit_trail audit LEFT JOIN ".TB_PREF."fiscal_year year ON year.begin<='$date' AND year.end>='$date'
                SET audit.gl_seq = IF(audit.id=".db_insert_id().", 0, NULL),"
-               ."audit.fiscal_year=year.id"
+               ."audit.fiscal_year=year.id, stamp=CURRENT_TIMESTAMP"
                . " WHERE type=".db_escape($trans_type)." AND trans_no="
                . db_escape($trans_no);
 
        db_query($sql, "Cannot update audit gl_seq");
+       commit_transaction();
 }
 
 function get_audit_trail_all($trans_type, $trans_no)
@@ -104,26 +107,33 @@ function close_transactions($todate) {
        return $errors;
 }
 
-/*
-       Closed transactions have gl_seq number assigned.
-*/
-function is_closed_trans($type, $trans_no) {
+function get_journal_number($type, $trans_no) {
 
        $cdate = get_company_pref('gl_closing_date');
        if (!$cdate)
                return false;
 
 // FIXME: gl_date can be badly entered for some transactions due to bug in previous FA versions 
-       $sql = "SELECT  gl_seq  FROM ".TB_PREF."audit_trail"
+       $sql = "SELECT  MAX(gl_seq) as gl_seq  FROM ".TB_PREF."audit_trail"
                . " WHERE type=".db_escape($type)
                ." AND trans_no=".db_escape($trans_no)
                ." AND gl_date<='$cdate'"; // date is stored in sql format
 //             ." AND (gl_date<='$cdate'" // some transaction can be not sequenced due to 0 amount, however after edition this could change
 //             ." OR gl_seq>0)";
-
        $res = db_query($sql, "Cannot check transaction");
+       if (db_num_rows($res))
+       {
+               $myrow =db_fetch($res);
+               return $myrow['gl_seq'] ? $myrow['gl_seq'] : _('None');
+       }
+       return "-";
+}
+
+/*
+       Closed transactions have gl_seq number assigned.
+*/
+function is_closed_trans($type, $trans_no) {
 
-       return db_num_rows($res);
+       return get_journal_number($type, $trans_no) > 0;
 }
 
-?>