Merged changes from main branch up to 2.1.3.
[fa-stable.git] / reporting / rep302.php
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 $page_security = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Inventory Planning
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
25 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
26 include_once($path_to_root . "/includes/db/manufacturing_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_inventory_planning();
31
32 function getTransactions($category, $location)
33 {
34         $sql = "SELECT ".TB_PREF."stock_master.category_id,
35                         ".TB_PREF."stock_category.description AS cat_description,
36                         ".TB_PREF."stock_master.stock_id,
37                         ".TB_PREF."stock_master.description,
38                         IF(".TB_PREF."stock_moves.stock_id IS NULL, '', ".TB_PREF."stock_moves.loc_code) AS loc_code,
39                         SUM(IF(".TB_PREF."stock_moves.stock_id IS NULL,0,".TB_PREF."stock_moves.qty)) AS qty_on_hand
40                 FROM (".TB_PREF."stock_master,
41                         ".TB_PREF."stock_category)
42                 LEFT JOIN ".TB_PREF."stock_moves ON
43                         (".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id OR ".TB_PREF."stock_master.stock_id IS NULL)
44                 WHERE ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
45                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
46         if ($category != 0)
47                 $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
48         if ($location != 'all')
49                 $sql .= " AND IF(".TB_PREF."stock_moves.stock_id IS NULL, '1=1',".TB_PREF."stock_moves.loc_code = '$location')";
50         $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
51                 ".TB_PREF."stock_category.description,
52                 ".TB_PREF."stock_master.stock_id,
53                 ".TB_PREF."stock_master.description
54                 ORDER BY ".TB_PREF."stock_master.category_id,
55                 ".TB_PREF."stock_master.stock_id";
56
57     return db_query($sql,"No transactions were returned");
58
59 }
60
61 function getPeriods($stockid, $location)
62 {
63         $date5 = date('Y-m-d');
64         $date4 = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
65         $date3 = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y')));
66         $date2 = date('Y-m-d',mktime(0,0,0,date('m')-2,1,date('Y')));
67         $date1 = date('Y-m-d',mktime(0,0,0,date('m')-3,1,date('Y')));
68         $date0 = date('Y-m-d',mktime(0,0,0,date('m')-4,1,date('Y')));
69
70         $sql = "SELECT SUM(CASE WHEN tran_date >= '$date0' AND tran_date < '$date1' THEN -qty ELSE 0 END) AS prd0,
71                                 SUM(CASE WHEN tran_date >= '$date1' AND tran_date < '$date2' THEN -qty ELSE 0 END) AS prd1,
72                                 SUM(CASE WHEN tran_date >= '$date2' AND tran_date < '$date3' THEN -qty ELSE 0 END) AS prd2,
73                                 SUM(CASE WHEN tran_date >= '$date3' AND tran_date < '$date4' THEN -qty ELSE 0 END) AS prd3,
74                                 SUM(CASE WHEN tran_date >= '$date4' AND tran_date <= '$date5' THEN -qty ELSE 0 END) AS prd4
75                         FROM ".TB_PREF."stock_moves
76                         WHERE stock_id='$stockid'
77                         AND loc_code ='$location'
78                         AND (type=13 OR type=11)
79                         AND visible=1";
80
81     $TransResult = db_query($sql,"No transactions were returned");
82         return db_fetch($TransResult);
83 }
84
85 //----------------------------------------------------------------------------------------------------
86
87 function print_inventory_planning()
88 {
89     global $path_to_root;
90
91     $category = $_POST['PARAM_0'];
92     $location = $_POST['PARAM_1'];
93     $comments = $_POST['PARAM_2'];
94         $destination = $_POST['PARAM_3'];
95         if ($destination)
96                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
97         else
98                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
99
100         if ($category == reserved_words::get_all_numeric())
101                 $category = 0;
102         if ($category == 0)
103                 $cat = _('All');
104         else
105                 $cat = get_category_name($category);
106
107         if ($location == reserved_words::get_all())
108                 $location = 'all';
109         if ($location == 'all')
110                 $loc = _('All');
111         else
112                 $loc = $location;
113
114         $cols = array(0, 50, 150, 180, 210, 240, 270, 300, 330, 390, 435, 480, 525);
115
116         $per0 = strftime('%b',mktime(0,0,0,date('m'),1,date('Y')));
117         $per1 = strftime('%b',mktime(0,0,0,date('m')-1,1,date('Y')));
118         $per2 = strftime('%b',mktime(0,0,0,date('m')-2,1,date('Y')));
119         $per3 = strftime('%b',mktime(0,0,0,date('m')-3,1,date('Y')));
120         $per4 = strftime('%b',mktime(0,0,0,date('m')-4,1,date('Y')));
121
122         $headers = array(_('Category'), '', $per4, $per3, $per2, $per1, $per0, '3*M',
123                 _('QOH'), _('Cust Ord'), _('Supp Ord'), _('Sugg Ord'));
124
125         $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
126                 'right', 'right', 'right', 'right');
127
128     $params =   array(  0 => $comments,
129                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
130                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
131
132     $rep = new FrontReport(_('Inventory Planning Report'), "InventoryPlanning", user_pagesize());
133
134     $rep->Font();
135     $rep->Info($params, $cols, $headers, $aligns);
136     $rep->Header();
137
138         $res = getTransactions($category, $location);
139         $catt = '';
140         while ($trans=db_fetch($res))
141         {
142                 if ($catt != $trans['cat_description'])
143                 {
144                         if ($catt != '')
145                         {
146                                 $rep->Line($rep->row - 2);
147                                 $rep->NewLine(2, 3);
148                         }
149                         $rep->TextCol(0, 1, $trans['category_id']);
150                         $rep->TextCol(1, 2, $trans['cat_description']);
151                         $catt = $trans['cat_description'];
152                         $rep->NewLine();
153                 }
154                 if ($location == 'all')
155                         $loc_code = "";
156                 else
157                         $loc_code = $location;
158                 $custqty = get_demand_qty($trans['stock_id'], $loc_code);
159                 $custqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
160                 $suppqty = get_on_porder_qty($trans['stock_id'], $loc_code);
161                 $suppqty += get_on_worder_qty($trans['stock_id'], $loc_code);
162                 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
163                 $rep->NewLine();
164                 $dec = get_qty_dec($trans['stock_id']);
165                 $rep->TextCol(0, 1, $trans['stock_id']);
166                 $rep->TextCol(1, 2, $trans['description']);
167                 $rep->AmountCol(2, 3, $period['prd0'], $dec);
168                 $rep->AmountCol(3, 4, $period['prd1'], $dec);
169                 $rep->AmountCol(4, 5, $period['prd2'], $dec);
170                 $rep->AmountCol(5, 6, $period['prd3'], $dec);
171                 $rep->AmountCol(6, 7, $period['prd4'], $dec);
172
173                 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
174                 $IdealStockHolding = $MaxMthSales * 3;
175                 $rep->AmountCol(7, 8, $IdealStockHolding, $dec);
176
177                 $rep->AmountCol(8, 9, $trans['qty_on_hand'], $dec);
178                 $rep->AmountCol(9, 10, $custqty, $dec);
179                 $rep->AmountCol(10, 11, $suppqty, $dec);
180
181                 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
182                 if ($SuggestedTopUpOrder < 0.0)
183                         $SuggestedTopUpOrder = 0.0;
184                 $rep->AmountCol(11, 12, $SuggestedTopUpOrder, $dec);
185         }
186         $rep->Line($rep->row - 4);
187         $rep->NewLine();
188     $rep->End();
189 }
190
191 ?>