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)
15 global $loc_notification, $path_to_root, $Refs;
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, prep_amount)
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 db_escape($order->prep_amount).")";
44 db_query($sql, "order Cannot be Added");
46 $order->trans_no = array($order_no=>0);
48 if ($loc_notification == 1)
50 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
54 $st_reorder = array();
56 foreach ($order->line_items as $line)
58 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
60 $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
61 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
62 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
63 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
64 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
65 $res = db_query($sql,"a location could not be retreived");
66 $loc = db_fetch($res);
67 if ($loc['email'] != "")
69 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
70 $qoh -= get_demand_qty($line->stock_id, $order->Location);
71 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
72 $qoh -= $line->quantity;
73 if ($qoh < $loc['reorder_level'])
75 $st_ids[] = $line->stock_id;
76 $st_names[] = $line->item_description;
77 $st_num[] = $qoh - $loc['reorder_level'];
78 $st_reorder[] = $loc['reorder_level'];
83 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, trans_type, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
84 $sql .= $order_no . ",".$order->trans_type .
85 ",".db_escape($line->stock_id).", "
86 .db_escape($line->item_description).", $line->price,
88 $line->discount_percent)";
89 db_query($sql, "order Details Cannot be Added");
91 // Now mark quotation line as processed
92 if ($order->trans_type == ST_SALESORDER && $line->src_id)
93 update_parent_line(ST_SALESORDER, $line->src_id, $line->qty_dispatched); // clear all the quote despite all or the part was ordered
94 } /* inserted line items into sales order details */
96 add_audit_trail($order->trans_type, $order_no, $order->document_date);
97 $Refs->save($order->trans_type, $order_no, $order->reference);
99 hook_db_postwrite($order, $order->trans_type);
100 commit_transaction();
102 if ($loc_notification == 1 && count($st_ids) > 0)
104 require_once($path_to_root . "/reporting/includes/class.mail.inc");
105 $company = get_company_prefs();
106 $mail = new email($company['coy_name'], $company['email']);
107 $from = $company['coy_name'] . " <" . $company['email'] . ">";
108 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
109 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
111 for ($i = 0; $i < count($st_ids); $i++)
112 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
113 $msg .= "\n" . _("Please reorder") . "\n\n";
114 $msg .= $company['coy_name'];
116 $mail->subject($subject);
118 $ret = $mail->send();
123 //----------------------------------------------------------------------------------------
125 function delete_sales_order($order_no, $trans_type)
128 hook_db_prevoid($trans_type, $order_no);
130 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . db_escape($order_no)
131 . " AND trans_type=".db_escape($trans_type);
133 db_query($sql, "order Header Delete");
135 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no ="
136 .db_escape($order_no) . " AND trans_type=".db_escape($trans_type);
137 db_query($sql, "order Detail Delete");
139 delete_reference($trans_type, $order_no);
141 add_audit_trail($trans_type, $order_no, Today(), _("Deleted."));
142 commit_transaction();
145 //----------------------------------------------------------------------------------------
146 // Mark changes in sales_order_details
148 function update_sales_order_version($order)
150 foreach ($order as $so_num => $so_ver) {
151 $sql= 'UPDATE '.TB_PREF.'sales_orders SET version=version+1 WHERE order_no='. db_escape($so_num).
152 ' AND version='.$so_ver . " AND trans_type=30";
153 db_query($sql, 'Concurrent editing conflict while sales order update');
157 //----------------------------------------------------------------------------------------
159 function update_sales_order($order)
161 global $loc_notification, $path_to_root, $Refs;
163 $del_date = date2sql($order->due_date);
164 $ord_date = date2sql($order->document_date);
165 $order_no = key($order->trans_no);
166 $version= current($order->trans_no);
167 $total = $order->get_trans_total();
170 hook_db_prewrite($order, $order->trans_type);
172 if ($order->trans_type == ST_SALESORDER)
173 $allocs = get_payments_for($order_no, ST_SALESORDER);
175 $sql = "UPDATE ".TB_PREF."sales_orders SET type =".db_escape($order->so_type)." ,
176 debtor_no = " . db_escape($order->customer_id) . ",
177 branch_code = " . db_escape($order->Branch) . ",
178 customer_ref = ". db_escape($order->cust_ref) .",
179 reference = ". db_escape($order->reference) .",
180 comments = ". db_escape($order->Comments) .",
181 ord_date = " . db_escape($ord_date) . ",
182 order_type = " .db_escape($order->sales_type) . ",
183 ship_via = " . db_escape($order->ship_via) .",
184 deliver_to = " . db_escape($order->deliver_to) . ",
185 delivery_address = " . db_escape($order->delivery_address) . ",
186 contact_phone = " .db_escape($order->phone) . ",
187 freight_cost = " .db_escape($order->freight_cost) .",
188 from_stk_loc = " .db_escape($order->Location) .",
189 delivery_date = " .db_escape($del_date). ",
190 version = ".($version+1).",
191 payment_terms = " .db_escape($order->payment). ",
192 total = ". db_escape($total). ",
193 prep_amount = ". db_escape($order->prep_amount) ."
194 WHERE order_no=" . db_escape($order_no) ."
195 AND trans_type=".$order->trans_type." AND version=".$version;
196 db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
198 $id_tokeep = array();
199 foreach ($order->line_items as $line) {
200 array_push($id_tokeep , $line->id);
202 $id_list = implode(', ', $id_tokeep);
204 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . db_escape($order_no) . " AND trans_type=".$order->trans_type;
205 $sql .= " AND id NOT IN ($id_list)";
207 db_query($sql, "Old order Cannot be Deleted");
209 if ($loc_notification == 1)
211 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
215 $st_reorder = array();
217 foreach ($order->line_items as $line)
219 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
221 $sql = "SELECT ".TB_PREF."loc_stock.*, "
222 .TB_PREF."locations.location_name, "
223 .TB_PREF."locations.email
224 FROM ".TB_PREF."loc_stock, "
226 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
227 AND ".TB_PREF."loc_stock.stock_id = ".db_escape($line->stock_id)."
228 AND ".TB_PREF."loc_stock.loc_code = ".db_escape($order->Location);
229 $res = db_query($sql,"a location could not be retreived");
230 $loc = db_fetch($res);
231 if ($loc['email'] != "")
233 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
234 $qoh -= get_demand_qty($line->stock_id, $order->Location);
235 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
236 $qoh -= $line->quantity;
237 if ($qoh < $loc['reorder_level'])
239 $st_ids[] = $line->stock_id;
240 $st_names[] = $line->item_description;
241 $st_num[] = $qoh - $loc['reorder_level'];
242 $st_reorder[] = $loc['reorder_level'];
246 if (!$line->id) //new line
247 $sql = "INSERT INTO ".TB_PREF."sales_order_details
248 (order_no, trans_type, stk_code, description, unit_price, quantity,
249 discount_percent, qty_sent)
250 VALUES (".$order_no . ",".$order->trans_type.","
251 .db_escape($line->stock_id) . ","
252 .db_escape($line->item_description) . ", "
253 .db_escape($line->price) . ", "
254 .db_escape($line->quantity) . ", "
255 .db_escape($line->discount_percent) . ", "
256 .db_escape($line->qty_done) ." )";
258 $sql = "UPDATE ".TB_PREF."sales_order_details
259 SET id=".db_escape($line->id).",
261 trans_type=".$order->trans_type.",
262 stk_code=".db_escape($line->stock_id).",
263 description=".db_escape($line->item_description).",
264 unit_price=".db_escape($line->price).",
265 quantity=".db_escape($line->quantity).",
266 discount_percent=".db_escape($line->discount_percent).",
267 qty_sent=".db_escape($line->qty_done)."
268 WHERE id = ".db_escape($line->id);
270 db_query($sql, "Old order Cannot be updated");
271 } /* inserted line items into sales order details */
273 if ($order->trans_type == ST_SALESORDER)
274 reallocate_payments($order_no, ST_SALESORDER, $ord_date, $total, $allocs);
275 add_audit_trail($order->trans_type, $order_no, $order->document_date, _("Updated."));
276 $Refs->save($order->trans_type, $order_no, $order->reference);
278 hook_db_postwrite($order, $order->trans_type);
279 commit_transaction();
280 if ($loc_notification == 1 && count($st_ids) > 0)
282 require_once($path_to_root . "/reporting/includes/class.mail.inc");
283 $company = get_company_prefs();
284 $mail = new email($company['coy_name'], $company['email']);
285 $from = $company['coy_name'] . " <" . $company['email'] . ">";
286 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
287 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
289 for ($i = 0; $i < count($st_ids); $i++)
290 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
291 . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
292 . _("Below") . ": " . $st_num[$i] . "\n";
293 $msg .= "\n" . _("Please reorder") . "\n\n";
294 $msg .= $company['coy_name'];
296 $mail->subject($subject);
298 $ret = $mail->send();
302 //----------------------------------------------------------------------------------------
304 function get_sales_order_header($order_no, $trans_type)
306 $sql = "SELECT sorder.*, "
310 ."loc.location_name, "
312 ."stype.sales_type, "
313 ."stype.id AS sales_type_id, "
314 ."stype.tax_included, "
316 ."ship.shipper_name, "
317 ."tax_group.name AS tax_group_name , "
318 ."tax_group.id AS tax_group_id, "
321 ."IFNULL(allocs.ord_allocs, 0)+IFNULL(inv.inv_allocs ,0) AS sum_paid,"
322 ."sorder.prep_amount>0 as prepaid"
323 ." FROM ".TB_PREF."sales_orders sorder
324 LEFT JOIN (SELECT trans_no_to, sum(amt) ord_allocs FROM ".TB_PREF."cust_allocations
325 WHERE trans_type_to=".ST_SALESORDER." AND trans_no_to=".db_escape($order_no)." GROUP BY trans_no_to)
326 allocs ON sorder.trans_type=".ST_SALESORDER." AND allocs.trans_no_to=sorder.order_no
327 LEFT JOIN (SELECT order_, sum(alloc) inv_allocs FROM ".TB_PREF."debtor_trans
328 WHERE type=".ST_SALESINVOICE." AND order_=".db_escape($order_no)." GROUP BY order_)
329 inv ON sorder.trans_type=".ST_SALESORDER." AND inv.order_=sorder.order_no
330 LEFT JOIN ".TB_PREF."shippers ship ON ship.shipper_id = sorder.ship_via,"
331 .TB_PREF."debtors_master cust,"
332 .TB_PREF."sales_types stype, "
333 .TB_PREF."tax_groups tax_group, "
334 .TB_PREF."cust_branch branch,"
335 .TB_PREF."locations loc
336 WHERE sorder.order_type=stype.id
337 AND branch.branch_code = sorder.branch_code
338 AND branch.tax_group_id = tax_group.id
339 AND sorder.debtor_no = cust.debtor_no
340 AND loc.loc_code = sorder.from_stk_loc
341 AND sorder.trans_type = " . db_escape($trans_type) ."
342 AND sorder.order_no = " . db_escape($order_no );
344 $result = db_query($sql, "order Retreival");
346 $num = db_num_rows($result);
349 display_warning("You have duplicate document in database: (type:$trans_type, number:$order_no).");
353 return db_fetch($result);
356 display_warning("You have missing or invalid sales document in database (type:$trans_type, number:$order_no).");
360 //----------------------------------------------------------------------------------------
362 function get_sales_order_details($order_no, $trans_type) {
363 $sql = "SELECT id, stk_code, unit_price, "
364 .TB_PREF."sales_order_details.description,"
365 .TB_PREF."sales_order_details.quantity,
367 qty_sent as qty_done, "
368 .TB_PREF."stock_master.units,"
369 .TB_PREF."stock_master.mb_flag,"
370 .TB_PREF."stock_master.material_cost + "
371 .TB_PREF."stock_master.labour_cost + "
372 .TB_PREF."stock_master.overhead_cost AS standard_cost
373 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
374 WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
375 AND order_no =" . db_escape($order_no)
376 ." AND trans_type = " . db_escape($trans_type) . " ORDER BY id";
378 return db_query($sql, "Retreive order Line Items");
380 //----------------------------------------------------------------------------------------
382 function read_sales_order($order_no, &$order, $trans_type)
385 $myrow = get_sales_order_header($order_no, $trans_type);
387 $order->trans_type = $myrow['trans_type'];
388 $order->so_type = $myrow["type"];
389 $order->trans_no = array($order_no=> $myrow["version"]);
391 $order->set_customer($myrow["debtor_no"], $myrow["name"],
392 $myrow["curr_code"], $myrow["discount"], $myrow["payment_terms"]);
394 $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
395 $myrow["tax_group_name"], $myrow["contact_phone"]);
397 $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"],
398 $myrow["tax_included"], $myrow["factor"]); // no default price calculations on edit
400 $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
402 $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
403 $myrow["delivery_address"], $myrow["freight_cost"]);
405 $order->cust_ref = $myrow["customer_ref"];
406 $order->sales_type =$myrow["order_type"];
407 $order->reference = $myrow["reference"];
408 $order->Comments = $myrow["comments"];
409 $order->due_date = sql2date($myrow["delivery_date"]);
410 $order->document_date = sql2date($myrow["ord_date"]);
412 $order->prepaid = $myrow["prepaid"];
413 $order->alloc = $myrow['alloc'];
414 $order->sum_paid = $myrow["sum_paid"]; // sum of all prepayments to so (also invoiced)
415 $order->prep_amount = $myrow["prep_amount"];
416 $order->prepayments = get_payments_for($order_no, $myrow['trans_type']);
418 $result = get_sales_order_details($order_no, $order->trans_type);
419 if (db_num_rows($result) > 0)
422 while ($myrow = db_fetch($result))
424 $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
425 $myrow["unit_price"], $myrow["discount_percent"],
426 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
434 //----------------------------------------------------------------------------------------
436 function sales_order_has_deliveries($order_no)
438 $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
439 "sales_order_details WHERE order_no=".db_escape($order_no)
440 ." AND trans_type=".ST_SALESORDER;
442 $result = db_query($sql, "could not query for sales order usage");
444 $row = db_fetch_row($result);
447 return true; // 2010-04-21 added check for eventually voided deliveries, Joe Hunt
448 $sql = "SELECT order_ FROM ".TB_PREF."debtor_trans WHERE type=".ST_CUSTDELIVERY." AND order_=".db_escape($order_no);
449 $result = db_query($sql,"The related delivery notes could not be retreived");
450 return (db_num_rows($result) > 0);
453 //----------------------------------------------------------------------------------------
455 function close_sales_order($order_no)
457 // set the quantity of each item to the already sent quantity. this will mark item as closed.
458 $sql = "UPDATE ".TB_PREF."sales_order_details
459 SET quantity = qty_sent WHERE order_no = ".db_escape($order_no)
460 ." AND trans_type=".ST_SALESORDER;
462 db_query($sql, "The sales order detail record could not be updated");
465 //---------------------------------------------------------------------------------------------------------------
467 function get_invoice_duedate($terms, $invdate)
469 if (!is_date($invdate))
471 return new_doc_date();
474 $myrow = get_payment_terms($terms);
479 if ($myrow['day_in_following_month'] > 0)
480 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
481 elseif ($myrow['days_before_due'] > 0)
482 $duedate = add_days($invdate, $myrow['days_before_due']);
488 function get_customer_to_order($customer_id) {
490 // Now check to ensure this account is not on hold */
491 $sql = "SELECT cust.name,
493 .TB_PREF."credit_status.dissallow_invoices,
494 cust.sales_type AS salestype,
504 cust.credit_limit - Sum(IFNULL(IF(trans.type=11 OR trans.type=12 OR trans.type=2,
505 -1, 1) * (ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount),0)) as cur_credit
506 FROM ".TB_PREF."debtors_master cust
507 LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.type!=".ST_CUSTDELIVERY." AND trans.debtor_no = cust.debtor_no,"
508 .TB_PREF."credit_status, "
509 .TB_PREF."sales_types stype
510 WHERE cust.sales_type=stype.id
511 AND cust.credit_status=".TB_PREF."credit_status.id
512 AND cust.debtor_no = ".db_escape($customer_id)
513 ." GROUP by cust.debtor_no";
515 $result =db_query($sql,"Customer Record Retreive");
516 return db_fetch($result);
519 function get_branch_to_order($customer_id, $branch_id) {
521 // 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
522 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
523 .TB_PREF."cust_branch.br_address, "
524 .TB_PREF."cust_branch.br_post_address, "
525 ." default_location, location_name, default_ship_via, "
526 .TB_PREF."tax_groups.name AS tax_group_name, "
527 .TB_PREF."tax_groups.id AS tax_group_id
528 FROM ".TB_PREF."cust_branch, "
529 .TB_PREF."tax_groups, "
531 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
532 AND ".TB_PREF."locations.loc_code=default_location
533 AND ".TB_PREF."cust_branch.branch_code=".db_escape($branch_id)."
534 AND ".TB_PREF."cust_branch.debtor_no = ".db_escape($customer_id);
536 return db_query($sql,"Customer Branch Record Retreive");
547 function get_sql_for_sales_orders_view($selected_customer, $trans_type, $trans_no, $filter,
548 $stock_item=null, $from='', $to='', $ref='', $location='', $customer_id=ALL_TEXT)
556 .($filter=='InvoiceTemplates'
557 || $filter=='DeliveryTemplates' ?
558 "sorder.comments, " : "sorder.customer_ref, ")
560 sorder.delivery_date,
562 Sum(line.unit_price*line.quantity*(1-line.discount_percent))+freight_cost AS OrderValue,
565 Sum(line.qty_sent) AS TotDelivered,
566 Sum(line.quantity) AS TotQuantity,
567 Sum(line.invoiced) AS TotInvoiced,
573 FROM ".TB_PREF."sales_orders as sorder
574 LEFT JOIN (SELECT trans_no_to, sum(amt) ord_payments FROM ".TB_PREF."cust_allocations WHERE trans_type_to=".ST_SALESORDER." GROUP BY trans_no_to)
575 allocs ON sorder.trans_type=".ST_SALESORDER." AND allocs.trans_no_to=sorder.order_no
576 LEFT JOIN (SELECT order_, sum(prep_amount) inv_payments FROM ".TB_PREF."debtor_trans WHERE type=".ST_SALESINVOICE." GROUP BY order_)
577 inv ON sorder.trans_type=".ST_SALESORDER." AND inv.order_=sorder.order_no,"
578 .TB_PREF."sales_order_details as line, "
579 .TB_PREF."debtors_master as debtor, "
580 .TB_PREF."cust_branch as branch
581 WHERE sorder.order_no = line.order_no
582 AND sorder.trans_type = line.trans_type
583 AND sorder.trans_type = ".db_escape($trans_type)."
584 AND sorder.debtor_no = debtor.debtor_no
585 AND sorder.branch_code = branch.branch_code
586 AND debtor.debtor_no = branch.debtor_no";
588 if (isset($trans_no) && $trans_no != "")
590 // search orders with number like
591 $number_like = "%".$trans_no;
592 $sql .= " AND sorder.order_no LIKE ".db_escape($number_like);
593 // ." GROUP BY sorder.order_no";
597 // search orders with reference like
598 $number_like = "%".$ref."%";
599 $sql .= " AND sorder.reference LIKE ".db_escape($number_like);
600 // ." GROUP BY sorder.order_no";
602 else // ... or select inquiry constraints
604 if ($filter!='DeliveryTemplates' && $filter!='InvoiceTemplates' && $filter!='OutstandingOnly')
606 $date_after = date2sql($from);
607 $date_before = date2sql($to);
609 $sql .= " AND sorder.ord_date >= '$date_after'"
610 ." AND sorder.ord_date <= '$date_before'";
613 if ($trans_type == ST_SALESQUOTE && !check_value('show_all'))
614 $sql .= " AND sorder.delivery_date >= '".date2sql(Today())."' AND line.qty_sent=0"; // show only outstanding, not realized quotes
616 if ($selected_customer != -1)
617 $sql .= " AND sorder.debtor_no=".db_escape($selected_customer);
619 if (isset($stock_item))
620 $sql .= " AND line.stk_code=".db_escape($stock_item);
623 $sql .= " AND sorder.from_stk_loc = ".db_escape($location);
625 if ($filter=='OutstandingOnly')
626 $sql .= " AND line.qty_sent < line.quantity";
628 if ($filter=='PrepaidOrders')
629 $sql .= " AND prep_amount>0";
631 elseif ($filter=='InvoiceTemplates' || $filter=='DeliveryTemplates')
632 $sql .= " AND sorder.type=1";
634 //Chaiatanya : New Filter
635 if ($customer_id != ALL_TEXT)
636 $sql .= " AND sorder.debtor_no = ".db_escape($customer_id);
638 $sql .= " GROUP BY sorder.order_no,
647 //--------------------------------------------------------------------------------------------------
648 function update_prepaid_so_line($line_id, $qty_invoiced)
650 $sql = "UPDATE ".TB_PREF."sales_order_details
651 SET invoiced = invoiced + ".(float)$qty_invoiced."
652 WHERE id=".db_escape($line_id);
654 db_query($sql, "The document detail record could not be updated with invoiced qty");
658 Returns array of all issued invoices to sales order $order_no, optinally up to trans_no==$up_to
660 function get_sales_order_invoices($order_no)
662 $sql = "SELECT trans_no, dt.type as type, tran_date, reference, prep_amount
663 FROM ".TB_PREF."debtor_trans dt
664 LEFT JOIN ".TB_PREF."voided v ON v.type=dt.type AND v.id=dt.trans_no
665 WHERE ISNULL(v.id) AND dt.type=".ST_SALESINVOICE." AND dt.order_=".db_escape($order_no)
666 ." ORDER BY dt.tran_date, dt.reference, dt.trans_no";
668 return db_query($sql, "cannot retrieve sales invoices for sales order");
671 function is_sales_order_started($order_no)
673 $sql = "SELECT count(*) FROM ".TB_PREF."sales_order_details WHERE order_no=".db_escape($order_no)." AND trans_type=".ST_SALESORDER
674 ." AND (invoiced!=0 OR qty_sent!=0)";
676 $result = db_fetch(db_query($sql, "cannot retrieve sales invoices for sales order"));