Release 1.15. Added email notification to stock location when available stock is...
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 5 Dec 2007 22:15:30 +0000 (22:15 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 5 Dec 2007 22:15:30 +0000 (22:15 +0000)
CHANGELOG.txt
config.php
gl/inquiry/gl_account_inquiry.php
gl/inquiry/gl_trial_balance.php
reporting/rep704.php
reporting/rep708.php
sales/includes/db/sales_order_db.inc

index d53aba0c34ad05ad9c6140851220ede921f36928..cad5cd43068e920ba8d69edebf4d7139051cdf37 100644 (file)
@@ -19,6 +19,18 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+-------------------- 1.15 Stable Released ----------------------
+05-Dec-2007 Joe Hunt
+ + Added email notification to stock location when available stock is below reorder level
+ $ config.php
+   /sales/includes/db/sales_order_db.inc
+   
+ # Fixed bugs in Open balances when account is not a balance account
+ $ /gl/inquiry/gl_trial_balance.php
+   /gl/inquiry/gl_account_inquiry.php
+   /reporting/rep704.php
+   /reporting/rep708.php
+   
 -------------------- 1.14 Stable Released ----------------------
 01-Oct-2007 Joe Hunt
  ! Major change in the installation of modules to also accept an SQL-file for upload.
index 6164128bd660b0402b96513d5dd0a2402e82faf1..44e0b706773a0bf8326b60c2b900e47d6e513e2f 100644 (file)
        // Main Title
        $app_title = "FrontAccounting";
     // application version
-    $version           = "1.14";
+    $version           = "1.15";
 
     // Build for development purposes
-    $build_version     = "79";
+    $build_version     = "32";
 
        // Powered by
        $power_by               = "FrontAccounting";
@@ -84,6 +84,9 @@
           2 = Islamic used by other arabic nations */
        $date_system = 0;
 
+       /* email stock location if order below reorder-level */
+       $loc_notification = 1;
+
        $dateformats    = array("MMDDYYYY", "DDMMYYYY", "YYYYMMDD");
        $dateseps               = array("/", ".", "-", " ");
        $thoseps                = array(",", ".", " ");
index 23ffa17bd3f0d8c718f7b698da8826b4b7071045..db669563290e841677f734d97941f0bbcd721811 100644 (file)
@@ -37,20 +37,20 @@ if (isset($_GET["Dimension2"]))
 function gl_inquiry_controls()
 {
        global $table_style2;
-       
+
        $dim = get_company_pref('use_dimension');
     start_form();
 
     //start_table($table_style2);
     start_table("class='tablestyle_noborder'");
        start_row();
-       
+
     gl_all_accounts_list_cells(_("Account:"), 'account', null);
 
        date_cells(_("from:"), 'TransFromDate', null, -30);
        date_cells(_("to:"), 'TransToDate');
     submit_cells('Show',_("Show"));
-    
+
     end_row();
 
        if ($dim >= 1)
@@ -58,7 +58,7 @@ function gl_inquiry_controls()
        if ($dim > 1)
                dimensions_list_row(_("Dimension")." 2", 'Dimension2', null, true, " ", false, 2);
        end_table();
-    
+
     end_form();
 }
 
@@ -97,11 +97,21 @@ function show_results()
        else if ($dim == 1)
                $th = array(_("Type"), _("#"), _("Date"), _("Dimension"),
                        _("Person/Item"), _("Debit"), _("Credit"), _("Balance"), _("Memo"));
-       else            
-               $th = array(_("Type"), _("#"), _("Date"), 
+       else
+               $th = array(_("Type"), _("#"), _("Date"),
                        _("Person/Item"), _("Debit"), _("Credit"), _("Balance"), _("Memo"));
        table_header($th);
-    $bfw = get_gl_balance_from_to("", $_POST['TransFromDate'], $_POST["account"], $_POST['Dimension'], $_POST['Dimension2']);
+       if (is_account_balancesheet($account["account_code"]))
+               $begin = "";
+       else
+       {
+               if ($from < $begin)
+                       $begin = add_days($_POST['TransFromDate'], -1);
+               else
+                       $begin = add_days(begin_fiscalyear(), -1);
+       }
+
+    $bfw = get_gl_balance_from_to($begin, $_POST['TransFromDate'], $_POST["account"], $_POST['Dimension'], $_POST['Dimension2']);
 
        start_row("class='inquirybg'");
        label_cell("<b>"._("Opening Balance")." - ".$_POST['TransFromDate']."</b>", "colspan=$colspan");
@@ -113,7 +123,7 @@ function show_results()
        $j = 1;
        $k = 0; //row colour counter
 
-       while ($myrow = db_fetch($result)) 
+       while ($myrow = db_fetch($result))
        {
 
        alt_table_row_color($k);
@@ -137,8 +147,8 @@ function show_results()
 
        $j++;
        if ($j == 12)
-       { 
-               $j = 1; 
+       {
+               $j = 1;
                table_header($th);
        }
        }
index 5ab5b30b6c1f61a16a6248ce57bc49c604142edc..4ab7a46512a99684cee31b522d1f5ec4ca5cdb3a 100644 (file)
@@ -42,7 +42,7 @@ function get_balance($account, $from, $to, $from_incl=true, $to_incl=true) {
        $sql = "SELECT SUM(amount) As TransactionSum FROM ".TB_PREF."gl_trans
                WHERE account='$account'";
 
-       if ($from) 
+       if ($from)
        {
                $from_date = date2sql($from);
                if ($from_incl)
@@ -51,7 +51,7 @@ function get_balance($account, $from, $to, $from_incl=true, $to_incl=true) {
                        $sql .= " AND tran_date > '$from_date'";
        }
 
-       if ($to) 
+       if ($to)
        {
                $to_date = date2sql($to);
                if ($to_incl)
@@ -94,10 +94,18 @@ function display_trial_balance()
 
        $accounts = get_gl_accounts();
 
-       while ($account = db_fetch($accounts)) 
+       while ($account = db_fetch($accounts))
        {
-
-               $prev_balance = get_balance($account["account_code"], null, $_POST['TransFromDate'], false, false);
+               if (is_account_balancesheet($account["account_code"]))
+                       $begin = null;
+               else
+               {
+                       if ($from < $begin)
+                               $begin = add_days($_POST['TransFromDate'], -1);
+                       else
+                               $begin = add_days(begin_fiscalyear(), -1);
+               }
+               $prev_balance = get_balance($account["account_code"], $begin, $_POST['TransFromDate'], false, false);
 
                $curr_balance = get_balance($account["account_code"], $_POST['TransFromDate'], $_POST['TransToDate']);
                if (check_value("NoZero") && !$prev_balance && !$curr_balance)
index 1c6b091f639ff832dc941408accae9fd0805e026..856efecb10ca71400e508f63cb62c5fcc11e1bdd 100644 (file)
@@ -60,12 +60,12 @@ function print_GL_transactions()
        $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
 
        if ($dim == 2)
-               $headers = array(_('Type'),     _('#'), _('Date'), _('Dimension')." 1", _('Dimension')." 2", 
+               $headers = array(_('Type'),     _('#'), _('Date'), _('Dimension')." 1", _('Dimension')." 2",
                        _('Person/Item'), _('Debit'),   _('Credit'), _('Balance'));
-       else if ($dim == 1)             
+       else if ($dim == 1)
                $headers = array(_('Type'),     _('#'), _('Date'), _('Dimension'), "", _('Person/Item'),
                        _('Debit'),     _('Credit'), _('Balance'));
-       else    
+       else
                $headers = array(_('Type'),     _('#'), _('Date'), "", "", _('Person/Item'),
                        _('Debit'),     _('Credit'), _('Balance'));
 
@@ -93,37 +93,41 @@ function print_GL_transactions()
                                    1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
                                    2 => array('text' => _('Accounts'),'from' => $fromacc,'to' => $toacc));
     }
-       
+
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->Header();
 
        $accounts = get_gl_accounts($fromacc, $toacc);
 
-       while ($account=db_fetch($accounts)) 
+       while ($account=db_fetch($accounts))
        {
-               $begin = begin_fiscalyear();
                if (is_account_balancesheet($account["account_code"]))
                        $begin = "";
-               elseif ($from < $begin)
-                       $begin = $from;
+               else
+               {
+                       if ($from < $begin)
+                               $begin = add_days($from, -1);
+                       else
+                               $begin = add_days(begin_fiscalyear(), -1);
+               }
                $prev_balance = get_gl_balance_from_to($begin, $from, $account["account_code"], $dimension, $dimension2);
 
                $trans = get_gl_transactions($from, $to, -1, $account['account_code'], $dimension, $dimension2);
                $rows = db_num_rows($trans);
                if ($prev_balance == 0.0 && $rows == 0)
                        continue;
-               $rep->Font('bold');     
+               $rep->Font('bold');
                $rep->TextCol(0, 3,     $account['account_code'] . " " . $account['account_name']);
                $rep->TextCol(3, 5, _('Opening Balance'));
                if ($prev_balance > 0.0)
                        $rep->TextCol(6, 7,     number_format2(abs($prev_balance), $dec));
                else
                        $rep->TextCol(7, 8,     number_format2(abs($prev_balance), $dec));
-               $rep->Font();   
+               $rep->Font();
                $total = $prev_balance;
                $rep->NewLine(2);
-               if ($rows > 0) 
+               if ($rows > 0)
                {
                        while ($myrow=db_fetch($trans))
                        {
@@ -139,11 +143,11 @@ function print_GL_transactions()
                                $rep->TextCol(5, 6,     payment_person_types::person_name($myrow["person_type_id"],$myrow["person_id"], false));
                                if ($myrow['amount'] > 0.0)
                                        $rep->TextCol(6, 7,     number_format2(abs($myrow['amount']), $dec));
-                               else    
+                               else
                                        $rep->TextCol(7, 8,     number_format2(abs($myrow['amount']), $dec));
                                $rep->TextCol(8, 9,     number_format2($total, $dec));
                                $rep->NewLine();
-                               if ($rep->row < $rep->bottomMargin + $rep->lineHeight) 
+                               if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
                                {
                                        $rep->Line($rep->row - 2);
                                        $rep->Header();
@@ -151,13 +155,13 @@ function print_GL_transactions()
                        }
                        $rep->NewLine();
                }
-               $rep->Font('bold');     
+               $rep->Font('bold');
                $rep->TextCol(3, 5,     _("Ending Balance"));
                if ($total > 0.0)
                        $rep->TextCol(6, 7,     number_format2(abs($total), $dec));
                else
                        $rep->TextCol(7, 8,     number_format2(abs($total), $dec));
-               $rep->Font();   
+               $rep->Font();
                $rep->Line($rep->row - $rep->lineHeight + 4);
                $rep->NewLine(2, 1);
        }
index cb19d72f95fd2310164f4d778f7acd551dc7910e..cb11746c132f49eb791ceaf6b0412f59a7a694d7 100644 (file)
@@ -51,19 +51,19 @@ function print_trial_balance()
 
        $cols2 = array(0, 50, 230, 330, 430, 530);
        //-------------0--1---2----3----4----5--
-       
+
        $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
-       
+
        $aligns2 = array('left', 'left', 'left', 'left', 'left');
-       
+
        $cols = array(0, 50, 200, 250, 300,     350, 400, 450, 500,     550);
        //------------0--1---2----3----4----5----6----7----8----9--
-       
+
        $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
                _('Credit'), _('Debit'), _('Credit'));
-       
+
        $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right', 'right');
-    
+
     if ($dim == 2)
     {
        $params =   array(      0 => $comments,
@@ -94,14 +94,19 @@ function print_trial_balance()
 
        $accounts = get_gl_accounts();
 
-       while ($account=db_fetch($accounts)) 
+       while ($account=db_fetch($accounts))
        {
 
-               $begin = begin_fiscalyear();
                if (is_account_balancesheet($account["account_code"]))
                        $begin = "";
-               elseif ($from < $begin)
-                       $begin = $from;
+               else
+               {
+                       if ($from < $begin)
+                               $begin = add_days($from, -1);
+                       else
+                               $begin = add_days(begin_fiscalyear(), -1);
+               }
+
                $prev_balance = get_gl_balance_from_to($begin, $from, $account["account_code"], $dimension, $dimension2);
 
                $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
@@ -127,7 +132,7 @@ function print_trial_balance()
 
                $rep->NewLine();
 
-               if ($rep->row < $rep->bottomMargin + $rep->lineHeight) 
+               if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
                {
                        $rep->Line($rep->row - 2);
                        $rep->Header();
index 7254a9667ad26295e25fd4294416a432d2eee2c5..4f75e6d08986373aeddbdd3afa0feac6b6bd51df 100644 (file)
 <?php
 
-//----------------------------------------------------------------------------------------     
+//----------------------------------------------------------------------------------------
+function get_demand_qty($stockid, $location)
+{
+       $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_invoiced) AS QtyDemand
+                               FROM ".TB_PREF."sales_order_details,
+                                       ".TB_PREF."sales_orders
+                               WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND
+                                       ".TB_PREF."sales_orders.from_stk_loc ='$location' AND
+                                       ".TB_PREF."sales_order_details.stk_code = '$stockid'";
+
+    $TransResult = db_query($sql,"No transactions were returned");
+       $DemandRow = db_fetch($TransResult);
+       return $DemandRow['QtyDemand'];
+}
+
+function get_demand_asm_qty($stockid, $location)
+{
+       $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_invoiced)*".TB_PREF."bom.quantity)
+                                  AS Dem
+                                  FROM ".TB_PREF."sales_order_details,
+                                               ".TB_PREF."sales_orders,
+                                               ".TB_PREF."bom,
+                                               ".TB_PREF."stock_master
+                                  WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
+                                  ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND
+                                  ".TB_PREF."sales_orders.from_stk_loc='$location' AND
+                                  ".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_invoiced > 0 AND
+                                  ".TB_PREF."bom.component='$stockid' AND
+                                  ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
+                                  ".TB_PREF."stock_master.mb_flag='A'";
+
+    $TransResult = db_query($sql,"No transactions were returned");
+       if (db_num_rows($TransResult)==1)
+       {
+               $DemandRow = db_fetch_row($TransResult);
+               $DemandQty = $DemandRow[0];
+       }
+       else
+               $DemandQty = 0.0;
+
+    return $DemandQty;
+}
 
 function add_sales_order($order)
 {
+       global $loc_notification, $path_to_root;
+
        begin_transaction();
 
        if ($order->direct_invoice)
                $del_date = date2sql($order->orig_order_date);
-       else    
+       else
                $del_date = date2sql($order->delivery_date);
 
-       $sql = "INSERT INTO ".TB_PREF."sales_orders (debtor_no, branch_code, customer_ref, Comments, ord_date, 
-               order_type, ship_via, deliver_to, delivery_address, contact_phone, 
-               contact_email, freight_cost, from_stk_loc, delivery_date) 
-               VALUES ('" . $order->customer_id . "', '" . $order->Branch . "', '". 
-                       $order->cust_ref ."','". db_escape($order->Comments) ."','" . 
-                       date2sql($order->orig_order_date) . "', '" . 
-                       $order->default_sales_type . "', " . 
-                       $_POST['ship_via'] .",'" . $order->deliver_to . "', '" . 
-                       $order->delivery_address . "', '" .  
-                       $order->phone . "', '" . $order->email . "', " . 
-                       $order->freight_cost .", '" . $order->Location ."', '" . 
+       $sql = "INSERT INTO ".TB_PREF."sales_orders (debtor_no, branch_code, customer_ref, Comments, ord_date,
+               order_type, ship_via, deliver_to, delivery_address, contact_phone,
+               contact_email, freight_cost, from_stk_loc, delivery_date)
+               VALUES ('" . $order->customer_id . "', '" . $order->Branch . "', '".
+                       $order->cust_ref ."','". db_escape($order->Comments) ."','" .
+                       date2sql($order->orig_order_date) . "', '" .
+                       $order->default_sales_type . "', " .
+                       $_POST['ship_via'] .",'" . $order->deliver_to . "', '" .
+                       $order->delivery_address . "', '" .
+                       $order->phone . "', '" . $order->email . "', " .
+                       $order->freight_cost .", '" . $order->Location ."', '" .
                        $del_date . "')";
 
        db_query($sql, "order Cannot be Added");
 
        $order_no = db_insert_id();
-       
-       foreach ($order->line_items as $stock_item) 
+       if ($loc_notification == 1)
        {
+               include_once($path_to_root . "/inventory/includes/inventory_db.inc");
+               $st_ids = array();
+               $st_names = array();
+               $st_num = array();
+               $st_reorder = array();
+       }
+       foreach ($order->line_items as $stock_item)
+       {
+               if ($loc_notification == 1 && is_inventory_item($stock_item->stock_id))
+               {
+                       $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
+                               FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
+                               WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
+                               AND ".TB_PREF."loc_stock.stock_id = '" . $stock_item->stock_id . "'
+                               AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
+                       $res = db_query($sql,"a location could not be retreived");
+                       $loc = db_fetch($res);
+                       if ($loc['email'] != "")
+                       {
+                               $qoh = get_qoh_on_date($stock_item->stock_id, $order->Location);
+                               $qoh -= get_demand_qty($stock_item->stock_id, $order->Location);
+                               $qoh -= get_demand_asm_qty($stock_item->stock_id, $order->Location);
+                               $qoh -= $stock_item->quantity;
+                               if ($qoh < $loc['reorder_level'])
+                               {
+                                       $st_ids[] = $stock_item->stock_id;
+                                       $st_names[] = $stock_item->item_description;
+                                       $st_num[] = $qoh - $loc['reorder_level'];
+                                       $st_reorder[] = $loc['reorder_level'];
+                               }
+                       }
+               }
                $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
-               $sql .= $order_no . 
-                               ",'$stock_item->stock_id', '$stock_item->item_description', $stock_item->price, 
-                               $stock_item->quantity, 
+               $sql .= $order_no .
+                               ",'$stock_item->stock_id', '$stock_item->item_description', $stock_item->price,
+                               $stock_item->quantity,
                                $stock_item->discount_percent)";
                db_query($sql, "order Details Cannot be Added");
-               
+
        } /* inserted line items into sales order details */
-       
-       add_forms_for_sys_type(systypes::sales_order(), $order_no);     
-       
+
+       add_forms_for_sys_type(systypes::sales_order(), $order_no);
+
        commit_transaction();
-       
-       return $order_no;               
+
+       if ($loc_notification == 1 && count($st_ids) > 0)
+       {
+               require_once($path_to_root . "/reporting/includes/class.mail.inc");
+               $company = get_company_prefs();
+               $mail = new email($company['coy_name'], $company['email']);
+               $from = $company['coy_name'] . " <" . $company['email'] . ">";
+               $to = $loc['location_name'] . " <" . $loc['email'] . ">";
+               $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
+               $msg = "\n";
+               for ($i = 0; $i < count($st_ids); $i++)
+                       $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
+               $msg .= "\n" . _("Please reorder") . "\n\n";
+               $msg .= $company['coy_name'];
+               $mail->to($to);
+               $mail->subject($subject);
+               $mail->text($msg);
+               $ret = $mail->send();
+       }
+       return $order_no;
 }
 
 //----------------------------------------------------------------------------------------
@@ -51,35 +143,37 @@ function add_sales_order($order)
 function delete_sales_order($order_no)
 {
        begin_transaction();
-               
+
        $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
        db_query($sql, "order Header Delete");
 
        $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
        db_query($sql, "order Detail Delete");
-       
+
        delete_forms_for_systype(systypes::sales_order(), $order_no);
-       
-       commit_transaction();   
+
+       commit_transaction();
 }
 
 //----------------------------------------------------------------------------------------
 
 function update_sales_order($order_no, $order)
 {
+       global $loc_notification, $path_to_root;
+
        $del_date = date2sql($order->delivery_date);
        $ord_date = date2sql($order->orig_order_date);
 
        begin_transaction();
 
-       $sql = "UPDATE ".TB_PREF."sales_orders SET debtor_no = '" . $order->customer_id . "', 
-               branch_code = '" . $order->Branch . "', 
-               customer_ref = '". $order->cust_ref ."', 
-               Comments = '". db_escape($order->Comments) ."', ord_date = '" . $ord_date . "', 
-               order_type = '" . $order->default_sales_type . "', ship_via = " . $order->ship_via .", 
-               deliver_to = '" . $order->deliver_to . "', delivery_address = '" . $order->delivery_address . "', 
-               contact_phone = '" . $order->phone . "', 
-               contact_email = '" . $order->email . "', freight_cost = " . $order->freight_cost .", 
+       $sql = "UPDATE ".TB_PREF."sales_orders SET debtor_no = '" . $order->customer_id . "',
+               branch_code = '" . $order->Branch . "',
+               customer_ref = '". $order->cust_ref ."',
+               Comments = '". db_escape($order->Comments) ."', ord_date = '" . $ord_date . "',
+               order_type = '" . $order->default_sales_type . "', ship_via = " . $order->ship_via .",
+               deliver_to = '" . $order->deliver_to . "', delivery_address = '" . $order->delivery_address . "',
+               contact_phone = '" . $order->phone . "',
+               contact_email = '" . $order->email . "', freight_cost = " . $order->freight_cost .",
                from_stk_loc = '" . $order->Location ."', delivery_date = '" . $del_date . "'
                WHERE order_no=" . $order_no;
 
@@ -89,11 +183,43 @@ function update_sales_order($order_no, $order)
 
        db_query($sql, "Old order Cannot be Deleted");
 
-       foreach ($order->line_items as $stock_item) 
+       if ($loc_notification == 1)
        {
+               include_once($path_to_root . "/inventory/includes/inventory_db.inc");
+               $st_ids = array();
+               $st_names = array();
+               $st_num = array();
+               $st_reorder = array();
+       }
+       foreach ($order->line_items as $stock_item)
+       {
+               if ($loc_notification == 1 && is_inventory_item($stock_item->stock_id))
+               {
+                       $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
+                               FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
+                               WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
+                               AND ".TB_PREF."loc_stock.stock_id = '" . $stock_item->stock_id . "'
+                               AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
+                       $res = db_query($sql,"a location could not be retreived");
+                       $loc = db_fetch($res);
+                       if ($loc['email'] != "")
+                       {
+                               $qoh = get_qoh_on_date($stock_item->stock_id, $order->Location);
+                               $qoh -= get_demand_qty($stock_item->stock_id, $order->Location);
+                               $qoh -= get_demand_asm_qty($stock_item->stock_id, $order->Location);
+                               $qoh -= $stock_item->quantity;
+                               if ($qoh < $loc['reorder_level'])
+                               {
+                                       $st_ids[] = $stock_item->stock_id;
+                                       $st_names[] = $stock_item->item_description;
+                                       $st_num[] = $qoh - $loc['reorder_level'];
+                                       $st_reorder[] = $loc['reorder_level'];
+                               }
+                       }
+               }
 
                $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code,  description, unit_price, quantity, discount_percent, qty_invoiced) VALUES (";
-               
+
                $sql .= $order_no . ",'" . $stock_item->stock_id . "','" . $stock_item->item_description . "', " . $stock_item->price . ", " . $stock_item->quantity . ", " . $stock_item->discount_percent . ", " . $stock_item->qty_inv . " )";
 
                db_query($sql, "Old order Cannot be Inserted");
@@ -101,38 +227,56 @@ function update_sales_order($order_no, $order)
        } /* inserted line items into sales order details */
 
        commit_transaction();
+       if ($loc_notification == 1 && count($st_ids) > 0)
+       {
+               require_once($path_to_root . "/reporting/includes/class.mail.inc");
+               $company = get_company_prefs();
+               $mail = new email($company['coy_name'], $company['email']);
+               $from = $company['coy_name'] . " <" . $company['email'] . ">";
+               $to = $loc['location_name'] . " <" . $loc['email'] . ">";
+               $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
+               $msg = "\n";
+               for ($i = 0; $i < count($st_ids); $i++)
+                       $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
+               $msg .= "\n" . _("Please reorder") . "\n\n";
+               $msg .= $company['coy_name'];
+               $mail->to($to);
+               $mail->subject($subject);
+               $mail->text($msg);
+               $ret = $mail->send();
+       }
 }
 
 //----------------------------------------------------------------------------------------
 
 function get_sales_order($order_no)
 {
-       $sql = "SELECT ".TB_PREF."sales_orders.*, ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."locations.location_name, 
+       $sql = "SELECT ".TB_PREF."sales_orders.*, ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."locations.location_name,
                ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."debtors_master.discount, ".TB_PREF."sales_types.sales_type, ".TB_PREF."shippers.shipper_name,
-               ".TB_PREF."tax_groups.name AS tax_group_name , ".TB_PREF."tax_groups.id AS tax_group_id 
-               FROM ".TB_PREF."sales_orders, ".TB_PREF."debtors_master, ".TB_PREF."sales_types, ".TB_PREF."tax_groups, ".TB_PREF."cust_branch, ".TB_PREF."locations, ".TB_PREF."shippers  
+               ".TB_PREF."tax_groups.name AS tax_group_name , ".TB_PREF."tax_groups.id AS tax_group_id
+               FROM ".TB_PREF."sales_orders, ".TB_PREF."debtors_master, ".TB_PREF."sales_types, ".TB_PREF."tax_groups, ".TB_PREF."cust_branch, ".TB_PREF."locations, ".TB_PREF."shippers
                        WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
-                               AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code 
+                               AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
                                AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
-                               AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no 
+                               AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
                                AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
                                AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
                                AND ".TB_PREF."sales_orders.order_no = " . $order_no;
-                                                                       
-       $result = db_query($sql, "order Retreival");    
 
-       $num = db_num_rows($result);    
-       if ($num > 1) 
+       $result = db_query($sql, "order Retreival");
+
+       $num = db_num_rows($result);
+       if ($num > 1)
        {
                display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
-       } 
-       else if ($num == 1) 
+       }
+       else if ($num == 1)
        {
                return db_fetch($result);
        }
        else
                display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
-                                                                               
+
 }
 
 //----------------------------------------------------------------------------------------
@@ -146,10 +290,10 @@ function read_sales_order($order_no, &$order, $skip_completed_items=false)
        $order->customer_name = $myrow["name"];
        $order->cust_ref = $myrow["customer_ref"];
        $order->default_sales_type =$myrow["order_type"];
-       $order->sales_type_name =$myrow["sales_type"];          
+       $order->sales_type_name =$myrow["sales_type"];
        $order->customer_currency = $myrow["curr_code"];
-       $order->default_discount = $myrow["discount"];          
-       
+       $order->default_discount = $myrow["discount"];
+
        $order->Comments = $myrow["comments"];
 
        $order->ship_via = $myrow["ship_via"];
@@ -162,35 +306,35 @@ function read_sales_order($order_no, &$order, $skip_completed_items=false)
        $order->Location = $myrow["from_stk_loc"];
        $order->location_name = $myrow["location_name"];
        $order->orig_order_date = sql2date($myrow["ord_date"]);
-       
+
        $order->tax_group_name = $myrow["tax_group_name"];
        $order->tax_group_id = $myrow["tax_group_id"];
-       
+
        $sql = "SELECT stk_code, unit_price, ".TB_PREF."sales_order_details.description,
-               ".TB_PREF."sales_order_details.quantity, discount_percent, 
+               ".TB_PREF."sales_order_details.quantity, discount_percent,
                qty_invoiced, ".TB_PREF."stock_master.units,
                ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS standard_cost
-               FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master 
-                       WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id 
+               FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
+                       WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
                                AND order_no =" . $order_no;
-                                               
+
        if ($skip_completed_items)
-               $sql .= " 
-                       AND ".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_invoiced > 0 ";                                                        
+               $sql .= "
+                       AND ".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_invoiced > 0 ";
 
        $result = db_query($sql, "Retreive order Line Items");
 
-       if (db_num_rows($result) > 0) 
+       if (db_num_rows($result) > 0)
        {
 
-               while ($myrow = db_fetch($result)) 
+               while ($myrow = db_fetch($result))
                {
                        $order->add_to_cart($myrow["stk_code"],$myrow["quantity"],
                                $myrow["unit_price"], $myrow["discount_percent"],
                                $myrow["qty_invoiced"], $myrow["standard_cost"], $myrow["description"]);
-               } 
-       } 
-       
+               }
+       }
+
        return true;
 }
 
@@ -199,12 +343,12 @@ function read_sales_order($order_no, &$order, $skip_completed_items=false)
 function sales_order_has_invoices($order_no)
 {
        $sql = "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE order_=$order_no";
-       
+
        $result = db_query($sql, "could not query for sales order usage");
-       
+
        $row = db_fetch_row($result);
-       
-       return ($row[0] > 0);   
+
+       return ($row[0] > 0);
 }
 
 //----------------------------------------------------------------------------------------
@@ -212,30 +356,30 @@ function sales_order_has_invoices($order_no)
 function close_sales_order($order_no)
 {
        // set the quantity of each item to the already invoiced quantity. this will mark item as closed.
-       $sql = "UPDATE ".TB_PREF."sales_order_details 
-               SET quantity = qty_invoiced 
-                       WHERE order_no = $order_no";    
-       
-       db_query($sql, "The sales order detail record could not be updated");                           
+       $sql = "UPDATE ".TB_PREF."sales_order_details
+               SET quantity = qty_invoiced
+                       WHERE order_no = $order_no";
+
+       db_query($sql, "The sales order detail record could not be updated");
 }
 
 //----------------------------------------------------------------------------------------
 
 function dispatch_sales_order_item($order_no, $stock_id, $qty_dispatched)
 {
-       $sql = "UPDATE ".TB_PREF."sales_order_details 
+       $sql = "UPDATE ".TB_PREF."sales_order_details
                SET qty_invoiced = qty_invoiced + $qty_dispatched ";
-       $sql .= " WHERE order_no = $order_no 
+       $sql .= " WHERE order_no = $order_no
                AND stk_code = '$stock_id'";
 
-       db_query($sql, "The sales order detail record could not be updated");   
+       db_query($sql, "The sales order detail record could not be updated");
 }
 
 //---------------------------------------------------------------------------------------------------------------
 
 function get_invoice_duedate($debtorno, $invdate)
 {
-       if (!is_date($invdate)) 
+       if (!is_date($invdate))
        {
                return Today();
        }