[0000304] Set locale functionality broken on Windows
[fa-stable.git] / includes / db / inventory_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 get_qoh_on_date($stock_id, $location=null, $date_=null, $exclude=0)
13 {
14         if ($date_ == null)
15                 $date_ = Today();
16
17         $date = date2sql($date_);
18
19         $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
20                 WHERE stock_id=".db_escape($stock_id)."
21                 AND tran_date <= '$date'";
22
23         if ($location != null)
24                 $sql .= " AND loc_code = ".db_escape($location);
25
26         $result = db_query($sql, "QOH calulcation failed");
27
28         $myrow = db_fetch_row($result);
29         if ($exclude > 0)
30         {
31                 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
32                         WHERE stock_id=".db_escape($stock_id)
33                         ." AND type=".db_escape($exclude)
34                         ." AND tran_date = '$date'";
35
36                 $result = db_query($sql, "QOH calulcation failed");
37                 $myrow2 = db_fetch_row($result);
38                 if ($myrow2 !== false)
39                         $myrow[0] -= $myrow2[0];
40         }
41
42         return $myrow[0];
43 }
44
45 //--------------------------------------------------------------------------------------
46
47 function get_item_edit_info($stock_id)
48 {
49         $sql = "SELECT material_cost + labour_cost + overhead_cost AS standard_cost, units, decimals
50                 FROM ".TB_PREF."stock_master,".TB_PREF."item_units
51                 WHERE stock_id=".db_escape($stock_id)
52                 ." AND ".TB_PREF."stock_master.units=".TB_PREF."item_units.abbr";
53         $result = db_query($sql, "The standard cost cannot be retrieved");
54
55         return db_fetch($result);
56 }
57
58 //--------------------------------------------------------------------------------------
59
60 function get_standard_cost($stock_id)
61 {
62         $sql = "SELECT IF(s.mb_flag='D', 0, material_cost + labour_cost + overhead_cost) AS std_cost
63                 FROM ".TB_PREF."stock_master s WHERE stock_id=".db_escape($stock_id);
64         $result = db_query($sql, "The standard cost cannot be retrieved");
65
66         $myrow = db_fetch_row($result);
67
68         return $myrow[0];
69 }
70
71 //--------------------------------------------------------------------------------------
72
73 function is_inventory_item($stock_id)
74 {
75         $sql = "SELECT stock_id FROM ".TB_PREF."stock_master
76                 WHERE stock_id=".db_escape($stock_id)." AND mb_flag <> 'D'";
77         $result = db_query($sql, "Cannot query is inventory item or not");
78
79         return db_num_rows($result) > 0;
80 }
81
82 //-------------------------------------------------------------------
83
84 function last_negative_stock_begin_date($stock_id, $to)
85 {
86         $to = date2sql($to);
87         $sql ="SET @q = 0";
88         db_query($sql);
89         $sql = "SET @flag = 0";
90         db_query($sql);
91         $sql = "SELECT SUM(qty), @q:= @q + qty, IF(@q < 0 AND @flag=0, @flag:=1,@flag:=0), IF(@q < 0 AND @flag=1, tran_date,'') AS begin_date 
92                 FROM ".TB_PREF."stock_moves
93                 WHERE stock_id=".db_escape($stock_id)." AND tran_date<='$to' 
94                 AND qty <> 0
95                 GROUP BY stock_id ORDER BY tran_date";
96
97         $result = db_query($sql, "The dstock moves could not be retrieved");
98         $row = db_fetch_row($result);
99         return $row[3];
100 }
101
102 //-------------------------------------------------------------------
103 // Newly written
104 function last_negative_stock_trans_id($stock_id, $to)
105 {
106         $sql = "SELECT * from ".TB_PREF."stock_moves
107                 WHERE stock_id=".db_escape($stock_id)." 
108                 AND qty <> 0 order by trans_id asc";
109         
110         $result = db_query($sql, "The query on stock moves failed.");
111         
112         $qty = 0;
113         $flag = 0;
114         $negative_trans_id = 1;
115         
116         while ($myrow = db_fetch($result))
117         {
118                 $qty += $myrow['qty'];
119                 if ($qty < 0 && $flag == 0)
120                 {
121                         $flag = 1;
122                         $negative_trans_id = $myrow['trans_id'];
123                 }
124                 if ($qty > 0)
125                         $flag = 0;              
126         }
127                 
128         return $negative_trans_id;
129 }
130
131 //-------------------------------------------------------------------
132
133 function get_deliveries_between($stock_id, $from, $to)
134 {
135         $from = date2sql($from);
136         $to = date2sql($to);
137         $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
138                 WHERE type=".ST_CUSTDELIVERY." AND stock_id=".db_escape($stock_id)." AND
139                         tran_date>='$from' AND tran_date<='$to' GROUP BY stock_id";
140
141         $result = db_query($sql, "The deliveries could not be updated");
142         return db_fetch_row($result);
143 }
144
145 //Newly written
146 function get_deliveries_from_trans($stock_id, $from)
147 {
148         // -ve qty is delivery either by ST_CUSTDELIVERY or inventory adjustment
149         $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
150                 WHERE stock_id=".db_escape($stock_id)." AND qty < 0 AND
151                         trans_id>='$from' GROUP BY stock_id";
152         $result = db_query($sql, "The deliveries could not be updated");
153         $row = db_fetch_row($result);
154         
155         display_notification('Row0 - '.$row[0].' Row1- '.$row[1]);
156         
157         //return $row;
158         
159         // Get Std cost of previsous transaction before the cut-over delivery
160         // This is useful to get inventory valuation
161         $prev_trans = $from - 1;
162         display_notification('From - '.$from.' Prev- '.$prev_trans);
163         $sql = "SELECT standard_cost FROM ".TB_PREF."stock_moves
164                 WHERE stock_id=".db_escape($stock_id)." AND 
165                         trans_id ='$prev_trans'";
166         $result = db_query($sql, "The deliveries could not be updated");
167         $cost = db_fetch_row($result);  
168         
169         display_notification('Last Delivery Cost - '.$cost[0]);
170         
171         // Adjusting QOH valuation 
172         $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
173                 WHERE stock_id=".db_escape($stock_id)." AND
174                         trans_id<'$from' GROUP BY stock_id";
175         $result = db_query($sql, "The deliveries could not be updated");
176         $qoh = db_fetch_row($result);
177         
178         display_notification('QOH before last delivery - '.$qoh[0]);
179         
180         $qty = $row[0] - $qoh[0]; //Qoh is minus from delivered in -ve
181         $final_cost = $row[1] - $qoh[0]*$cost[0];
182         
183         display_notification('Qty - '.$qty.' cost- '.$final_cost);
184         
185         return array($qty,$final_cost); 
186 }
187
188 //Newly written
189 function get_purchases_from_trans($stock_id, $from)
190 {
191         // Calculate All inward stock moves i.e. qty > 0
192         $sql = "SELECT SUM(qty), SUM(qty*standard_cost) FROM ".TB_PREF."stock_moves
193                 WHERE stock_id=".db_escape($stock_id)." AND qty > 0 AND 
194                         trans_id>'$from' GROUP BY stock_id";
195         $result = db_query($sql, "The deliveries could not be updated");
196         $row = db_fetch_row($result);
197         
198         display_notification('Purchase Qty - '.$row[0].' Cost- '.$row[1]);
199         
200         return $row;
201 }
202
203 //-------------------------------------------------------------------
204 /* Original Code V0 Leave as is a while
205 function adjust_deliveries_v0($stock_id, $material_cost, $to)
206 {
207         if (!is_inventory_item($stock_id))
208                 return;
209         $from = last_negative_stock_begin_date($stock_id, $to);
210         if ($from == false || $from == "")
211                 return;
212         $from = sql2date($from);
213         $row = get_deliveries_between($stock_id, $from, $to);
214         if ($row == false)
215                 return; 
216         $old_cost = $row[1];
217         $new_cost = $row[0] * $material_cost;
218         $diff = $new_cost - $old_cost;
219         if ($diff != 0)
220         {
221                 $update_no = get_next_trans_no(ST_COSTUPDATE);
222                 if (!is_date_in_fiscalyear($to))
223                         $to = end_fiscalyear();
224            
225                 $stock_gl_code = get_stock_gl_code($stock_id);
226
227                 $memo_ = sprintf(_("Cost was %s changed to %s for item '%s'"),
228                         $old_cost, $new_cost, $stock_id);
229                 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["cogs_account"], 
230                         $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], $memo_, $diff);
231
232                 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["inventory_account"], 
233                         0, 0, $memo_, -$diff);
234                 add_audit_trail(ST_COSTUPDATE, $update_no, $to);
235         }
236 }
237 */
238 //New written function
239 function adjust_deliveries($stock_id, $material_cost, $to)
240 {
241         if (!is_inventory_item($stock_id))
242                 return;
243         
244         $from = last_negative_stock_trans_id($stock_id, $to);
245         if ($from == false || $from == "")
246                 return;
247
248         $row = get_deliveries_from_trans($stock_id, $from);
249                 
250         if ($row == false)
251                 return; 
252         $old_sales_cost = $row[1];
253         $new_sales_cost = $row[0] * $material_cost;
254         $sales_diff = $new_sales_cost - $old_sales_cost;
255         
256         $row = get_purchases_from_trans($stock_id, $from);
257         $purchase_diff = 0;
258         $old_purchase_cost = 0;
259         if ($row != false)
260         {
261                 $old_purchase_cost = $row[1];
262                 $new_purchase_cost = $row[0] * $material_cost;
263                 $purchase_diff = $new_purchase_cost - $old_purchase_cost;
264         }
265         
266         $diff =  $sales_diff - $purchase_diff;
267         
268         display_notification('Sales Diff - '.$sales_diff.' Purchase Diff - '.$purchase_diff);
269         
270         if ($diff != 0)
271         {
272                 $update_no = get_next_trans_no(ST_COSTUPDATE);
273                 if (!is_date_in_fiscalyear($to))
274                         $to = end_fiscalyear();
275            
276                 $stock_gl_code = get_stock_gl_code($stock_id);
277
278                 $dec = user_price_dec();
279                 $old_cost = -round2($old_sales_cost-$old_purchase_cost,$dec);
280                 $new_cost = -round2($new_sales_cost-$new_purchase_cost,$dec);
281                 
282                 $memo_ = _("Cost was ") . $old_cost. _(" changed to ") . $new_cost . _(" for item ")."'$stock_id'";
283                 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["cogs_account"], 
284                         $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], $memo_, $diff);           
285
286                 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["inventory_account"], 0, 0, $memo_, 
287                         -$diff);
288                 add_audit_trail(ST_COSTUPDATE, $update_no, $to);
289         }
290 }
291
292 function get_stock_gl_code($stock_id)
293 {
294         /*Gets the GL Codes relevant to the item account  */
295
296         $sql = "SELECT inventory_account, cogs_account,
297                 adjustment_account, sales_account, assembly_account, dimension_id, dimension2_id FROM
298                 ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
299
300         $get = db_query($sql,"retreive stock gl code");
301         return db_fetch($get);
302 }
303
304 //--------------------------------------------------------------------------------------
305
306 // $date_ - display / non-sql date
307 // $std_cost - in HOME currency
308 // $show_or_hide - wil this move be visible in reports, etc
309 // $price - in $person_id's currency
310
311 function add_stock_move($type, $stock_id, $trans_no, $location,
312     $date_, $reference, $quantity, $std_cost, $person_id=0, $show_or_hide=1,
313     $price=0, $discount_percent=0, $error_msg="")
314 {
315         // do not add a stock move if it's a non-inventory item
316         if (!is_inventory_item($stock_id))
317                 return null;
318
319         $date = date2sql($date_);
320
321         $sql = "INSERT INTO ".TB_PREF."stock_moves (stock_id, trans_no, type, loc_code,
322                 tran_date, person_id, reference, qty, standard_cost, visible, price,
323                 discount_percent) VALUES (".db_escape($stock_id)
324                 .", ".db_escape($trans_no).", ".db_escape($type)
325                 .",     ".db_escape($location).", '$date', "
326                 .db_escape($person_id).", ".db_escape($reference).", "
327                 .db_escape($quantity).", ".db_escape($std_cost).","
328                 .db_escape($show_or_hide).", ".db_escape($price).", "
329                 .db_escape($discount_percent).")";
330
331         if ($error_msg == "")
332                 $error_msg = "The stock movement record cannot be inserted";
333
334         db_query($sql, $error_msg);
335
336         return db_insert_id();
337 }
338
339 function update_stock_move_pid($type, $stock_id, $from, $to, $pid, $cost)
340 {
341         $from = date2sql($from);
342         $to = date2sql($to);
343                 $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=".db_escape($cost)
344                         ." WHERE type=".db_escape($type)
345                         ."      AND stock_id=".db_escape($stock_id)
346                         ."  AND tran_date>='$from' AND tran_date<='$to' 
347                         AND person_id = ".db_escape($pid);
348         db_query($sql, "The stock movement standard_cost cannot be updated");
349 }
350
351 //--------------------------------------------------------------------------------------------------
352
353 function get_stock_moves($type, $type_no, $visible=false)
354 {
355         $sql = "SELECT ".TB_PREF."stock_moves.*, ".TB_PREF."stock_master.description, "
356                 .TB_PREF."stock_master.units,".TB_PREF."locations.location_name,"
357                 .TB_PREF."stock_master.material_cost + "
358                         .TB_PREF."stock_master.labour_cost + "
359                         .TB_PREF."stock_master.overhead_cost AS FixedStandardCost
360                 FROM ".TB_PREF."stock_moves,".TB_PREF."locations,".TB_PREF."stock_master
361                 WHERE ".TB_PREF."stock_moves.stock_id = ".TB_PREF."stock_master.stock_id
362                 AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code
363                 AND type=".db_escape($type)." AND trans_no=".db_escape($type_no)." ORDER BY trans_id";
364         if ($visible)
365                 $sql .= " AND ".TB_PREF."stock_moves.visible=1";
366
367         return db_query($sql, "Could not get stock moves");
368 }
369
370 //--------------------------------------------------------------------------------------------------
371
372 function void_stock_move($type, $type_no)
373 {
374         $sql = "UPDATE ".TB_PREF."stock_moves SET qty=0, price=0, discount_percent=0,
375                 standard_cost=0 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
376
377         db_query($sql, "Could not void stock moves");
378 }
379
380 //--------------------------------------------------------------------------------------------------
381
382 function get_location_name($loc_code)
383 {
384         $sql = "SELECT location_name FROM ".TB_PREF."locations WHERE loc_code="
385                 .db_escape($loc_code);
386
387         $result = db_query($sql, "could not retreive the location name for $loc_code");
388
389         if (db_num_rows($result) == 1)
390         {
391                 $row = db_fetch_row($result);
392                 return $row[0];
393         }
394
395         display_db_error("could not retreive the location name for $loc_code", $sql, true);
396 }
397
398 //--------------------------------------------------------------------------------------------------
399
400
401 ?>