Removed sparse postread hook.
[fa-stable.git] / sales / includes / db / sales_order_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 //----------------------------------------------------------------------------------------
13 function add_sales_order(&$order)
14 {
15         global $loc_notification, $path_to_root, $Refs;
16
17         begin_transaction();
18         hook_db_prewrite($order, $order->trans_type);
19         $order_no = get_next_trans_no($order->trans_type);
20         $del_date = date2sql($order->due_date);
21         $order_type = 0; // this is default on new order
22         $total = $order->get_trans_total();
23         $sql = "INSERT INTO ".TB_PREF."sales_orders (order_no, type, debtor_no, trans_type, branch_code, customer_ref, reference, comments, ord_date,
24                 order_type, ship_via, deliver_to, delivery_address, contact_phone,
25                 freight_cost, from_stk_loc, delivery_date, payment_terms, total)
26                 VALUES (" .db_escape($order_no) . "," .db_escape($order_type) . "," . db_escape($order->customer_id) .
27                  ", " .db_escape($order->trans_type) . "," .db_escape($order->Branch) . ", ".
28                         db_escape($order->cust_ref) .",". 
29                         db_escape($order->reference) .",". 
30                         db_escape($order->Comments) .",'" . 
31                         date2sql($order->document_date) . "', " .
32                         db_escape($order->sales_type) . ", " .
33                         db_escape($order->ship_via)."," . 
34                         db_escape($order->deliver_to) . "," .
35                         db_escape($order->delivery_address) . ", " .
36                         db_escape($order->phone) . ", " . 
37                         db_escape($order->freight_cost) .", " . 
38                         db_escape($order->Location) .", " .
39                         db_escape($del_date) . "," .
40                         db_escape($order->payment) . "," .
41                         db_escape($total). ")";
42
43         db_query($sql, "order Cannot be Added");
44
45         $order->trans_no = array($order_no=>0);
46
47         if ($loc_notification == 1)
48         {
49                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
50                 $st_ids = array();
51                 $st_names = array();
52                 $st_num = array();
53                 $st_reorder = array();
54         }
55         foreach ($order->line_items as $line)
56         {
57                 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
58                 {
59                         $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
60                                 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
61                                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
62                                 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
63                                 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
64                         $res = db_query($sql,"a location could not be retreived");
65                         $loc = db_fetch($res);
66                         if ($loc['email'] != "")
67                         {
68                                 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
69                                 $qoh -= get_demand_qty($line->stock_id, $order->Location);
70                                 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
71                                 $qoh -= $line->quantity;
72                                 if ($qoh < $loc['reorder_level'])
73                                 {
74                                         $st_ids[] = $line->stock_id;
75                                         $st_names[] = $line->item_description;
76                                         $st_num[] = $qoh - $loc['reorder_level'];
77                                         $st_reorder[] = $loc['reorder_level'];
78                                 }
79                         }
80                 }
81
82                 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, trans_type, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
83                 $sql .= $order_no . ",".$order->trans_type .
84                                 ",".db_escape($line->stock_id).", "
85                                 .db_escape($line->item_description).", $line->price,
86                                 $line->quantity,
87                                 $line->discount_percent)";
88                 db_query($sql, "order Details Cannot be Added");
89
90         } /* inserted line items into sales order details */
91
92         add_audit_trail($order->trans_type, $order_no, $order->document_date);
93         $Refs->save($order->trans_type, $order_no, $order->reference);
94
95         hook_db_postwrite($order, $order->trans_type);
96         commit_transaction();
97
98         if ($loc_notification == 1 && count($st_ids) > 0)
99         {
100                 require_once($path_to_root . "/reporting/includes/class.mail.inc");
101                 $company = get_company_prefs();
102                 $mail = new email($company['coy_name'], $company['email']);
103                 $from = $company['coy_name'] . " <" . $company['email'] . ">";
104                 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
105                 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
106                 $msg = "\n";
107                 for ($i = 0; $i < count($st_ids); $i++)
108                         $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
109                 $msg .= "\n" . _("Please reorder") . "\n\n";
110                 $msg .= $company['coy_name'];
111                 $mail->to($to);
112                 $mail->subject($subject);
113                 $mail->text($msg);
114                 $ret = $mail->send();
115         }
116         return $order_no;
117 }
118
119 //----------------------------------------------------------------------------------------
120
121 function delete_sales_order($order_no, $trans_type)
122 {
123         begin_transaction();
124         hook_db_prevoid($trans_type, $order_no);
125
126         $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . db_escape($order_no) 
127                 . " AND trans_type=".db_escape($trans_type);
128
129         db_query($sql, "order Header Delete");
130
131         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" 
132                 .db_escape($order_no) . " AND trans_type=".db_escape($trans_type);
133         db_query($sql, "order Detail Delete");
134
135         delete_reference($trans_type, $order_no);
136
137         add_audit_trail($trans_type, $order_no, Today(), _("Deleted."));
138         commit_transaction();
139 }
140
141 //----------------------------------------------------------------------------------------
142 // Mark changes in sales_order_details
143 //
144 function update_sales_order_version($order)
145 {
146   foreach ($order as $so_num => $so_ver) {
147   $sql= 'UPDATE '.TB_PREF.'sales_orders SET version=version+1 WHERE order_no='. db_escape($so_num).
148         ' AND version='.$so_ver . " AND trans_type=30";
149   db_query($sql, 'Concurrent editing conflict while sales order update');
150   }
151 }
152
153 //----------------------------------------------------------------------------------------
154
155 function update_sales_order($order)
156 {
157         global $loc_notification, $path_to_root, $Refs;
158
159         $del_date = date2sql($order->due_date);
160         $ord_date = date2sql($order->document_date);
161         $order_no =  key($order->trans_no);
162         $version= current($order->trans_no);
163         $total = $order->get_trans_total();
164
165         begin_transaction();
166
167         $sql = "UPDATE ".TB_PREF."sales_orders SET type =".db_escape($order->so_type)." ,
168                 debtor_no = " . db_escape($order->customer_id) . ",
169                 branch_code = " . db_escape($order->Branch) . ",
170                 customer_ref = ". db_escape($order->cust_ref) .",
171                 reference = ". db_escape($order->reference) .",
172                 comments = ". db_escape($order->Comments) .",
173                 ord_date = " . db_escape($ord_date) . ",
174                 order_type = " .db_escape($order->sales_type) . ",
175                 ship_via = " . db_escape($order->ship_via) .",
176                 deliver_to = " . db_escape($order->deliver_to) . ",
177                 delivery_address = " . db_escape($order->delivery_address) . ",
178                 contact_phone = " .db_escape($order->phone) . ",
179                 freight_cost = " .db_escape($order->freight_cost) .",
180                 from_stk_loc = " .db_escape($order->Location) .",
181                 delivery_date = " .db_escape($del_date). ",
182                 version = ".($version+1).",
183                 payment_terms = " .db_escape($order->payment). ",
184                 total = ". db_escape($total) ."
185          WHERE order_no=" . db_escape($order_no) ."
186          AND trans_type=".$order->trans_type." AND version=".$version;
187         db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
188
189         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . db_escape($order_no) . " AND trans_type=".$order->trans_type;
190
191         db_query($sql, "Old order Cannot be Deleted");
192
193         if ($loc_notification == 1)
194         {
195                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
196                 $st_ids = array();
197                 $st_names = array();
198                 $st_num = array();
199                 $st_reorder = array();
200         }
201         foreach ($order->line_items as $line)
202         {
203                 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
204                 {
205                         $sql = "SELECT ".TB_PREF."loc_stock.*, "
206                                   .TB_PREF."locations.location_name, "
207                                   .TB_PREF."locations.email
208                                 FROM ".TB_PREF."loc_stock, "
209                                   .TB_PREF."locations
210                                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
211                                  AND ".TB_PREF."loc_stock.stock_id = ".db_escape($line->stock_id)."
212                                  AND ".TB_PREF."loc_stock.loc_code = ".db_escape($order->Location);
213                         $res = db_query($sql,"a location could not be retreived");
214                         $loc = db_fetch($res);
215                         if ($loc['email'] != "")
216                         {
217                                 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
218                                 $qoh -= get_demand_qty($line->stock_id, $order->Location);
219                                 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
220                                 $qoh -= $line->quantity;
221                                 if ($qoh < $loc['reorder_level'])
222                                 {
223                                         $st_ids[] = $line->stock_id;
224                                         $st_names[] = $line->item_description;
225                                         $st_num[] = $qoh - $loc['reorder_level'];
226                                         $st_reorder[] = $loc['reorder_level'];
227                                 }
228                         }
229                 }
230                 $sql = "INSERT INTO ".TB_PREF."sales_order_details
231                  (id, order_no, trans_type, stk_code,  description, unit_price, quantity,
232                   discount_percent, qty_sent)
233                  VALUES (";
234                 $sql .= db_escape($line->id ? $line->id : 0) . ","
235                   .$order_no . ",".$order->trans_type.","
236                   .db_escape($line->stock_id) . ","
237                   .db_escape($line->item_description) . ", "
238                   .db_escape($line->price) . ", "
239                   .db_escape($line->quantity) . ", "
240                   .db_escape($line->discount_percent) . ", "
241                   .db_escape($line->qty_done) ." )";
242
243                 db_query($sql, "Old order Cannot be Inserted");
244
245         } /* inserted line items into sales order details */
246
247         add_audit_trail($order->trans_type, $order_no, $order->document_date, _("Updated."));
248         $Refs->save($order->trans_type, $order_no, $order->reference);
249         commit_transaction();
250         if ($loc_notification == 1 && count($st_ids) > 0)
251         {
252                 require_once($path_to_root . "/reporting/includes/class.mail.inc");
253                 $company = get_company_prefs();
254                 $mail = new email($company['coy_name'], $company['email']);
255                 $from = $company['coy_name'] . " <" . $company['email'] . ">";
256                 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
257                 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
258                 $msg = "\n";
259                 for ($i = 0; $i < count($st_ids); $i++)
260                         $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
261                           . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
262                           . _("Below") . ": " . $st_num[$i] . "\n";
263                 $msg .= "\n" . _("Please reorder") . "\n\n";
264                 $msg .= $company['coy_name'];
265                 $mail->to($to);
266                 $mail->subject($subject);
267                 $mail->text($msg);
268                 $ret = $mail->send();
269         }
270 }
271
272 //----------------------------------------------------------------------------------------
273
274 function get_sales_order_header($order_no, $trans_type)
275 {
276         $sql = "SELECT sorder.*, "
277           ."cust.name, "
278           ."cust.curr_code, "
279           ."loc.location_name, "
280           ."cust.discount, "
281           ."stype.sales_type, "
282           ."stype.id AS sales_type_id, "
283           ."stype.tax_included, "
284           ."ship.shipper_name, "
285           ."tax_group.name AS tax_group_name , "
286           ."tax_group.id AS tax_group_id "
287         ."FROM ".TB_PREF."sales_orders sorder, "
288           .TB_PREF."debtors_master cust,"
289           .TB_PREF."sales_types stype, "
290           .TB_PREF."tax_groups tax_group, "
291           .TB_PREF."cust_branch branch,"
292           .TB_PREF."locations loc, "
293           .TB_PREF."shippers ship
294         WHERE sorder.order_type=stype.id
295                 AND branch.branch_code = sorder.branch_code
296                 AND branch.tax_group_id = tax_group.id
297                 AND sorder.debtor_no = cust.debtor_no
298                 AND loc.loc_code = sorder.from_stk_loc
299                 AND ship.shipper_id = sorder.ship_via
300                 AND sorder.trans_type = " . db_escape($trans_type) ."
301                 AND sorder.order_no = " . db_escape($order_no );
302
303         $result = db_query($sql, "order Retreival");
304
305         $num = db_num_rows($result);
306         if ($num > 1)
307         {
308                 display_warning("You have duplicate document in database: (type:$trans_type, number:$order_no).");
309         }
310         else if ($num == 1)
311         {
312                 return db_fetch($result);
313         }
314         else
315                 display_warning("You have missing or invalid sales document in database (type:$trans_type, number:$order_no).");
316
317 }
318
319 //----------------------------------------------------------------------------------------
320
321 function get_sales_order_details($order_no, $trans_type) {
322         $sql = "SELECT id, stk_code, unit_price, "
323                 .TB_PREF."sales_order_details.description,"
324                 .TB_PREF."sales_order_details.quantity,
325                   discount_percent,
326                   qty_sent as qty_done, "
327                 .TB_PREF."stock_master.units,
328                 ".TB_PREF."stock_master.material_cost + "
329                         .TB_PREF."stock_master.labour_cost + "
330                         .TB_PREF."stock_master.overhead_cost AS standard_cost
331         FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
332         WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
333         AND order_no =" . db_escape($order_no) 
334                 ." AND trans_type = " . db_escape($trans_type) . " ORDER BY id";
335
336         return db_query($sql, "Retreive order Line Items");
337 }
338 //----------------------------------------------------------------------------------------
339
340 function read_sales_order($order_no, &$order, $trans_type)
341 {
342
343         $myrow = get_sales_order_header($order_no, $trans_type);
344
345         $order->trans_type = $myrow['trans_type'];
346         $order->so_type =  $myrow["type"];
347         $order->trans_no = array($order_no=> $myrow["version"]);
348
349         $order->set_customer($myrow["debtor_no"], $myrow["name"],
350           $myrow["curr_code"], $myrow["discount"], $myrow["payment_terms"]);
351
352         $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
353           $myrow["tax_group_name"], $myrow["contact_phone"]);
354
355         $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"], 
356             $myrow["tax_included"], 0); // no default price calculations on edit
357
358         $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
359
360         $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
361           $myrow["delivery_address"], $myrow["freight_cost"]);
362
363         $order->cust_ref = $myrow["customer_ref"];
364         $order->sales_type =$myrow["order_type"];
365         $order->reference = $myrow["reference"];
366         $order->Comments = $myrow["comments"];
367         $order->due_date = sql2date($myrow["delivery_date"]);
368         $order->document_date = sql2date($myrow["ord_date"]);
369
370         $result = get_sales_order_details($order_no, $order->trans_type);
371         if (db_num_rows($result) > 0)
372         {
373                 $line_no=0;
374                 while ($myrow = db_fetch($result))
375                 {
376                         $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
377                                 $myrow["unit_price"], $myrow["discount_percent"],
378                                 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
379                 $line_no++;
380                 }
381         }
382
383         return true;
384 }
385
386 //----------------------------------------------------------------------------------------
387
388 function sales_order_has_deliveries($order_no)
389 {
390         $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
391         "sales_order_details WHERE order_no=".db_escape($order_no)
392         ." AND trans_type=".ST_SALESORDER;
393
394         $result = db_query($sql, "could not query for sales order usage");
395
396         $row = db_fetch_row($result);
397
398         if ($row[0] > 0)
399                 return true;  // 2010-04-21 added check for eventually voided deliveries, Joe Hunt
400         $sql = "SELECT order_ FROM ".TB_PREF."debtor_trans WHERE type=".ST_CUSTDELIVERY." AND order_=".db_escape($order_no);
401         $result = db_query($sql,"The related delivery notes could not be retreived");
402         return (db_num_rows($result) > 0);      
403 }
404
405 //----------------------------------------------------------------------------------------
406
407 function close_sales_order($order_no)
408 {
409         // set the quantity of each item to the already sent quantity. this will mark item as closed.
410         $sql = "UPDATE ".TB_PREF."sales_order_details
411                 SET quantity = qty_sent WHERE order_no = ".db_escape($order_no)
412                 ." AND trans_type=".ST_SALESORDER;
413
414         db_query($sql, "The sales order detail record could not be updated");
415 }
416
417 //---------------------------------------------------------------------------------------------------------------
418
419 function get_invoice_duedate($terms, $invdate)
420 {
421         if (!is_date($invdate))
422         {
423                 return new_doc_date();
424         }
425         
426         $myrow = get_payment_terms($terms);
427         
428         if (!$myrow)
429                 return $invdate;
430
431         if ($myrow['day_in_following_month'] > 0)
432                 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
433         else
434                 $duedate = add_days($invdate, $myrow['days_before_due']);
435         return $duedate;
436 }
437
438 function get_customer_to_order($customer_id) {
439
440         // Now check to ensure this account is not on hold */
441         $sql = "SELECT cust.name, 
442                   cust.address, "
443                   .TB_PREF."credit_status.dissallow_invoices, 
444                   cust.sales_type AS salestype,
445                   cust.dimension_id,
446                   cust.dimension2_id,
447                   stype.sales_type,
448                   stype.tax_included,
449                   stype.factor,
450                   cust.curr_code,
451                   cust.discount,
452                   cust.payment_terms,
453                   cust.pymt_discount,
454                   cust.credit_limit - Sum(IFNULL(IF(trans.type=11 OR trans.type=12 OR trans.type=2,
455                         -1, 1) * (ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount),0)) as cur_credit
456                 FROM ".TB_PREF."debtors_master cust
457                   LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.type!=".ST_CUSTDELIVERY." AND trans.debtor_no = cust.debtor_no,"
458                   .TB_PREF."credit_status, "
459                   .TB_PREF."sales_types stype
460                 WHERE cust.sales_type=stype.id
461                         AND cust.credit_status=".TB_PREF."credit_status.id
462                         AND cust.debtor_no = ".db_escape($customer_id)
463                 ." GROUP by cust.debtor_no";
464
465         $result =db_query($sql,"Customer Record Retreive");
466         return  db_fetch($result);
467 }
468
469 function get_branch_to_order($customer_id, $branch_id) {
470
471         // 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
472                 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
473                         .TB_PREF."cust_branch.br_address, "
474                         .TB_PREF."cust_branch.br_post_address, "
475                         ." default_location, location_name, default_ship_via, "
476                         .TB_PREF."tax_groups.name AS tax_group_name, "
477                         .TB_PREF."tax_groups.id AS tax_group_id
478                         FROM ".TB_PREF."cust_branch, "
479                           .TB_PREF."tax_groups, "
480                           .TB_PREF."locations
481                         WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
482                                 AND ".TB_PREF."locations.loc_code=default_location
483                                 AND ".TB_PREF."cust_branch.branch_code=".db_escape($branch_id)."
484                                 AND ".TB_PREF."cust_branch.debtor_no = ".db_escape($customer_id);
485
486             return db_query($sql,"Customer Branch Record Retreive");
487 }
488
489 function get_sql_for_sales_orders_view($selected_customer, $trans_type)
490 {
491         global $selected_stock_item;
492         
493         $sql = "SELECT 
494                         sorder.order_no,
495                         sorder.reference,
496                         debtor.name,
497                         branch.br_name,"
498                         .($_POST['order_view_mode']=='InvoiceTemplates' 
499                                 || $_POST['order_view_mode']=='DeliveryTemplates' ?
500                          "sorder.comments, " : "sorder.customer_ref, ")
501                         ."sorder.ord_date,
502                         sorder.delivery_date,
503                         sorder.deliver_to,
504                         Sum(line.unit_price*line.quantity*(1-line.discount_percent))+freight_cost AS OrderValue,
505                         sorder.type,
506                         debtor.curr_code,
507                         Sum(line.qty_sent) AS TotDelivered,
508                         Sum(line.quantity) AS TotQuantity
509                 FROM ".TB_PREF."sales_orders as sorder, "
510                         .TB_PREF."sales_order_details as line, "
511                         .TB_PREF."debtors_master as debtor, "
512                         .TB_PREF."cust_branch as branch
513                         WHERE sorder.order_no = line.order_no
514                         AND sorder.trans_type = line.trans_type
515                         AND sorder.trans_type = ".db_escape($trans_type)."
516                         AND sorder.debtor_no = debtor.debtor_no
517                         AND sorder.branch_code = branch.branch_code
518                         AND debtor.debtor_no = branch.debtor_no";
519
520         if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
521         {
522                 // search orders with number like 
523                 $number_like = "%".$_POST['OrderNumber'];
524                 $sql .= " AND sorder.order_no LIKE ".db_escape($number_like)
525                                 ." GROUP BY sorder.order_no";
526         }
527         elseif (isset($_POST['OrderReference']) && $_POST['OrderReference'] != "")
528         {
529                 // search orders with reference like 
530                 $number_like = "%".$_POST['OrderReference']."%";
531                 $sql .= " AND sorder.reference LIKE ".db_escape($number_like)
532                                 ." GROUP BY sorder.order_no";
533         }
534         else    // ... or select inquiry constraints
535         {
536                 if ($_POST['order_view_mode']!='DeliveryTemplates' && $_POST['order_view_mode']!='InvoiceTemplates')
537                 {
538                         $date_after = date2sql($_POST['OrdersAfterDate']);
539                         $date_before = date2sql($_POST['OrdersToDate']);
540
541                         $sql .=  " AND sorder.ord_date >= '$date_after'"
542                                         ." AND sorder.ord_date <= '$date_before'";
543                 }
544                 if ($trans_type == ST_SALESQUOTE && !check_value('show_all'))
545                         $sql .= " AND sorder.delivery_date >= '".date2sql(Today())."'";
546                 if ($selected_customer != -1)
547                         $sql .= " AND sorder.debtor_no=".db_escape($selected_customer);
548
549                 if (isset($selected_stock_item))
550                         $sql .= " AND line.stk_code=".db_escape($selected_stock_item);
551
552                 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
553                         $sql .= " AND sorder.from_stk_loc = ".db_escape($_POST['StockLocation']);
554
555                 if ($_POST['order_view_mode']=='OutstandingOnly')
556                         $sql .= " AND line.qty_sent < line.quantity";
557                 elseif ($_POST['order_view_mode']=='InvoiceTemplates' || $_POST['order_view_mode']=='DeliveryTemplates')
558                         $sql .= " AND sorder.type=1";
559
560                 $sql .= " GROUP BY sorder.order_no,
561                                         sorder.debtor_no,
562                                         sorder.branch_code,
563                                         sorder.customer_ref,
564                                         sorder.ord_date,
565                                         sorder.deliver_to";
566         }
567         return $sql;
568 }
569 ?>