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