9d2b83bae24da5a40723db8748892bbb91f142ae
[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 get_domestic_price($myrow, $stock_id, $qty, $old_std_cost, $old_qty)
32 {
33         if ($myrow['type'] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
34         {
35                 $price = $myrow['price'];
36                 if ($myrow['type'] == ST_SUPPRECEIVE)
37                 {
38                         // Has the supplier invoice increased the receival price?
39                         $sql = "SELECT DISTINCT act_price FROM ".TB_PREF."purch_order_details pod INNER JOIN ".TB_PREF."grn_batch grn ON pod.order_no =
40                                 grn.purch_order_no WHERE grn.id = ".$myrow['trans_no']." AND pod.item_code = '$stock_id'";
41                         $result = db_query($sql, "Could not retrieve act_price from purch_order_details");
42                         $row = db_fetch_row($result);
43                         if ($row[0] > 0 AND $row[0] <> $myrow['price'])
44                                 $price = $row[0];
45                 }
46                 if ($myrow['person_id'] > 0)
47                 {
48                         // Do we have foreign currency?
49                         $supp = get_supplier($myrow['person_id']);
50                         $currency = $supp['curr_code'];
51                         $ex_rate = get_exchange_rate_to_home_currency($currency, sql2date($myrow['tran_date']));
52                         $price /= $ex_rate;
53                 }       
54         }
55         elseif ($myrow['type'] != ST_INVADJUST) // calcutale the price from avg. price
56                 $price = ($myrow['standard_cost'] * $qty - $old_std_cost * $old_qty) / $myrow['qty'];
57         else
58                 $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
59         return $price;
60 }       
61
62 function getAverageCost($stock_id, $to_date)
63 {
64         if ($to_date == null)
65                 $to_date = Today();
66
67         $to_date = date2sql($to_date);
68
69         $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM ".TB_PREF."stock_moves
70                 WHERE stock_id=".db_escape($stock_id)."
71                 AND tran_date <= '$to_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
72
73         $sql .= " ORDER BY tran_date";  
74
75         $result = db_query($sql, "No standard cost transactions were returned");
76     if ($result == false)
77         return 0;
78         $qty = $old_qty = $count = $old_std_cost = $tot_cost = 0;
79         while ($row=db_fetch($result))
80         {
81                 $qty += $row['qty'];    
82
83                 $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty);
84
85                 $old_std_cost = $row['standard_cost'];
86                 $tot_cost += $price;
87                 $count++;
88                 $old_qty = $qty;
89         }
90         if ($count == 0)
91                 return 0;
92         return $tot_cost / $count;
93 }
94     
95 function getTransactions($category, $location, $date)
96 {
97         $date = date2sql($date);
98         
99         $sql = "SELECT ".TB_PREF."stock_master.category_id,
100                         ".TB_PREF."stock_category.description AS cat_description,
101                         ".TB_PREF."stock_master.stock_id,
102                         ".TB_PREF."stock_master.units,
103                         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
104                         ".TB_PREF."stock_moves.loc_code,
105                         SUM(".TB_PREF."stock_moves.qty) AS QtyOnHand, 
106                         ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS UnitCost,
107                         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 
108                         FROM ".TB_PREF."stock_master,
109                         ".TB_PREF."stock_category,
110                         ".TB_PREF."stock_moves
111                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
112                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
113                 AND ".TB_PREF."stock_master.mb_flag<>'D' 
114                 AND ".TB_PREF."stock_moves.tran_date <= '$date'
115                 GROUP BY ".TB_PREF."stock_master.category_id,
116                         ".TB_PREF."stock_category.description, ";
117                 if ($location != 'all')
118                         $sql .= TB_PREF."stock_moves.loc_code, ";
119                 $sql .= TB_PREF."stock_master.stock_id,
120                         ".TB_PREF."stock_master.description
121                 HAVING SUM(".TB_PREF."stock_moves.qty) != 0";
122                 if ($category != 0)
123                         $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
124                 if ($location != 'all')
125                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location);
126                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
127                         ".TB_PREF."stock_master.stock_id";
128
129     return db_query($sql,"No transactions were returned");
130 }
131
132 //----------------------------------------------------------------------------------------------------
133
134 function print_inventory_valuation_report()
135 {
136     global $path_to_root, $use_costed_values;;
137
138         $date = $_POST['PARAM_0'];
139     $category = $_POST['PARAM_1'];
140     $location = $_POST['PARAM_2'];
141     $detail = $_POST['PARAM_3'];
142     $comments = $_POST['PARAM_4'];
143         $orientation = $_POST['PARAM_5'];
144         $destination = $_POST['PARAM_6'];
145         if ($destination)
146                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
147         else
148                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
149         $detail = !$detail;
150     $dec = user_price_dec();
151
152         $orientation = ($orientation ? 'L' : 'P');
153         if ($category == ALL_NUMERIC)
154                 $category = 0;
155         if ($category == 0)
156                 $cat = _('All');
157         else
158                 $cat = get_category_name($category);
159
160         if ($location == ALL_TEXT)
161                 $location = 'all';
162         if ($location == 'all')
163                 $loc = _('All');
164         else
165                 $loc = get_location_name($location);
166
167         $cols = array(0, 75, 225, 250, 350, 450,        515);
168
169         $headers = array(_('Category'), '', _('UOM'), _('Quantity'), _('Unit Cost'), _('Value'));
170
171         $aligns = array('left', 'left', 'left', 'right', 'right', 'right');
172
173     $params =   array(  0 => $comments,
174                                         1 => array('text' => _('End Date'), 'from' => $date,            'to' => ''),
175                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
176                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
177
178     $rep = new FrontReport(_('Inventory Valuation Report'), "InventoryValReport", user_pagesize(), 9, $orientation);
179     if ($orientation == 'L')
180         recalculate_cols($cols);
181     $rep->Font();
182     $rep->Info($params, $cols, $headers, $aligns);
183     $rep->NewPage();
184
185         $res = getTransactions($category, $location, $date);
186         $total = $grandtotal = 0.0;
187         $catt = '';
188         while ($trans=db_fetch($res))
189         {
190                 if ($catt != $trans['cat_description'])
191                 {
192                         if ($catt != '')
193                         {
194                                 if ($detail)
195                                 {
196                                         $rep->NewLine(2, 3);
197                                         $rep->TextCol(0, 4, _('Total'));
198                                 }
199                                 $rep->AmountCol(5, 6, $total, $dec);
200                                 if ($detail)
201                                 {
202                                         $rep->Line($rep->row - 2);
203                                         $rep->NewLine();
204                                 }
205                                 $rep->NewLine();
206                                 $total = 0.0;
207                         }
208                         $rep->TextCol(0, 1, $trans['category_id']);
209                         $rep->TextCol(1, 2, $trans['cat_description']);
210                         $catt = $trans['cat_description'];
211                         if ($detail)
212                                 $rep->NewLine();
213                 }
214                 if (isset($use_costed_values) && $use_costed_values==1)
215                 {
216                         $UnitCost = getAverageCost($trans['stock_id'], $date);
217                         $ItemTotal = $trans['QtyOnHand'] * $UnitCost;
218                 }       
219                 else
220                 {
221                         $UnitCost = $trans['UnitCost'];
222                         $ItemTotal = $trans['ItemTotal'];
223                 }       
224                 if ($detail)
225                 {
226                         $rep->NewLine();
227                         $rep->fontSize -= 2;
228                         $rep->TextCol(0, 1, $trans['stock_id']);
229                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
230                         $rep->TextCol(2, 3, $trans['units']);
231                         $rep->AmountCol(3, 4, $trans['QtyOnHand'], get_qty_dec($trans['stock_id']));
232                         
233                         $dec2 = 0;
234                         price_decimal_format($UnitCost, $dec2);
235                         $rep->AmountCol(4, 5, $UnitCost, $dec2);
236                         $rep->AmountCol(5, 6, $ItemTotal, $dec);
237                         $rep->fontSize += 2;
238                 }
239                 $total += $ItemTotal;
240                 $grandtotal += $ItemTotal;
241         }
242         if ($detail)
243         {
244                 $rep->NewLine(2, 3);
245                 $rep->TextCol(0, 4, _('Total'));
246         }
247         $rep->Amountcol(5, 6, $total, $dec);
248         if ($detail)
249         {
250                 $rep->Line($rep->row - 2);
251                 $rep->NewLine();
252         }
253         $rep->NewLine(2, 1);
254         $rep->TextCol(0, 4, _('Grand Total'));
255         $rep->AmountCol(5, 6, $grandtotal, $dec);
256         $rep->Line($rep->row  - 4);
257         $rep->NewLine();
258     $rep->End();
259 }
260
261 ?>