Removed redundant form_types functions and the file /reporting/includes/form_types...
[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         $del_date = date2sql($order->due_date);
53         $order_type = 0; // this is default on new order
54         $sql = "INSERT INTO ".TB_PREF."sales_orders (type, debtor_no, branch_code, customer_ref, comments, ord_date,
55                 order_type, ship_via, deliver_to, delivery_address, contact_phone,
56                 contact_email, freight_cost, from_stk_loc, delivery_date)
57                 VALUES ('" . $order_type . "', '" . $order->customer_id . "', '" . $order->Branch . "', '".
58                         $order->cust_ref ."','". db_escape($order->Comments) ."','" .
59                         date2sql($order->document_date) . "', '" .
60                         $order->default_sales_type . "', " .
61                         $_POST['ship_via'] .",'" . $order->deliver_to . "', '" .
62                         $order->delivery_address . "', '" .
63                         $order->phone . "', '" . $order->email . "', " .
64                         $order->freight_cost .", '" . $order->Location ."', '" .
65                         $del_date . "')";
66
67         db_query($sql, "order Cannot be Added");
68
69         $order_no = db_insert_id();
70         $order->trans_no = array($order_no=>0);
71
72         if ($loc_notification == 1)
73         {
74                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
75                 $st_ids = array();
76                 $st_names = array();
77                 $st_num = array();
78                 $st_reorder = array();
79         }
80         foreach ($order->line_items as $line)
81         {
82                 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
83                 {
84                         $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
85                                 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
86                                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
87                                 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
88                                 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
89                         $res = db_query($sql,"a location could not be retreived");
90                         $loc = db_fetch($res);
91                         if ($loc['email'] != "")
92                         {
93                                 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
94                                 $qoh -= get_demand_qty($line->stock_id, $order->Location);
95                                 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
96                                 $qoh -= $line->quantity;
97                                 if ($qoh < $loc['reorder_level'])
98                                 {
99                                         $st_ids[] = $line->stock_id;
100                                         $st_names[] = $line->item_description;
101                                         $st_num[] = $qoh - $loc['reorder_level'];
102                                         $st_reorder[] = $loc['reorder_level'];
103                                 }
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         commit_transaction();
117
118         if ($loc_notification == 1 && count($st_ids) > 0)
119         {
120                 require_once($path_to_root . "/reporting/includes/class.mail.inc");
121                 $company = get_company_prefs();
122                 $mail = new email($company['coy_name'], $company['email']);
123                 $from = $company['coy_name'] . " <" . $company['email'] . ">";
124                 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
125                 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
126                 $msg = "\n";
127                 for ($i = 0; $i < count($st_ids); $i++)
128                         $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
129                 $msg .= "\n" . _("Please reorder") . "\n\n";
130                 $msg .= $company['coy_name'];
131                 $mail->to($to);
132                 $mail->subject($subject);
133                 $mail->text($msg);
134                 $ret = $mail->send();
135         }
136         return $order_no;
137 }
138
139 //----------------------------------------------------------------------------------------
140
141 function delete_sales_order($order_no)
142 {
143         begin_transaction();
144
145         $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
146         db_query($sql, "order Header Delete");
147
148         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
149         db_query($sql, "order Detail Delete");
150
151         commit_transaction();
152 }
153
154 //----------------------------------------------------------------------------------------
155 // Mark changes in sales_order_details
156 //
157 function update_sales_order_version($order)
158 {
159   foreach ($order as $so_num => $so_ver) {
160   $sql= 'UPDATE '.TB_PREF.'sales_orders SET version=version+1 WHERE order_no='. $so_num.
161         ' AND version='.$so_ver;
162   db_query($sql, 'Concurrent editing conflict while sales order update');
163   }
164 }
165
166 //----------------------------------------------------------------------------------------
167
168 function update_sales_order($order)
169 {
170         global $loc_notification, $path_to_root;
171
172         $del_date = date2sql($order->due_date);
173         $ord_date = date2sql($order->document_date);
174         $order_no =  key($order->trans_no);
175         $version= current($order->trans_no);
176
177         begin_transaction();
178
179         $sql = "UPDATE ".TB_PREF."sales_orders SET type =".$order->so_type." ,
180                 debtor_no = '" . $order->customer_id . "',
181                 branch_code = '" . $order->Branch . "',
182                 customer_ref = '". $order->cust_ref ."',
183                 comments = '". db_escape($order->Comments) ."',
184                 ord_date = '" . $ord_date . "',
185                 order_type = '" . $order->default_sales_type . "',
186                 ship_via = " . $order->ship_via .",
187                 deliver_to = '" . $order->deliver_to . "',
188                 delivery_address = '" . $order->delivery_address . "',
189                 contact_phone = '" . $order->phone . "',
190                 contact_email = '" . $order->email . "',
191                 freight_cost = " . $order->freight_cost .",
192                 from_stk_loc = '" . $order->Location ."',
193                 delivery_date = '" . $del_date . "',
194                 version = ".($version+1)."
195          WHERE order_no=" . $order_no ."
196          AND version=".$version;
197
198         db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
199
200         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
201
202         db_query($sql, "Old order Cannot be Deleted");
203
204         if ($loc_notification == 1)
205         {
206                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
207                 $st_ids = array();
208                 $st_names = array();
209                 $st_num = array();
210                 $st_reorder = array();
211         }
212         foreach ($order->line_items as $line)
213         {
214                 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
215                 {
216                         $sql = "SELECT ".TB_PREF."loc_stock.*, "
217                                   .TB_PREF."locations.location_name, "
218                                   .TB_PREF."locations.email
219                                 FROM ".TB_PREF."loc_stock, "
220                                   .TB_PREF."locations
221                                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
222                                  AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
223                                  AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
224                         $res = db_query($sql,"a location could not be retreived");
225                         $loc = db_fetch($res);
226                         if ($loc['email'] != "")
227                         {
228                                 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
229                                 $qoh -= get_demand_qty($line->stock_id, $order->Location);
230                                 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
231                                 $qoh -= $line->quantity;
232                                 if ($qoh < $loc['reorder_level'])
233                                 {
234                                         $st_ids[] = $line->stock_id;
235                                         $st_names[] = $line->item_description;
236                                         $st_num[] = $qoh - $loc['reorder_level'];
237                                         $st_reorder[] = $loc['reorder_level'];
238                                 }
239                         }
240                 }
241                 $sql = "INSERT INTO ".TB_PREF."sales_order_details
242                  (order_no, stk_code,  description, unit_price, quantity,
243                   discount_percent, qty_sent)
244                  VALUES (";
245                 $sql .= $order_no . ",'"
246                   .$line->stock_id . "','"
247                   .$line->item_description . "', "
248                   .$line->price . ", "
249                   .$line->quantity . ", "
250                   .$line->discount_percent . ", "
251                   .$line->qty_done ." )";
252
253                 db_query($sql, "Old order Cannot be Inserted");
254
255         } /* inserted line items into sales order details */
256
257         commit_transaction();
258         if ($loc_notification == 1 && count($st_ids) > 0)
259         {
260                 require_once($path_to_root . "/reporting/includes/class.mail.inc");
261                 $company = get_company_prefs();
262                 $mail = new email($company['coy_name'], $company['email']);
263                 $from = $company['coy_name'] . " <" . $company['email'] . ">";
264                 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
265                 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
266                 $msg = "\n";
267                 for ($i = 0; $i < count($st_ids); $i++)
268                         $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
269                           . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
270                           . _("Below") . ": " . $st_num[$i] . "\n";
271                 $msg .= "\n" . _("Please reorder") . "\n\n";
272                 $msg .= $company['coy_name'];
273                 $mail->to($to);
274                 $mail->subject($subject);
275                 $mail->text($msg);
276                 $ret = $mail->send();
277         }
278 }
279
280 //----------------------------------------------------------------------------------------
281
282 function get_sales_order_header($order_no)
283 {
284         $sql = "SELECT ".TB_PREF."sales_orders.*, "
285           .TB_PREF."debtors_master.name, "
286           .TB_PREF."debtors_master.curr_code, "
287           .TB_PREF."locations.location_name, "
288           .TB_PREF."debtors_master.payment_terms, "
289           .TB_PREF."debtors_master.discount, "
290           .TB_PREF."sales_types.sales_type, "
291           .TB_PREF."sales_types.id AS sales_type_id, "
292           .TB_PREF."sales_types.tax_included, "
293           .TB_PREF."shippers.shipper_name, "
294           .TB_PREF."tax_groups.name AS tax_group_name , "
295           .TB_PREF."tax_groups.id AS tax_group_id
296         FROM ".TB_PREF."sales_orders, "
297           .TB_PREF."debtors_master, "
298           .TB_PREF."sales_types, "
299           .TB_PREF."tax_groups, "
300           .TB_PREF."cust_branch, "
301           .TB_PREF."locations, "
302           .TB_PREF."shippers
303         WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
304                 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
305                 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
306                 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
307                 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
308                 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
309                 AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
310         $result = db_query($sql, "order Retreival");
311
312         $num = db_num_rows($result);
313         if ($num > 1)
314         {
315                 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
316         }
317         else if ($num == 1)
318         {
319                 return db_fetch($result);
320         }
321         else
322                 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
323
324 }
325
326 //----------------------------------------------------------------------------------------
327
328 function get_sales_order_details($order_no) {
329         $sql = "SELECT id, stk_code, unit_price, "
330                 .TB_PREF."sales_order_details.description,"
331                 .TB_PREF."sales_order_details.quantity,
332                   discount_percent,
333                   qty_sent as qty_done, "
334                 .TB_PREF."stock_master.units,
335                 ".TB_PREF."stock_master.material_cost + "
336                         .TB_PREF."stock_master.labour_cost + "
337                         .TB_PREF."stock_master.overhead_cost AS standard_cost
338         FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
339         WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
340         AND order_no =" . $order_no . " ORDER BY id";
341
342         return db_query($sql, "Retreive order Line Items");
343 }
344 //----------------------------------------------------------------------------------------
345
346 function read_sales_order($order_no, &$order)
347 {
348         $myrow = get_sales_order_header($order_no);
349
350         $order->trans_type = 30;
351         $order->so_type =  $myrow["type"];
352         $order->trans_no = array($order_no=> $myrow["version"]);
353
354         $order->set_customer($myrow["debtor_no"], $myrow["name"],
355           $myrow["curr_code"], $myrow["discount"]);
356
357         $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
358           $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
359
360         $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"], $myrow["tax_included"]);
361
362         $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
363
364         $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
365           $myrow["delivery_address"], $myrow["freight_cost"]);
366
367         $order->cust_ref = $myrow["customer_ref"];
368         $order->default_sales_type =$myrow["order_type"];
369         $order->Comments = $myrow["comments"];
370         $order->due_date = sql2date($myrow["delivery_date"]);
371         $order->document_date = sql2date($myrow["ord_date"]);
372
373         $result = get_sales_order_details($order_no);
374         if (db_num_rows($result) > 0)
375         {
376                 $line_no=0;
377                 while ($myrow = db_fetch($result))
378                 {
379                         $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
380                                 $myrow["unit_price"], $myrow["discount_percent"],
381                                 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
382                 $line_no++;
383                 }
384         }
385
386         return true;
387 }
388
389 //----------------------------------------------------------------------------------------
390
391 function sales_order_has_deliveries($order_no)
392 {
393         $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
394         "sales_order_details WHERE order_no=$order_no";
395
396         $result = db_query($sql, "could not query for sales order usage");
397
398         $row = db_fetch_row($result);
399
400         return ($row[0] > 0);
401 }
402
403 //----------------------------------------------------------------------------------------
404
405 function close_sales_order($order_no)
406 {
407         // set the quantity of each item to the already sent quantity. this will mark item as closed.
408         $sql = "UPDATE ".TB_PREF."sales_order_details
409                 SET quantity = qty_sent,
410                         type = 0,
411                         WHERE order_no = $order_no";
412
413         db_query($sql, "The sales order detail record could not be updated");
414 }
415
416 //---------------------------------------------------------------------------------------------------------------
417
418 function get_invoice_duedate($debtorno, $invdate)
419 {
420         if (!is_date($invdate))
421         {
422                 return Today();
423         }
424         $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
425                 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
426                 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
427
428         $result = db_query($sql,"The customer details could not be retrieved");
429         $myrow = db_fetch($result);
430
431         if (db_num_rows($result) == 0)
432                 return $invdate;
433         if ($myrow['day_in_following_month'] > 0)
434                 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
435         else
436                 $duedate = add_days($invdate, $myrow['days_before_due']);
437         return $duedate;
438 }
439
440 function get_customer_to_order($customer_id) {
441
442         // Now check to ensure this account is not on hold */
443         $sql = "SELECT ".TB_PREF."debtors_master.name, "
444                   .TB_PREF."debtors_master.address, "
445                   .TB_PREF."credit_status.dissallow_invoices, "
446                   .TB_PREF."debtors_master.sales_type AS salestype, "
447                   .TB_PREF."sales_types.sales_type, "
448                   .TB_PREF."sales_types.tax_included, "
449                   .TB_PREF."debtors_master.curr_code, "
450                   .TB_PREF."debtors_master.discount
451                 FROM ".TB_PREF."debtors_master, "
452                   .TB_PREF."credit_status, "
453                   .TB_PREF."sales_types
454                 WHERE ".TB_PREF."debtors_master.sales_type="
455                   .TB_PREF."sales_types.id
456                 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
457                 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
458
459         $result =db_query($sql,"Customer Record Retreive");
460         return  db_fetch($result);
461 }
462
463 function get_branch_to_order($customer_id, $branch_id) {
464
465         // the branch was also selected from the customer selection so default the delivery details from the customer branches table cust_branch. The order process will ask for branch details later anyway
466                 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
467                         .TB_PREF."cust_branch.br_address, "
468                         .TB_PREF."cust_branch.br_post_address, "
469                         .TB_PREF."cust_branch.phone, "
470                         .TB_PREF."cust_branch.email,
471                           default_location, location_name, default_ship_via, "
472                         .TB_PREF."tax_groups.name AS tax_group_name, "
473                         .TB_PREF."tax_groups.id AS tax_group_id
474                         FROM ".TB_PREF."cust_branch, "
475                           .TB_PREF."tax_groups, "
476                           .TB_PREF."locations
477                         WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
478                                 AND ".TB_PREF."locations.loc_code=default_location
479                                 AND ".TB_PREF."cust_branch.branch_code='" . $branch_id . "'
480                                 AND ".TB_PREF."cust_branch.debtor_no = '" . $customer_id . "'";
481
482             return db_query($sql,"Customer Branch Record Retreive");
483 }
484 ?>