Implemented Selectable Print Orientation (Portrait, Landscape) for all reports in...
[fa-stable.git] / reporting / rep301.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_ITEMSVALREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Inventory Valuation
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
27 //----------------------------------------------------------------------------------------------------
28
29 print_inventory_valuation_report();
30
31 function getTransactions($category, $location, $date)
32 {
33         $date = date2sql($date);
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.units,
38                         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
39                         ".TB_PREF."stock_moves.loc_code,
40                         SUM(".TB_PREF."stock_moves.qty) AS QtyOnHand,
41                         ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS UnitCost,
42                         SUM(".TB_PREF."stock_moves.qty) *(".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost) AS ItemTotal
43                 FROM ".TB_PREF."stock_master,
44                         ".TB_PREF."stock_category,
45                         ".TB_PREF."stock_moves
46                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
47                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
48                 AND ".TB_PREF."stock_master.mb_flag<>'D' 
49                 AND ".TB_PREF."stock_moves.tran_date <= '$date'
50                 GROUP BY ".TB_PREF."stock_master.category_id,
51                         ".TB_PREF."stock_category.description, ";
52                 if ($location != 'all')
53                         $sql .= TB_PREF."stock_moves.loc_code, ";
54                 $sql .= "UnitCost,
55                         ".TB_PREF."stock_master.stock_id,
56                         ".TB_PREF."stock_master.description
57                 HAVING SUM(".TB_PREF."stock_moves.qty) != 0";
58                 if ($category != 0)
59                         $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
60                 if ($location != 'all')
61                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location);
62                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
63                         ".TB_PREF."stock_master.stock_id";
64
65     return db_query($sql,"No transactions were returned");
66 }
67
68 //----------------------------------------------------------------------------------------------------
69
70 function print_inventory_valuation_report()
71 {
72     global $path_to_root;
73
74         $date = $_POST['PARAM_0'];
75     $category = $_POST['PARAM_1'];
76     $location = $_POST['PARAM_2'];
77     $detail = $_POST['PARAM_3'];
78     $comments = $_POST['PARAM_4'];
79         $orientation = $_POST['PARAM_5'];
80         $destination = $_POST['PARAM_6'];
81         if ($destination)
82                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
83         else
84                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
85         $detail = !$detail;
86     $dec = user_price_dec();
87
88         $orientation = ($orientation ? 'L' : 'P');
89         if ($category == ALL_NUMERIC)
90                 $category = 0;
91         if ($category == 0)
92                 $cat = _('All');
93         else
94                 $cat = get_category_name($category);
95
96         if ($location == ALL_TEXT)
97                 $location = 'all';
98         if ($location == 'all')
99                 $loc = _('All');
100         else
101                 $loc = get_location_name($location);
102
103         $cols = array(0, 75, 225, 250, 350, 450,        515);
104
105         $headers = array(_('Category'), '', _('UOM'), _('Quantity'), _('Unit Cost'), _('Value'));
106
107         $aligns = array('left', 'left', 'left', 'right', 'right', 'right');
108
109     $params =   array(  0 => $comments,
110                                         1 => array('text' => _('End Date'), 'from' => $date,            'to' => ''),
111                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
112                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
113
114     $rep = new FrontReport(_('Inventory Valuation Report'), "InventoryValReport", user_pagesize(), 9, $orientation);
115     if ($orientation == 'L')
116         $rep->recalculate_cols($cols);
117     $rep->Font();
118     $rep->Info($params, $cols, $headers, $aligns);
119     $rep->NewPage();
120
121         $res = getTransactions($category, $location, $date);
122         $total = $grandtotal = 0.0;
123         $catt = '';
124         while ($trans=db_fetch($res))
125         {
126                 if ($catt != $trans['cat_description'])
127                 {
128                         if ($catt != '')
129                         {
130                                 if ($detail)
131                                 {
132                                         $rep->NewLine(2, 3);
133                                         $rep->TextCol(0, 4, _('Total'));
134                                 }
135                                 $rep->AmountCol(5, 6, $total, $dec);
136                                 if ($detail)
137                                 {
138                                         $rep->Line($rep->row - 2);
139                                         $rep->NewLine();
140                                 }
141                                 $rep->NewLine();
142                                 $total = 0.0;
143                         }
144                         $rep->TextCol(0, 1, $trans['category_id']);
145                         $rep->TextCol(1, 2, $trans['cat_description']);
146                         $catt = $trans['cat_description'];
147                         if ($detail)
148                                 $rep->NewLine();
149                 }
150                 if ($detail)
151                 {
152                         $rep->NewLine();
153                         $rep->fontSize -= 2;
154                         $rep->TextCol(0, 1, $trans['stock_id']);
155                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
156                         $rep->TextCol(2, 3, $trans['units']);
157                         $rep->AmountCol(3, 4, $trans['QtyOnHand'], get_qty_dec($trans['stock_id']));
158                         $dec2 = 0;
159                         price_decimal_format($trans['UnitCost'], $dec2);
160                         $rep->AmountCol(4, 5, $trans['UnitCost'], $dec2);
161                         $rep->AmountCol(5, 6, $trans['ItemTotal'], $dec);
162                         $rep->fontSize += 2;
163                 }
164                 $total += $trans['ItemTotal'];
165                 $grandtotal += $trans['ItemTotal'];
166         }
167         if ($detail)
168         {
169                 $rep->NewLine(2, 3);
170                 $rep->TextCol(0, 4, _('Total'));
171         }
172         $rep->Amountcol(5, 6, $total, $dec);
173         if ($detail)
174         {
175                 $rep->Line($rep->row - 2);
176                 $rep->NewLine();
177         }
178         $rep->NewLine(2, 1);
179         $rep->TextCol(0, 4, _('Grand Total'));
180         $rep->AmountCol(5, 6, $grandtotal, $dec);
181         $rep->Line($rep->row  - 4);
182         $rep->NewLine();
183     $rep->End();
184 }
185
186 ?>