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