87aa6515f797b3c9719d946933d9e7b8caef0dba
[fa-stable.git] / includes / db / references_db.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 //--------------------------------------------------------------------------------------------------
13
14 function get_reference($type, $id)
15 {
16         $sql = "SELECT * FROM ".TB_PREF."refs WHERE type=".db_escape($type)." AND id=".db_escape($id);
17
18         $result = db_query($sql, "could not query reference table");
19     $row = db_fetch($result);
20     return $row['reference'];
21 }
22
23 //--------------------------------------------------------------------------------------------------
24  
25 function update_reference($type, $id, $reference)
26 {
27     $sql = "REPLACE ".TB_PREF."refs SET reference=".db_escape($reference)
28                         .", type=".db_escape($type).", id=".db_escape($id);
29     db_query($sql, "could not update reference entry");
30 }
31
32 //--------------------------------------------------------------------------------------------------
33
34 function delete_reference($type, $id)
35 {
36         $sql = "DELETE FROM ".TB_PREF."refs WHERE type=".db_escape($type)." AND id=".db_escape($id);
37
38         return db_query($sql, "could not delete from reference table");
39 }
40
41 //--------------------------------------------------------------------------------------------------
42
43 function find_reference($type, $reference)
44 {
45         // ignore refs references for voided transactions
46         $sql = "SELECT r.id FROM ".TB_PREF."refs r LEFT JOIN ".TB_PREF."voided v ON"
47                 ." r.type=v.type AND r.id=v.id"
48                 ." WHERE r.type=".db_escape($type)
49                 ." AND reference=".db_escape($reference)
50                 ." AND ISNULL(`memo_`)";
51
52         $result = db_query($sql, "could not query reference table");
53
54     return (db_num_rows($result) > 0);
55 }
56
57 //--------------------------------------------------------------------------------------------------
58
59 function save_next_reference($type, $reference)
60 {
61     $sql = "UPDATE ".TB_PREF."sys_types SET next_reference=" . db_escape(trim($reference)) 
62                 . " WHERE type_id = ".db_escape($type);
63
64         db_query($sql, "The next transaction ref for $type could not be updated");
65 }
66
67 //--------------------------------------------------------------------------------------------------
68
69 function get_next_reference($type)
70 {
71     $sql = "SELECT next_reference FROM ".TB_PREF."sys_types WHERE type_id = ".db_escape($type);
72
73     $result = db_query($sql,"The last transaction ref for $type could not be retreived");
74
75     $row = db_fetch_row($result);
76     return $row[0];
77 }
78
79 //----------------------------------------------------------------------------
80 //
81 //      Check if reference was not used so far (for other transaction than $trans_no)
82 //
83 function is_new_reference($ref, $type, $trans_no=0)
84 {
85         $db_info = get_systype_db_info($type);
86         $db_name = $db_info[0];
87         $db_type = $db_info[1];
88         $db_trans = $db_info[2];
89         $db_ref = $db_info[3];
90         
91         $ref = db_escape(trim($ref));
92         $type = db_escape($type);
93         
94         if ($db_ref == null) { // journal or bank trans store references in refs table
95                 $db_name = TB_PREF."refs";
96                 $db_type = 'type';
97                 $db_trans = 'id';
98                 $db_ref = 'reference';
99         }
100
101         if ($db_type != null) {
102                 $sql = "SELECT $db_ref FROM $db_name tbl
103                         LEFT JOIN ".TB_PREF."voided v ON 
104                                 tbl.$db_type=v.type AND tbl.$db_trans=v.id
105                         WHERE $db_ref=$ref AND ISNULL(v.id)
106                                 AND tbl.$db_type=$type";
107         } else {
108                 $sql = "SELECT $db_ref ref FROM $db_name tbl
109                         LEFT JOIN ".TB_PREF."voided v ON 
110                                 v.type=$type AND tbl.$db_trans=v.id
111                         WHERE $db_ref=$ref AND ISNULL(v.id)";
112         }
113         if ($trans_no)
114                         $sql .= " AND tbl.`$db_trans` != ".db_escape($trans_no);
115
116         $result = db_query($sql, "could not test for unique reference");
117
118         return (db_num_rows($result) == 0);
119
120 }
121
122
123 ?>