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