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 ('" . $order_type . "', '" . $order->customer_id . "', '" . $order->Branch . "', '".
58 $order->cust_ref ."','". db_escape($order->Comments) ."','" .
59 date2sql($order->document_date) . "', '" .
60 $order->sales_type . "', " .
61 $_POST['ship_via'] .",'" . $order->deliver_to . "', '" .
62 $order->delivery_address . "', '" .
63 $order->phone . "', '" . $order->email . "', " .
64 $order->freight_cost .", '" . $order->Location ."', '" .
67 db_query($sql, "order Cannot be Added");
69 $order_no = db_insert_id();
70 $order->trans_no = array($order_no=>0);
72 if ($loc_notification == 1)
74 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
78 $st_reorder = array();
80 foreach ($order->line_items as $line)
82 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
84 $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
85 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
86 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
87 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
88 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
89 $res = db_query($sql,"a location could not be retreived");
90 $loc = db_fetch($res);
91 if ($loc['email'] != "")
93 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
94 $qoh -= get_demand_qty($line->stock_id, $order->Location);
95 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
96 $qoh -= $line->quantity;
97 if ($qoh < $loc['reorder_level'])
99 $st_ids[] = $line->stock_id;
100 $st_names[] = $line->item_description;
101 $st_num[] = $qoh - $loc['reorder_level'];
102 $st_reorder[] = $loc['reorder_level'];
107 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
109 ",'$line->stock_id', '$line->item_description', $line->price,
111 $line->discount_percent)";
112 db_query($sql, "order Details Cannot be Added");
114 } /* inserted line items into sales order details */
116 commit_transaction();
118 if ($loc_notification == 1 && count($st_ids) > 0)
120 require_once($path_to_root . "/reporting/includes/class.mail.inc");
121 $company = get_company_prefs();
122 $mail = new email($company['coy_name'], $company['email']);
123 $from = $company['coy_name'] . " <" . $company['email'] . ">";
124 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
125 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
127 for ($i = 0; $i < count($st_ids); $i++)
128 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
129 $msg .= "\n" . _("Please reorder") . "\n\n";
130 $msg .= $company['coy_name'];
132 $mail->subject($subject);
134 $ret = $mail->send();
139 //----------------------------------------------------------------------------------------
141 function delete_sales_order($order_no)
145 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
146 db_query($sql, "order Header Delete");
148 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
149 db_query($sql, "order Detail Delete");
151 commit_transaction();
154 //----------------------------------------------------------------------------------------
155 // Mark changes in sales_order_details
157 function update_sales_order_version($order)
159 foreach ($order as $so_num => $so_ver) {
160 $sql= 'UPDATE '.TB_PREF.'sales_orders SET version=version+1 WHERE order_no='. $so_num.
161 ' AND version='.$so_ver;
162 db_query($sql, 'Concurrent editing conflict while sales order update');
166 //----------------------------------------------------------------------------------------
168 function update_sales_order($order)
170 global $loc_notification, $path_to_root;
172 $del_date = date2sql($order->due_date);
173 $ord_date = date2sql($order->document_date);
174 $order_no = key($order->trans_no);
175 $version= current($order->trans_no);
179 $sql = "UPDATE ".TB_PREF."sales_orders SET type =".$order->so_type." ,
180 debtor_no = '" . $order->customer_id . "',
181 branch_code = '" . $order->Branch . "',
182 customer_ref = '". $order->cust_ref ."',
183 comments = '". db_escape($order->Comments) ."',
184 ord_date = '" . $ord_date . "',
185 order_type = '" . $order->sales_type . "',
186 ship_via = " . $order->ship_via .",
187 deliver_to = '" . $order->deliver_to . "',
188 delivery_address = '" . $order->delivery_address . "',
189 contact_phone = '" . $order->phone . "',
190 contact_email = '" . $order->email . "',
191 freight_cost = " . $order->freight_cost .",
192 from_stk_loc = '" . $order->Location ."',
193 delivery_date = '" . $del_date . "',
194 version = ".($version+1)."
195 WHERE order_no=" . $order_no ."
196 AND version=".$version;
198 db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
200 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
202 db_query($sql, "Old order Cannot be Deleted");
204 if ($loc_notification == 1)
206 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
210 $st_reorder = array();
212 foreach ($order->line_items as $line)
214 if ($loc_notification == 1 && is_inventory_item($line->stock_id))
216 $sql = "SELECT ".TB_PREF."loc_stock.*, "
217 .TB_PREF."locations.location_name, "
218 .TB_PREF."locations.email
219 FROM ".TB_PREF."loc_stock, "
221 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
222 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
223 AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
224 $res = db_query($sql,"a location could not be retreived");
225 $loc = db_fetch($res);
226 if ($loc['email'] != "")
228 $qoh = get_qoh_on_date($line->stock_id, $order->Location);
229 $qoh -= get_demand_qty($line->stock_id, $order->Location);
230 $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
231 $qoh -= $line->quantity;
232 if ($qoh < $loc['reorder_level'])
234 $st_ids[] = $line->stock_id;
235 $st_names[] = $line->item_description;
236 $st_num[] = $qoh - $loc['reorder_level'];
237 $st_reorder[] = $loc['reorder_level'];
241 $sql = "INSERT INTO ".TB_PREF."sales_order_details
242 (order_no, stk_code, description, unit_price, quantity,
243 discount_percent, qty_sent)
245 $sql .= $order_no . ",'"
246 .$line->stock_id . "','"
247 .$line->item_description . "', "
249 .$line->quantity . ", "
250 .$line->discount_percent . ", "
251 .$line->qty_done ." )";
253 db_query($sql, "Old order Cannot be Inserted");
255 } /* inserted line items into sales order details */
257 commit_transaction();
258 if ($loc_notification == 1 && count($st_ids) > 0)
260 require_once($path_to_root . "/reporting/includes/class.mail.inc");
261 $company = get_company_prefs();
262 $mail = new email($company['coy_name'], $company['email']);
263 $from = $company['coy_name'] . " <" . $company['email'] . ">";
264 $to = $loc['location_name'] . " <" . $loc['email'] . ">";
265 $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
267 for ($i = 0; $i < count($st_ids); $i++)
268 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
269 . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
270 . _("Below") . ": " . $st_num[$i] . "\n";
271 $msg .= "\n" . _("Please reorder") . "\n\n";
272 $msg .= $company['coy_name'];
274 $mail->subject($subject);
276 $ret = $mail->send();
280 //----------------------------------------------------------------------------------------
282 function get_sales_order_header($order_no)
284 $sql = "SELECT ".TB_PREF."sales_orders.*, "
285 .TB_PREF."debtors_master.name, "
286 .TB_PREF."debtors_master.curr_code, "
287 .TB_PREF."locations.location_name, "
288 .TB_PREF."debtors_master.payment_terms, "
289 .TB_PREF."debtors_master.discount, "
290 .TB_PREF."sales_types.sales_type, "
291 .TB_PREF."sales_types.id AS sales_type_id, "
292 .TB_PREF."sales_types.tax_included, "
293 .TB_PREF."shippers.shipper_name, "
294 .TB_PREF."tax_groups.name AS tax_group_name , "
295 .TB_PREF."tax_groups.id AS tax_group_id
296 FROM ".TB_PREF."sales_orders, "
297 .TB_PREF."debtors_master, "
298 .TB_PREF."sales_types, "
299 .TB_PREF."tax_groups, "
300 .TB_PREF."cust_branch, "
301 .TB_PREF."locations, "
303 WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
304 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
305 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
306 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
307 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
308 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
309 AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
310 $result = db_query($sql, "order Retreival");
312 $num = db_num_rows($result);
315 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
319 return db_fetch($result);
322 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
326 //----------------------------------------------------------------------------------------
328 function get_sales_order_details($order_no) {
329 $sql = "SELECT id, stk_code, unit_price, "
330 .TB_PREF."sales_order_details.description,"
331 .TB_PREF."sales_order_details.quantity,
333 qty_sent as qty_done, "
334 .TB_PREF."stock_master.units,
335 ".TB_PREF."stock_master.material_cost + "
336 .TB_PREF."stock_master.labour_cost + "
337 .TB_PREF."stock_master.overhead_cost AS standard_cost
338 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
339 WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
340 AND order_no =" . $order_no . " ORDER BY id";
342 return db_query($sql, "Retreive order Line Items");
344 //----------------------------------------------------------------------------------------
346 function read_sales_order($order_no, &$order)
348 $myrow = get_sales_order_header($order_no);
350 $order->trans_type = 30;
351 $order->so_type = $myrow["type"];
352 $order->trans_no = array($order_no=> $myrow["version"]);
354 $order->set_customer($myrow["debtor_no"], $myrow["name"],
355 $myrow["curr_code"], $myrow["discount"]);
357 $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
358 $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
360 $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"], $myrow["tax_included"]);
362 $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
364 $order->set_delivery($myrow["ship_via"], $myrow["deliver_to"],
365 $myrow["delivery_address"], $myrow["freight_cost"]);
367 $order->cust_ref = $myrow["customer_ref"];
368 $order->sales_type =$myrow["order_type"];
369 $order->Comments = $myrow["comments"];
370 $order->due_date = sql2date($myrow["delivery_date"]);
371 $order->document_date = sql2date($myrow["ord_date"]);
373 $result = get_sales_order_details($order_no);
374 if (db_num_rows($result) > 0)
377 while ($myrow = db_fetch($result))
379 $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"],
380 $myrow["unit_price"], $myrow["discount_percent"],
381 $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] );
389 //----------------------------------------------------------------------------------------
391 function sales_order_has_deliveries($order_no)
393 $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
394 "sales_order_details WHERE order_no=$order_no";
396 $result = db_query($sql, "could not query for sales order usage");
398 $row = db_fetch_row($result);
400 return ($row[0] > 0);
403 //----------------------------------------------------------------------------------------
405 function close_sales_order($order_no)
407 // set the quantity of each item to the already sent quantity. this will mark item as closed.
408 $sql = "UPDATE ".TB_PREF."sales_order_details
409 SET quantity = qty_sent,
411 WHERE order_no = $order_no";
413 db_query($sql, "The sales order detail record could not be updated");
416 //---------------------------------------------------------------------------------------------------------------
418 function get_invoice_duedate($debtorno, $invdate)
420 if (!is_date($invdate))
424 $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
425 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
426 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
428 $result = db_query($sql,"The customer details could not be retrieved");
429 $myrow = db_fetch($result);
431 if (db_num_rows($result) == 0)
433 if ($myrow['day_in_following_month'] > 0)
434 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
436 $duedate = add_days($invdate, $myrow['days_before_due']);
440 function get_customer_to_order($customer_id) {
442 // Now check to ensure this account is not on hold */
443 $sql = "SELECT ".TB_PREF."debtors_master.name, "
444 .TB_PREF."debtors_master.address, "
445 .TB_PREF."credit_status.dissallow_invoices, "
446 .TB_PREF."debtors_master.sales_type AS salestype, "
447 .TB_PREF."sales_types.sales_type, "
448 .TB_PREF."sales_types.tax_included, "
449 .TB_PREF."debtors_master.curr_code, "
450 .TB_PREF."debtors_master.discount
451 FROM ".TB_PREF."debtors_master, "
452 .TB_PREF."credit_status, "
453 .TB_PREF."sales_types
454 WHERE ".TB_PREF."debtors_master.sales_type="
455 .TB_PREF."sales_types.id
456 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
457 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
459 $result =db_query($sql,"Customer Record Retreive");
460 return db_fetch($result);
463 function get_branch_to_order($customer_id, $branch_id) {
465 // 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
466 $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
467 .TB_PREF."cust_branch.br_address, "
468 .TB_PREF."cust_branch.br_post_address, "
469 .TB_PREF."cust_branch.phone, "
470 .TB_PREF."cust_branch.email,
471 default_location, location_name, default_ship_via, "
472 .TB_PREF."tax_groups.name AS tax_group_name, "
473 .TB_PREF."tax_groups.id AS tax_group_id
474 FROM ".TB_PREF."cust_branch, "
475 .TB_PREF."tax_groups, "
477 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
478 AND ".TB_PREF."locations.loc_code=default_location
479 AND ".TB_PREF."cust_branch.branch_code='" . $branch_id . "'
480 AND ".TB_PREF."cust_branch.debtor_no = '" . $customer_id . "'";
482 return db_query($sql,"Customer Branch Record Retreive");