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