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