Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[fa-stable.git] / reporting / rep310.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:     Stefan Sotirov, modified slightly by Joe Hunt.
16 // date_:       01-12-2017
17 // Title:       Inventory Purchasing - Transaction Based
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,
42                         supplier.supp_name AS supplier_name,
43                         move.trans_no,
44                         move.tran_date,
45                         move.qty AS qty,
46                         move.price
47                 FROM ".TB_PREF."stock_moves move
48                                 LEFT JOIN ".TB_PREF."supp_trans credit ON credit.trans_no=move.trans_no AND credit.type=move.type
49                                 LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=move.trans_no AND 25=move.type
50                                 LEFT JOIN ".TB_PREF."suppliers supplier ON IFNULL(grn.supplier_id, credit.supplier_id)=supplier.supplier_id,
51                         ".TB_PREF."stock_master item,
52                         ".TB_PREF."stock_category category
53                 WHERE item.stock_id=move.stock_id
54                 AND item.category_id=category.category_id
55                 AND move.tran_date>='$from'
56                 AND move.tran_date<='$to'
57                 AND (move.type=".ST_SUPPRECEIVE." OR move.type=".ST_SUPPCREDIT.")
58                 AND (item.mb_flag='B' OR item.mb_flag='M')";
59                 if ($category != 0)
60                         $sql .= " AND item.category_id = ".db_escape($category);
61                 if ($location != '')
62                         $sql .= " AND move.loc_code = ".db_escape($location);
63                 if ($fromsupp != '')
64                         $sql .= " AND supplier.supplier_id = ".db_escape($fromsupp);
65                 if ($item != '')
66                         $sql .= " AND item.stock_id = ".db_escape($item);
67                 $sql .= " ORDER BY move.tran_date, move.trans_no,
68                         supplier.supp_name, item.category_id, item.stock_id";
69     return db_query($sql,"No transactions were returned");
70
71 }
72
73 function get_supp_inv_reference($supplier_id, $stock_id, $date)
74 {
75         $sql = "SELECT trans.supp_reference
76                 FROM ".TB_PREF."supp_trans trans,
77                         ".TB_PREF."supp_invoice_items line,
78                         ".TB_PREF."grn_batch batch,
79                         ".TB_PREF."grn_items item
80                 WHERE trans.type=line.supp_trans_type
81                 AND trans.trans_no=line.supp_trans_no
82                 AND item.grn_batch_id=batch.id
83                 AND item.item_code=line.stock_id
84                 AND trans.supplier_id=".db_escape($supplier_id)."
85                 AND line.stock_id=".db_escape($stock_id)."
86                 AND trans.tran_date=".db_escape($date);
87     $result = db_query($sql,"No transactions were returned");
88     $row = db_fetch_row($result);
89         return is_array($row) ? $row[0] : '';
90 }
91
92 //----------------------------------------------------------------------------------------------------
93
94 function print_inventory_purchase()
95 {
96     global $path_to_root;
97
98         $from = $_POST['PARAM_0'];
99         $to = $_POST['PARAM_1'];
100     $category = $_POST['PARAM_2'];
101     $location = $_POST['PARAM_3'];
102     $fromsupp = $_POST['PARAM_4'];
103     $item = $_POST['PARAM_5'];
104         $comments = $_POST['PARAM_6'];
105         $orientation = $_POST['PARAM_7'];
106         $destination = $_POST['PARAM_8'];
107         if ($destination)
108                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
109         else
110                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
111
112         $orientation = ($orientation ? 'L' : 'P');
113     $dec = user_price_dec();
114
115         if ($category == ALL_NUMERIC)
116                 $category = 0;
117         if ($category == 0)
118                 $cat = _('All');
119         else
120                 $cat = get_category_name($category);
121
122         if ($location == '')
123                 $loc = _('All');
124         else
125                 $loc = get_location_name($location);
126
127         if ($fromsupp == '')
128                 $froms = _('All');
129         else
130                 $froms = get_supplier_name($fromsupp);
131
132         if ($item == '')
133                 $itm = _('All');
134         else
135                 $itm = $item;
136
137         $cols = array(0, 60, 180, 230, 275, 400, 420, 465,      520);
138
139         $headers = array(_('Item'), _('Description'), _('Date'), _('#'), _('Supplier'), _('Qty'), _('Unit Price'), _('Location'));
140         if ($fromsupp != '')
141                 $headers[4] = '';
142
143         $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right', 'right');
144
145     $params =   array(  0 => $comments,
146                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
147                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
148                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
149                                     4 => array('text' => _('Supplier'), 'from' => $froms, 'to' => ''),
150                                     5 => array('text' => _('Item'), 'from' => $itm, 'to' => ''),
151                         6 => array('text' => _('Note'), 'from' => _('The lines separate the transactions.'),
152                                 'to' => ''));
153
154     $rep = new FrontReport(_('Inventory Purchasing - Transaction Based'), "InventoryPurchasingTransactionsReport", 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); //left if someone needs them for own needs
165         //($total_qty = 0.0);
166         $catt = $stock_description = $stock_id = $supplier_name = $event = '';
167         while ($trans=db_fetch($res))
168         {
169                 if ($event != $trans['trans_no'])
170                 {
171                         if ($event != '')
172                                 $rep->Line($rep->row - 2);
173                         $event = $trans['trans_no'];
174                 }
175                 $stock_id = $trans['stock_id'];
176                 $stock_description = $trans['description'];
177                 $curr = get_supplier_currency($trans['supplier_id']);
178                 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
179                 $trans['price'] *= $rate;
180                 //$rep->NewLine();
181                 $trans['supp_reference'] = get_supp_inv_reference($trans['supplier_id'], $trans['stock_id'], $trans['tran_date']);
182
183                 if ($fromsupp == ALL_TEXT)
184                 {
185                         $rep->TextCol(0, 1, $trans['stock_id']);
186                         $rep->TextCol(1, 2, $trans['description']. ($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
187                         $rep->TextCol(2, 3, sql2date($trans['tran_date']));
188                         $rep->TextCol(3, 4, $trans['supp_reference']);
189                         $rep->TextCol(4, 5, $trans['supplier_name']);
190                 }
191                 else
192                 {
193                         $rep->TextCol(0, 1, $trans['stock_id']);
194                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
195                         $rep->TextCol(2, 3, sql2date($trans['tran_date']));
196                         $rep->TextCol(3, 4, $trans['supp_reference']);
197                 }       
198                 $rep->AmountCol(5, 6, $trans['qty'], get_qty_dec($trans['stock_id']));
199                 $rep->AmountCol(6, 7, $trans['price'], $dec);
200                 $amt = $trans['qty'] * $trans['price'];
201                 $rep->TextCol(7, 8, get_location_name($trans['loc_code']));
202                 $rep->NewLine();
203
204 //------------Left if somebody needs them for own needs
205 //              $total += $amt;
206 //              $total_supp += $amt;
207 //              $grandtotal += $amt;
208 //              $total_qty += $trans['qty'];
209         }
210
211         $rep->Line($rep->row - 4);
212         $rep->NewLine();
213         $rep->End();
214 }
215