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