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