5e4cee82fd1aa114bcfe7da2a4c3ea5018c5c26b
[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/inventory.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/items_adjust_db.inc");
24 include_once($path_to_root . "/inventory/includes/db/items_transfer_db.inc");
25 include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");
26
27 function item_img_name($stock_id)
28 {
29         $stock_id = strtr($stock_id, "><\\/:|*?", '________');
30         return clean_file_name($stock_id);
31 }
32
33 function get_stock_movements($stock_id, $StockLocation, $BeforeDate, $AfterDate)
34 {
35         $before_date = date2sql($BeforeDate);
36         $after_date = date2sql($AfterDate);
37   $sql = "SELECT move.*, IF(ISNULL(supplier.supplier_id), debtor.name, supplier.supp_name) name";
38
39         if(!$StockLocation) {
40                  $sql .= ", move.loc_code";
41         }
42   $sql.=    " FROM ".TB_PREF."stock_moves move
43                                 LEFT JOIN ".TB_PREF."supp_trans credit ON credit.trans_no=move.trans_no AND credit.type=move.type
44                                 LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=move.trans_no AND 25=move.type
45                                 LEFT JOIN ".TB_PREF."suppliers supplier ON IFNULL(grn.supplier_id, credit.supplier_id)=supplier.supplier_id
46                                 LEFT JOIN ".TB_PREF."debtor_trans cust_trans ON cust_trans.trans_no=move.trans_no AND cust_trans.type=move.type
47                                 LEFT JOIN ".TB_PREF."debtors_master debtor ON cust_trans.debtor_no=debtor.debtor_no
48                 WHERE";
49
50   if ($StockLocation) {
51     $sql.= " move.loc_code=".db_escape($StockLocation)." AND";
52         }
53
54         $sql.= " move.tran_date >= '". $after_date . "'
55         AND move.tran_date <= '" . $before_date . "'
56         AND move.stock_id = ".db_escape($stock_id) . " ORDER BY move.tran_date, move.trans_id";
57
58   return db_query($sql, "could not query stock moves");
59 }
60
61 function calculate_reorder_level($location, $line, &$st_ids, &$st_names, &$st_num, &$st_reorder)
62 {
63         $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
64                 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
65                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
66                 AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
67                 AND ".TB_PREF."loc_stock.loc_code = '" . $location . "'";
68         $res = db_query($sql,"a location could not be retreived");
69         $loc = db_fetch($res);
70         if ($loc['email'] != "")
71         {
72                 $qoh = get_qoh_on_date($line->stock_id, $location);
73                 $qoh -= get_demand_qty($line->stock_id, $location);
74                 $qoh -= get_demand_asm_qty($line->stock_id, $location);
75                 $qoh -= $line->quantity;
76                 if ($qoh < $loc['reorder_level'])
77                 {
78                         $st_ids[] = $line->stock_id;
79                         $st_names[] = $line->item_description;
80                         $st_num[] = $qoh - $loc['reorder_level'];
81                         $st_reorder[] = $loc['reorder_level'];
82                 }
83         }
84         return $loc;
85 }
86
87 function send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder)
88 {
89         global $path_to_root;
90         
91         require_once($path_to_root . "/reporting/includes/class.mail.inc");
92         $company = get_company_prefs();
93         $mail = new email($company['coy_name'], $company['email']);
94         $from = $company['coy_name'] . " <" . $company['email'] . ">";
95         $to = $loc['location_name'] . " <" . $loc['email'] . ">";
96         $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
97         $msg = "\n";
98         for ($i = 0; $i < count($st_ids); $i++)
99                 $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
100         $msg .= "\n" . _("Please reorder") . "\n\n";
101         $msg .= $company['coy_name'];
102         $mail->to($to);
103         $mail->subject($subject);
104         $mail->text($msg);
105         return $mail->send();
106 }