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 $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)
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) . ")";
41 db_query($sql, "order Cannot be Added");
43 $order->trans_no = array($order_no=>0);
45 if ($loc_notification == 1)
47 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
51 $st_reorder = array();
53 foreach ($order->line_items as $line)
55 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
57 $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
58 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
59 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
60 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
61 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
62 $res = db_query($sql,"a location could not be retreived");
63 $loc = db_fetch($res);
64 if ($loc['email'] != "")
66 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
67 $qoh -= get_demand_qty($line->stock_id, $order->Location);
68 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
69 $qoh -= $line->quantity;
70 if ($qoh < $loc['reorder_level'])
72 $st_ids[] = $line->stock_id;
73 $st_names[] = $line->item_description;
74 $st_num[] = $qoh - $loc['reorder_level'];
75 $st_reorder[] = $loc['reorder_level'];
80 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, trans_type, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
81 $sql .= $order_no . ",".$order->trans_type .
82 ",".db_escape($line->stock_id).", "
83 .db_escape($line->item_description).", $line->price,
85 $line->discount_percent)";
86 db_query($sql, "order Details Cannot be Added");
88 } /* inserted line items into sales order details */
90 add_audit_trail($order->trans_type, $order_no, $order->document_date);
91 references::save($order->trans_type, $order_no, $order->reference);
94 if ($loc_notification == 1 && count($st_ids) > 0)
96 require_once($path_to_root . "/reporting/includes/class.mail.inc");
97 $company = get_company_prefs();
98 $mail = new email($company['coy_name'], $company['email']);
99 $from = $company['coy_name'] . " <" . $company['email'] . ">";
100 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
101 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
103 for ($i = 0; $i < count($st_ids); $i++)
104 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
105 $msg .= "\n" . _("Please reorder") . "\n\n";
106 $msg .= $company['coy_name'];
108 $mail->subject($subject);
110 $ret = $mail->send();
115 //----------------------------------------------------------------------------------------
117 function delete_sales_order($order_no, $trans_type)
121 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no . " AND trans_type=".$trans_type;
122 db_query($sql, "order Header Delete");
124 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no . " AND trans_type=".$trans_type;
125 db_query($sql, "order Detail Delete");
127 add_audit_trail($trans_type, $order_no, Today(), _("Deleted."));
128 commit_transaction();
131 //----------------------------------------------------------------------------------------
132 // Mark changes in sales_order_details
134 function update_sales_order_version($order)
136 foreach ($order as $so_num => $so_ver) {
137 $sql= 'UPDATE '.TB_PREF.'sales_orders SET version=version+1 WHERE order_no='. $so_num.
138 ' AND version='.$so_ver . " AND trans_type=30";
139 db_query($sql, 'Concurrent editing conflict while sales order update');
143 //----------------------------------------------------------------------------------------
145 function update_sales_order($order)
147 global $loc_notification, $path_to_root;
149 $del_date = date2sql($order->due_date);
150 $ord_date = date2sql($order->document_date);
151 $order_no = key($order->trans_no);
152 $version= current($order->trans_no);
156 $sql = "UPDATE ".TB_PREF."sales_orders SET type =".$order->so_type." ,
157 debtor_no = " . db_escape($order->customer_id) . ",
158 branch_code = " . db_escape($order->Branch) . ",
159 customer_ref = ". db_escape($order->cust_ref) .",
160 reference = ". db_escape($order->reference) .",
161 comments = ". db_escape($order->Comments) .",
162 ord_date = " . db_escape($ord_date) . ",
163 order_type = " .db_escape($order->sales_type) . ",
164 ship_via = " . db_escape($order->ship_via) .",
165 deliver_to = " . db_escape($order->deliver_to) . ",
166 delivery_address = " . db_escape($order->delivery_address) . ",
167 contact_phone = " .db_escape($order->phone) . ",
168 contact_email = " .db_escape($order->email) . ",
169 freight_cost = " .db_escape($order->freight_cost) .",
170 from_stk_loc = " .db_escape($order->Location) .",
171 delivery_date = " .db_escape($del_date). ",
172 version = ".($version+1)."
173 WHERE order_no=" . $order_no ."
174 AND trans_type=".$order->trans_type." AND version=".$version;
175 db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
177 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no . " AND trans_type=".$order->trans_type;
179 db_query($sql, "Old order Cannot be Deleted");
181 if ($loc_notification == 1)
183 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
187 $st_reorder = array();
189 foreach ($order->line_items as $line)
191 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
193 $sql = "SELECT ".TB_PREF."loc_stock.*, "
194 .TB_PREF."locations.location_name, "
195 .TB_PREF."locations.email
196 FROM ".TB_PREF."loc_stock, "
198 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
199 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
200 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
201 $res = db_query($sql,"a location could not be retreived");
202 $loc = db_fetch($res);
203 if ($loc['email'] != "")
205 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
206 $qoh -= get_demand_qty($line->stock_id, $order->Location);
207 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
208 $qoh -= $line->quantity;
209 if ($qoh < $loc['reorder_level'])
211 $st_ids[] = $line->stock_id;
212 $st_names[] = $line->item_description;
213 $st_num[] = $qoh - $loc['reorder_level'];
214 $st_reorder[] = $loc['reorder_level'];
218 $sql = "INSERT INTO ".TB_PREF."sales_order_details
219 (order_no, trans_type, stk_code, description, unit_price, quantity,
220 discount_percent, qty_sent)
222 $sql .= $order_no . ",".$order->trans_type.","
223 .db_escape($line->stock_id) . ","
224 .db_escape($line->item_description) . ", "
225 .db_escape($line->price) . ", "
226 .db_escape($line->quantity) . ", "
227 .db_escape($line->discount_percent) . ", "
228 .db_escape($line->qty_done) ." )";
230 db_query($sql, "Old order Cannot be Inserted");
232 } /* inserted line items into sales order details */
234 add_audit_trail($order->trans_type, $order_no, $order->document_date, _("Updated."));
235 references::delete($order->trans_type, $order_no);
236 references::save($order->trans_type, $order_no, $order->reference);
237 commit_transaction();
238 if ($loc_notification == 1 && count($st_ids) > 0)
240 require_once($path_to_root . "/reporting/includes/class.mail.inc");
241 $company = get_company_prefs();
242 $mail = new email($company['coy_name'], $company['email']);
243 $from = $company['coy_name'] . " <" . $company['email'] . ">";
244 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
245 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
247 for ($i = 0; $i < count($st_ids); $i++)
248 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
249 . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
250 . _("Below") . ": " . $st_num[$i] . "\n";
251 $msg .= "\n" . _("Please reorder") . "\n\n";
252 $msg .= $company['coy_name'];
254 $mail->subject($subject);
256 $ret = $mail->send();
260 //----------------------------------------------------------------------------------------
262 function get_sales_order_header($order_no, $trans_type)
264 $sql = "SELECT ".TB_PREF."sales_orders.*, "
265 .TB_PREF."debtors_master.name, "
266 .TB_PREF."debtors_master.curr_code, "
267 .TB_PREF."debtors_master.email AS master_email, "
268 .TB_PREF."locations.location_name, "
269 .TB_PREF."debtors_master.payment_terms, "
270 .TB_PREF."debtors_master.discount, "
271 .TB_PREF."sales_types.sales_type, "
272 .TB_PREF."sales_types.id AS sales_type_id, "
273 .TB_PREF."sales_types.tax_included, "
274 .TB_PREF."shippers.shipper_name, "
275 .TB_PREF."tax_groups.name AS tax_group_name , "
276 .TB_PREF."tax_groups.id AS tax_group_id
277 FROM ".TB_PREF."sales_orders, "
278 .TB_PREF."debtors_master, "
279 .TB_PREF."sales_types, "
280 .TB_PREF."tax_groups, "
281 .TB_PREF."cust_branch, "
282 .TB_PREF."locations, "
284 WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
285 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
286 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
287 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
288 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
289 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
290 AND ".TB_PREF."sales_orders.trans_type = " . $trans_type ."
291 AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
292 $result = db_query($sql, "order Retreival");
294 $num = db_num_rows($result);
297 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
301 return db_fetch($result);
304 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
308 //----------------------------------------------------------------------------------------
310 function get_sales_order_details($order_no, $trans_type) {
311 $sql = "SELECT id, stk_code, unit_price, "
312 .TB_PREF."sales_order_details.description,"
313 .TB_PREF."sales_order_details.quantity,
315 qty_sent as qty_done, "
316 .TB_PREF."stock_master.units,
317 ".TB_PREF."stock_master.material_cost + "
318 .TB_PREF."stock_master.labour_cost + "
319 .TB_PREF."stock_master.overhead_cost AS standard_cost
320 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
321 WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
322 AND order_no =" . $order_no . " AND trans_type = " . $trans_type . " ORDER BY id";
324 return db_query($sql, "Retreive order Line Items");
326 //----------------------------------------------------------------------------------------
328 function read_sales_order($order_no, &$order, $trans_type)
330 $myrow = get_sales_order_header($order_no, $trans_type);
332 $order->trans_type = $myrow['trans_type'];
333 $order->so_type = $myrow["type"];
334 $order->trans_no = array($order_no=> $myrow["version"]);
336 $order->set_customer($myrow["debtor_no"], $myrow["name"],
337 $myrow["curr_code"], $myrow["discount"]);
339 $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
340 $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
342 $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"],
343 $myrow["tax_included"], 0); // no default price calculations on edit
345 $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
347 $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
348 $myrow["delivery_address"], $myrow["freight_cost"]);
350 $order->cust_ref = $myrow["customer_ref"];
351 $order->sales_type =$myrow["order_type"];
352 $order->reference = $myrow["reference"];
353 $order->Comments = $myrow["comments"];
354 $order->due_date = sql2date($myrow["delivery_date"]);
355 $order->document_date = sql2date($myrow["ord_date"]);
357 $result = get_sales_order_details($order_no, $order->trans_type);
358 if (db_num_rows($result) > 0)
361 while ($myrow = db_fetch($result))
363 $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
364 $myrow["unit_price"], $myrow["discount_percent"],
365 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
373 //----------------------------------------------------------------------------------------
375 function sales_order_has_deliveries($order_no)
377 $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
378 "sales_order_details WHERE order_no=$order_no AND trans_type=30";
380 $result = db_query($sql, "could not query for sales order usage");
382 $row = db_fetch_row($result);
384 return ($row[0] > 0);
387 //----------------------------------------------------------------------------------------
389 function close_sales_order($order_no)
391 // set the quantity of each item to the already sent quantity. this will mark item as closed.
392 $sql = "UPDATE ".TB_PREF."sales_order_details
393 SET quantity = qty_sent,
395 WHERE order_no = $order_no AND trans_type=30";
397 db_query($sql, "The sales order detail record could not be updated");
400 //---------------------------------------------------------------------------------------------------------------
402 function get_invoice_duedate($debtorno, $invdate)
404 if (!is_date($invdate))
406 return new_doc_date();
408 $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
409 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
410 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
412 $result = db_query($sql,"The customer details could not be retrieved");
413 $myrow = db_fetch($result);
415 if (db_num_rows($result) == 0)
417 if ($myrow['day_in_following_month'] > 0)
418 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
420 $duedate = add_days($invdate, $myrow['days_before_due']);
424 function get_customer_to_order($customer_id) {
426 // Now check to ensure this account is not on hold */
427 $sql = "SELECT ".TB_PREF."debtors_master.name, "
428 .TB_PREF."debtors_master.address, "
429 .TB_PREF."credit_status.dissallow_invoices, "
430 .TB_PREF."debtors_master.sales_type AS salestype, "
431 .TB_PREF."debtors_master.dimension_id, "
432 .TB_PREF."debtors_master.dimension2_id, "
433 .TB_PREF."sales_types.sales_type, "
434 .TB_PREF."sales_types.tax_included, "
435 .TB_PREF."sales_types.factor, "
436 .TB_PREF."debtors_master.curr_code, "
437 .TB_PREF."debtors_master.discount,"
438 .TB_PREF."debtors_master.pymt_discount
439 FROM ".TB_PREF."debtors_master, "
440 .TB_PREF."credit_status, "
441 .TB_PREF."sales_types
442 WHERE ".TB_PREF."debtors_master.sales_type="
443 .TB_PREF."sales_types.id
444 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
445 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
447 $result =db_query($sql,"Customer Record Retreive");
448 return db_fetch($result);
451 function get_branch_to_order($customer_id, $branch_id) {
453 // 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
454 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
455 .TB_PREF."cust_branch.br_address, "
456 .TB_PREF."cust_branch.br_post_address, "
457 .TB_PREF."cust_branch.phone, "
458 .TB_PREF."cust_branch.email,
459 default_location, location_name, default_ship_via, "
460 .TB_PREF."tax_groups.name AS tax_group_name, "
461 .TB_PREF."tax_groups.id AS tax_group_id
462 FROM ".TB_PREF."cust_branch, "
463 .TB_PREF."tax_groups, "
465 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
466 AND ".TB_PREF."locations.loc_code=default_location
467 AND ".TB_PREF."cust_branch.branch_code='" . $branch_id . "'
468 AND ".TB_PREF."cust_branch.debtor_no = '" . $customer_id . "'";
470 return db_query($sql,"Customer Branch Record Retreive");