Improvement to rep107.php (Invoice).
[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 $print_invoice_no;
31
32         $ref = ($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, $alternative_tax_include_on_docs, $suppress_tax_rates, $no_zero_lines_amount;
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         $orientation = $_POST['PARAM_6'];
65
66         $orientation = ($orientation ? 'L' : 'P');
67         if (!$from || !$to) return;
68
69         $dec = user_price_dec();
70
71         $fno = explode("-", $from);
72         $tno = explode("-", $to);
73         $from = min($fno[0], $tno[0]);
74         $to = max($fno[0], $tno[0]);
75
76         //-------------code-Descr-Qty--uom--tax--prc--Disc-Tot--//
77         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
78
79         // $headers in doctext.inc
80         $aligns = array('left', 'left', 'right', 'center', 'right', 'right', 'right');
81
82         $params = array('comments' => $comments);
83
84         $cur = get_company_Pref('curr_default');
85
86         if ($email == 0)
87                 $rep = new FrontReport(_('INVOICE'), "InvoiceBulk", user_pagesize(), 9, $orientation);
88     if ($orientation == 'L')
89         $rep->recalculate_cols($cols);
90
91         $range = get_invoice_range($from, $to);
92         while($row = db_fetch($range))
93         {
94                         $i = $row['trans_no'];
95                         if (!exists_customer_trans(ST_SALESINVOICE, $i))
96                                 continue;
97                         $sign = 1;
98                         $myrow = get_customer_trans($i, ST_SALESINVOICE);
99                         $baccount = get_default_bank_account($myrow['curr_code']);
100                         $params['bankaccount'] = $baccount['id'];
101
102                         $branch = get_branch($myrow["branch_code"]);
103                         $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
104                         if ($email == 1)
105                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
106                     $rep->SetHeaderType('Header2');
107                         $rep->currency = $cur;
108                         $rep->Font();
109                         $rep->title = _('INVOICE');
110                         $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
111                         $rep->Info($params, $cols, null, $aligns);
112
113                         $contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], false);
114                         $baccount['payment_service'] = $pay_service;
115                         $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts);
116                         $rep->NewPage();
117                         // calculate summary start row for later use
118                         $summary_start_row = $rep->bottomMargin + (15 * $rep->lineHeight);
119
120                         if ($rep->formData['prepaid'])
121                         {
122                                 $result = get_sales_order_invoices($myrow['order_']);
123                                 $prepayments = array();
124                                 while($inv = db_fetch($result))
125                                 {
126                                         $prepayments[] = $inv;
127                                         if ($inv['trans_no'] == $i)
128                                         break;
129                                 }
130
131                                 if (count($prepayments) > ($show_this_payment ? 0 : 1))
132                                         $summary_start_row += (count($prepayments)) * $rep->lineHeight;
133                                 else
134                                         unset($prepayments);
135                         }
136
137                         $result = get_customer_trans_details(ST_SALESINVOICE, $i);
138                         $SubTotal = 0;
139                         while ($myrow2=db_fetch($result))
140                         {
141                                 if ($myrow2["quantity"] == 0)
142                                         continue;
143
144                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
145                                    user_price_dec());
146                                 $SubTotal += $Net;
147                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
148                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
149                         $DisplayNet = number_format2($Net,$dec);
150                         if ($myrow2["discount_percent"]==0)
151                                         $DisplayDiscount ="";
152                         else
153                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
154                                 $c=0;
155                                 $rep->TextCol($c++, $c, $myrow2['stock_id'], -2);
156                                 $oldrow = $rep->row;
157                                 $rep->TextColLines($c++, $c, $myrow2['StockDescription'], -2);
158                                 $newrow = $rep->row;
159                                 $rep->row = $oldrow;
160                                 if ($Net != 0.0 || !is_service($myrow2['mb_flag']) || !isset($no_zero_lines_amount) || $no_zero_lines_amount == 0)
161                                 {
162                                         $rep->TextCol($c++, $c, $DisplayQty, -2);
163                                         $rep->TextCol($c++, $c, $myrow2['units'], -2);
164                                         $rep->TextCol($c++, $c, $DisplayPrice, -2);
165                                         $rep->TextCol($c++, $c, $DisplayDiscount, -2);
166                                         $rep->TextCol($c++, $c, $DisplayNet, -2);
167                                 }
168                                 $rep->row = $newrow;
169                                 //$rep->NewLine(1);
170                                 if ($rep->row < $summary_start_row)
171                                         $rep->NewPage();
172                         }
173
174                         $memo = get_comments_string(ST_SALESINVOICE, $i);
175                         if ($memo != "")
176                         {
177                                 $rep->NewLine();
178                                 $rep->TextColLines(1, 5, $memo, -2);
179                         }
180
181                         $DisplaySubTot = number_format2($SubTotal,$dec);
182                         $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
183
184                         // set to start of summary line:
185                 $rep->row = $summary_start_row;
186                         if (isset($prepayments))
187                         {
188                                 // Partial invoices table
189                                 //$rep->NewLine();
190                                 $rep->TextCol(0, 3,_("Prepayments invoiced to this order up to day:"));
191                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
192                                 $rep->cols[2] -= 20;
193                                 $rep->aligns[2] = 'right';
194                                 $rep->NewLine(); $c = 0; $tot_pym=0;
195                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
196                                 $rep->TextCol($c++, $c, _("Date"));
197                                 $rep->TextCol($c++, $c, _("Invoice reference"));
198                                 $rep->TextCol($c++, $c, _("Amount"));
199
200                                 foreach ($prepayments as $invoice)
201                                 {
202                                         if ($show_this_payment || ($invoice['reference'] != $myrow['reference']))
203                                         {
204                                                 $rep->NewLine();
205                                                 $c = 0; $tot_pym += $invoice['prep_amount'];
206                                                 $rep->TextCol($c++, $c, sql2date($invoice['tran_date']));
207                                                 $rep->TextCol($c++, $c, $invoice['reference']);
208                                                 $rep->TextCol($c++, $c, number_format2($invoice['prep_amount'], $dec));
209                                         }
210                                         if ($invoice['reference']==$myrow['reference']) break;
211                                 }
212                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
213                                 $rep->NewLine();
214                                 $rep->TextCol(1, 2,     _("Total payments:"));
215                                 $rep->TextCol(2, 3,     number_format2($tot_pym, $dec));
216                         }
217
218
219                         $doctype = ST_SALESINVOICE;
220                 $rep->row = $summary_start_row;
221                         $rep->cols[2] += 20;
222                         $rep->cols[3] += 20;
223                         $rep->aligns[3] = 'left';
224
225                         $rep->TextCol(3, 6, _("Sub-total"), -2);
226                         $rep->TextCol(6, 7,     $DisplaySubTot, -2);
227                         $rep->NewLine();
228                         $rep->TextCol(3, 6, _("Shipping"), -2);
229                         $rep->TextCol(6, 7,     $DisplayFreight, -2);
230                         $rep->NewLine();
231                         $tax_items = get_trans_tax_details(ST_SALESINVOICE, $i);
232                         $first = true;
233                 while ($tax_item = db_fetch($tax_items))
234                 {
235                         if ($tax_item['amount'] == 0)
236                                 continue;
237                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
238
239                         if (isset($suppress_tax_rates) && $suppress_tax_rates == 1)
240                                 $tax_type_name = $tax_item['tax_type_name'];
241                         else
242                                 $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
243
244                         if ($myrow['tax_included'])
245                         {
246                                 if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
247                                 {
248                                         if ($first)
249                                         {
250                                                         $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
251                                                         $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
252                                                         $rep->NewLine();
253                                         }
254                                                 $rep->TextCol(3, 6, $tax_type_name, -2);
255                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
256                                                 $first = false;
257                                 }
258                                 else
259                                                 $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
260                                 }
261                         else
262                         {
263                                         $rep->TextCol(3, 6, $tax_type_name, -2);
264                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
265                                 }
266                                 $rep->NewLine();
267                 }
268
269                 $rep->NewLine();
270                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
271                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
272                         $rep->Font('bold');
273                         if (!$myrow['prepaid']) $rep->Font('bold');
274                                 $rep->TextCol(3, 6, $rep->formData['prepaid'] ? _("TOTAL ORDER VAT INCL.") : _("TOTAL INVOICE"), - 2);
275                         $rep->TextCol(6, 7, $DisplayTotal, -2);
276                         if ($rep->formData['prepaid'])
277                         {
278                                 $rep->NewLine();
279                                 $rep->Font('bold');
280                                 $rep->TextCol(3, 6, $rep->formData['prepaid']=='final' ? _("THIS INVOICE") : _("TOTAL INVOICE"), - 2);
281                                 $rep->TextCol(6, 7, number_format2($myrow['prep_amount'], $dec), -2);
282                         }
283                         $words = price_in_words($rep->formData['prepaid'] ? $myrow['prep_amount'] : $myrow['Total']
284                                 , array( 'type' => ST_SALESINVOICE, 'currency' => $myrow['curr_code']));
285                         if ($words != "")
286                         {
287                                 $rep->NewLine(1);
288                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
289                         }
290                         $rep->Font();
291                         if ($email == 1)
292                         {
293                                 $rep->End($email);
294                         }
295         }
296         if ($email == 0)
297                 $rep->End();
298 }
299
300 ?>