Layout improvements
[fa-stable.git] / includes / references.inc
index 260c7a769664407f2123fbc2d52ca647e0f86b18..8720ed4d777dde1e400fe42255219b366c6eb83c 100644 (file)
@@ -18,7 +18,7 @@ class references
        {
                add_reference($type, $id, $reference);
                if ($reference != 'auto')
-                       references::save_last($reference, $type);
+                       $this->save_last($reference, $type);
        }
        
        function get($type, $id) 
@@ -31,6 +31,13 @@ class references
                delete_reference($type, $id);
        }       
        
+       function update($type, $id, $reference) 
+       {
+           update_reference($type, $id, $reference);
+               if ($reference != 'auto')
+                       $this->save_last($reference, $type);
+       }
+       // check if reference is used for any non voided transaction (used for ST_JOURNALENTRY type)
        function exists($type, $reference) 
        {
                return (find_reference($type, $reference) != null);
@@ -38,7 +45,7 @@ class references
        
        function save_last($reference, $type) 
        {
-               $next = references::increment($reference);
+               $next = $this->increment($reference);
                save_next_reference($type, $next);
        }
        
@@ -46,6 +53,17 @@ class references
        {
                return get_next_reference($type);
        }
+       //
+       //      Restore previous reference if voided trans ref was the last one.
+       //
+       function restore_last($type, $id) 
+       {
+               $reference = $this->get($type, $id);
+               $last = $this->increment($this->get_next($type), true); //decrement
+               if ($reference==$last) {
+                       save_next_reference($type, $last);
+               }
+       }
        
        //------------------------------------
 
@@ -53,25 +71,30 @@ class references
        {
                return strlen(trim($reference)) > 0;
        }
-       
-       function increment($reference) 
+       //
+       //      Increments (or decrements if $back==true) reference template
+       //
+       function increment($reference, $back=false) 
        {
                // New method done by Pete. So f.i. WA036 will increment to WA037 and so on.
-               // If $reference is trailed by digits, and digits only,
-        // extract them and add 1, then put the alpha prefix back on
+               // If $reference contains at least one group of digits,
+        // extract first didgits group and add 1, then put all together.
         // NB. preg_match returns 1 if the regex matches completely 
         // also $result[0] holds entire string, 1 the first captured, 2 the 2nd etc.
-        if (preg_match('/^(.*?)(\d+)$/', $reference, $result) == 1) 
+        //
+        if (preg_match('/^(\D*?)(\d+)(.*)/', $reference, $result) == 1) 
         {
-            $dig_count = strlen($result[2]); // How many digits? eg. 0003 = 4
-            $fmt = '%0' . $dig_count . 'd'; // Make a format string - leading zeroes
-            $nextval =  $result[1] . sprintf($fmt, intval($result[2] + 1)); // Add one on, and put prefix back on
-            return $nextval;
+                       list($all, $prefix, $number, $postfix) = $result;
+                       $dig_count = strlen($number); // How many digits? eg. 0003 = 4
+                       $fmt = '%0' . $dig_count . 'd'; // Make a format string - leading zeroes
+                       $val = intval($number + ($back ? ($number<1 ? 0 : -1) : 1));
+                       $nextval =  sprintf($fmt, $val); // Add one on, and put prefix back on
+
+                       return $prefix.$nextval.$postfix;
         }
         else 
             return $reference;
        }
-       
        //------------------------------------
 }
 
@@ -82,14 +105,23 @@ function is_new_reference($ref, $type)
        $db_info = get_systype_db_info($type);
        $db_name = $db_info[0];
        $db_type = $db_info[1];
+       $db_trans = $db_info[2];
        $db_ref = $db_info[3];
        
        if ($db_ref != null) 
        {
-               $sql = "SELECT $db_ref FROM $db_name WHERE $db_ref='$ref'";
-               if ($db_type != null)
-                       $sql .= " AND $db_type=$type";
-                        
+               if ($db_type != null) {
+                       $sql = "SELECT $db_ref FROM $db_name 
+                               LEFT JOIN ".TB_PREF."voided v ON 
+                                       $db_name.$db_type=v.type AND $db_name.$db_trans=v.id
+                               WHERE $db_name.$db_ref='$ref' AND ISNULL(v.id)
+                                       AND $db_name.$db_type=$type";
+               } else {
+                       $sql = "SELECT $db_ref FROM $db_name 
+                               LEFT JOIN ".TB_PREF."voided v ON 
+                                       v.type=$type AND $db_name.$db_trans=v.id
+                               WHERE $db_ref='$ref' AND ISNULL(v.id)";
+               }
                $result = db_query($sql, "could not test for unique reference");
                
                return (db_num_rows($result) == 0);
@@ -99,4 +131,5 @@ function is_new_reference($ref, $type)
        return true;
 }
 
+
 ?>
\ No newline at end of file