Huge sales module changes toward delivery and invoicing separation. Includes some...
[fa-stable.git] / sales / includes / db / sales_order_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------
4 function get_demand_qty($stockid, $location)
5 {
6         $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS QtyDemand
7                                 FROM ".TB_PREF."sales_order_details,
8                                         ".TB_PREF."sales_orders
9                                 WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND
10                                         ".TB_PREF."sales_orders.from_stk_loc ='$location' AND
11                                         ".TB_PREF."sales_order_details.stk_code = '$stockid'";
12
13     $TransResult = db_query($sql,"No transactions were returned");
14         $DemandRow = db_fetch($TransResult);
15         return $DemandRow['QtyDemand'];
16 }
17
18 function get_demand_asm_qty($stockid, $location)
19 {
20         $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
21                                    AS Dem
22                                    FROM ".TB_PREF."sales_order_details,
23                                                 ".TB_PREF."sales_orders,
24                                                 ".TB_PREF."bom,
25                                                 ".TB_PREF."stock_master
26                                    WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
27                                    ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND
28                                    ".TB_PREF."sales_orders.from_stk_loc='$location' AND
29                                    ".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
30                                    ".TB_PREF."bom.component='$stockid' AND
31                                    ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
32                                    ".TB_PREF."stock_master.mb_flag='A'";
33
34     $TransResult = db_query($sql,"No transactions were returned");
35         if (db_num_rows($TransResult)==1)
36         {
37                 $DemandRow = db_fetch_row($TransResult);
38                 $DemandQty = $DemandRow[0];
39         }
40         else
41                 $DemandQty = 0.0;
42
43     return $DemandQty;
44 }
45
46 function add_sales_order($order)
47 {
48         global $loc_notification, $path_to_root;
49
50         begin_transaction();
51
52         if ($order->trans_type=='invoice')
53                 $del_date = date2sql($order->orig_order_date);
54         else
55                 $del_date = date2sql($order->delivery_date);
56
57         $sql = "INSERT INTO ".TB_PREF."sales_orders (debtor_no, branch_code, customer_ref, Comments, ord_date,
58                 order_type, ship_via, deliver_to, delivery_address, contact_phone,
59                 contact_email, freight_cost, from_stk_loc, delivery_date)
60                 VALUES ('" . $order->customer_id . "', '" . $order->Branch . "', '".
61                         $order->cust_ref ."','". db_escape($order->Comments) ."','" .
62                         date2sql($order->orig_order_date) . "', '" .
63                         $order->default_sales_type . "', " .
64                         $_POST['ship_via'] .",'" . $order->deliver_to . "', '" .
65                         $order->delivery_address . "', '" .
66                         $order->phone . "', '" . $order->email . "', " .
67                         $order->freight_cost .", '" . $order->Location ."', '" .
68                         $del_date . "')";
69
70         db_query($sql, "order Cannot be Added");
71
72         $order_no = db_insert_id();
73         if ($loc_notification == 1)
74         {
75                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
76                 $st_ids = array();
77                 $st_names = array();
78                 $st_num = array();
79                 $st_reorder = array();
80         }
81         foreach ($order->line_items as $line)
82         {
83                 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
84                 {
85                         $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
86                                 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
87                                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
88                                 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
89                                 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
90                         $res = db_query($sql,"a location could not be retreived");
91                         $loc = db_fetch($res);
92                         if ($loc['email'] != "")
93                         {
94                                 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
95                                 $qoh -= get_demand_qty($line->stock_id, $order->Location);
96                                 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
97                                 $qoh -= $line->quantity;
98                                 if ($qoh < $loc['reorder_level'])
99                                 {
100                                         $st_ids[] = $line->stock_id;
101                                         $st_names[] = $line->item_description;
102                                         $st_num[] = $qoh - $loc['reorder_level'];
103                                         $st_reorder[] = $loc['reorder_level'];
104                                 }
105                         }
106                 }
107                 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
108                 $sql .= $order_no .
109                                 ",'$line->stock_id', '$line->item_description', $line->price,
110                                 $line->quantity,
111                                 $line->discount_percent)";
112                 db_query($sql, "order Details Cannot be Added");
113
114         } /* inserted line items into sales order details */
115
116         add_forms_for_sys_type(systypes::sales_order(), $order_no);
117
118         commit_transaction();
119
120         if ($loc_notification == 1 && count($st_ids) > 0)
121         {
122                 require_once($path_to_root . "/reporting/includes/class.mail.inc");
123                 $company = get_company_prefs();
124                 $mail = new email($company['coy_name'], $company['email']);
125                 $from = $company['coy_name'] . " <" . $company['email'] . ">";
126                 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
127                 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
128                 $msg = "\n";
129                 for ($i = 0; $i < count($st_ids); $i++)
130                         $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
131                 $msg .= "\n" . _("Please reorder") . "\n\n";
132                 $msg .= $company['coy_name'];
133                 $mail->to($to);
134                 $mail->subject($subject);
135                 $mail->text($msg);
136                 $ret = $mail->send();
137         }
138         return $order_no;
139 }
140
141 //----------------------------------------------------------------------------------------
142
143 function delete_sales_order($order_no)
144 {
145         begin_transaction();
146
147         $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
148         db_query($sql, "order Header Delete");
149
150         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
151         db_query($sql, "order Detail Delete");
152
153         delete_forms_for_systype(systypes::sales_order(), $order_no);
154
155         commit_transaction();
156 }
157
158 //----------------------------------------------------------------------------------------
159
160 function update_sales_order($order_no, $order)
161 {
162         global $loc_notification, $path_to_root;
163
164         $del_date = date2sql($order->delivery_date);
165         $ord_date = date2sql($order->orig_order_date);
166
167         begin_transaction();
168
169         $sql = "UPDATE ".TB_PREF."sales_orders SET debtor_no = '" . $order->customer_id . "',
170                 branch_code = '" . $order->Branch . "',
171                 customer_ref = '". $order->cust_ref ."',
172                 Comments = '". db_escape($order->Comments) ."', ord_date = '" . $ord_date . "',
173                 order_type = '" . $order->default_sales_type . "', ship_via = " . $order->ship_via .",
174                 deliver_to = '" . $order->deliver_to . "', delivery_address = '" . $order->delivery_address . "',
175                 contact_phone = '" . $order->phone . "',
176                 contact_email = '" . $order->email . "', freight_cost = " . $order->freight_cost .",
177                 from_stk_loc = '" . $order->Location ."', delivery_date = '" . $del_date . "'
178                 WHERE order_no=" . $order_no;
179
180         db_query($sql, "order Cannot be Updated");
181
182         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
183
184         db_query($sql, "Old order Cannot be Deleted");
185
186         if ($loc_notification == 1)
187         {
188                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
189                 $st_ids = array();
190                 $st_names = array();
191                 $st_num = array();
192                 $st_reorder = array();
193         }
194         foreach ($order->line_items as $line)
195         {
196                 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
197                 {
198                         $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
199                                 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
200                                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
201                                 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
202                                 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
203                         $res = db_query($sql,"a location could not be retreived");
204                         $loc = db_fetch($res);
205                         if ($loc['email'] != "")
206                         {
207                                 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
208                                 $qoh -= get_demand_qty($line->stock_id, $order->Location);
209                                 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
210                                 $qoh -= $line->quantity;
211                                 if ($qoh < $loc['reorder_level'])
212                                 {
213                                         $st_ids[] = $line->stock_id;
214                                         $st_names[] = $line->item_description;
215                                         $st_num[] = $qoh - $loc['reorder_level'];
216                                         $st_reorder[] = $loc['reorder_level'];
217                                 }
218                         }
219                 }
220                 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code,  description, unit_price, quantity, discount_percent, qty_sent) VALUES (";
221                 $sql .= $order_no . ",'" . $line->stock_id . "','" . $line->item_description . "', " . $line->price . ", " . $line->quantity . ", " . $line->discount_percent . ", " . $line->qty_done ." )";
222
223                 db_query($sql, "Old order Cannot be Inserted");
224
225         } /* inserted line items into sales order details */
226
227         commit_transaction();
228         if ($loc_notification == 1 && count($st_ids) > 0)
229         {
230                 require_once($path_to_root . "/reporting/includes/class.mail.inc");
231                 $company = get_company_prefs();
232                 $mail = new email($company['coy_name'], $company['email']);
233                 $from = $company['coy_name'] . " <" . $company['email'] . ">";
234                 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
235                 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
236                 $msg = "\n";
237                 for ($i = 0; $i < count($st_ids); $i++)
238                         $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
239                 $msg .= "\n" . _("Please reorder") . "\n\n";
240                 $msg .= $company['coy_name'];
241                 $mail->to($to);
242                 $mail->subject($subject);
243                 $mail->text($msg);
244                 $ret = $mail->send();
245         }
246 }
247
248 //----------------------------------------------------------------------------------------
249
250 function get_sales_order($order_no)
251 {
252         $sql = "SELECT ".TB_PREF."sales_orders.*, ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."locations.location_name,
253                 ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."debtors_master.discount, ".TB_PREF."sales_types.sales_type, ".TB_PREF."shippers.shipper_name,
254                 ".TB_PREF."tax_groups.name AS tax_group_name , ".TB_PREF."tax_groups.id AS tax_group_id
255                 FROM ".TB_PREF."sales_orders, ".TB_PREF."debtors_master, ".TB_PREF."sales_types, ".TB_PREF."tax_groups, ".TB_PREF."cust_branch, ".TB_PREF."locations, ".TB_PREF."shippers
256                         WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
257                                 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
258                                 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
259                                 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
260                                 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
261                                 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
262                                 AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
263
264         $result = db_query($sql, "order Retreival");
265
266         $num = db_num_rows($result);
267         if ($num > 1)
268         {
269                 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
270         }
271         else if ($num == 1)
272         {
273                 return db_fetch($result);
274         }
275         else
276                 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
277
278 }
279
280 //----------------------------------------------------------------------------------------
281
282 function read_sales_order($order_no, &$order)
283 {
284         $myrow = get_sales_order($order_no);
285
286         $order->trans_no = $order_no;
287         $order->customer_id = $myrow["debtor_no"];
288         $order->Branch = $myrow["branch_code"];
289         $order->customer_name = $myrow["name"];
290         $order->cust_ref = $myrow["customer_ref"];
291         $order->default_sales_type =$myrow["order_type"];
292         $order->sales_type_name =$myrow["sales_type"];
293         $order->customer_currency = $myrow["curr_code"];
294         $order->default_discount = $myrow["discount"];
295
296         $order->Comments = $myrow["comments"];
297
298         $order->ship_via = $myrow["ship_via"];
299         $order->deliver_to = $myrow["deliver_to"];
300         $order->delivery_date = sql2date($myrow["delivery_date"]);
301         $order->freight_cost = $myrow["freight_cost"];
302         $order->delivery_address = $myrow["delivery_address"];
303         $order->phone = $myrow["contact_phone"];
304         $order->email = $myrow["contact_email"];
305         $order->Location = $myrow["from_stk_loc"];
306         $order->location_name = $myrow["location_name"];
307         $order->orig_order_date = sql2date($myrow["ord_date"]);
308
309         $order->tax_group_name = $myrow["tax_group_name"];
310         $order->tax_group_id = $myrow["tax_group_id"];
311
312         $sql = "SELECT id, stk_code, unit_price, ".TB_PREF."sales_order_details.description,
313                 ".TB_PREF."sales_order_details.quantity, discount_percent,
314                 qty_sent, ".TB_PREF."stock_master.units,
315                 ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS standard_cost
316                 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
317                         WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
318                                 AND order_no =" . $order_no . " ORDER BY id";
319
320 //              $sql .= "
321 //                      AND ".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent > 0 ";
322         $result = db_query($sql, "Retreive order Line Items");
323
324         if (db_num_rows($result) > 0)
325         {
326
327                 while ($myrow = db_fetch($result))
328                 {
329                         $order->add_to_cart($myrow["stk_code"],$myrow["quantity"],
330                                 $myrow["unit_price"], $myrow["discount_percent"],
331                                  $myrow["qty_sent"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
332                 }
333         }
334
335  foreach($order->line_items as $key=>$line) 
336         $order->line_items[$key]->qty_dispatched = 
337         $order->line_items[$key]->quantity - $order->line_items[$key]->qty_done; 
338
339         return true;
340 }
341
342 //----------------------------------------------------------------------------------------
343
344 function sales_order_has_deliveries($order_no)
345 {
346         $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
347         "sales_order_details WHERE order_no=$order_no";
348
349         $result = db_query($sql, "could not query for sales order usage");
350
351         $row = db_fetch_row($result);
352
353         return ($row[0] > 0);
354 }
355
356 //----------------------------------------------------------------------------------------
357
358 function close_sales_order($order_no)
359 {
360         // set the quantity of each item to the already sent quantity. this will mark item as closed.
361         $sql = "UPDATE ".TB_PREF."sales_order_details
362                 SET quantity = qty_sent
363                         WHERE order_no = $order_no";
364
365         db_query($sql, "The sales order detail record could not be updated");
366 }
367
368 //----------------------------------------------------------------------------------------
369
370 function dispatch_sales_order_item($order_no, $stock_id, $qty_dispatched, $id)
371 {
372         $sql = "UPDATE ".TB_PREF."sales_order_details ";
373
374         $sql .= "SET qty_sent = qty_sent + $qty_dispatched ";
375
376         $sql .= " WHERE id = $id";
377
378         db_query($sql, "The sales order detail record could not be updated");
379 }
380
381 //---------------------------------------------------------------------------------------------------------------
382
383 function get_invoice_duedate($debtorno, $invdate)
384 {
385         if (!is_date($invdate))
386         {
387                 return Today();
388         }
389     $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
390                 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
391                 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
392
393     $result = db_query($sql,"The customer details could not be retrieved");
394     $myrow = db_fetch($result);
395
396     if (db_num_rows($result) == 0)
397         return $invdate;
398     if ($myrow['day_in_following_month'] > 0)
399         $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
400         else
401         $duedate = add_days($invdate, $myrow['days_before_due']);
402     return $duedate;
403 }
404
405
406 ?>