Rerun of implemented Selectable Print Orientation (Portrait, Landscape) for all repor...
[fa-stable.git] / reporting / rep306.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_SALESANALYTIC';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Inventory Sales Report
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/data_checks.inc");
24 include_once($path_to_root . "/includes/banking.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_inventory_purchase();
31
32 function getTransactions($category, $location, $fromsupp, $item, $from, $to)
33 {
34         $from = date2sql($from);
35         $to = date2sql($to);
36         $sql = "SELECT ".TB_PREF."stock_master.category_id,
37                         ".TB_PREF."stock_category.description AS cat_description,
38                         ".TB_PREF."stock_master.stock_id,
39                         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
40                         ".TB_PREF."stock_moves.loc_code,
41                         ".TB_PREF."suppliers.supplier_id,
42                         ".TB_PREF."suppliers.supp_name AS supplier_name,
43                         ".TB_PREF."stock_moves.tran_date,
44                         ".TB_PREF."stock_moves.qty AS qty,
45                         ".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent) AS price
46                 FROM ".TB_PREF."stock_master,
47                         ".TB_PREF."stock_category,
48                         ".TB_PREF."suppliers,
49                         ".TB_PREF."stock_moves
50                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
51                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
52                 AND ".TB_PREF."stock_moves.person_id=".TB_PREF."suppliers.supplier_id
53                 AND ".TB_PREF."stock_moves.tran_date>='$from'
54                 AND ".TB_PREF."stock_moves.tran_date<='$to'
55                 AND (".TB_PREF."stock_moves.type=".ST_SUPPRECEIVE." OR ".TB_PREF."stock_moves.type=".ST_SUPPCREDIT.")
56                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
57                 if ($category != 0)
58                         $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
59                 if ($location != '')
60                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location);
61                 if ($fromsupp != '')
62                         $sql .= " AND ".TB_PREF."suppliers.supplier_id = ".db_escape($fromsupp);
63                 if ($item != '')
64                         $sql .= " AND ".TB_PREF."stock_master.stock_id = ".db_escape($item);
65                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
66                         ".TB_PREF."suppliers.supp_name, ".TB_PREF."stock_master.stock_id, ".TB_PREF."stock_moves.tran_date";
67     return db_query($sql,"No transactions were returned");
68
69 }
70
71 function get_supp_inv_reference($supplier_id, $stock_id, $date)
72 {
73         $sql = "SELECT ".TB_PREF."supp_trans.supp_reference
74                 FROM ".TB_PREF."supp_trans,
75                         ".TB_PREF."supp_invoice_items,
76                         ".TB_PREF."grn_batch,
77                         ".TB_PREF."grn_items
78                 WHERE ".TB_PREF."supp_trans.type=".TB_PREF."supp_invoice_items.supp_trans_type
79                 AND ".TB_PREF."supp_trans.trans_no=".TB_PREF."supp_invoice_items.supp_trans_no
80                 AND ".TB_PREF."grn_items.grn_batch_id=".TB_PREF."grn_batch.id
81                 AND ".TB_PREF."grn_items.item_code=".TB_PREF."supp_invoice_items.stock_id
82                 AND ".TB_PREF."supp_trans.supplier_id=".db_escape($supplier_id)."
83                 AND ".TB_PREF."supp_invoice_items.stock_id=".db_escape($stock_id)."
84                 AND ".TB_PREF."supp_trans.tran_date=".db_escape($date);
85     $result = db_query($sql,"No transactions were returned");
86     $row = db_fetch_row($result);
87     if (isset($row[0]))
88         return $row[0];
89     else
90         return '';
91 }    
92     
93         
94 //----------------------------------------------------------------------------------------------------
95
96 function print_inventory_purchase()
97 {
98     global $path_to_root;
99
100         $from = $_POST['PARAM_0'];
101         $to = $_POST['PARAM_1'];
102     $category = $_POST['PARAM_2'];
103     $location = $_POST['PARAM_3'];
104     $fromsupp = $_POST['PARAM_4'];
105     $item = $_POST['PARAM_5'];
106         $comments = $_POST['PARAM_6'];
107         $orientation = $_POST['PARAM_7'];
108         $destination = $_POST['PARAM_8'];
109         if ($destination)
110                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
111         else
112                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
113
114         $orientation = ($orientation ? 'L' : 'P');
115     $dec = user_price_dec();
116
117         if ($category == ALL_NUMERIC)
118                 $category = 0;
119         if ($category == 0)
120                 $cat = _('All');
121         else
122                 $cat = get_category_name($category);
123
124         if ($location == '')
125                 $loc = _('All');
126         else
127                 $loc = get_location_name($location);
128
129         if ($fromsupp == '')
130                 $froms = _('All');
131         else
132                 $froms = get_supplier_name($fromsupp);
133
134         if ($item == '')
135                 $itm = _('All');
136         else
137                 $itm = $item;
138
139         $cols = array(0, 60, 180, 225, 275, 400, 420, 465,      520);
140
141         $headers = array(_('Category'), _('Description'), _('Date'), _('#'), _('Supplier'), _('Qty'), _('Unit Price'), _('Total'));
142         if ($fromsupp != '')
143                 $headers[4] = '';
144
145         $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right', 'right');
146
147     $params =   array(  0 => $comments,
148                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
149                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
150                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
151                                     4 => array('text' => _('Supplier'), 'from' => $froms, 'to' => ''),
152                                     5 => array('text' => _('Item'), 'from' => $itm, 'to' => ''));
153
154     $rep = new FrontReport(_('Inventory Purchasing Report'), "InventoryPurchasingReport", user_pagesize(), 9, $orientation);
155     if ($orientation == 'L')
156         recalculate_cols($cols);
157
158     $rep->Font();
159     $rep->Info($params, $cols, $headers, $aligns);
160     $rep->NewPage();
161
162         $res = getTransactions($category, $location, $fromsupp, $item, $from, $to);
163
164         $total = $total_supp = $grandtotal = 0.0;
165         $total_qty = 0.0;
166         $catt = $stock_description = $supplier_name = '';
167         while ($trans=db_fetch($res))
168         {
169                 if ($stock_description != $trans['description'])
170                 {
171                         if ($stock_description != '')
172                         {
173                                 if ($supplier_name != '')
174                                 {
175                                         $rep->NewLine(2, 3);
176                                         $rep->TextCol(0, 1, _('Total'));
177                                         $rep->TextCol(1, 4, $stock_description);
178                                         $rep->TextCol(4, 5, $supplier_name);
179                                         $rep->TextCol(5, 7, $total_qty);
180                                         $rep->AmountCol(7, 8, $total_supp, $dec);
181                                         $rep->Line($rep->row - 2);
182                                         $rep->NewLine();
183                                         $total_supp = $total_qty = 0.0;
184                                         $supplier_name = $trans['supplier_name'];
185                                 }       
186                         }
187                         $stock_description = $trans['description'];
188                 }
189
190                 if ($supplier_name != $trans['supplier_name'])
191                 {
192                         if ($supplier_name != '')
193                         {
194                                 $rep->NewLine(2, 3);
195                                 $rep->TextCol(0, 1, _('Total'));
196                                 $rep->TextCol(1, 4, $stock_description);
197                                 $rep->TextCol(4, 5, $supplier_name);
198                                 $rep->TextCol(5, 7, $total_qty);
199                                 $rep->AmountCol(7, 8, $total_supp, $dec);
200                                 $rep->Line($rep->row - 2);
201                                 $rep->NewLine();
202                                 $total_supp = $total_qty = 0.0;
203                         }
204                         $supplier_name = $trans['supplier_name'];
205                 }
206                 if ($catt != $trans['cat_description'])
207                 {
208                         if ($catt != '')
209                         {
210                                 $rep->NewLine(2, 3);
211                                 $rep->TextCol(0, 1, _('Total'));
212                                 $rep->TextCol(1, 7, $catt);
213                                 $rep->AmountCol(7, 8, $total, $dec);
214                                 $rep->Line($rep->row - 2);
215                                 $rep->NewLine();
216                                 $rep->NewLine();
217                                 $total = 0.0;
218                         }
219                         $rep->TextCol(0, 1, $trans['category_id']);
220                         $rep->TextCol(1, 6, $trans['cat_description']);
221                         $catt = $trans['cat_description'];
222                         $rep->NewLine();
223                 }
224                 
225                 $curr = get_supplier_currency($trans['supplier_id']);
226                 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
227                 $trans['price'] *= $rate;
228                 $rep->NewLine();
229                 $trans['supp_reference'] = get_supp_inv_reference($trans['supplier_id'], $trans['stock_id'], $trans['tran_date']);
230                 $rep->fontSize -= 2;
231                 $rep->TextCol(0, 1, $trans['stock_id']);
232                 if ($fromsupp == ALL_TEXT)
233                 {
234                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
235                         $rep->TextCol(2, 3, $trans['tran_date']);
236                         $rep->TextCol(3, 4, $trans['supp_reference']);
237                         $rep->TextCol(4, 5, $trans['supplier_name']);
238                 }
239                 else
240                 {
241                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
242                         $rep->TextCol(2, 3, $trans['tran_date']);
243                         $rep->TextCol(3, 4, $trans['supp_reference']);
244                 }       
245                 $rep->AmountCol(5, 6, $trans['qty'], get_qty_dec($trans['stock_id']));
246                 $rep->AmountCol(6, 7, $trans['price'], $dec);
247                 $amt = $trans['qty'] * $trans['price'];
248                 $rep->AmountCol(7, 8, $amt, $dec);
249                 $rep->fontSize += 2;
250                 $total += $amt;
251                 $total_supp += $amt;
252                 $grandtotal += $amt;
253                 $total_qty += $trans['qty'];
254         }
255         if ($stock_description != '')
256         {
257                 if ($supplier_name != '')
258                 {
259                         $rep->NewLine(2, 3);
260                         $rep->TextCol(0, 1, _('Total'));
261                         $rep->TextCol(1, 4, $stock_description);
262                         $rep->TextCol(4, 5, $supplier_name);
263                         $rep->TextCol(5, 7, $total_qty);
264                         $rep->AmountCol(7, 8, $total_supp, $dec);
265                         $rep->Line($rep->row - 2);
266                         $rep->NewLine();
267                         $rep->NewLine();
268                         $total_supp = $total_qty = 0.0;
269                         $supplier_name = $trans['supplier_name'];
270                 }       
271         }
272         if ($supplier_name != '')
273         {
274                 $rep->NewLine(2, 3);
275                 $rep->TextCol(0, 1, _('Total'));
276                 $rep->TextCol(1, 4, $stock_description);
277                 $rep->TextCol(4, 5, $supplier_name);
278                 $rep->TextCol(5, 7, $total_qty);
279                 $rep->AmountCol(7, 8, $total_supp, $dec);
280                 $rep->Line($rep->row - 2);
281                 $rep->NewLine();
282                 $rep->NewLine();
283         }
284
285         $rep->NewLine(2, 3);
286         $rep->TextCol(0, 1, _('Total'));
287         $rep->TextCol(1, 7, $catt);
288         $rep->AmountCol(7, 8, $total, $dec);
289         $rep->Line($rep->row - 2);
290         $rep->NewLine();
291         $rep->NewLine(2, 1);
292         $rep->TextCol(0, 7, _('Grand Total'));
293         $rep->AmountCol(7, 8, $grandtotal, $dec);
294
295         $rep->Line($rep->row  - 4);
296         $rep->NewLine();
297     $rep->End();
298 }
299
300 ?>