Optimized Inventory Valuation Reports, 301 and 308.
[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.3 $
15 // Creator:     Joe Hunt, boxygen
16 // date_:       2017-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 print_inventory_valuation_report();
28
29 function get_domestic_price($myrow, $stock_id)
30 {
31     if ($myrow['type'] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
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     }
43     else
44         $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
45     return $price;
46 }
47
48 function getAverageCost($stock_id, $location, $to_date)
49 {
50     if ($to_date == null)
51         $to_date = Today();
52     
53     $to_date = date2sql($to_date);
54     
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;
58         
59         if ($location != 'all')
60                 $sql .= " AND loc_code = ".db_escape($location);
61    
62     $sql .= " ORDER BY tran_date";
63     $result = db_query($sql, "No standard cost transactions were returned");
64     
65     if ($result == false)
66         return 0;
67     $qty = $tot_cost = 0;
68     while ($row=db_fetch($result))
69     {
70         $qty += $row['qty'];
71         $price = get_domestic_price($row, $stock_id);
72         $tran_cost = $row['qty'] * $price;
73         $tot_cost += $tran_cost;
74     }
75     if ($qty == 0)
76         return 0;
77     return $tot_cost / $qty;
78 }
79
80 function getTransactions($category, $location, $date)
81 {
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";
106     if ($category != 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";
112     
113     return db_query($sql,"No transactions were returned");
114 }
115 //----------------------------------------------------------------------------------------------------
116 function print_inventory_valuation_report()
117 {
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'];
126     if ($destination)
127         include_once($path_to_root . "/reporting/includes/excel_report.inc");
128     else
129         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
130     $detail = !$detail;
131     $dec = user_price_dec();
132     $orientation = ($orientation ? 'L' : 'P');
133     if ($category == ALL_NUMERIC)
134         $category = 0;
135     if ($category == 0)
136         $cat = _('All');
137     else
138         $cat = get_category_name($category);
139     if ($location == ALL_TEXT)
140         $location = 'all';
141     if ($location == 'all')
142         $loc = _('All');
143     else
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);
155     $rep->Font();
156     $rep->Info($params, $cols, $headers, $aligns);
157     $rep->NewPage();
158     $res = getTransactions($category, $location, $date);
159     $total = $grandtotal = 0.0;
160     $catt = '';
161     while ($trans=db_fetch($res))
162     {
163         if ($catt != $trans['cat_description'])
164         {
165             if ($catt != '')
166             {
167                 if ($detail)
168                 {
169                     $rep->NewLine(2, 3);
170                     $rep->TextCol(0, 4, _('Total'));
171                 }
172                 $rep->AmountCol(5, 6, $total, $dec);
173                 if ($detail)
174                 {
175                     $rep->Line($rep->row - 2);
176                     $rep->NewLine();
177                 }
178                 $rep->NewLine();
179                 $total = 0.0;
180             }
181             $rep->TextCol(0, 1, $trans['category_id']);
182             $rep->TextCol(1, 2, $trans['cat_description']);
183             $catt = $trans['cat_description'];
184             if ($detail)
185                 $rep->NewLine();
186         }
187         if (isset($use_costed_values) && $use_costed_values==1)
188         {
189             $UnitCost = getAverageCost($trans['stock_id'], $location, $date);
190             $ItemTotal = $trans['QtyOnHand'] * $UnitCost;
191         }
192         else
193         {
194             $UnitCost = $trans['UnitCost'];
195             $ItemTotal = $trans['ItemTotal'];
196         }
197         if ($detail)
198         {
199             $rep->NewLine();
200             $rep->fontSize -= 2;
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']));
205             $dec2 = 0;
206             price_decimal_format($UnitCost, $dec2);
207             $rep->AmountCol(4, 5, $UnitCost, $dec2);
208             $rep->AmountCol(5, 6, $ItemTotal, $dec);
209             $rep->fontSize += 2;
210         }
211         $total += $ItemTotal;
212         $grandtotal += $ItemTotal;
213     }
214     if ($detail)
215     {
216         $rep->NewLine(2, 3);
217         $rep->TextCol(0, 4, _('Total'));
218     }
219     $rep->Amountcol(5, 6, $total, $dec);
220     if ($detail)
221     {
222         $rep->Line($rep->row - 2);
223         $rep->NewLine();
224     }
225     $rep->NewLine(2, 1);
226     $rep->TextCol(0, 4, _('Grand Total'));
227     $rep->AmountCol(5, 6, $grandtotal, $dec);
228     $rep->Line($rep->row  - 4);
229     $rep->NewLine();
230     $rep->End();
231 }
232 ?>