Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[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, $tmonths;
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 = $tmonths[date('n',mktime(0,0,0,date('m'),1,date('Y')))];
117         $per1 = $tmonths[date('n',mktime(0,0,0,date('m')-1,1,date('Y')))];
118         $per2 = $tmonths[date('n',mktime(0,0,0,date('m')-2,1,date('Y')))];
119         $per3 = $tmonths[date('n',mktime(0,0,0,date('m')-3,1,date('Y')))];
120         $per4 = $tmonths[date('n',mktime(0,0,0,date('m')-4,1,date('Y')))];
121         $headers = array(_('Category'), '', $per4, $per3, $per2, $per1, $per0, '3*M',
122                 _('QOH'), _('Cust Ord'), _('Supp Ord'), _('Sugg Ord'));
123
124         $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
125                 'right', 'right', 'right', 'right');
126
127     $params =   array(  0 => $comments,
128                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
129                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
130
131     $rep = new FrontReport(_('Inventory Planning Report'), "InventoryPlanning", user_pagesize(), 9, $orientation);
132     if ($orientation == 'L')
133         recalculate_cols($cols);
134
135     $rep->Font();
136     $rep->Info($params, $cols, $headers, $aligns);
137     $rep->NewPage();
138
139         $res = getTransactions($category, $location);
140         $catt = '';
141         while ($trans=db_fetch($res))
142         {
143                 if ($catt != $trans['cat_description'])
144                 {
145                         if ($catt != '')
146                         {
147                                 $rep->Line($rep->row - 2);
148                                 $rep->NewLine(2, 3);
149                         }
150                         $rep->TextCol(0, 1, $trans['category_id']);
151                         $rep->TextCol(1, 2, $trans['cat_description']);
152                         $catt = $trans['cat_description'];
153                         $rep->NewLine();
154                 }
155                 if ($location == 'all')
156                         $loc_code = "";
157                 else
158                         $loc_code = $location;
159                 $custqty = get_demand_qty($trans['stock_id'], $loc_code);
160                 $custqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
161                 $suppqty = get_on_porder_qty($trans['stock_id'], $loc_code);
162                 $suppqty += get_on_worder_qty($trans['stock_id'], $loc_code);
163                 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
164                 $rep->NewLine();
165                 $dec = get_qty_dec($trans['stock_id']);
166                 $rep->TextCol(0, 1, $trans['stock_id']);
167                 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
168                 $rep->AmountCol(2, 3, $period['prd0'], $dec);
169                 $rep->AmountCol(3, 4, $period['prd1'], $dec);
170                 $rep->AmountCol(4, 5, $period['prd2'], $dec);
171                 $rep->AmountCol(5, 6, $period['prd3'], $dec);
172                 $rep->AmountCol(6, 7, $period['prd4'], $dec);
173                 
174                 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
175                 $IdealStockHolding = $MaxMthSales * 3;
176                 $rep->AmountCol(7, 8, $IdealStockHolding, $dec);
177
178                 $rep->AmountCol(8, 9, $trans['qty_on_hand'], $dec);
179                 $rep->AmountCol(9, 10, $custqty, $dec);
180                 $rep->AmountCol(10, 11, $suppqty, $dec);
181
182                 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
183                 if ($SuggestedTopUpOrder < 0.0)
184                         $SuggestedTopUpOrder = 0.0;
185                 $rep->AmountCol(11, 12, $SuggestedTopUpOrder, $dec);
186         }
187         $rep->Line($rep->row - 4);
188         $rep->NewLine();
189     $rep->End();
190 }
191