Added fixed assets module
[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, $no_sale, $editable, $no_purchase,
16         $depreciation_method = 'D', $depreciation_rate=100, $depreciation_start=null,
17         $fa_class_id=null)
18 {
19         $sql = "UPDATE ".TB_PREF."stock_master SET long_description=".db_escape($long_description).",
20                 description=".db_escape($description).",
21                 category_id=".db_escape($category_id).",
22                 sales_account=".db_escape($sales_account).",
23                 inventory_account=".db_escape($inventory_account).",
24                 cogs_account=".db_escape($cogs_account).",
25                 adjustment_account=".db_escape($adjustment_account).",
26                 assembly_account=".db_escape($assembly_account).",
27                 dimension_id=".db_escape($dimension_id).",
28                 dimension2_id=".db_escape($dimension2_id).",
29                 tax_type_id=".db_escape($tax_type_id).",
30                 no_sale=".db_escape($no_sale).",
31                 no_purchase=".db_escape($no_purchase).",
32                 editable=".db_escape($editable).",
33                 depreciation_method=".db_escape($depreciation_method).",
34                 depreciation_rate=".db_escape($depreciation_rate).",
35                 fa_class_id=".db_escape($fa_class_id);
36
37         if ($units != '')
38                 $sql .= ", units=".db_escape($units);
39
40         if ($mb_flag != '')
41                 $sql .= ", mb_flag=".db_escape($mb_flag);
42
43         if ($depreciation_start != '') {
44                 $sql .= ", depreciation_start='".date2sql($depreciation_start)."'"
45                         .", depreciation_date='".date2sql($depreciation_start)."'";
46         }
47
48         $sql .= " WHERE stock_id=".db_escape($stock_id);
49
50         db_query($sql, "The item could not be updated");
51
52         update_item_code(-1, $stock_id, $stock_id, $description, $category_id, 1, 0);
53 }
54
55 function add_item($stock_id, $description, $long_description, $category_id, 
56         $tax_type_id, $units, $mb_flag, $sales_account, $inventory_account, 
57         $cogs_account, $adjustment_account,     $assembly_account, $dimension_id, 
58         $dimension2_id, $no_sale, $editable, $no_purchase,
59         $depreciation_method='D', $depreciation_rate=100, $depreciation_start=null,
60         $fa_class_id=null)
61 {
62         $sql = "INSERT INTO ".TB_PREF."stock_master (stock_id, description, long_description, category_id,
63                 tax_type_id, units, mb_flag, sales_account, inventory_account, cogs_account,
64                 adjustment_account, assembly_account, dimension_id, dimension2_id, no_sale, no_purchase, editable,
65                 depreciation_method, depreciation_rate, depreciation_start, depreciation_date, fa_class_id)
66                 VALUES (".db_escape($stock_id).", ".db_escape($description).", ".db_escape($long_description).",
67                 ".db_escape($category_id).", ".db_escape($tax_type_id).", "
68                 .db_escape($units).", ".db_escape($mb_flag).",
69                 ".db_escape($sales_account).", ".db_escape($inventory_account)
70                 .", ".db_escape($cogs_account).",".db_escape($adjustment_account)
71                 .", ".db_escape($assembly_account).", "
72                 .db_escape($dimension_id).", ".db_escape($dimension2_id).","
73                 .db_escape($no_sale).","
74                 .db_escape($no_purchase).","
75                 .db_escape($editable).","
76                 .db_escape($depreciation_method).",".db_escape($depreciation_rate).",'"
77                 .date2sql($depreciation_start)."','".date2sql($depreciation_start)."',"
78                 .db_escape($fa_class_id).")";
79
80         db_query($sql, "The item could not be added");
81
82         $sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id)
83                 SELECT loc_code, ".db_escape($stock_id)
84                 ." FROM ".TB_PREF."locations";
85
86         db_query($sql, "The item locstock could not be added");
87
88         add_item_code($stock_id, $stock_id, $description, $category_id, 1, 0);
89 }
90
91 function delete_item($stock_id)
92 {
93         $sql="DELETE FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id);
94         db_query($sql, "could not delete stock item");
95
96         /*and cascade deletes in loc_stock */
97         $sql ="DELETE FROM ".TB_PREF."loc_stock WHERE stock_id=".db_escape($stock_id);
98         db_query($sql, "could not delete stock item loc stock");
99
100         /*and cascade deletes in purch_data */
101         $sql ="DELETE FROM ".TB_PREF."purch_data WHERE stock_id=".db_escape($stock_id);
102         db_query($sql, "could not delete stock item purch data");
103
104         /*and cascade deletes in prices */
105         $sql ="DELETE FROM ".TB_PREF."prices WHERE stock_id=".db_escape($stock_id);
106         db_query($sql, "could not delete stock item prices");
107
108         /*and cascade delete the bill of material if any */
109         $sql = "DELETE FROM ".TB_PREF."bom WHERE parent=".db_escape($stock_id);
110         db_query($sql, "could not delete stock item bom");
111
112         delete_item_kit($stock_id);
113 }
114
115 function get_item($stock_id)
116 {
117         $sql = "SELECT item.*, taxtype.name AS tax_type_name
118                 FROM ".TB_PREF."stock_master item,"
119                         .TB_PREF."item_tax_types taxtype
120                 WHERE taxtype.id=item.tax_type_id
121                 AND stock_id=".db_escape($stock_id);
122         $result = db_query($sql,"an item could not be retreived");
123
124         return db_fetch($result);
125 }
126
127 function get_items($fixed_asset = 0)
128 {
129         $sql = "SELECT * FROM ".TB_PREF."stock_master WHERE fixed_asset=".db_escape($fixed_asset);
130         return db_query($sql,"items could not be retreived");
131 }
132
133 function item_in_foreign_codes($stock_id)
134 {
135         $sqls=  array(
136         "SELECT COUNT(*) FROM "
137                 .TB_PREF."stock_moves WHERE stock_id=".db_escape($stock_id) =>
138          _('Cannot delete this item because there are stock movements that refer to this item.'),
139         "SELECT COUNT(*) FROM "
140                 .TB_PREF."bom WHERE component=".db_escape($stock_id)=>
141          _('Cannot delete this item record because there are bills of material that require this part as a component.'),
142         "SELECT COUNT(*) FROM "
143                 .TB_PREF."sales_order_details WHERE stk_code=".db_escape($stock_id) =>
144          _('Cannot delete this item because there are existing purchase order items for it.'),
145         "SELECT COUNT(*) FROM "
146                 .TB_PREF."purch_order_details WHERE item_code=".db_escape($stock_id)=>
147          _('Cannot delete this item because there are existing purchase order items for it.')
148         );
149
150         $msg = '';
151
152         foreach($sqls as $sql=>$err) {
153                 $result = db_query($sql, "could not query stock usage");
154                 $myrow = db_fetch_row($result);
155                 if ($myrow[0] > 0) 
156                 {
157                         $msg = $err; break;
158                 }
159         }
160         if ($msg == '') {       
161
162                 $kits = get_where_used($stock_id);
163                 $num_kits = db_num_rows($kits);
164                 if ($num_kits) {
165                         $msg = _("This item cannot be deleted because some code aliases 
166                                 or foreign codes was entered for it, or there are kits defined 
167                                 using this item as component")
168                                 .':<br>';
169
170                         while($num_kits--) {
171                                 $kit = db_fetch($kits);
172                                 $msg .= "'".$kit[0]."'";
173                                 if ($num_kits) $msg .= ',';
174                         }
175
176                 }
177         }
178         return $msg;
179 }