0631e46dba74f0a1535d9a7e45dfa31718831eac
[fa-stable.git] / reporting / rep308.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.0 $
15 // Creator:     Jujuk, Joe Hunt
16 // date_:       2011-05-24
17 // Title:       Stock Movements
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/ui/ui_input.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
27 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
28
29 //----------------------------------------------------------------------------------------------------
30
31 inventory_movements();
32
33 function get_domestic_price($myrow, $stock_id, $qty, $old_std_cost, $old_qty)
34 {
35         if ($myrow['type'] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
36         {
37                 $price = $myrow['price'];
38                 if ($myrow['type'] == ST_SUPPRECEIVE)
39                 {
40                         // Has the supplier invoice increased the receival price?
41                         $sql = "SELECT DISTINCT act_price FROM ".TB_PREF."purch_order_details pod 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'])
46                                 $price = $row[0];
47                 }
48                 if ($myrow['person_id'] > 0)
49                 {
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']));
54                         $price /= $ex_rate;
55                 }       
56         }
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'];
59         else
60                 $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
61         return $price;
62 }       
63
64 function fetch_items($category=0)
65 {
66                 $sql = "SELECT stock_id, stock.description AS name,
67                                 stock.category_id,units,
68                                 cat.description
69                         FROM ".TB_PREF."stock_master stock LEFT JOIN ".TB_PREF."stock_category cat ON stock.category_id=cat.category_id
70                                 WHERE mb_flag <> 'D'";
71                 if ($category != 0)
72                         $sql .= " AND cat.category_id = ".db_escape($category);
73                 $sql .= " ORDER BY stock.category_id, stock_id";
74
75     return db_query($sql,"No transactions were returned");
76 }
77
78 function trans_qty($stock_id, $location=null, $from_date, $to_date, $inward = true)
79 {
80         if ($from_date == null)
81                 $from_date = Today();
82
83         $from_date = date2sql($from_date);      
84
85         if ($to_date == null)
86                 $to_date = Today();
87
88         $to_date = date2sql($to_date);
89
90         $sql = "SELECT ".($inward ? '' : '-')."SUM(qty) FROM ".TB_PREF."stock_moves
91                 WHERE stock_id=".db_escape($stock_id)."
92                 AND tran_date >= '$from_date' 
93                 AND tran_date <= '$to_date' AND type <> ".ST_LOCTRANSFER;
94
95         if ($location != '')
96                 $sql .= " AND loc_code = ".db_escape($location);
97
98         if ($inward)
99                 $sql .= " AND qty > 0 ";
100         else
101                 $sql .= " AND qty < 0 ";
102
103         $result = db_query($sql, "QOH calculation failed");
104
105         $myrow = db_fetch_row($result); 
106
107         return $myrow[0];
108
109 }
110
111 function avg_unit_cost($stock_id, $location=null, $to_date)
112 {
113         if ($to_date == null)
114                 $to_date = Today();
115
116         $to_date = date2sql($to_date);
117
118         $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id  FROM ".TB_PREF."stock_moves
119                 WHERE stock_id=".db_escape($stock_id)."
120                 AND tran_date < '$to_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
121
122         if ($location != '')
123                 $sql .= " AND loc_code = ".db_escape($location);
124         $sql .= " ORDER BY tran_date";  
125
126         $result = db_query($sql, "No standard cost transactions were returned");
127     if ($result == false)
128         return 0;
129         $qty = $old_qty = $count = $old_std_cost = $tot_cost = 0;
130         while ($row=db_fetch($result))
131         {
132                 $qty += $row['qty'];    
133
134                 $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty);
135         
136                 $old_std_cost = $row['standard_cost'];
137                 $tot_cost += $price;
138                 $count++;
139                 $old_qty = $qty;
140         }
141         if ($count == 0)
142                 return 0;
143         return $tot_cost / $count;
144 }
145
146 //----------------------------------------------------------------------------------------------------
147
148 function trans_qty_unit_cost($stock_id, $location=null, $from_date, $to_date, $inward = true)
149 {
150         if ($from_date == null)
151                 $from_date = Today();
152
153         $from_date = date2sql($from_date);      
154
155         if ($to_date == null)
156                 $to_date = Today();
157
158         $to_date = date2sql($to_date);
159
160         $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM ".TB_PREF."stock_moves
161                 WHERE stock_id=".db_escape($stock_id)."
162                 AND tran_date <= '$to_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
163
164         if ($location != '')
165                 $sql .= " AND loc_code = ".db_escape($location);
166
167         if ($inward)
168                 $sql .= " AND qty > 0 ";
169         else
170                 $sql .= " AND qty < 0 ";
171         $sql .= " ORDER BY tran_date";
172         $result = db_query($sql, "No standard cost transactions were returned");
173     if ($result == false)
174         return 0;
175         $qty = $count = $old_qty = $old_std_cost = $tot_cost = 0;
176         while ($row=db_fetch($result))
177         {
178                 $qty += $row['qty'];
179
180                 $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty);
181         
182                 if (strncmp($row['tran_date'], $from_date,10) >= 0)
183                 {
184                         $tot_cost += $price;
185                         $count++;
186                 }
187                 
188                 $old_std_cost = $row['standard_cost'];
189                 $old_qty = $qty;
190         }       
191         if ($count == 0)
192                 return 0;
193         return $tot_cost / $count;
194
195 }
196
197 //----------------------------------------------------------------------------------------------------
198
199 function inventory_movements()
200 {
201     global $path_to_root;
202
203     $from_date = $_POST['PARAM_0'];
204     $to_date = $_POST['PARAM_1'];
205     $category = $_POST['PARAM_2'];
206         $location = $_POST['PARAM_3'];
207     $comments = $_POST['PARAM_4'];
208         $orientation = $_POST['PARAM_5'];
209         $destination = $_POST['PARAM_6'];
210         if ($destination)
211                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
212         else
213                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
214
215         $orientation = ($orientation ? 'L' : 'P');
216         if ($category == ALL_NUMERIC)
217                 $category = 0;
218         if ($category == 0)
219                 $cat = _('All');
220         else
221                 $cat = get_category_name($category);
222
223         if ($location == '')
224                 $loc = _('All');
225         else
226                 $loc = get_location_name($location);
227
228         $cols = array(0, 60, 130, 160, 185, 215, 250, 275, 305, 340, 365, 395, 430, 455, 485, 520);
229
230         $headers = array(_('Category'), _('Description'),       _('UOM'), '', '', _('OpeningStock'), '', '',_('StockIn'), '', '', _('Delivery'), '', '', _('ClosingStock'));
231         $headers2 = array("", "", "", _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"));
232
233         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right','right' ,'right', 'right', 'right','right', 'right', 'right', 'right');
234
235     $params =   array(  0 => $comments,
236                                                 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
237                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
238                                                 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
239
240     $rep = new FrontReport(_('Costed Inventory Movements'), "CostedInventoryMovements", user_pagesize(), 8, $orientation);
241     if ($orientation == 'L')
242         recalculate_cols($cols);
243
244     $rep->Font();
245     $rep->Info($params, $cols, $headers2, $aligns, $cols, $headers, $aligns);
246     $rep->NewPage();
247
248         $totval_open = $totval_in = $totval_out = $totval_close = 0; 
249         $result = fetch_items($category);
250
251         $dec = user_price_dec();
252         $catgor = '';
253         while ($myrow=db_fetch($result))
254         {
255                 if ($catgor != $myrow['description'])
256                 {
257                         $rep->NewLine(2);
258                         $rep->fontSize += 2;
259                         $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
260                         $catgor = $myrow['description'];
261                         $rep->fontSize -= 2;
262                         $rep->NewLine();
263                 }
264                 $qoh_start = get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
265                 $qoh_end = get_qoh_on_date($myrow['stock_id'], $location, $to_date);
266                 
267                 $inward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
268                 $outward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
269                 $openCost = avg_unit_cost($myrow['stock_id'], $location, $from_date);
270                 $unitCost = avg_unit_cost($myrow['stock_id'], $location, add_days($to_date, 1));
271                 if ($qoh_start == 0 && $inward == 0 && $outward == 0 && $qoh_end == 0)
272                         continue;
273                 $rep->NewLine();
274                 $rep->TextCol(0, 1,     $myrow['stock_id']);
275                 $rep->TextCol(1, 2, $myrow['name']);
276                 $rep->TextCol(2, 3, $myrow['units']);
277                 $rep->AmountCol(3, 4, $qoh_start, get_qty_dec($myrow['stock_id']));
278                 $rep->AmountCol(4, 5, $openCost, $dec);
279                 $openCost *= $qoh_start;
280                 $totval_open += $openCost;
281                 $rep->AmountCol(5, 6, $openCost);
282                 
283                 if($inward>0){
284                         $rep->AmountCol(6, 7, $inward, get_qty_dec($myrow['stock_id']));
285                         $unitCost_in = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date);
286                         $rep->AmountCol(7, 8, $unitCost_in,$dec);
287                         $unitCost_in *= $inward;
288                         $totval_in += $unitCost_in;
289                         $rep->AmountCol(8, 9, $unitCost_in);
290                 }
291                 
292                 if($outward>0){
293                         $rep->AmountCol(9, 10, $outward, get_qty_dec($myrow['stock_id']));
294                         $unitCost_out = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date, false);
295                         $rep->AmountCol(10, 11, $unitCost_out,$dec);
296                         $unitCost_out *= $outward;
297                         $totval_out += $unitCost_out;
298                         $rep->AmountCol(11, 12, $unitCost_out);
299                 }
300                 
301                 $rep->AmountCol(12, 13, $qoh_end, get_qty_dec($myrow['stock_id']));
302                 $rep->AmountCol(13, 14, $unitCost,$dec);
303                 $unitCost *= $qoh_end;
304                 $totval_close += $unitCost;
305                 $rep->AmountCol(14, 15, $unitCost);
306                 
307                 $rep->NewLine(0, 1);
308         }
309         $rep->Line($rep->row  - 4);
310         $rep->NewLine(2);
311         $rep->TextCol(0, 1,     _("Total"));
312         $rep->AmountCol(5, 6, $totval_open);
313         $rep->AmountCol(8, 9, $totval_in);
314         $rep->AmountCol(11, 12, $totval_out);
315         $rep->AmountCol(14, 15, $totval_close);
316         $rep->Line($rep->row  - 4);
317
318     $rep->End();
319 }
320
321 ?>