Merged changes from master branch up to current state.
[fa-stable.git] / taxes / db / item_tax_types_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 function add_item_tax_type($name, $exempt, $exempt_from)
13 {
14         begin_transaction();
15         
16         $sql = "INSERT INTO ".TB_PREF."item_tax_types (name, exempt) 
17                 VALUES (".db_escape($name).",".db_escape($exempt).")";
18                 
19         db_query($sql, "could not add item tax type");  
20         
21         $id = db_insert_id();
22         
23         // add the exemptions
24         add_item_tax_type_exemptions($id, $exempt_from);
25         
26         commit_transaction();
27 }
28
29 function update_item_tax_type($id, $name, $exempt, $exempt_from)
30 {
31         begin_transaction();
32         
33         $sql = "UPDATE ".TB_PREF."item_tax_types SET name=".db_escape($name).
34         ",      exempt=".db_escape($exempt)." WHERE id=".db_escape($id);
35         
36         db_query($sql, "could not update item tax type");       
37         
38         // readd the exemptions
39         delete_item_tax_type_exemptions($id);
40         add_item_tax_type_exemptions($id, $exempt_from);                
41         
42         commit_transaction();   
43 }
44
45 function get_all_item_tax_types($also_inactive=false)
46 {
47         $sql = "SELECT * FROM ".TB_PREF."item_tax_types";
48         if (!$also_inactive)
49                 $sql .= " WHERE !inactive";
50
51         return db_query($sql, "could not get all item tax type");
52
53
54 function get_item_tax_type($id)
55 {
56         $sql = "SELECT * FROM ".TB_PREF."item_tax_types WHERE id=".db_escape($id);
57         
58         $result = db_query($sql, "could not get item tax type");
59         
60         return db_fetch($result);
61 }
62
63 function get_item_tax_type_for_item($stock_id)
64 {
65         $sql = "SELECT ".TB_PREF."item_tax_types.* FROM ".TB_PREF."item_tax_types,".TB_PREF."stock_master WHERE 
66                 ".TB_PREF."stock_master.stock_id=".db_escape($stock_id)."
67                 AND ".TB_PREF."item_tax_types.id=".TB_PREF."stock_master.tax_type_id";
68         
69         $result = db_query($sql, "could not get item tax type");
70         
71         return db_fetch($result);       
72 }
73
74 function delete_item_tax_type($id)
75 {
76         begin_transaction();
77                 
78         $sql = "DELETE FROM ".TB_PREF."item_tax_types WHERE id=".db_escape($id);
79                 
80         db_query($sql, "could not delete item tax type");
81         // also delete all exemptions
82         delete_item_tax_type_exemptions($id);
83         
84         commit_transaction();   
85 }
86
87 function add_item_tax_type_exemptions($id, $exemptions)
88 {
89         for ($i = 0; $i < count($exemptions); $i++) 
90         {
91                 $sql = "INSERT INTO ".TB_PREF."item_tax_type_exemptions (item_tax_type_id, tax_type_id)
92                         VALUES (".db_escape($id).",  ".db_escape($exemptions[$i]).")";
93                 db_query($sql, "could not add item tax type exemptions");                                       
94         }               
95 }
96
97 function delete_item_tax_type_exemptions($id)
98 {
99         $sql = "DELETE FROM ".TB_PREF."item_tax_type_exemptions WHERE item_tax_type_id=".db_escape($id);
100         
101         db_query($sql, "could not delete item tax type exemptions");                                    
102 }
103
104 function get_item_tax_type_exemptions($id)
105 {
106         $sql = "SELECT * FROM ".TB_PREF."item_tax_type_exemptions WHERE item_tax_type_id=".db_escape($id);
107         
108         return db_query($sql, "could not get item tax type exemptions");
109 }
110