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