Avoid running same query multiple times. Rep307.php. Fixed
[fa-stable.git] / reporting / rep307.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,
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' AND mb_flag <>'F'";
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 inventory_movements()
84 {
85     global $path_to_root;
86
87     $from_date = $_POST['PARAM_0'];
88     $to_date = $_POST['PARAM_1'];
89     $category = $_POST['PARAM_2'];
90         $location = $_POST['PARAM_3'];
91     $comments = $_POST['PARAM_4'];
92         $orientation = $_POST['PARAM_5'];
93         $destination = $_POST['PARAM_6'];
94         if ($destination)
95                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
96         else
97                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
98
99         $orientation = ($orientation ? 'L' : 'P');
100         if ($category == ALL_NUMERIC)
101                 $category = 0;
102         if ($category == 0)
103                 $cat = _('All');
104         else
105                 $cat = get_category_name($category);
106
107         if ($location == '')
108                 $loc = _('All');
109         else
110                 $loc = get_location_name($location);
111
112         $cols = array(0, 60, 220, 240, 310, 380, 450, 520);
113
114         $headers = array(_('Category'), _('Description'),       _('UOM'),       _('Opening'), _('Quantity In'), _('Quantity Out'), _('Balance'));
115
116         $aligns = array('left', 'left', 'left', 'right', 'right', 'right','right');
117
118     $params =   array(  0 => $comments,
119                                                 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
120                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
121                                                 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
122
123     $rep = new FrontReport(_('Inventory Movements'), "InventoryMovements", user_pagesize(), 9, $orientation);
124     if ($orientation == 'L')
125         recalculate_cols($cols);
126
127     $rep->Font();
128     $rep->Info($params, $cols, $headers, $aligns);
129     $rep->NewPage();
130
131         $result = fetch_items($category);
132
133         $catgor = '';
134         while ($myrow=db_fetch($result))
135         {
136                 if ($catgor != $myrow['description'])
137                 {
138                         $rep->Line($rep->row  - $rep->lineHeight);
139                         $rep->NewLine(2);
140                         $rep->fontSize += 2;
141                         $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
142                         $catgor = $myrow['description'];
143                         $rep->fontSize -= 2;
144                         $rep->NewLine();
145                 }
146                 $rep->NewLine();
147                 $rep->TextCol(0, 1,     $myrow['stock_id']);
148                 $rep->TextCol(1, 2, $myrow['name']);
149                 $rep->TextCol(2, 3, $myrow['units']);
150                 $qoh_start= $inward = $outward = $qoh_end = 0; 
151                 
152                 $qoh_start += get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
153                 $qoh_end += get_qoh_on_date($myrow['stock_id'], $location, $to_date);
154                 
155                 $inward += trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
156                 $outward += trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
157
158                 $stock_qty_dec = get_qty_dec($myrow['stock_id']);
159                 $rep->AmountCol(3, 4, $qoh_start, $stock_qty_dec);
160                 $rep->AmountCol(4, 5, $inward, $stock_qty_dec);
161                 $rep->AmountCol(5, 6, $outward, $stock_qty_dec);
162                 $rep->AmountCol(6, 7, $qoh_end, $stock_qty_dec);
163                 $rep->NewLine(0, 1);
164         }
165         $rep->Line($rep->row  - 4);
166
167         $rep->NewLine();
168     $rep->End();
169 }
170