3 //----------------------------------------------------------------------------------------
4 function get_demand_qty($stockid, $location)
6 $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS QtyDemand
7 FROM ".TB_PREF."sales_order_details,
8 ".TB_PREF."sales_orders
9 WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND
10 ".TB_PREF."sales_orders.from_stk_loc ='$location' AND
11 ".TB_PREF."sales_order_details.stk_code = '$stockid'";
13 $TransResult = db_query($sql,"No transactions were returned");
14 $DemandRow = db_fetch($TransResult);
15 return $DemandRow['QtyDemand'];
18 function get_demand_asm_qty($stockid, $location)
20 $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
22 FROM ".TB_PREF."sales_order_details,
23 ".TB_PREF."sales_orders,
25 ".TB_PREF."stock_master
26 WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
27 ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND
28 ".TB_PREF."sales_orders.from_stk_loc='$location' AND
29 ".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
30 ".TB_PREF."bom.component='$stockid' AND
31 ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
32 ".TB_PREF."stock_master.mb_flag='A'";
34 $TransResult = db_query($sql,"No transactions were returned");
35 if (db_num_rows($TransResult)==1)
37 $DemandRow = db_fetch_row($TransResult);
38 $DemandQty = $DemandRow[0];
46 function add_sales_order(&$order)
48 global $loc_notification, $path_to_root;
52 $del_date = date2sql($order->due_date);
53 $order_type = 0; // this is default on new order
54 $sql = "INSERT INTO ".TB_PREF."sales_orders (type, debtor_no, branch_code, customer_ref, comments, ord_date,
55 order_type, ship_via, deliver_to, delivery_address, contact_phone,
56 contact_email, freight_cost, from_stk_loc, delivery_date)
57 VALUES (" .db_escape($order_type) . "," . db_escape($order->customer_id) .
58 ", " . db_escape($order->Branch) . ", ".
59 db_escape($order->cust_ref) .",".
60 db_escape($order->Comments) .",'" .
61 date2sql($order->document_date) . "', " .
62 db_escape($order->sales_type) . ", " .
63 db_escape($order->ship_via)."," .
64 db_escape($order->deliver_to) . "," .
65 db_escape($order->delivery_address) . ", " .
66 db_escape($order->phone) . ", " .
67 db_escape($order->email) . ", " .
68 db_escape($order->freight_cost) .", " .
69 db_escape($order->Location) .", " .
70 db_escape($del_date) . ")";
72 db_query($sql, "order Cannot be Added");
74 $order_no = db_insert_id();
75 $order->trans_no = array($order_no=>0);
77 if ($loc_notification == 1)
79 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
83 $st_reorder = array();
85 foreach ($order->line_items as $line)
87 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
89 $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
90 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
91 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
92 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
93 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
94 $res = db_query($sql,"a location could not be retreived");
95 $loc = db_fetch($res);
96 if ($loc['email'] != "")
98 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
99 $qoh -= get_demand_qty($line->stock_id, $order->Location);
100 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
101 $qoh -= $line->quantity;
102 if ($qoh < $loc['reorder_level'])
104 $st_ids[] = $line->stock_id;
105 $st_names[] = $line->item_description;
106 $st_num[] = $qoh - $loc['reorder_level'];
107 $st_reorder[] = $loc['reorder_level'];
112 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
114 ",".db_escape($line->stock_id).", "
115 .db_escape($line->item_description).", $line->price,
117 $line->discount_percent)";
118 db_query($sql, "order Details Cannot be Added");
120 } /* inserted line items into sales order details */
122 commit_transaction();
124 if ($loc_notification == 1 && count($st_ids) > 0)
126 require_once($path_to_root . "/reporting/includes/class.mail.inc");
127 $company = get_company_prefs();
128 $mail = new email($company['coy_name'], $company['email']);
129 $from = $company['coy_name'] . " <" . $company['email'] . ">";
130 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
131 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
133 for ($i = 0; $i < count($st_ids); $i++)
134 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
135 $msg .= "\n" . _("Please reorder") . "\n\n";
136 $msg .= $company['coy_name'];
138 $mail->subject($subject);
140 $ret = $mail->send();
145 //----------------------------------------------------------------------------------------
147 function delete_sales_order($order_no)
151 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
152 db_query($sql, "order Header Delete");
154 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
155 db_query($sql, "order Detail Delete");
157 commit_transaction();
160 //----------------------------------------------------------------------------------------
161 // Mark changes in sales_order_details
163 function update_sales_order_version($order)
165 foreach ($order as $so_num => $so_ver) {
166 $sql= 'UPDATE '.TB_PREF.'sales_orders SET version=version+1 WHERE order_no='. $so_num.
167 ' AND version='.$so_ver;
168 db_query($sql, 'Concurrent editing conflict while sales order update');
172 //----------------------------------------------------------------------------------------
174 function update_sales_order($order)
176 global $loc_notification, $path_to_root;
178 $del_date = date2sql($order->due_date);
179 $ord_date = date2sql($order->document_date);
180 $order_no = key($order->trans_no);
181 $version= current($order->trans_no);
185 $sql = "UPDATE ".TB_PREF."sales_orders SET type =".$order->so_type." ,
186 debtor_no = " . db_escape($order->customer_id) . ",
187 branch_code = " . db_escape($order->Branch) . ",
188 customer_ref = ". db_escape($order->cust_ref) .",
189 comments = ". db_escape($order->Comments) .",
190 ord_date = " . db_escape($ord_date) . ",
191 order_type = " .db_escape($order->sales_type) . ",
192 ship_via = " . db_escape($order->ship_via) .",
193 deliver_to = " . db_escape($order->deliver_to) . ",
194 delivery_address = " . db_escape($order->delivery_address) . ",
195 contact_phone = " .db_escape($order->phone) . ",
196 contact_email = " .db_escape($order->email) . ",
197 freight_cost = " .db_escape($order->freight_cost) .",
198 from_stk_loc = " .db_escape($order->Location) .",
199 delivery_date = " .db_escape($del_date). ",
200 version = ".($version+1)."
201 WHERE order_no=" . $order_no ."
202 AND version=".$version;
203 db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
205 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
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 = '" . $line->stock_id . "'
228 AND ".TB_PREF."loc_stock.loc_code = '" . $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 $sql = "INSERT INTO ".TB_PREF."sales_order_details
247 (order_no, stk_code, description, unit_price, quantity,
248 discount_percent, qty_sent)
250 $sql .= $order_no . ","
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 db_query($sql, "Old order Cannot be Inserted");
260 } /* inserted line items into sales order details */
262 commit_transaction();
263 if ($loc_notification == 1 && count($st_ids) > 0)
265 require_once($path_to_root . "/reporting/includes/class.mail.inc");
266 $company = get_company_prefs();
267 $mail = new email($company['coy_name'], $company['email']);
268 $from = $company['coy_name'] . " <" . $company['email'] . ">";
269 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
270 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
272 for ($i = 0; $i < count($st_ids); $i++)
273 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
274 . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
275 . _("Below") . ": " . $st_num[$i] . "\n";
276 $msg .= "\n" . _("Please reorder") . "\n\n";
277 $msg .= $company['coy_name'];
279 $mail->subject($subject);
281 $ret = $mail->send();
285 //----------------------------------------------------------------------------------------
287 function get_sales_order_header($order_no)
289 $sql = "SELECT ".TB_PREF."sales_orders.*, "
290 .TB_PREF."debtors_master.name, "
291 .TB_PREF."debtors_master.curr_code, "
292 .TB_PREF."locations.location_name, "
293 .TB_PREF."debtors_master.payment_terms, "
294 .TB_PREF."debtors_master.discount, "
295 .TB_PREF."sales_types.sales_type, "
296 .TB_PREF."sales_types.id AS sales_type_id, "
297 .TB_PREF."sales_types.tax_included, "
298 .TB_PREF."shippers.shipper_name, "
299 .TB_PREF."tax_groups.name AS tax_group_name , "
300 .TB_PREF."tax_groups.id AS tax_group_id
301 FROM ".TB_PREF."sales_orders, "
302 .TB_PREF."debtors_master, "
303 .TB_PREF."sales_types, "
304 .TB_PREF."tax_groups, "
305 .TB_PREF."cust_branch, "
306 .TB_PREF."locations, "
308 WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
309 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
310 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
311 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
312 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
313 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
314 AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
315 $result = db_query($sql, "order Retreival");
317 $num = db_num_rows($result);
320 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
324 return db_fetch($result);
327 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
331 //----------------------------------------------------------------------------------------
333 function get_sales_order_details($order_no) {
334 $sql = "SELECT id, stk_code, unit_price, "
335 .TB_PREF."sales_order_details.description,"
336 .TB_PREF."sales_order_details.quantity,
338 qty_sent as qty_done, "
339 .TB_PREF."stock_master.units,
340 ".TB_PREF."stock_master.material_cost + "
341 .TB_PREF."stock_master.labour_cost + "
342 .TB_PREF."stock_master.overhead_cost AS standard_cost
343 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
344 WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
345 AND order_no =" . $order_no . " ORDER BY id";
347 return db_query($sql, "Retreive order Line Items");
349 //----------------------------------------------------------------------------------------
351 function read_sales_order($order_no, &$order)
353 $myrow = get_sales_order_header($order_no);
355 $order->trans_type = 30;
356 $order->so_type = $myrow["type"];
357 $order->trans_no = array($order_no=> $myrow["version"]);
359 $order->set_customer($myrow["debtor_no"], $myrow["name"],
360 $myrow["curr_code"], $myrow["discount"]);
362 $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
363 $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
365 $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"],
366 $myrow["tax_included"], 0); // no default price calculations on edit
368 $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
370 $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
371 $myrow["delivery_address"], $myrow["freight_cost"]);
373 $order->cust_ref = $myrow["customer_ref"];
374 $order->sales_type =$myrow["order_type"];
375 $order->Comments = $myrow["comments"];
376 $order->due_date = sql2date($myrow["delivery_date"]);
377 $order->document_date = sql2date($myrow["ord_date"]);
379 $result = get_sales_order_details($order_no);
380 if (db_num_rows($result) > 0)
383 while ($myrow = db_fetch($result))
385 $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
386 $myrow["unit_price"], $myrow["discount_percent"],
387 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
395 //----------------------------------------------------------------------------------------
397 function sales_order_has_deliveries($order_no)
399 $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
400 "sales_order_details WHERE order_no=$order_no";
402 $result = db_query($sql, "could not query for sales order usage");
404 $row = db_fetch_row($result);
406 return ($row[0] > 0);
409 //----------------------------------------------------------------------------------------
411 function close_sales_order($order_no)
413 // set the quantity of each item to the already sent quantity. this will mark item as closed.
414 $sql = "UPDATE ".TB_PREF."sales_order_details
415 SET quantity = qty_sent,
417 WHERE order_no = $order_no";
419 db_query($sql, "The sales order detail record could not be updated");
422 //---------------------------------------------------------------------------------------------------------------
424 function get_invoice_duedate($debtorno, $invdate)
426 if (!is_date($invdate))
430 $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
431 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
432 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
434 $result = db_query($sql,"The customer details could not be retrieved");
435 $myrow = db_fetch($result);
437 if (db_num_rows($result) == 0)
439 if ($myrow['day_in_following_month'] > 0)
440 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
442 $duedate = add_days($invdate, $myrow['days_before_due']);
446 function get_customer_to_order($customer_id) {
448 // Now check to ensure this account is not on hold */
449 $sql = "SELECT ".TB_PREF."debtors_master.name, "
450 .TB_PREF."debtors_master.address, "
451 .TB_PREF."credit_status.dissallow_invoices, "
452 .TB_PREF."debtors_master.sales_type AS salestype, "
453 .TB_PREF."debtors_master.dimension_id, "
454 .TB_PREF."debtors_master.dimension2_id, "
455 .TB_PREF."sales_types.sales_type, "
456 .TB_PREF."sales_types.tax_included, "
457 .TB_PREF."sales_types.factor, "
458 .TB_PREF."debtors_master.curr_code, "
459 .TB_PREF."debtors_master.discount,"
460 .TB_PREF."debtors_master.pymt_discount
461 FROM ".TB_PREF."debtors_master, "
462 .TB_PREF."credit_status, "
463 .TB_PREF."sales_types
464 WHERE ".TB_PREF."debtors_master.sales_type="
465 .TB_PREF."sales_types.id
466 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
467 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
469 $result =db_query($sql,"Customer Record Retreive");
470 return db_fetch($result);
473 function get_branch_to_order($customer_id, $branch_id) {
475 // 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
476 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
477 .TB_PREF."cust_branch.br_address, "
478 .TB_PREF."cust_branch.br_post_address, "
479 .TB_PREF."cust_branch.phone, "
480 .TB_PREF."cust_branch.email,
481 default_location, location_name, default_ship_via, "
482 .TB_PREF."tax_groups.name AS tax_group_name, "
483 .TB_PREF."tax_groups.id AS tax_group_id
484 FROM ".TB_PREF."cust_branch, "
485 .TB_PREF."tax_groups, "
487 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
488 AND ".TB_PREF."locations.loc_code=default_location
489 AND ".TB_PREF."cust_branch.branch_code='" . $branch_id . "'
490 AND ".TB_PREF."cust_branch.debtor_no = '" . $customer_id . "'";
492 return db_query($sql,"Customer Branch Record Retreive");