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 // ----------------------------------------------------------------
15 // Creator: Joe Hunt, boxygen
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");
26 //----------------------------------------------------------------------------------------------------
27 print_inventory_valuation_report();
29 function get_domestic_price($myrow, $stock_id)
31 if ($myrow['type'] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
33 $price = $myrow['price'];
34 if ($myrow['person_id'] > 0)
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']));
44 $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
48 function getAverageCost($stock_id, $location, $to_date)
53 $to_date = date2sql($to_date);
55 $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM ".TB_PREF."stock_moves
56 WHERE stock_id=".db_escape($stock_id)."
57 AND tran_date <= '$to_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
59 if ($location != 'all')
60 $sql .= " AND loc_code = ".db_escape($location);
62 $sql .= " ORDER BY tran_date";
63 $result = db_query($sql, "No standard cost transactions were returned");
68 while ($row=db_fetch($result))
71 $price = get_domestic_price($row, $stock_id);
72 $tran_cost = $row['qty'] * $price;
73 $tot_cost += $tran_cost;
77 return $tot_cost / $qty;
80 function getTransactions($category, $location, $date)
82 $date = date2sql($date);
83 $sql = "SELECT ".TB_PREF."stock_master.category_id,
84 ".TB_PREF."stock_category.description AS cat_description,
85 ".TB_PREF."stock_master.stock_id,
86 ".TB_PREF."stock_master.units,
87 ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
88 ".TB_PREF."stock_moves.loc_code,
89 SUM(".TB_PREF."stock_moves.qty) AS QtyOnHand,
90 ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS UnitCost,
91 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
92 FROM ".TB_PREF."stock_master,
93 ".TB_PREF."stock_category,
94 ".TB_PREF."stock_moves
95 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
96 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
97 AND ".TB_PREF."stock_master.mb_flag<>'D'
98 AND ".TB_PREF."stock_moves.tran_date <= '$date'
99 GROUP BY ".TB_PREF."stock_master.category_id,
100 ".TB_PREF."stock_category.description, ";
101 if ($location != 'all')
102 $sql .= TB_PREF."stock_moves.loc_code, ";
103 $sql .= TB_PREF."stock_master.stock_id,
104 ".TB_PREF."stock_master.description
105 HAVING SUM(".TB_PREF."stock_moves.qty) != 0";
107 $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
108 if ($location != 'all')
109 $sql .= " AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location);
110 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
111 ".TB_PREF."stock_master.stock_id";
113 return db_query($sql,"No transactions were returned");
115 //----------------------------------------------------------------------------------------------------
116 function print_inventory_valuation_report()
118 global $path_to_root, $use_costed_values;;
119 $date = $_POST['PARAM_0'];
120 $category = $_POST['PARAM_1'];
121 $location = $_POST['PARAM_2'];
122 $detail = $_POST['PARAM_3'];
123 $comments = $_POST['PARAM_4'];
124 $orientation = $_POST['PARAM_5'];
125 $destination = $_POST['PARAM_6'];
127 include_once($path_to_root . "/reporting/includes/excel_report.inc");
129 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
131 $dec = user_price_dec();
132 $orientation = ($orientation ? 'L' : 'P');
133 if ($category == ALL_NUMERIC)
138 $cat = get_category_name($category);
139 if ($location == ALL_TEXT)
141 if ($location == 'all')
144 $loc = get_location_name($location);
145 $cols = array(0, 75, 225, 250, 350, 450, 515);
146 $headers = array(_('Category'), '', _('UOM'), _('Quantity'), _('Unit Cost'), _('Value'));
147 $aligns = array('left', 'left', 'left', 'right', 'right', 'right');
148 $params = array( 0 => $comments,
149 1 => array('text' => _('End Date'), 'from' => $date, 'to' => ''),
150 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
151 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
152 $rep = new FrontReport(_('Inventory Valuation Report'), "InventoryValReport", user_pagesize(), 9, $orientation);
153 if ($orientation == 'L')
154 recalculate_cols($cols);
156 $rep->Info($params, $cols, $headers, $aligns);
158 $res = getTransactions($category, $location, $date);
159 $total = $grandtotal = 0.0;
161 while ($trans=db_fetch($res))
163 if ($catt != $trans['cat_description'])
170 $rep->TextCol(0, 4, _('Total'));
172 $rep->AmountCol(5, 6, $total, $dec);
175 $rep->Line($rep->row - 2);
181 $rep->TextCol(0, 1, $trans['category_id']);
182 $rep->TextCol(1, 2, $trans['cat_description']);
183 $catt = $trans['cat_description'];
187 if (isset($use_costed_values) && $use_costed_values==1)
189 $UnitCost = getAverageCost($trans['stock_id'], $location, $date);
190 $ItemTotal = $trans['QtyOnHand'] * $UnitCost;
194 $UnitCost = $trans['UnitCost'];
195 $ItemTotal = $trans['ItemTotal'];
201 $rep->TextCol(0, 1, $trans['stock_id']);
202 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
203 $rep->TextCol(2, 3, $trans['units']);
204 $rep->AmountCol(3, 4, $trans['QtyOnHand'], get_qty_dec($trans['stock_id']));
206 price_decimal_format($UnitCost, $dec2);
207 $rep->AmountCol(4, 5, $UnitCost, $dec2);
208 $rep->AmountCol(5, 6, $ItemTotal, $dec);
211 $total += $ItemTotal;
212 $grandtotal += $ItemTotal;
217 $rep->TextCol(0, 4, _('Total'));
219 $rep->Amountcol(5, 6, $total, $dec);
222 $rep->Line($rep->row - 2);
226 $rep->TextCol(0, 4, _('Grand Total'));
227 $rep->AmountCol(5, 6, $grandtotal, $dec);
228 $rep->Line($rep->row - 4);