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