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 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'])
46 if ($myrow['person_id'] > 0)
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']));
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'];
58 $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
62 function getAverageCost($stock_id, $to_date)
67 $to_date = date2sql($to_date);
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;
73 $sql .= " ORDER BY tran_date";
75 $result = db_query($sql, "No standard cost transactions were returned");
78 $qty = $old_qty = $count = $old_std_cost = $tot_cost = 0;
79 while ($row=db_fetch($result))
83 $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty);
85 $old_std_cost = $row['standard_cost'];
92 return $tot_cost / $count;
95 function getTransactions($category, $location, $date)
97 $date = date2sql($date);
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";
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";
129 return db_query($sql,"No transactions were returned");
132 //----------------------------------------------------------------------------------------------------
134 function print_inventory_valuation_report()
136 global $path_to_root, $use_costed_values;;
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'];
146 include_once($path_to_root . "/reporting/includes/excel_report.inc");
148 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
150 $dec = user_price_dec();
152 $orientation = ($orientation ? 'L' : 'P');
153 if ($category == ALL_NUMERIC)
158 $cat = get_category_name($category);
160 if ($location == ALL_TEXT)
162 if ($location == 'all')
165 $loc = get_location_name($location);
167 $cols = array(0, 75, 225, 250, 350, 450, 515);
169 $headers = array(_('Category'), '', _('UOM'), _('Quantity'), _('Unit Cost'), _('Value'));
171 $aligns = array('left', 'left', 'left', 'right', 'right', 'right');
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' => ''));
178 $rep = new FrontReport(_('Inventory Valuation Report'), "InventoryValReport", user_pagesize(), 9, $orientation);
179 if ($orientation == 'L')
180 recalculate_cols($cols);
182 $rep->Info($params, $cols, $headers, $aligns);
185 $res = getTransactions($category, $location, $date);
186 $total = $grandtotal = 0.0;
188 while ($trans=db_fetch($res))
190 if ($catt != $trans['cat_description'])
197 $rep->TextCol(0, 4, _('Total'));
199 $rep->AmountCol(5, 6, $total, $dec);
202 $rep->Line($rep->row - 2);
208 $rep->TextCol(0, 1, $trans['category_id']);
209 $rep->TextCol(1, 2, $trans['cat_description']);
210 $catt = $trans['cat_description'];
214 if (isset($use_costed_values) && $use_costed_values==1)
216 $UnitCost = getAverageCost($trans['stock_id'], $date);
217 $ItemTotal = $trans['QtyOnHand'] * $UnitCost;
221 $UnitCost = $trans['UnitCost'];
222 $ItemTotal = $trans['ItemTotal'];
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']));
234 price_decimal_format($UnitCost, $dec2);
235 $rep->AmountCol(4, 5, $UnitCost, $dec2);
236 $rep->AmountCol(5, 6, $ItemTotal, $dec);
239 $total += $ItemTotal;
240 $grandtotal += $ItemTotal;
245 $rep->TextCol(0, 4, _('Total'));
247 $rep->Amountcol(5, 6, $total, $dec);
250 $rep->Line($rep->row - 2);
254 $rep->TextCol(0, 4, _('Grand Total'));
255 $rep->AmountCol(5, 6, $grandtotal, $dec);
256 $rep->Line($rep->row - 4);