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