Changed $path_to_root and stamped copyright
[fa-stable.git] / reporting / rep301.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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 // trial_inquiry_controls();
30 print_inventory_valuation_report();
31
32 function getTransactions($category, $location)
33 {
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.description,
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 = '$category'";
57                 if ($location != 'all')
58                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$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     include_once($path_to_root . "/reporting/includes/pdf_report.inc");
72
73     $category = $_POST['PARAM_0'];
74     $location = $_POST['PARAM_1'];
75     $detail = $_POST['PARAM_2'];
76     $comments = $_POST['PARAM_3'];
77
78     $dec = user_price_dec();
79
80         if ($category == reserved_words::get_all_numeric())
81                 $category = 0;
82         if ($category == 0)
83                 $cat = _('All');
84         else
85                 $cat = get_category_name($category);
86
87         if ($location == reserved_words::get_all())
88                 $location = 'all';
89         if ($location == 'all')
90                 $loc = _('All');
91         else
92                 $loc = $location;
93
94         $cols = array(0, 100, 250, 350, 450,    515);
95
96         $headers = array(_('Category'), '', _('Quantity'), _('Unit Cost'), _('Value'));
97
98         $aligns = array('left', 'left', 'right', 'right', 'right');
99
100     $params =   array(  0 => $comments,
101                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
102                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
103
104     $rep = new FrontReport(_('Inventory Valuation Report'), "InventoryValReport.pdf", user_pagesize());
105
106     $rep->Font();
107     $rep->Info($params, $cols, $headers, $aligns);
108     $rep->Header();
109
110         $res = getTransactions($category, $location);
111         $total = $grandtotal = 0.0;
112         $catt = '';
113         while ($trans=db_fetch($res))
114         {
115                 if ($catt != $trans['cat_description'])
116                 {
117                         if ($catt != '')
118                         {
119                                 if ($detail)
120                                 {
121                                         $rep->NewLine(2, 3);
122                                         $rep->TextCol(0, 4, _('Total'));
123                                 }
124                                 $rep->Textcol(4, 5, number_format2($total, $dec));
125                                 if ($detail)
126                                 {
127                                         $rep->Line($rep->row - 2);
128                                         $rep->NewLine();
129                                 }
130                                 $rep->NewLine();
131                                 $total = 0.0;
132                         }
133                         $rep->TextCol(0, 1, $trans['category_id']);
134                         $rep->TextCol(1, 2, $trans['cat_description']);
135                         $catt = $trans['cat_description'];
136                         if ($detail)
137                                 $rep->NewLine();
138                 }
139                 if ($detail)
140                 {
141                         $rep->NewLine();
142                         $rep->fontsize -= 2;
143                         $rep->TextCol(0, 1, $trans['stock_id']);
144                         $rep->TextCol(1, 2, $trans['description']);
145                         $rep->TextCol(2, 3, number_format2($trans['QtyOnHand'], get_qty_dec($trans['stock_id'])));
146                         $rep->TextCol(3, 4, number_format2($trans['UnitCost'], $dec));
147                         $rep->TextCol(4, 5, number_format2($trans['ItemTotal'], $dec));
148                         $rep->fontsize += 2;
149                 }
150                 $total += $trans['ItemTotal'];
151                 $grandtotal += $trans['ItemTotal'];
152         }
153         if ($detail)
154         {
155                 $rep->NewLine(2, 3);
156                 $rep->TextCol(0, 4, _('Total'));
157         }
158         $rep->Textcol(4, 5, number_format2($total, $dec));
159         if ($detail)
160         {
161                 $rep->Line($rep->row - 2);
162                 $rep->NewLine();
163         }
164         $rep->NewLine(2, 1);
165         $rep->TextCol(0, 4, _('Grand Total'));
166         $rep->TextCol(4, 5, number_format2($grandtotal, $dec));
167         $rep->Line($rep->row  - 4);
168     $rep->End();
169 }
170
171 ?>