Update from usntable branch.
[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 add_reference($type, $id, $reference)
26 {
27         $sql = "INSERT INTO ".TB_PREF."refs (type, id, reference)
28                 VALUES (".db_escape($type).", ".db_escape($id).", "
29                         . db_escape(trim($reference)) . ")";
30
31         db_query($sql, "could not add reference entry");
32 }
33
34 //--------------------------------------------------------------------------------------------------
35  
36 function update_reference($type, $id, $reference)
37 {
38     $sql = "UPDATE ".TB_PREF."refs SET reference=".db_escape($reference)
39                         ." WHERE type=".db_escape($type)." AND id=".db_escape($id);
40     db_query($sql, "could not update reference entry");
41 }
42
43 //--------------------------------------------------------------------------------------------------
44
45 function delete_reference($type, $id)
46 {
47         $sql = "DELETE FROM ".TB_PREF."refs WHERE type=$type AND id=".db_escape($id);
48
49         return db_query($sql, "could not delete from reference table");
50 }
51
52 //--------------------------------------------------------------------------------------------------
53
54 function find_reference($type, $reference)
55 {
56         $sql = "SELECT id FROM ".TB_PREF."refs WHERE type=".db_escape($type)
57                 ." AND reference=".db_escape($reference);
58
59         $result = db_query($sql, "could not query reference table");
60
61     return (db_num_rows($result) > 0);
62 }
63
64 //--------------------------------------------------------------------------------------------------
65
66 function save_next_reference($type, $reference)
67 {
68     $sql = "UPDATE ".TB_PREF."sys_types SET next_reference=" . db_escape(trim($reference)) 
69                 . " WHERE type_id = ".db_escape($type);
70
71         db_query($sql, "The next transaction ref for $type could not be updated");
72 }
73
74 //--------------------------------------------------------------------------------------------------
75
76 function get_next_reference($type)
77 {
78     $sql = "SELECT next_reference FROM ".TB_PREF."sys_types WHERE type_id = ".db_escape($type);
79
80     $result = db_query($sql,"The last transaction ref for $type could not be retreived");
81
82     $row = db_fetch_row($result);
83     return $row[0];
84 }
85
86 ?>