Sending location email when below reorder also in Itmes Transfer and Adjustment
[fa-stable.git] / inventory / includes / inventory_db.inc
1 <?php
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 include_once($path_to_root . "/includes/date_functions.inc");
13 include_once($path_to_root . "/includes/banking.inc");
14 include_once($path_to_root . "/includes/manufacturing.inc");
15
16 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
17 include_once($path_to_root . "/inventory/includes/db/items_trans_db.inc");
18 include_once($path_to_root . "/inventory/includes/db/items_prices_db.inc");
19 include_once($path_to_root . "/inventory/includes/db/items_purchases_db.inc");
20 include_once($path_to_root . "/inventory/includes/db/items_codes_db.inc");
21 include_once($path_to_root . "/inventory/includes/db/items_db.inc");
22 include_once($path_to_root . "/inventory/includes/db/items_locations_db.inc");
23 include_once($path_to_root . "/inventory/includes/db/movement_types_db.inc");
24 include_once($path_to_root . "/inventory/includes/db/items_adjust_db.inc");
25 include_once($path_to_root . "/inventory/includes/db/items_transfer_db.inc");
26 include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");
27
28 function item_img_name($stock_id)
29 {
30         $stock_id = strtr($stock_id, "><\\/:|*?", '________');
31         return clean_file_name($stock_id);
32 }
33
34 function calculate_reorder_level($location, $line, &$st_ids, &$st_names, &$st_num, &$st_reorder)
35 {
36         $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
37                 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
38                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
39                 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
40                 AND ".TB_PREF."loc_stock.loc_code = '" . $location . "'";
41         $res = db_query($sql,"a location could not be retreived");
42         $loc = db_fetch($res);
43         if ($loc['email'] != "")
44         {
45                 $qoh = get_qoh_on_date($line->stock_id, $location);
46                 $qoh -= get_demand_qty($line->stock_id, $location);
47                 $qoh -= get_demand_asm_qty($line->stock_id, $location);
48                 $qoh -= $line->quantity;
49                 if ($qoh < $loc['reorder_level'])
50                 {
51                         $st_ids[] = $line->stock_id;
52                         $st_names[] = $line->item_description;
53                         $st_num[] = $qoh - $loc['reorder_level'];
54                         $st_reorder[] = $loc['reorder_level'];
55                 }
56         }
57         return $loc;
58 }
59
60 function send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder)
61 {
62         global $path_to_root;
63         
64         require_once($path_to_root . "/reporting/includes/class.mail.inc");
65         $company = get_company_prefs();
66         $mail = new email($company['coy_name'], $company['email']);
67         $from = $company['coy_name'] . " <" . $company['email'] . ">";
68         $to = $loc['location_name'] . " <" . $loc['email'] . ">";
69         $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
70         $msg = "\n";
71         for ($i = 0; $i < count($st_ids); $i++)
72                 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
73         $msg .= "\n" . _("Please reorder") . "\n\n";
74         $msg .= $company['coy_name'];
75         $mail->to($to);
76         $mail->subject($subject);
77         $mail->text($msg);
78         return $mail->send();
79 }
80 ?>