e0286c63bc31e3f90cc6cf7040dd3d11a059fced
[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 fetch_items($category=0)
34 {
35                 $sql = "SELECT stock_id, stock.description AS name,
36                                 stock.category_id,units,
37                                 cat.description
38                         FROM ".TB_PREF."stock_master stock LEFT JOIN ".TB_PREF."stock_category cat ON stock.category_id=cat.category_id
39                                 WHERE mb_flag <> 'D'";
40                 if ($category != 0)
41                         $sql .= " AND cat.category_id = ".db_escape($category);
42                 $sql .= " ORDER BY stock.category_id, stock_id";
43
44     return db_query($sql,"No transactions were returned");
45 }
46
47 function trans_qty($stock_id, $location=null, $from_date, $to_date, $inward = true)
48 {
49         if ($from_date == null)
50                 $from_date = Today();
51
52         $from_date = date2sql($from_date);      
53
54         if ($to_date == null)
55                 $to_date = Today();
56
57         $to_date = date2sql($to_date);
58
59         $sql = "SELECT ".($inward ? '' : '-')."SUM(qty) FROM ".TB_PREF."stock_moves
60                 WHERE stock_id=".db_escape($stock_id)."
61                 AND tran_date >= '$from_date' 
62                 AND tran_date <= '$to_date'";
63
64         if ($location != '')
65                 $sql .= " AND loc_code = ".db_escape($location);
66
67         if ($inward)
68                 $sql .= " AND qty > 0 ";
69         else
70                 $sql .= " AND qty < 0 ";
71
72         $result = db_query($sql, "QOH calculation failed");
73
74         $myrow = db_fetch_row($result); 
75
76         return $myrow[0];
77
78 }
79
80 function avg_unit_cost($stock_id, $location=null, $to_date)
81 {
82         if ($to_date == null)
83                 $to_date = Today();
84
85         $to_date = date2sql($to_date);
86
87         $sql = "SELECT AVG (standard_cost)   FROM ".TB_PREF."stock_moves
88                 WHERE stock_id=".db_escape($stock_id)."
89                 AND tran_date < '$to_date'";
90
91         if ($location != '')
92                 $sql .= " AND loc_code = ".db_escape($location);
93
94         $result = db_query($sql, "QOH calculation failed");
95
96         $myrow = db_fetch_row($result); 
97
98         return $myrow[0];
99
100 }
101
102 //----------------------------------------------------------------------------------------------------
103
104 function trans_qty_unit_cost($stock_id, $location=null, $from_date, $to_date, $inward = true)
105 {
106         if ($from_date == null)
107                 $from_date = Today();
108
109         $from_date = date2sql($from_date);      
110
111         if ($to_date == null)
112                 $to_date = Today();
113
114         $to_date = date2sql($to_date);
115
116         $sql = "SELECT AVG (standard_cost)   FROM ".TB_PREF."stock_moves
117                 WHERE stock_id=".db_escape($stock_id)."
118                 AND tran_date >= '$from_date' 
119                 AND tran_date <= '$to_date'";
120
121         if ($location != '')
122                 $sql .= " AND loc_code = ".db_escape($location);
123
124         if ($inward)
125                 $sql .= " AND qty > 0 ";
126         else
127                 $sql .= " AND qty < 0 ";
128
129         $result = db_query($sql, "QOH calculation failed");
130
131         $myrow = db_fetch_row($result); 
132
133         return $myrow[0];
134
135 }
136
137 //----------------------------------------------------------------------------------------------------
138
139 function inventory_movements()
140 {
141     global $path_to_root;
142
143     $from_date = $_POST['PARAM_0'];
144     $to_date = $_POST['PARAM_1'];
145     $category = $_POST['PARAM_2'];
146         $location = $_POST['PARAM_3'];
147     $comments = $_POST['PARAM_4'];
148         $orientation = $_POST['PARAM_5'];
149         $destination = $_POST['PARAM_6'];
150         if ($destination)
151                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
152         else
153                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
154
155         $orientation = ($orientation ? 'L' : 'P');
156         if ($category == ALL_NUMERIC)
157                 $category = 0;
158         if ($category == 0)
159                 $cat = _('All');
160         else
161                 $cat = get_category_name($category);
162
163         if ($location == '')
164                 $loc = _('All');
165         else
166                 $loc = get_location_name($location);
167
168         $cols = array(0, 60, 130, 160, 185, 215, 250, 275, 305, 340, 365, 395, 430, 455, 485, 520);
169
170         $headers = array(_('Category'), _('Description'),       _('UOM'), '', '', _('OpeningStock'), '', '',_('StockIn'), '', '', _('Delivery'), '', '', _('ClosingStock'));
171         $headers2 = array("", "", "", _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"));
172
173         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right','right' ,'right', 'right', 'right','right', 'right', 'right', 'right');
174
175     $params =   array(  0 => $comments,
176                                                 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
177                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
178                                                 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
179
180     $rep = new FrontReport(_('Costed Inventory Movements'), "CostedInventoryMovements", user_pagesize(), 8, $orientation);
181     if ($orientation == 'L')
182         recalculate_cols($cols);
183
184     $rep->Font();
185     $rep->Info($params, $cols, $headers2, $aligns, $cols, $headers, $aligns);
186     $rep->NewPage();
187
188         $totval_open = $totval_in = $totval_out = $totval_close = 0; 
189         $result = fetch_items($category);
190
191         $dec = user_price_dec();
192         $catgor = '';
193         while ($myrow=db_fetch($result))
194         {
195                 if ($catgor != $myrow['description'])
196                 {
197                         $rep->NewLine(2);
198                         $rep->fontSize += 2;
199                         $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
200                         $catgor = $myrow['description'];
201                         $rep->fontSize -= 2;
202                         $rep->NewLine();
203                 }
204                 $rep->NewLine();
205                 $rep->TextCol(0, 1,     $myrow['stock_id']);
206                 $rep->TextCol(1, 2, $myrow['name']);
207                 $rep->TextCol(2, 3, $myrow['units']);
208                 
209                 $qoh_start = get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
210                 $qoh_end = get_qoh_on_date($myrow['stock_id'], $location, $to_date);
211                 
212                 $inward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
213                 $outward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
214                 $openCost = avg_unit_cost($myrow['stock_id'], $location, $from_date);
215                 $unitCost = avg_unit_cost($myrow['stock_id'], $location, add_days($to_date, 1));
216                 $rep->AmountCol(3, 4, $qoh_start, get_qty_dec($myrow['stock_id']));
217                 $rep->AmountCol(4, 5, $openCost, $dec);
218                 $openCost *= $qoh_start;
219                 $totval_open += $openCost;
220                 $rep->AmountCol(5, 6, $openCost);
221                 
222                 if($inward>0){
223                         $rep->AmountCol(6, 7, $inward, get_qty_dec($myrow['stock_id']));
224                         $unitCost_in = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date);
225                         $rep->AmountCol(7, 8, $unitCost_in,$dec);
226                         $unitCost_in *= $inward;
227                         $totval_in += $unitCost_in;
228                         $rep->AmountCol(8, 9, $unitCost_in);
229                 }
230                 
231                 if($outward>0){
232                         $rep->AmountCol(9, 10, $outward, get_qty_dec($myrow['stock_id']));
233                         $unitCost_out = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date, false);
234                         $rep->AmountCol(10, 11, $unitCost_out,$dec);
235                         $unitCost_out *= $outward;
236                         $totval_out += $unitCost_out;
237                         $rep->AmountCol(11, 12, $unitCost_out);
238                 }
239                 
240                 $rep->AmountCol(12, 13, $qoh_end, get_qty_dec($myrow['stock_id']));
241                 $rep->AmountCol(13, 14, $unitCost,$dec);
242                 $unitCost *= $qoh_end;
243                 $totval_close += $unitCost;
244                 $rep->AmountCol(14, 15, $unitCost);
245                 
246                 $rep->NewLine(0, 1);
247         }
248         $rep->Line($rep->row  - 4);
249         $rep->NewLine(2);
250         $rep->TextCol(0, 1,     _("Total"));
251         $rep->AmountCol(5, 6, $totval_open);
252         $rep->AmountCol(8, 9, $totval_in);
253         $rep->AmountCol(11, 12, $totval_out);
254         $rep->AmountCol(14, 15, $totval_close);
255         $rep->Line($rep->row  - 4);
256
257     $rep->End();
258 }
259
260 ?>