Copyright notes at top op every source file
[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 Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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).",$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=$exempt WHERE id=$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()
46 {
47         $sql = "SELECT * FROM ".TB_PREF."item_tax_types";
48         
49         return db_query($sql, "could not get all item tax type");
50
51
52 function get_item_tax_type($id)
53 {
54         $sql = "SELECT * FROM ".TB_PREF."item_tax_types WHERE id=$id";
55         
56         $result = db_query($sql, "could not get item tax type");
57         
58         return db_fetch($result);
59 }
60
61 function get_item_tax_type_for_item($stock_id)
62 {
63         $sql = "SELECT ".TB_PREF."item_tax_types.* FROM ".TB_PREF."item_tax_types,".TB_PREF."stock_master WHERE ".TB_PREF."stock_master.stock_id='$stock_id'
64                 AND ".TB_PREF."item_tax_types.id=".TB_PREF."stock_master.tax_type_id";
65         
66         $result = db_query($sql, "could not get item tax type");
67         
68         return db_fetch($result);       
69 }
70
71 function delete_item_tax_type($id)
72 {
73         begin_transaction();
74                 
75         $sql = "DELETE FROM ".TB_PREF."item_tax_types WHERE id=$id";
76                 
77         db_query($sql, "could not delete item tax type");
78         // also delete all exemptions
79         delete_item_tax_type_exemptions($id);
80         
81         commit_transaction();   
82 }
83
84 function add_item_tax_type_exemptions($id, $exemptions)
85 {
86         for ($i = 0; $i < count($exemptions); $i++) 
87         {
88                 $sql = "INSERT INTO ".TB_PREF."item_tax_type_exemptions (item_tax_type_id, tax_type_id)
89                         VALUES ($id,  " . $exemptions[$i] . ")";
90                 db_query($sql, "could not add item tax type exemptions");                                       
91         }               
92 }
93
94 function delete_item_tax_type_exemptions($id)
95 {
96         $sql = "DELETE FROM ".TB_PREF."item_tax_type_exemptions WHERE item_tax_type_id=$id";
97         
98         db_query($sql, "could not delete item tax type exemptions");                                    
99 }
100
101 function get_item_tax_type_exemptions($id)
102 {
103         $sql = "SELECT * FROM ".TB_PREF."item_tax_type_exemptions WHERE item_tax_type_id=$id";
104         
105         return db_query($sql, "could not get item tax type exemptions");
106 }
107
108 ?>