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;
19 $del_date = date2sql($order->due_date);
20 $order_type = 0; // this is default on new order
21 $sql = "INSERT INTO ".TB_PREF."sales_orders (type, debtor_no, branch_code, customer_ref, comments, ord_date,
22 order_type, ship_via, deliver_to, delivery_address, contact_phone,
23 contact_email, freight_cost, from_stk_loc, delivery_date)
24 VALUES (" .db_escape($order_type) . "," . db_escape($order->customer_id) .
25 ", " . db_escape($order->Branch) . ", ".
26 db_escape($order->cust_ref) .",".
27 db_escape($order->Comments) .",'" .
28 date2sql($order->document_date) . "', " .
29 db_escape($order->sales_type) . ", " .
30 db_escape($order->ship_via)."," .
31 db_escape($order->deliver_to) . "," .
32 db_escape($order->delivery_address) . ", " .
33 db_escape($order->phone) . ", " .
34 db_escape($order->email) . ", " .
35 db_escape($order->freight_cost) .", " .
36 db_escape($order->Location) .", " .
37 db_escape($del_date) . ")";
39 db_query($sql, "order Cannot be Added");
41 $order_no = db_insert_id();
42 $order->trans_no = array($order_no=>0);
44 if ($loc_notification == 1)
46 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
50 $st_reorder = array();
52 foreach ($order->line_items as $line)
54 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
56 $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
57 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
58 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
59 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
60 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
61 $res = db_query($sql,"a location could not be retreived");
62 $loc = db_fetch($res);
63 if ($loc['email'] != "")
65 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
66 $qoh -= get_demand_qty($line->stock_id, $order->Location);
67 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
68 $qoh -= $line->quantity;
69 if ($qoh < $loc['reorder_level'])
71 $st_ids[] = $line->stock_id;
72 $st_names[] = $line->item_description;
73 $st_num[] = $qoh - $loc['reorder_level'];
74 $st_reorder[] = $loc['reorder_level'];
79 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
81 ",".db_escape($line->stock_id).", "
82 .db_escape($line->item_description).", $line->price,
84 $line->discount_percent)";
85 db_query($sql, "order Details Cannot be Added");
87 } /* inserted line items into sales order details */
91 if ($loc_notification == 1 && count($st_ids) > 0)
93 require_once($path_to_root . "/reporting/includes/class.mail.inc");
94 $company = get_company_prefs();
95 $mail = new email($company['coy_name'], $company['email']);
96 $from = $company['coy_name'] . " <" . $company['email'] . ">";
97 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
98 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
100 for ($i = 0; $i < count($st_ids); $i++)
101 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
102 $msg .= "\n" . _("Please reorder") . "\n\n";
103 $msg .= $company['coy_name'];
105 $mail->subject($subject);
107 $ret = $mail->send();
112 //----------------------------------------------------------------------------------------
114 function delete_sales_order($order_no)
118 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
119 db_query($sql, "order Header Delete");
121 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
122 db_query($sql, "order Detail Delete");
124 commit_transaction();
127 //----------------------------------------------------------------------------------------
128 // Mark changes in sales_order_details
130 function update_sales_order_version($order)
132 foreach ($order as $so_num => $so_ver) {
133 $sql= 'UPDATE '.TB_PREF.'sales_orders SET version=version+1 WHERE order_no='. $so_num.
134 ' AND version='.$so_ver;
135 db_query($sql, 'Concurrent editing conflict while sales order update');
139 //----------------------------------------------------------------------------------------
141 function update_sales_order($order)
143 global $loc_notification, $path_to_root;
145 $del_date = date2sql($order->due_date);
146 $ord_date = date2sql($order->document_date);
147 $order_no = key($order->trans_no);
148 $version= current($order->trans_no);
152 $sql = "UPDATE ".TB_PREF."sales_orders SET type =".$order->so_type." ,
153 debtor_no = " . db_escape($order->customer_id) . ",
154 branch_code = " . db_escape($order->Branch) . ",
155 customer_ref = ". db_escape($order->cust_ref) .",
156 comments = ". db_escape($order->Comments) .",
157 ord_date = " . db_escape($ord_date) . ",
158 order_type = " .db_escape($order->sales_type) . ",
159 ship_via = " . db_escape($order->ship_via) .",
160 deliver_to = " . db_escape($order->deliver_to) . ",
161 delivery_address = " . db_escape($order->delivery_address) . ",
162 contact_phone = " .db_escape($order->phone) . ",
163 contact_email = " .db_escape($order->email) . ",
164 freight_cost = " .db_escape($order->freight_cost) .",
165 from_stk_loc = " .db_escape($order->Location) .",
166 delivery_date = " .db_escape($del_date). ",
167 version = ".($version+1)."
168 WHERE order_no=" . $order_no ."
169 AND version=".$version;
170 db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
172 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
174 db_query($sql, "Old order Cannot be Deleted");
176 if ($loc_notification == 1)
178 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
182 $st_reorder = array();
184 foreach ($order->line_items as $line)
186 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
188 $sql = "SELECT ".TB_PREF."loc_stock.*, "
189 .TB_PREF."locations.location_name, "
190 .TB_PREF."locations.email
191 FROM ".TB_PREF."loc_stock, "
193 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
194 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
195 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
196 $res = db_query($sql,"a location could not be retreived");
197 $loc = db_fetch($res);
198 if ($loc['email'] != "")
200 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
201 $qoh -= get_demand_qty($line->stock_id, $order->Location);
202 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
203 $qoh -= $line->quantity;
204 if ($qoh < $loc['reorder_level'])
206 $st_ids[] = $line->stock_id;
207 $st_names[] = $line->item_description;
208 $st_num[] = $qoh - $loc['reorder_level'];
209 $st_reorder[] = $loc['reorder_level'];
213 $sql = "INSERT INTO ".TB_PREF."sales_order_details
214 (order_no, stk_code, description, unit_price, quantity,
215 discount_percent, qty_sent)
217 $sql .= $order_no . ","
218 .db_escape($line->stock_id) . ","
219 .db_escape($line->item_description) . ", "
220 .db_escape($line->price) . ", "
221 .db_escape($line->quantity) . ", "
222 .db_escape($line->discount_percent) . ", "
223 .db_escape($line->qty_done) ." )";
225 db_query($sql, "Old order Cannot be Inserted");
227 } /* inserted line items into sales order details */
229 commit_transaction();
230 if ($loc_notification == 1 && count($st_ids) > 0)
232 require_once($path_to_root . "/reporting/includes/class.mail.inc");
233 $company = get_company_prefs();
234 $mail = new email($company['coy_name'], $company['email']);
235 $from = $company['coy_name'] . " <" . $company['email'] . ">";
236 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
237 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
239 for ($i = 0; $i < count($st_ids); $i++)
240 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
241 . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
242 . _("Below") . ": " . $st_num[$i] . "\n";
243 $msg .= "\n" . _("Please reorder") . "\n\n";
244 $msg .= $company['coy_name'];
246 $mail->subject($subject);
248 $ret = $mail->send();
252 //----------------------------------------------------------------------------------------
254 function get_sales_order_header($order_no)
256 $sql = "SELECT ".TB_PREF."sales_orders.*, "
257 .TB_PREF."debtors_master.name, "
258 .TB_PREF."debtors_master.curr_code, "
259 .TB_PREF."locations.location_name, "
260 .TB_PREF."debtors_master.payment_terms, "
261 .TB_PREF."debtors_master.discount, "
262 .TB_PREF."sales_types.sales_type, "
263 .TB_PREF."sales_types.id AS sales_type_id, "
264 .TB_PREF."sales_types.tax_included, "
265 .TB_PREF."shippers.shipper_name, "
266 .TB_PREF."tax_groups.name AS tax_group_name , "
267 .TB_PREF."tax_groups.id AS tax_group_id
268 FROM ".TB_PREF."sales_orders, "
269 .TB_PREF."debtors_master, "
270 .TB_PREF."sales_types, "
271 .TB_PREF."tax_groups, "
272 .TB_PREF."cust_branch, "
273 .TB_PREF."locations, "
275 WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
276 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
277 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
278 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
279 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
280 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
281 AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
282 $result = db_query($sql, "order Retreival");
284 $num = db_num_rows($result);
287 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
291 return db_fetch($result);
294 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
298 //----------------------------------------------------------------------------------------
300 function get_sales_order_details($order_no) {
301 $sql = "SELECT id, stk_code, unit_price, "
302 .TB_PREF."sales_order_details.description,"
303 .TB_PREF."sales_order_details.quantity,
305 qty_sent as qty_done, "
306 .TB_PREF."stock_master.units,
307 ".TB_PREF."stock_master.material_cost + "
308 .TB_PREF."stock_master.labour_cost + "
309 .TB_PREF."stock_master.overhead_cost AS standard_cost
310 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
311 WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
312 AND order_no =" . $order_no . " ORDER BY id";
314 return db_query($sql, "Retreive order Line Items");
316 //----------------------------------------------------------------------------------------
318 function read_sales_order($order_no, &$order)
320 $myrow = get_sales_order_header($order_no);
322 $order->trans_type = 30;
323 $order->so_type = $myrow["type"];
324 $order->trans_no = array($order_no=> $myrow["version"]);
326 $order->set_customer($myrow["debtor_no"], $myrow["name"],
327 $myrow["curr_code"], $myrow["discount"]);
329 $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
330 $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
332 $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"],
333 $myrow["tax_included"], 0); // no default price calculations on edit
335 $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
337 $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
338 $myrow["delivery_address"], $myrow["freight_cost"]);
340 $order->cust_ref = $myrow["customer_ref"];
341 $order->sales_type =$myrow["order_type"];
342 $order->Comments = $myrow["comments"];
343 $order->due_date = sql2date($myrow["delivery_date"]);
344 $order->document_date = sql2date($myrow["ord_date"]);
346 $result = get_sales_order_details($order_no);
347 if (db_num_rows($result) > 0)
350 while ($myrow = db_fetch($result))
352 $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
353 $myrow["unit_price"], $myrow["discount_percent"],
354 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
362 //----------------------------------------------------------------------------------------
364 function sales_order_has_deliveries($order_no)
366 $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
367 "sales_order_details WHERE order_no=$order_no";
369 $result = db_query($sql, "could not query for sales order usage");
371 $row = db_fetch_row($result);
373 return ($row[0] > 0);
376 //----------------------------------------------------------------------------------------
378 function close_sales_order($order_no)
380 // set the quantity of each item to the already sent quantity. this will mark item as closed.
381 $sql = "UPDATE ".TB_PREF."sales_order_details
382 SET quantity = qty_sent,
384 WHERE order_no = $order_no";
386 db_query($sql, "The sales order detail record could not be updated");
389 //---------------------------------------------------------------------------------------------------------------
391 function get_invoice_duedate($debtorno, $invdate)
393 if (!is_date($invdate))
397 $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
398 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
399 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
401 $result = db_query($sql,"The customer details could not be retrieved");
402 $myrow = db_fetch($result);
404 if (db_num_rows($result) == 0)
406 if ($myrow['day_in_following_month'] > 0)
407 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
409 $duedate = add_days($invdate, $myrow['days_before_due']);
413 function get_customer_to_order($customer_id) {
415 // Now check to ensure this account is not on hold */
416 $sql = "SELECT ".TB_PREF."debtors_master.name, "
417 .TB_PREF."debtors_master.address, "
418 .TB_PREF."credit_status.dissallow_invoices, "
419 .TB_PREF."debtors_master.sales_type AS salestype, "
420 .TB_PREF."debtors_master.dimension_id, "
421 .TB_PREF."debtors_master.dimension2_id, "
422 .TB_PREF."sales_types.sales_type, "
423 .TB_PREF."sales_types.tax_included, "
424 .TB_PREF."sales_types.factor, "
425 .TB_PREF."debtors_master.curr_code, "
426 .TB_PREF."debtors_master.discount,"
427 .TB_PREF."debtors_master.pymt_discount
428 FROM ".TB_PREF."debtors_master, "
429 .TB_PREF."credit_status, "
430 .TB_PREF."sales_types
431 WHERE ".TB_PREF."debtors_master.sales_type="
432 .TB_PREF."sales_types.id
433 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
434 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
436 $result =db_query($sql,"Customer Record Retreive");
437 return db_fetch($result);
440 function get_branch_to_order($customer_id, $branch_id) {
442 // 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
443 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
444 .TB_PREF."cust_branch.br_address, "
445 .TB_PREF."cust_branch.br_post_address, "
446 .TB_PREF."cust_branch.phone, "
447 .TB_PREF."cust_branch.email,
448 default_location, location_name, default_ship_via, "
449 .TB_PREF."tax_groups.name AS tax_group_name, "
450 .TB_PREF."tax_groups.id AS tax_group_id
451 FROM ".TB_PREF."cust_branch, "
452 .TB_PREF."tax_groups, "
454 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
455 AND ".TB_PREF."locations.loc_code=default_location
456 AND ".TB_PREF."cust_branch.branch_code='" . $branch_id . "'
457 AND ".TB_PREF."cust_branch.debtor_no = '" . $customer_id . "'";
459 return db_query($sql,"Customer Branch Record Retreive");