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, $wip_account, $dimension_id,
15 $dimension2_id, $no_sale, $editable, $no_purchase,
16 $depreciation_method = 'D', $depreciation_rate=100, $depreciation_factor=1,
17 $depreciation_start=null, $fa_class_id=null, $vat_category='', $shipper_id=0)
19 begin_transaction(__FUNCTION__, func_get_args());
20 $sql = "UPDATE ".TB_PREF."stock_master SET long_description=".db_escape($long_description).",
21 description=".db_escape($description).",
22 category_id=".db_escape($category_id).",
23 sales_account=".db_escape($sales_account).",
24 inventory_account=".db_escape($inventory_account).",
25 cogs_account=".db_escape($cogs_account).",
26 adjustment_account=".db_escape($adjustment_account).",
27 wip_account=".db_escape($wip_account).",
28 dimension_id=".db_escape($dimension_id).",
29 dimension2_id=".db_escape($dimension2_id).",
30 tax_type_id=".db_escape($tax_type_id).",
31 no_sale=".db_escape($no_sale).",
32 no_purchase=".db_escape($no_purchase).",
33 editable=".db_escape($editable).",
34 depreciation_method=".db_escape($depreciation_method).",
35 depreciation_rate=".db_escape($depreciation_rate).",
36 depreciation_factor=".db_escape($depreciation_factor).",
37 fa_class_id=".db_escape($fa_class_id).",
38 shipper_id=".db_escape($shipper_id);
41 $sql .= ", units=".db_escape($units);
44 $sql .= ", mb_flag=".db_escape($mb_flag);
46 if ($vat_category != '')
47 $sql .= ", vat_category=".db_escape($vat_category);
49 if (isset($depreciation_start)) {
50 $sql .= ", depreciation_start='".date2sql($depreciation_start)."'"
51 .", depreciation_date='".date2sql($depreciation_start)."'";
54 $sql .= " WHERE stock_id=".db_escape($stock_id);
56 db_query($sql, "The item could not be updated");
58 update_item_code(-1, $stock_id, $stock_id, $description, $category_id, 1, 0);
62 function add_item($stock_id, $description, $long_description, $category_id,
63 $tax_type_id, $units, $mb_flag, $sales_account, $inventory_account,
64 $cogs_account, $adjustment_account, $wip_account, $dimension_id,
65 $dimension2_id, $no_sale, $editable, $no_purchase,
66 $depreciation_method='D', $depreciation_rate=100, $depreciation_factor=1, $depreciation_start=null,
67 $fa_class_id=null, $vat_category=0, $shipper_id=0)
69 begin_transaction(__FUNCTION__, func_get_args());
70 $sql = "INSERT INTO ".TB_PREF."stock_master (stock_id, description, long_description, category_id,
71 tax_type_id, units, mb_flag, sales_account, inventory_account, cogs_account,
72 adjustment_account, wip_account, dimension_id, dimension2_id, no_sale, no_purchase, editable, vat_category,
73 depreciation_method, depreciation_rate, depreciation_factor"
74 .(isset($depreciation_start) ? ", depreciation_start, depreciation_date, fa_class_id" : "")
75 .",shipper_id) VALUES (".db_escape($stock_id).", ".db_escape($description).", ".db_escape($long_description).",
76 ".db_escape($category_id).", ".db_escape($tax_type_id).", "
77 .db_escape($units).", ".db_escape($mb_flag).",
78 ".db_escape($sales_account).", ".db_escape($inventory_account)
79 .", ".db_escape($cogs_account).",".db_escape($adjustment_account)
80 .", ".db_escape($wip_account).", "
81 .db_escape($dimension_id).", ".db_escape($dimension2_id).","
82 .db_escape($no_sale).","
83 .db_escape($no_purchase).","
84 .db_escape($editable).",".db_escape($vat_category).","
85 .db_escape($depreciation_method).",".db_escape($depreciation_rate).",".db_escape($depreciation_factor)
86 .(isset($depreciation_start) ? ",'".date2sql($depreciation_start)."','".date2sql($depreciation_start)."',".db_escape($fa_class_id) : "")
87 .",".db_escape($shipper_id).")";
89 db_query($sql, "The item could not be added");
91 $sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id)
92 SELECT loc_code, ".db_escape($stock_id)
93 ." FROM ".TB_PREF."locations";
95 db_query($sql, "The item locstock could not be added");
97 add_item_code($stock_id, $stock_id, $description, $category_id, 1, 0);
101 function delete_item($stock_id)
103 begin_transaction(__FUNCTION__, func_get_args());
104 $sql="DELETE FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id);
105 db_query($sql, "could not delete stock item");
107 /*and cascade deletes in loc_stock */
108 $sql ="DELETE FROM ".TB_PREF."loc_stock WHERE stock_id=".db_escape($stock_id);
109 db_query($sql, "could not delete stock item loc stock");
111 /*and cascade deletes in purch_data */
112 $sql ="DELETE FROM ".TB_PREF."purch_data WHERE stock_id=".db_escape($stock_id);
113 db_query($sql, "could not delete stock item purch data");
115 /*and cascade deletes in prices */
116 $sql ="DELETE FROM ".TB_PREF."prices WHERE stock_id=".db_escape($stock_id);
117 db_query($sql, "could not delete stock item prices");
119 /*and cascade delete the bill of material if any */
120 $sql = "DELETE FROM ".TB_PREF."bom WHERE parent=".db_escape($stock_id);
121 db_query($sql, "could not delete stock item bom");
123 delete_item_kit($stock_id);
124 commit_transaction();
127 function get_item($stock_id)
129 $sql = "SELECT item.*, taxtype.name AS tax_type_name
130 FROM ".TB_PREF."stock_master item,"
131 .TB_PREF."item_tax_types taxtype
132 WHERE taxtype.id=item.tax_type_id
133 AND stock_id=".db_escape($stock_id);
134 $result = db_query($sql,"an item could not be retreived");
136 return db_fetch_assoc($result);
139 function get_items($fixed_asset = 0)
141 $sql = "SELECT * FROM ".TB_PREF."stock_master WHERE fixed_asset=".db_escape($fixed_asset);
142 return db_query($sql,"items could not be retreived");
145 function item_in_foreign_codes($stock_id)
148 "SELECT COUNT(*) FROM "
149 .TB_PREF."stock_moves WHERE stock_id=".db_escape($stock_id) =>
150 _('Cannot delete this item because there are stock movements that refer to this item.'),
151 "SELECT COUNT(*) FROM "
152 .TB_PREF."bom WHERE component=".db_escape($stock_id)=>
153 _('Cannot delete this item record because there are bills of material that require this part as a component.'),
154 "SELECT COUNT(*) FROM "
155 .TB_PREF."sales_order_details WHERE stk_code=".db_escape($stock_id) =>
156 _('Cannot delete this item because there are existing sales order items for it.'),
157 "SELECT COUNT(*) FROM "
158 .TB_PREF."purch_order_details WHERE item_code=".db_escape($stock_id)=>
159 _('Cannot delete this item because there are existing purchase order items for it.'),
160 "SELECT COUNT(*) FROM "
161 .TB_PREF."sales_orders WHERE ship_via=".db_escape($stock_id)=>
162 _('Cannot delete this item because there are existing sales transactions for it.'),
167 foreach($sqls as $sql=>$err) {
168 $result = db_query($sql, "could not query stock usage");
169 $myrow = db_fetch_row($result);
177 $kits = get_where_used($stock_id);
178 $num_kits = db_num_rows($kits);
180 $msg = _("This item cannot be deleted because some code aliases
181 or foreign codes was entered for it, or there are kits defined
182 using this item as component")
186 $kit = db_fetch($kits);
187 $msg .= "'".$kit[0]."'";
188 if ($num_kits) $msg .= ',';
196 function get_items_search($description, $type)
200 $sql = "SELECT COUNT(i.item_code) AS kit, i.item_code, i.description, c.description category
201 FROM ".TB_PREF."stock_master s, ".TB_PREF."item_codes i
202 LEFT JOIN ".TB_PREF."stock_category c ON i.category_id=c.category_id
203 WHERE i.stock_id=s.stock_id
204 AND !i.inactive AND !s.inactive
205 AND ( i.item_code LIKE " . db_escape("%" . $description. "%") . " OR
206 i.description LIKE " . db_escape("%" . get_post("description"). "%") . " OR
207 c.description LIKE " . db_escape("%" . get_post("description"). "%") . ") ";
211 $sql .= " AND !s.no_sale AND mb_flag <> 'F'";
214 $sql .= " AND mb_flag = 'M'";
217 $sql .= " AND NOT no_purchase AND mb_flag <> 'F' AND i.item_code=i.stock_id";
220 $sql .= " AND mb_flag <> 'D' AND mb_flag <> 'F' AND i.item_code=i.stock_id";
223 $parent = $_GET['parent'];
224 $sql .= " AND i.item_code=i.stock_id AND i.stock_id <> '$parent' AND mb_flag <> 'F' ";
227 $sql .= " AND !i.is_foreign AND i.item_code!=i.stock_id AND mb_flag <> 'F'";
230 $sql .= " AND mb_flag <> 'F' AND i.item_code=i.stock_id";
234 if (isset($SysPrefs->max_rows_in_search))
235 $limit = $SysPrefs->max_rows_in_search;
239 $sql .= " GROUP BY i.item_code ORDER BY i.description LIMIT 0,".(int)($limit);
241 return db_query($sql, "Failed in retreiving item list.");