Cleanups
[fa-stable.git] / includes / references.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/includes/db/references_db.inc");
13
14 class references 
15 {
16         
17         function save($type, $id, $reference) 
18         {
19                 add_reference($type, $id, $reference);
20                 if ($reference != 'auto')
21                         $this->save_last($reference, $type);
22         }
23         
24         function get($type, $id) 
25         {
26                 return get_reference($type, $id);
27         }       
28         
29         function delete($type, $id) 
30         {
31                 delete_reference($type, $id);
32         }       
33         
34         function update($type, $id, $reference) 
35         {
36             update_reference($type, $id, $reference);
37                 if ($reference != 'auto')
38                         $this->save_last($reference, $type);
39         }
40         // check if reference is used for any non voided transaction (used for ST_JOURNALENTRY type)
41         function exists($type, $reference) 
42         {
43                 return (find_reference($type, $reference) != null);
44         }
45         
46         function save_last($reference, $type) 
47         {
48                 $next = $this->increment($reference);
49                 save_next_reference($type, $next);
50         }
51         
52         function get_next($type) 
53         {
54                 return get_next_reference($type);
55         }
56         //
57         //      Restore previous reference if voided trans ref was the last one.
58         //
59         function restore_last($type, $id) 
60         {
61                 $reference = $this->get($type, $id);
62                 $last = $this->increment($this->get_next($type), true); //decrement
63                 if ($reference==$last) {
64                         save_next_reference($type, $last);
65                 }
66         }
67         
68         //------------------------------------
69
70         function is_valid($reference) 
71         {
72                 return strlen(trim($reference)) > 0;
73         }
74         //
75         //      Increments (or decrements if $back==true) reference template
76         //
77         function increment($reference, $back=false) 
78         {
79                 // New method done by Pete. So f.i. WA036 will increment to WA037 and so on.
80         // If $reference contains at least one group of digits,
81         // extract first didgits group and add 1, then put all together.
82         // NB. preg_match returns 1 if the regex matches completely 
83         // also $result[0] holds entire string, 1 the first captured, 2 the 2nd etc.
84         //
85         if (preg_match('/^(\D*?)(\d+)(.*)/', $reference, $result) == 1) 
86         {
87                         list($all, $prefix, $number, $postfix) = $result;
88                         $dig_count = strlen($number); // How many digits? eg. 0003 = 4
89                         $fmt = '%0' . $dig_count . 'd'; // Make a format string - leading zeroes
90                         $val = intval($number + ($back ? ($number<1 ? 0 : -1) : 1));
91                         $nextval =  sprintf($fmt, $val); // Add one on, and put prefix back on
92
93                         return $prefix.$nextval.$postfix;
94         }
95         else 
96             return $reference;
97         }
98         //------------------------------------
99 }
100
101 //----------------------------------------------------------------------------
102
103 function is_new_reference($ref, $type)
104 {
105         $db_info = get_systype_db_info($type);
106         $db_name = $db_info[0];
107         $db_type = $db_info[1];
108         $db_trans = $db_info[2];
109         $db_ref = $db_info[3];
110         
111         if ($db_ref != null) 
112         {
113                 if ($db_type != null) {
114                         $sql = "SELECT $db_ref FROM $db_name 
115                                 LEFT JOIN ".TB_PREF."voided v ON 
116                                         $db_name.$db_type=v.type AND $db_name.$db_trans=v.id
117                                 WHERE $db_name.$db_ref='$ref' AND ISNULL(v.id)
118                                         AND $db_name.$db_type=$type";
119                 } else {
120                         $sql = "SELECT $db_ref FROM $db_name 
121                                 LEFT JOIN ".TB_PREF."voided v ON 
122                                         v.type=$type AND $db_name.$db_trans=v.id
123                                 WHERE $db_ref='$ref' AND ISNULL(v.id)";
124                 }
125                 $result = db_query($sql, "could not test for unique reference");
126                 
127                 return (db_num_rows($result) == 0);
128         }
129         
130         // it's a type that doesn't use references - shouldn't be calling here, but say yes anyways
131         return true;
132 }
133
134
135 ?>