Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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 = 'SA_ITEMSANALYTIC';
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 item.category_id,
35                         category.description AS cat_description,
36                         item.stock_id,
37                         item.description, item.inactive,
38                         IF(move.stock_id IS NULL, '', move.loc_code) AS loc_code,
39                         SUM(IF(move.stock_id IS NULL, 0, move.qty)) AS qty_on_hand
40                 FROM (".TB_PREF."stock_master item,"
41                         .TB_PREF."stock_category category)
42                         LEFT JOIN ".TB_PREF."stock_moves move ON item.stock_id=move.stock_id
43                 WHERE item.category_id=category.category_id
44                 AND (item.mb_flag='B' OR item.mb_flag='M')";
45         if ($category != 0)
46                 $sql .= " AND item.category_id = ".db_escape($category);
47         if ($location != 'all')
48                 $sql .= " AND IF(move.stock_id IS NULL, '1=1',move.loc_code = ".db_escape($location).")";
49         $sql .= " GROUP BY item.category_id,
50                 category.description,
51                 item.stock_id,
52                 item.description
53                 ORDER BY item.category_id,
54                 item.stock_id";
55
56     return db_query($sql,"No transactions were returned");
57
58 }
59
60 function getPeriods($stockid, $location)
61 {
62         $date5 = date('Y-m-d');
63         $date4 = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
64         $date3 = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y')));
65         $date2 = date('Y-m-d',mktime(0,0,0,date('m')-2,1,date('Y')));
66         $date1 = date('Y-m-d',mktime(0,0,0,date('m')-3,1,date('Y')));
67         $date0 = date('Y-m-d',mktime(0,0,0,date('m')-4,1,date('Y')));
68
69         $sql = "SELECT SUM(CASE WHEN tran_date >= '$date0' AND tran_date < '$date1' THEN -qty ELSE 0 END) AS prd0,
70                                 SUM(CASE WHEN tran_date >= '$date1' AND tran_date < '$date2' THEN -qty ELSE 0 END) AS prd1,
71                                 SUM(CASE WHEN tran_date >= '$date2' AND tran_date < '$date3' THEN -qty ELSE 0 END) AS prd2,
72                                 SUM(CASE WHEN tran_date >= '$date3' AND tran_date < '$date4' THEN -qty ELSE 0 END) AS prd3,
73                                 SUM(CASE WHEN tran_date >= '$date4' AND tran_date <= '$date5' THEN -qty ELSE 0 END) AS prd4
74                         FROM ".TB_PREF."stock_moves
75                         WHERE stock_id='$stockid'
76                         AND loc_code ='$location'
77                         AND (type=13 OR type=11)";
78
79     $TransResult = db_query($sql,"No transactions were returned");
80         return db_fetch($TransResult);
81 }
82
83 //----------------------------------------------------------------------------------------------------
84
85 function print_inventory_planning()
86 {
87     global $path_to_root;
88
89     $category = $_POST['PARAM_0'];
90     $location = $_POST['PARAM_1'];
91     $comments = $_POST['PARAM_2'];
92         $orientation = $_POST['PARAM_3'];
93         $destination = $_POST['PARAM_4'];
94         if ($destination)
95                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
96         else
97                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
98
99         $orientation = ($orientation ? 'L' : 'P');
100         if ($category == ALL_NUMERIC)
101                 $category = 0;
102         if ($category == 0)
103                 $cat = _('All');
104         else
105                 $cat = get_category_name($category);
106
107         if ($location == ALL_TEXT)
108                 $location = 'all';
109         if ($location == 'all')
110                 $loc = _('All');
111         else
112                 $loc = get_location_name($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(), 9, $orientation);
133     if ($orientation == 'L')
134         recalculate_cols($cols);
135
136     $rep->Font();
137     $rep->Info($params, $cols, $headers, $aligns);
138     $rep->NewPage();
139
140         $res = getTransactions($category, $location);
141         $catt = '';
142         while ($trans=db_fetch($res))
143         {
144                 if ($catt != $trans['cat_description'])
145                 {
146                         if ($catt != '')
147                         {
148                                 $rep->Line($rep->row - 2);
149                                 $rep->NewLine(2, 3);
150                         }
151                         $rep->TextCol(0, 1, $trans['category_id']);
152                         $rep->TextCol(1, 2, $trans['cat_description']);
153                         $catt = $trans['cat_description'];
154                         $rep->NewLine();
155                 }
156                 if ($location == 'all')
157                         $loc_code = "";
158                 else
159                         $loc_code = $location;
160                 $custqty = get_demand_qty($trans['stock_id'], $loc_code);
161                 $custqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
162                 $suppqty = get_on_porder_qty($trans['stock_id'], $loc_code);
163                 $suppqty += get_on_worder_qty($trans['stock_id'], $loc_code);
164                 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
165                 $rep->NewLine();
166                 $dec = get_qty_dec($trans['stock_id']);
167                 $rep->TextCol(0, 1, $trans['stock_id']);
168                 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
169                 $rep->AmountCol(2, 3, $period['prd0'], $dec);
170                 $rep->AmountCol(3, 4, $period['prd1'], $dec);
171                 $rep->AmountCol(4, 5, $period['prd2'], $dec);
172                 $rep->AmountCol(5, 6, $period['prd3'], $dec);
173                 $rep->AmountCol(6, 7, $period['prd4'], $dec);
174                 
175                 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
176                 $IdealStockHolding = $MaxMthSales * 3;
177                 $rep->AmountCol(7, 8, $IdealStockHolding, $dec);
178
179                 $rep->AmountCol(8, 9, $trans['qty_on_hand'], $dec);
180                 $rep->AmountCol(9, 10, $custqty, $dec);
181                 $rep->AmountCol(10, 11, $suppqty, $dec);
182
183                 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
184                 if ($SuggestedTopUpOrder < 0.0)
185                         $SuggestedTopUpOrder = 0.0;
186                 $rep->AmountCol(11, 12, $SuggestedTopUpOrder, $dec);
187         }
188         $rep->Line($rep->row - 4);
189         $rep->NewLine();
190     $rep->End();
191 }
192