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