Fixed SQL quotation.
[fa-stable.git] / inventory / includes / db / items_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 update_item($stock_id, $description, $long_description, $category_id, 
13         $tax_type_id, $units='', $mb_flag='', $sales_account, $inventory_account, 
14         $cogs_account,  $adjustment_account, $assembly_account, $dimension_id, 
15         $dimension2_id)
16 {
17         $sql = "UPDATE ".TB_PREF."stock_master SET long_description=".db_escape($long_description).",
18                 description=".db_escape($description).",
19                 category_id='$category_id',
20                 sales_account='$sales_account',
21                 inventory_account='$inventory_account',
22                 cogs_account='$cogs_account',
23                 adjustment_account='$adjustment_account',
24                 assembly_account='$assembly_account',
25                 dimension_id=$dimension_id,
26                 dimension2_id=$dimension2_id,
27                 tax_type_id=$tax_type_id";
28
29         if ($units != '')
30                 $sql .= ", units='$units'";
31
32         if ($mb_flag != '')
33                 $sql .= ", mb_flag='$mb_flag'";
34
35         $sql .= " WHERE stock_id='$stock_id'";
36
37         db_query($sql, "The item could not be updated");
38
39         update_item_code(-1, $stock_id, $stock_id, $description, $category_id, 1, 0);
40 }
41
42 function add_item($stock_id, $description, $long_description, $category_id, 
43         $tax_type_id, $units, $mb_flag, $sales_account, $inventory_account, 
44         $cogs_account, $adjustment_account,     $assembly_account, $dimension_id, 
45         $dimension2_id)
46 {
47         $sql = "INSERT INTO ".TB_PREF."stock_master (stock_id, description, long_description, category_id,
48                 tax_type_id, units, mb_flag, sales_account, inventory_account, cogs_account,
49                 adjustment_account, assembly_account, dimension_id, dimension2_id)
50                 VALUES (".db_escape($stock_id).", ".db_escape($description).", ".db_escape($long_description).",
51                 '$category_id', $tax_type_id, '$units', '$mb_flag',
52                 '$sales_account', '$inventory_account', '$cogs_account',
53                 '$adjustment_account', '$assembly_account', $dimension_id, $dimension2_id)";
54
55         db_query($sql, "The item could not be added");
56
57         $sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id)
58                 SELECT ".TB_PREF."locations.loc_code, '$stock_id' FROM ".TB_PREF."locations";
59
60         db_query($sql, "The item locstock could not be added");
61
62         add_item_code($stock_id, $stock_id, $description, $category_id, 1, 0);
63 }
64
65 function delete_item($stock_id)
66 {
67         $sql="DELETE FROM ".TB_PREF."stock_master WHERE stock_id='$stock_id'";
68         db_query($sql, "could not delete stock item");
69
70         /*and cascade deletes in loc_stock */
71         $sql ="DELETE FROM ".TB_PREF."loc_stock WHERE stock_id='$stock_id'";
72         db_query($sql, "could not delete stock item loc stock");
73
74         /*and cascade deletes in purch_data */
75         $sql ="DELETE FROM ".TB_PREF."purch_data WHERE stock_id='$stock_id'";
76         db_query($sql, "could not delete stock item purch data");
77
78         /*and cascade deletes in prices */
79         $sql ="DELETE FROM ".TB_PREF."prices WHERE stock_id='$stock_id'";
80         db_query($sql, "could not delete stock item prices");
81
82         /*and cascade delete the bill of material if any */
83         $sql = "DELETE FROM ".TB_PREF."bom WHERE parent='$stock_id'";
84         db_query($sql, "could not delete stock item bom");
85
86         delete_item_kit($stock_id);
87 }
88
89 function get_item($stock_id)
90 {
91         $sql = "SELECT ".TB_PREF."stock_master.*,".TB_PREF."item_tax_types.name AS tax_type_name
92                 FROM ".TB_PREF."stock_master,".TB_PREF."item_tax_types
93                 WHERE ".TB_PREF."item_tax_types.id=".TB_PREF."stock_master.tax_type_id
94                 AND stock_id='$stock_id'";
95         $result = db_query($sql,"an item could not be retreived");
96
97         return db_fetch($result);
98 }
99
100 function get_items()
101 {
102         $sql = "SELECT * FROM ".TB_PREF."stock_master";
103         return db_query($sql,"items could not be retreived");
104 }
105
106 ?>