Sending location email when below reorder also in Itmes Transfer and Adjustment
authorJoe <unknown>
Tue, 8 Apr 2014 07:20:13 +0000 (09:20 +0200)
committerJoe <unknown>
Tue, 8 Apr 2014 07:20:13 +0000 (09:20 +0200)
inventory/includes/db/items_adjust_db.inc
inventory/includes/db/items_transfer_db.inc
inventory/includes/inventory_db.inc
sales/includes/db/sales_order_db.inc

index 7be0f9bbb05a1b759b9cb816f8a14af19592008d..f9ecd6f314de160d939ff1969ee42b84e658775a 100644 (file)
@@ -13,7 +13,7 @@
 
 function add_stock_adjustment($items, $location, $date_, $type, $increase, $reference, $memo_)
 {
-       global $Refs;
+       global $loc_notification, $path_to_root, $Refs;
 
        begin_transaction();
        $args = func_get_args();
@@ -24,9 +24,20 @@ function add_stock_adjustment($items, $location, $date_, $type, $increase, $refe
 
        $adj_id = get_next_trans_no(ST_INVADJUST);
 
+       if ($loc_notification == 1 && !$increase)
+       {
+               include_once($path_to_root . "/inventory/includes/inventory_db.inc");
+               $st_ids = array();
+               $st_names = array();
+               $st_num = array();
+               $st_reorder = array();
+       }
        foreach ($items as $line_item)
        {
 
+               if ($loc_notification == 1 && !$increase)
+                       $loc = calculate_reorder_level($location, $line_item, $st_ids, $st_names, $st_num, $st_reorder); 
+
                if (!$increase)
                        $line_item->quantity = -$line_item->quantity;
 
@@ -42,6 +53,8 @@ function add_stock_adjustment($items, $location, $date_, $type, $increase, $refe
        $args->trans_no = $adj_id;
        hook_db_postwrite($args, ST_INVADJUST);
        commit_transaction();
+       if ($loc_notification == 1 && !$increase && count($st_ids) > 0)
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
 
        return $adj_id;
 }
index 5d5181f78ce89d7660123c76565e46086f107590..6da83936d00aea7fbd0d5ca19a81444c6d73e462 100644 (file)
@@ -13,7 +13,7 @@
 
 function add_stock_transfer($Items, $location_from, $location_to, $date_, $type, $reference, $memo_)
 {
-       global $Refs;
+       global $loc_notification, $path_to_root, $Refs;
 
        begin_transaction();
        $args = func_get_args();
@@ -24,8 +24,18 @@ function add_stock_transfer($Items, $location_from, $location_to, $date_, $type,
 
        $transfer_id = get_next_trans_no(ST_LOCTRANSFER);
 
+       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 ($Items as $line_item)
        {
+               if ($loc_notification == 1)
+                       $loc = calculate_reorder_level($location_from, $line_item, $st_ids, $st_names, $st_num, $st_reorder); 
                add_stock_transfer_item($transfer_id, $line_item->stock_id, $location_from,
                        $location_to, $date_, $type, $reference, $line_item->quantity);
        }
@@ -39,6 +49,9 @@ function add_stock_transfer($Items, $location_from, $location_to, $date_, $type,
        hook_db_postwrite($args, ST_LOCTRANSFER);
 
        commit_transaction();
+       
+       if ($loc_notification == 1 && count($st_ids) > 0)
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
 
        return $transfer_id;
 }
index 79fda1196e44bc13f6811f186c7fc8082ab0d8c7..5c2aec1781457b62e9352a95afe609de8670370c 100644 (file)
@@ -31,4 +31,50 @@ function item_img_name($stock_id)
        return clean_file_name($stock_id);
 }
 
+function calculate_reorder_level($location, $line, &$st_ids, &$st_names, &$st_num, &$st_reorder)
+{
+       $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 = '" . $line->stock_id . "'
+               AND ".TB_PREF."loc_stock.loc_code = '" . $location . "'";
+       $res = db_query($sql,"a location could not be retreived");
+       $loc = db_fetch($res);
+       if ($loc['email'] != "")
+       {
+               $qoh = get_qoh_on_date($line->stock_id, $location);
+               $qoh -= get_demand_qty($line->stock_id, $location);
+               $qoh -= get_demand_asm_qty($line->stock_id, $location);
+               $qoh -= $line->quantity;
+               if ($qoh < $loc['reorder_level'])
+               {
+                       $st_ids[] = $line->stock_id;
+                       $st_names[] = $line->item_description;
+                       $st_num[] = $qoh - $loc['reorder_level'];
+                       $st_reorder[] = $loc['reorder_level'];
+               }
+       }
+       return $loc;
+}
+
+function send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder)
+{
+       global $path_to_root;
+       
+       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);
+       return $mail->send();
+}
 ?>
index a0cf388046b4ca2e0f58bc11e1fe9d31e3b052b7..5ead6dd49e4e1d1578d7c6ea74217582d9ef1aec 100644 (file)
@@ -55,29 +55,7 @@ function add_sales_order(&$order)
        foreach ($order->line_items as $line)
        {
                if ($loc_notification == 1 && is_inventory_item($line->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 = '" . $line->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($line->stock_id, $order->Location);
-                               $qoh -= get_demand_qty($line->stock_id, $order->Location);
-                               $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
-                               $qoh -= $line->quantity;
-                               if ($qoh < $loc['reorder_level'])
-                               {
-                                       $st_ids[] = $line->stock_id;
-                                       $st_names[] = $line->item_description;
-                                       $st_num[] = $qoh - $loc['reorder_level'];
-                                       $st_reorder[] = $loc['reorder_level'];
-                               }
-                       }
-               }
+                       $loc = calculate_reorder_level($order->Location, $line, $st_ids, $st_names, $st_num, $st_reorder); 
 
                $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, trans_type, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
                $sql .= $order_no . ",".$order->trans_type .
@@ -98,23 +76,7 @@ function add_sales_order(&$order)
        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();
-       }
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
        return $order_no;
 }
 
@@ -211,32 +173,7 @@ function update_sales_order($order)
        foreach ($order->line_items as $line)
        {
                if ($loc_notification == 1 && is_inventory_item($line->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 = ".db_escape($line->stock_id)."
-                                AND ".TB_PREF."loc_stock.loc_code = ".db_escape($order->Location);
-                       $res = db_query($sql,"a location could not be retreived");
-                       $loc = db_fetch($res);
-                       if ($loc['email'] != "")
-                       {
-                               $qoh = get_qoh_on_date($line->stock_id, $order->Location);
-                               $qoh -= get_demand_qty($line->stock_id, $order->Location);
-                               $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
-                               $qoh -= $line->quantity;
-                               if ($qoh < $loc['reorder_level'])
-                               {
-                                       $st_ids[] = $line->stock_id;
-                                       $st_names[] = $line->item_description;
-                                       $st_num[] = $qoh - $loc['reorder_level'];
-                                       $st_reorder[] = $loc['reorder_level'];
-                               }
-                       }
-               }
+                       $loc = calculate_reorder_level($order->Location, $line, $st_ids, $st_names, $st_num, $st_reorder); 
                if($line->id) {
                        $sql = "UPDATE ".TB_PREF."sales_order_details
                                                SET description = ".db_escape($line->item_description) . ", 
@@ -270,25 +207,7 @@ function update_sales_order($order)
        hook_db_postwrite($order, $order->trans_type);
        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();
-       }
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
 }
 
 //----------------------------------------------------------------------------------------