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