d4b26f962c0a28f41dc7b1f4a4de3705370acded
[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         }
38         
39         function exists($type, $reference) 
40         {
41                 return (find_reference($type, $reference) != null);
42         }
43         
44         function save_last($reference, $type) 
45         {
46                 $next = $this->increment($reference);
47                 save_next_reference($type, $next);
48         }
49         
50         function get_next($type) 
51         {
52                 return get_next_reference($type);
53         }
54         
55         //------------------------------------
56
57         function is_valid($reference) 
58         {
59                 return strlen(trim($reference)) > 0;
60         }
61         
62         function increment($reference) 
63         {
64                 // New method done by Pete. So f.i. WA036 will increment to WA037 and so on.
65         // If $reference is trailed by digits, and digits only,
66         // extract them and add 1, then put the alpha prefix back on
67         // NB. preg_match returns 1 if the regex matches completely 
68         // also $result[0] holds entire string, 1 the first captured, 2 the 2nd etc.
69         if (preg_match('/^(.*?)(\d+)$/', $reference, $result) == 1) 
70         {
71             $dig_count = strlen($result[2]); // How many digits? eg. 0003 = 4
72             $fmt = '%0' . $dig_count . 'd'; // Make a format string - leading zeroes
73             $nextval =  $result[1] . sprintf($fmt, intval($result[2] + 1)); // Add one on, and put prefix back on
74             return $nextval;
75         }
76         else 
77             return $reference;
78         }
79         
80         //------------------------------------
81 }
82
83 //----------------------------------------------------------------------------
84
85 function is_new_reference($ref, $type)
86 {
87         $db_info = get_systype_db_info($type);
88         $db_name = $db_info[0];
89         $db_type = $db_info[1];
90         $db_ref = $db_info[3];
91         
92         if ($db_ref != null) 
93         {
94                 $sql = "SELECT $db_ref FROM $db_name WHERE $db_ref='$ref'";
95                 if ($db_type != null)
96                         $sql .= " AND $db_type=$type";
97                          
98                 $result = db_query($sql, "could not test for unique reference");
99                 
100                 return (db_num_rows($result) == 0);
101         }
102         
103         // it's a type that doesn't use references - shouldn't be calling here, but say yes anyways
104         return true;
105 }
106
107 ?>