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