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."debtors_master.email AS master_email, "
260 .TB_PREF."locations.location_name, "
261 .TB_PREF."debtors_master.payment_terms, "
262 .TB_PREF."debtors_master.discount, "
263 .TB_PREF."sales_types.sales_type, "
264 .TB_PREF."sales_types.id AS sales_type_id, "
265 .TB_PREF."sales_types.tax_included, "
266 .TB_PREF."shippers.shipper_name, "
267 .TB_PREF."tax_groups.name AS tax_group_name , "
268 .TB_PREF."tax_groups.id AS tax_group_id
269 FROM ".TB_PREF."sales_orders, "
270 .TB_PREF."debtors_master, "
271 .TB_PREF."sales_types, "
272 .TB_PREF."tax_groups, "
273 .TB_PREF."cust_branch, "
274 .TB_PREF."locations, "
276 WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
277 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
278 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
279 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
280 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
281 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
282 AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
283 $result = db_query($sql, "order Retreival");
285 $num = db_num_rows($result);
288 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
292 return db_fetch($result);
295 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
299 //----------------------------------------------------------------------------------------
301 function get_sales_order_details($order_no) {
302 $sql = "SELECT id, stk_code, unit_price, "
303 .TB_PREF."sales_order_details.description,"
304 .TB_PREF."sales_order_details.quantity,
306 qty_sent as qty_done, "
307 .TB_PREF."stock_master.units,
308 ".TB_PREF."stock_master.material_cost + "
309 .TB_PREF."stock_master.labour_cost + "
310 .TB_PREF."stock_master.overhead_cost AS standard_cost
311 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
312 WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
313 AND order_no =" . $order_no . " ORDER BY id";
315 return db_query($sql, "Retreive order Line Items");
317 //----------------------------------------------------------------------------------------
319 function read_sales_order($order_no, &$order)
321 $myrow = get_sales_order_header($order_no);
323 $order->trans_type = 30;
324 $order->so_type = $myrow["type"];
325 $order->trans_no = array($order_no=> $myrow["version"]);
327 $order->set_customer($myrow["debtor_no"], $myrow["name"],
328 $myrow["curr_code"], $myrow["discount"]);
330 $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
331 $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
333 $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"],
334 $myrow["tax_included"], 0); // no default price calculations on edit
336 $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
338 $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
339 $myrow["delivery_address"], $myrow["freight_cost"]);
341 $order->cust_ref = $myrow["customer_ref"];
342 $order->sales_type =$myrow["order_type"];
343 $order->Comments = $myrow["comments"];
344 $order->due_date = sql2date($myrow["delivery_date"]);
345 $order->document_date = sql2date($myrow["ord_date"]);
347 $result = get_sales_order_details($order_no);
348 if (db_num_rows($result) > 0)
351 while ($myrow = db_fetch($result))
353 $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
354 $myrow["unit_price"], $myrow["discount_percent"],
355 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
363 //----------------------------------------------------------------------------------------
365 function sales_order_has_deliveries($order_no)
367 $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
368 "sales_order_details WHERE order_no=$order_no";
370 $result = db_query($sql, "could not query for sales order usage");
372 $row = db_fetch_row($result);
374 return ($row[0] > 0);
377 //----------------------------------------------------------------------------------------
379 function close_sales_order($order_no)
381 // set the quantity of each item to the already sent quantity. this will mark item as closed.
382 $sql = "UPDATE ".TB_PREF."sales_order_details
383 SET quantity = qty_sent,
385 WHERE order_no = $order_no";
387 db_query($sql, "The sales order detail record could not be updated");
390 //---------------------------------------------------------------------------------------------------------------
392 function get_invoice_duedate($debtorno, $invdate)
394 if (!is_date($invdate))
398 $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
399 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
400 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
402 $result = db_query($sql,"The customer details could not be retrieved");
403 $myrow = db_fetch($result);
405 if (db_num_rows($result) == 0)
407 if ($myrow['day_in_following_month'] > 0)
408 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
410 $duedate = add_days($invdate, $myrow['days_before_due']);
414 function get_customer_to_order($customer_id) {
416 // Now check to ensure this account is not on hold */
417 $sql = "SELECT ".TB_PREF."debtors_master.name, "
418 .TB_PREF."debtors_master.address, "
419 .TB_PREF."credit_status.dissallow_invoices, "
420 .TB_PREF."debtors_master.sales_type AS salestype, "
421 .TB_PREF."debtors_master.dimension_id, "
422 .TB_PREF."debtors_master.dimension2_id, "
423 .TB_PREF."sales_types.sales_type, "
424 .TB_PREF."sales_types.tax_included, "
425 .TB_PREF."sales_types.factor, "
426 .TB_PREF."debtors_master.curr_code, "
427 .TB_PREF."debtors_master.discount,"
428 .TB_PREF."debtors_master.pymt_discount
429 FROM ".TB_PREF."debtors_master, "
430 .TB_PREF."credit_status, "
431 .TB_PREF."sales_types
432 WHERE ".TB_PREF."debtors_master.sales_type="
433 .TB_PREF."sales_types.id
434 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
435 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
437 $result =db_query($sql,"Customer Record Retreive");
438 return db_fetch($result);
441 function get_branch_to_order($customer_id, $branch_id) {
443 // 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
444 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
445 .TB_PREF."cust_branch.br_address, "
446 .TB_PREF."cust_branch.br_post_address, "
447 .TB_PREF."cust_branch.phone, "
448 .TB_PREF."cust_branch.email,
449 default_location, location_name, default_ship_via, "
450 .TB_PREF."tax_groups.name AS tax_group_name, "
451 .TB_PREF."tax_groups.id AS tax_group_id
452 FROM ".TB_PREF."cust_branch, "
453 .TB_PREF."tax_groups, "
455 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
456 AND ".TB_PREF."locations.loc_code=default_location
457 AND ".TB_PREF."cust_branch.branch_code='" . $branch_id . "'
458 AND ".TB_PREF."cust_branch.debtor_no = '" . $customer_id . "'";
460 return db_query($sql,"Customer Branch Record Retreive");