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