Sales Invoice: fixed tax included amount position.
[fa-stable.git] / reporting / rep107.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 = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?
13         'SA_SALESTRANSVIEW' : 'SA_SALESBULKREP';
14 // ----------------------------------------------------------------
15 // $ Revision:  2.0 $
16 // Creator:     Joe Hunt
17 // date_:       2005-05-19
18 // Title:       Print Invoices
19 // ----------------------------------------------------------------
20 $path_to_root="..";
21
22 include_once($path_to_root . "/includes/session.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/sales/includes/sales_db.inc");
26
27 //----------------------------------------------------------------------------------------------------
28 function get_invoice_range($from, $to)
29 {
30         global $SysPrefs;
31
32         $ref = ($SysPrefs->print_invoice_no() == 1 ? "trans_no" : "reference");
33
34         $sql = "SELECT trans.trans_no, trans.reference
35                 FROM ".TB_PREF."debtor_trans trans 
36                         LEFT JOIN ".TB_PREF."voided voided ON trans.type=voided.type AND trans.trans_no=voided.id
37                 WHERE trans.type=".ST_SALESINVOICE
38                         ." AND ISNULL(voided.id)"
39                         ." AND trans.reference>=".db_escape(get_reference(ST_SALESINVOICE, $from))
40                         ." AND trans.reference<=".db_escape(get_reference(ST_SALESINVOICE, $to))
41                 ." ORDER BY trans.tran_date, trans.$ref";
42
43         return db_query($sql, "Cant retrieve invoice range");
44 }
45
46 print_invoices();
47
48 //----------------------------------------------------------------------------------------------------
49
50 function print_invoices()
51 {
52         global $path_to_root, $SysPrefs;
53         
54         $show_this_payment = true; // include payments invoiced here in summary
55
56         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
57
58         $from = $_POST['PARAM_0'];
59         $to = $_POST['PARAM_1'];
60         $currency = $_POST['PARAM_2'];
61         $email = $_POST['PARAM_3'];
62         $pay_service = $_POST['PARAM_4'];
63         $comments = $_POST['PARAM_5'];
64         $customer = $_POST['PARAM_6'];
65         $orientation = $_POST['PARAM_7'];
66
67         if (!$from || !$to) return;
68
69         $orientation = ($orientation ? 'L' : 'P');
70         $dec = user_price_dec();
71
72         $fno = explode("-", $from);
73         $tno = explode("-", $to);
74         $from = min($fno[0], $tno[0]);
75         $to = max($fno[0], $tno[0]);
76
77         //-------------code-Descr-Qty--uom--tax--prc--Disc-Tot--//
78         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
79
80         // $headers in doctext.inc
81         $aligns = array('left', 'left', 'right', 'center', 'right', 'right', 'right');
82
83         $params = array('comments' => $comments);
84
85         $cur = get_company_Pref('curr_default');
86
87         if ($email == 0)
88                 $rep = new FrontReport(_('INVOICE'), "InvoiceBulk", user_pagesize(), 9, $orientation);
89         if ($orientation == 'L')
90                 recalculate_cols($cols);
91
92         $range = get_invoice_range($from, $to);
93         while($row = db_fetch($range))
94         {
95                         if (!exists_customer_trans(ST_SALESINVOICE, $row['trans_no']))
96                                 continue;
97                         $sign = 1;
98                         $myrow = get_customer_trans($row['trans_no'], ST_SALESINVOICE);
99
100                         if ($customer && $myrow['debtor_no'] != $customer) {
101                                 continue;
102                         }
103                         if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
104                                 continue;
105                         }
106                         $baccount = get_default_bank_account($myrow['curr_code']);
107                         $params['bankaccount'] = $baccount['id'];
108
109                         $branch = get_branch($myrow["branch_code"]);
110                         $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
111                         if ($email == 1)
112                         {
113                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
114                                 $rep->title = _('INVOICE');
115                                 $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
116                         }       
117                         $rep->currency = $cur;
118                         $rep->Font();
119                         $rep->Info($params, $cols, null, $aligns);
120
121                         $contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);
122                         $baccount['payment_service'] = $pay_service;
123                         $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts);
124                         $rep->SetHeaderType('Header2');
125                         $rep->NewPage();
126                         // calculate summary start row for later use
127                         $summary_start_row = $rep->bottomMargin + (15 * $rep->lineHeight);
128
129                         if ($rep->formData['prepaid'])
130                         {
131                                 $result = get_sales_order_invoices($myrow['order_']);
132                                 $prepayments = array();
133                                 while($inv = db_fetch($result))
134                                 {
135                                         $prepayments[] = $inv;
136                                         if ($inv['trans_no'] == $row['trans_no'])
137                                         break;
138                                 }
139
140                                 if (count($prepayments) > ($show_this_payment ? 0 : 1))
141                                         $summary_start_row += (count($prepayments)) * $rep->lineHeight;
142                                 else
143                                         unset($prepayments);
144                         }
145
146                         $result = get_customer_trans_details(ST_SALESINVOICE, $row['trans_no']);
147                         $SubTotal = 0;
148                         while ($myrow2=db_fetch($result))
149                         {
150                                 if ($myrow2["quantity"] == 0)
151                                         continue;
152
153                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
154                                    user_price_dec());
155                                 $SubTotal += $Net;
156                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
157                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
158                         $DisplayNet = number_format2($Net,$dec);
159                         if ($myrow2["discount_percent"]==0)
160                                         $DisplayDiscount ="";
161                         else
162                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
163                                 $c=0;
164                                 $rep->TextCol($c++, $c, $myrow2['stock_id'], -2);
165                                 $oldrow = $rep->row;
166                                 $rep->TextColLines($c++, $c, $myrow2['StockDescription'], -2);
167                                 $newrow = $rep->row;
168                                 $rep->row = $oldrow;
169                                 if ($Net != 0.0 || !is_service($myrow2['mb_flag']) || !$SysPrefs->no_zero_lines_amount())
170                                 {
171                                         $rep->TextCol($c++, $c, $DisplayQty, -2);
172                                         $rep->TextCol($c++, $c, $myrow2['units'], -2);
173                                         $rep->TextCol($c++, $c, $DisplayPrice, -2);
174                                         $rep->TextCol($c++, $c, $DisplayDiscount, -2);
175                                         $rep->TextCol($c++, $c, $DisplayNet, -2);
176                                 }
177                                 $rep->row = $newrow;
178                                 //$rep->NewLine(1);
179                                 if ($rep->row < $summary_start_row)
180                                         $rep->NewPage();
181                         }
182
183                         $memo = get_comments_string(ST_SALESINVOICE, $row['trans_no']);
184                         if ($memo != "")
185                         {
186                                 $rep->NewLine();
187                                 $rep->TextColLines(1, 5, $memo, -2);
188                         }
189
190                         $DisplaySubTot = number_format2($SubTotal,$dec);
191
192                         // set to start of summary line:
193                 $rep->row = $summary_start_row;
194                         if (isset($prepayments))
195                         {
196                                 // Partial invoices table
197                                 $rep->TextCol(0, 3,_("Prepayments invoiced to this order up to day:"));
198                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
199                                 $rep->cols[2] -= 20;
200                                 $rep->aligns[2] = 'right';
201                                 $rep->NewLine(); $c = 0; $tot_pym=0;
202                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
203                                 $rep->TextCol($c++, $c, _("Date"));
204                                 $rep->TextCol($c++, $c, _("Invoice reference"));
205                                 $rep->TextCol($c++, $c, _("Amount"));
206
207                                 foreach ($prepayments as $invoice)
208                                 {
209                                         if ($show_this_payment || ($invoice['reference'] != $myrow['reference']))
210                                         {
211                                                 $rep->NewLine();
212                                                 $c = 0; $tot_pym += $invoice['prep_amount'];
213                                                 $rep->TextCol($c++, $c, sql2date($invoice['tran_date']));
214                                                 $rep->TextCol($c++, $c, $invoice['reference']);
215                                                 $rep->TextCol($c++, $c, number_format2($invoice['prep_amount'], $dec));
216                                         }
217                                         if ($invoice['reference']==$myrow['reference']) break;
218                                 }
219                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
220                                 $rep->NewLine();
221                                 $rep->TextCol(1, 2,     _("Total payments:"));
222                                 $rep->TextCol(2, 3,     number_format2($tot_pym, $dec));
223                         }
224
225
226                         $doctype = ST_SALESINVOICE;
227                 $rep->row = $summary_start_row;
228                         $rep->cols[2] += 20;
229                         $rep->cols[3] += 20;
230                         $rep->aligns[3] = 'left';
231
232                         $rep->TextCol(3, 6, _("Sub-total"), -2);
233                         $rep->TextCol(6, 7,     $DisplaySubTot, -2);
234                         $rep->NewLine();
235                         if ($myrow['ov_freight'] != 0.0)
236                         {
237                                 $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
238                                 $rep->TextCol(3, 6, _("Shipping"), -2);
239                                 $rep->TextCol(6, 7,     $DisplayFreight, -2);
240                                 $rep->NewLine();
241                         }       
242                         $tax_items = get_trans_tax_details(ST_SALESINVOICE, $row['trans_no']);
243                         $first = true;
244                 while ($tax_item = db_fetch($tax_items))
245                 {
246                         if ($tax_item['amount'] == 0)
247                                 continue;
248                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
249
250                         if ($SysPrefs->suppress_tax_rates() == 1)
251                                 $tax_type_name = $tax_item['tax_type_name'];
252                         else
253                                 $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
254
255                         if ($myrow['tax_included'])
256                         {
257                                 if ($SysPrefs->alternative_tax_include_on_docs() == 1)
258                                 {
259                                         if ($first)
260                                         {
261                                                         $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
262                                                         $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
263                                                         $rep->NewLine();
264                                         }
265                                                 $rep->TextCol(3, 6, $tax_type_name, -2);
266                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
267                                                 $first = false;
268                                 }
269                                 else
270                                                 $rep->TextCol(3, 6, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
271                                 }
272                         else
273                         {
274                                         $rep->TextCol(3, 6, $tax_type_name, -2);
275                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
276                                 }
277                                 $rep->NewLine();
278                 }
279
280                 $rep->NewLine();
281                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
282                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
283                         $rep->Font('bold');
284                         if (!$myrow['prepaid']) $rep->Font('bold');
285                                 $rep->TextCol(3, 6, $rep->formData['prepaid'] ? _("TOTAL ORDER VAT INCL.") : _("TOTAL INVOICE"), - 2);
286                         $rep->TextCol(6, 7, $DisplayTotal, -2);
287                         if ($rep->formData['prepaid'])
288                         {
289                                 $rep->NewLine();
290                                 $rep->Font('bold');
291                                 $rep->TextCol(3, 6, $rep->formData['prepaid']=='final' ? _("THIS INVOICE") : _("TOTAL INVOICE"), - 2);
292                                 $rep->TextCol(6, 7, number_format2($myrow['prep_amount'], $dec), -2);
293                         }
294                         $words = price_in_words($rep->formData['prepaid'] ? $myrow['prep_amount'] : $myrow['Total']
295                                 , array( 'type' => ST_SALESINVOICE, 'currency' => $myrow['curr_code']));
296                         if ($words != "")
297                         {
298                                 $rep->NewLine(1);
299                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
300                         }
301                         $rep->Font();
302                         if ($email == 1)
303                         {
304                                 $rep->End($email);
305                         }
306         }
307         if ($email == 0)
308                 $rep->End();
309 }
310