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 // ----------------------------------------------------------------
17 // Title: Inventory Valuation
18 // ----------------------------------------------------------------
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");
27 //----------------------------------------------------------------------------------------------------
29 print_inventory_valuation_report();
31 function get_domestic_price($myrow, $stock_id, $qty, $old_std_cost, $old_qty)
33 if ($myrow['type'] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
35 $price = $myrow['price'];
36 if ($myrow['type'] == ST_SUPPRECEIVE)
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'])
48 if ($myrow['person_id'] > 0)
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']));
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'];
60 $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
64 function getAverageCost($stock_id, $to_date)
69 $to_date = date2sql($to_date);
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;
75 $sql .= " ORDER BY tran_date";
77 $result = db_query($sql, "No standard cost transactions were returned");
80 $qty = $old_qty = $count = $old_std_cost = $tot_cost = 0;
81 while ($row=db_fetch($result))
85 $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty);
87 $old_std_cost = $row['standard_cost'];
94 return $tot_cost / $count;
97 function getTransactions($category, $location, $date)
99 $date = date2sql($date);
101 $sql = "SELECT item.category_id,
102 category.description AS cat_description,
105 item.description, item.inactive,
107 SUM(move.qty) AS QtyOnHand,
108 item.material_cost AS UnitCost,
109 SUM(move.qty) * item.material_cost AS ItemTotal
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,
124 HAVING SUM(move.qty) != 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,
132 return db_query($sql,"No transactions were returned");
135 //----------------------------------------------------------------------------------------------------
137 function print_inventory_valuation_report()
139 global $path_to_root, $SysPrefs;
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'];
149 include_once($path_to_root . "/reporting/includes/excel_report.inc");
151 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
153 $dec = user_price_dec();
155 $orientation = ($orientation ? 'L' : 'P');
156 if ($category == ALL_NUMERIC)
161 $cat = get_category_name($category);
163 if ($location == ALL_TEXT)
165 if ($location == 'all')
168 $loc = get_location_name($location);
170 $cols = array(0, 75, 225, 250, 350, 450, 515);
172 $headers = array(_('Category'), '', _('UOM'), _('Quantity'), _('Unit Cost'), _('Value'));
174 $aligns = array('left', 'left', 'left', 'right', 'right', 'right');
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' => ''));
181 $rep = new FrontReport(_('Inventory Valuation Report'), "InventoryValReport", user_pagesize(), 9, $orientation);
182 if ($orientation == 'L')
183 recalculate_cols($cols);
185 $rep->Info($params, $cols, $headers, $aligns);
188 $res = getTransactions($category, $location, $date);
189 $total = $grandtotal = 0.0;
191 while ($trans=db_fetch($res))
193 if ($catt != $trans['cat_description'])
200 $rep->TextCol(0, 4, _('Total'));
202 $rep->AmountCol(5, 6, $total, $dec);
205 $rep->Line($rep->row - 2);
211 $rep->TextCol(0, 1, $trans['category_id']);
212 $rep->TextCol(1, 2, $trans['cat_description']);
213 $catt = $trans['cat_description'];
217 if (isset($SysPrefs->use_costed_values) && $SysPrefs->use_costed_values==1)
219 $UnitCost = getAverageCost($trans['stock_id'], $date);
220 $ItemTotal = $trans['QtyOnHand'] * $UnitCost;
224 $UnitCost = $trans['UnitCost'];
225 $ItemTotal = $trans['ItemTotal'];
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']));
237 price_decimal_format($UnitCost, $dec2);
238 $rep->AmountCol(4, 5, $UnitCost, $dec2);
239 $rep->AmountCol(5, 6, $ItemTotal, $dec);
242 $total += $ItemTotal;
243 $grandtotal += $ItemTotal;
248 $rep->TextCol(0, 4, _('Total'));
250 $rep->Amountcol(5, 6, $total, $dec);
253 $rep->Line($rep->row - 2);
257 $rep->TextCol(0, 4, _('Grand Total'));
258 $rep->AmountCol(5, 6, $grandtotal, $dec);
259 $rep->Line($rep->row - 4);