Replaced the Excel Writer engine with XLS driver (the XML driver can later be downloaded)
[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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Print Invoices
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 . "/sales/includes/sales_db.inc");
25
26 //----------------------------------------------------------------------------------------------------
27
28 print_invoices();
29
30 //----------------------------------------------------------------------------------------------------
31
32 function print_invoices()
33 {
34         global $path_to_root;
35
36         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
37
38         $from = $_POST['PARAM_0'];
39         $to = $_POST['PARAM_1'];
40         $currency = $_POST['PARAM_2'];
41         $bankaccount = $_POST['PARAM_3'];
42         $email = $_POST['PARAM_4'];
43         $paylink = $_POST['PARAM_5'];
44         $comments = $_POST['PARAM_6'];
45
46         if ($from == null)
47                 $from = 0;
48         if ($to == null)
49                 $to = 0;
50         $dec = user_price_dec();
51
52         $fno = explode("-", $from);
53         $tno = explode("-", $to);
54
55         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
56
57         // $headers in doctext.inc
58         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
59
60         $params = array('comments' => $comments,
61                                         'bankaccount' => $bankaccount);
62
63         $baccount = get_bank_account($params['bankaccount']);
64         $cur = get_company_Pref('curr_default');
65
66         if ($email == 0)
67         {
68                 $rep = new FrontReport(_('INVOICE'), "InvoiceBulk", user_pagesize());
69                 $rep->currency = $cur;
70                 $rep->Font();
71                 $rep->Info($params, $cols, null, $aligns);
72         }
73
74         for ($i = $fno[0]; $i <= $tno[0]; $i++)
75         {
76                 for ($j = 10; $j <= 11; $j++)
77                 {
78                         if (isset($_POST['PARAM_7']) && $_POST['PARAM_7'] != $j)
79                                 continue;
80                         if (!exists_customer_trans($j, $i))
81                                 continue;
82                         $sign = $j==10 ? 1 : -1;
83                         $myrow = get_customer_trans($i, $j);
84                         $branch = get_branch($myrow["branch_code"]);
85                         $branch['disable_branch'] = $paylink; // helper
86                         if ($j == 10)
87                                 $sales_order = get_sales_order_header($myrow["order_"]);
88                         else
89                                 $sales_order = null;
90                         if ($email == 1)
91                         {
92                                 $rep = new FrontReport("", "", user_pagesize());
93                                 $rep->currency = $cur;
94                                 $rep->Font();
95                                 if ($j == 10)
96                                 {
97                                         $rep->title = _('INVOICE');
98                                         $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
99                                 }
100                                 else
101                                 {
102                                         $rep->title = _('CREDIT NOTE');
103                                         $rep->filename = "CreditNote" . $myrow['reference'] . ".pdf";
104                                 }
105                                 $rep->Info($params, $cols, null, $aligns);
106                         }
107                         else
108                                 $rep->title = ($j == 10) ? _('INVOICE') : _('CREDIT NOTE');
109                         $rep->Header2($myrow, $branch, $sales_order, $baccount, $j);
110
111                         $result = get_customer_trans_details($j, $i);
112                         $SubTotal = 0;
113                         while ($myrow2=db_fetch($result))
114                         {
115                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
116                                    user_price_dec());
117                                 $SubTotal += $Net;
118                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
119                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
120                         $DisplayNet = number_format2($Net,$dec);
121                         if ($myrow2["discount_percent"]==0)
122                                         $DisplayDiscount ="";
123                         else
124                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
125                                 $rep->TextCol(0, 1,     $myrow2['stock_id'], -2);
126                                 $oldrow = $rep->row;
127                                 $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
128                                 $newrow = $rep->row;
129                                 $rep->row = $oldrow;
130                                 $rep->TextCol(2, 3,     $DisplayQty, -2);
131                                 $rep->TextCol(3, 4,     $myrow2['units'], -2);
132                                 $rep->TextCol(4, 5,     $DisplayPrice, -2);
133                                 $rep->TextCol(5, 6,     $DisplayDiscount, -2);
134                                 $rep->TextCol(6, 7,     $DisplayNet, -2);
135                                 $rep->row = $newrow;
136                                 //$rep->NewLine(1);
137                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
138                                         $rep->Header2($myrow, $branch, $sales_order, $baccount,$j);
139                         }
140
141                         $comments = get_comments($j, $i);
142                         if ($comments && db_num_rows($comments))
143                         {
144                                 $rep->NewLine();
145                         while ($comment=db_fetch($comments))
146                                 $rep->TextColLines(0, 6, $comment['memo_'], -2);
147                         }
148
149                         $DisplaySubTot = number_format2($SubTotal,$dec);
150                         $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
151
152                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
153                         $linetype = true;
154                         $doctype = $j;
155                         if ($rep->currency != $myrow['curr_code'])
156                         {
157                                 include($path_to_root . "/reporting/includes/doctext2.inc");
158                         }
159                         else
160                         {
161                                 include($path_to_root . "/reporting/includes/doctext.inc");
162                         }
163
164                         $rep->TextCol(3, 6, $doc_Sub_total, -2);
165                         $rep->TextCol(6, 7,     $DisplaySubTot, -2);
166                         $rep->NewLine();
167                         $rep->TextCol(3, 6, $doc_Shipping, -2);
168                         $rep->TextCol(6, 7,     $DisplayFreight, -2);
169                         $rep->NewLine();
170                         $tax_items = get_trans_tax_details($j, $i);
171                 while ($tax_item = db_fetch($tax_items))
172                 {
173                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
174
175                         if ($tax_item['included_in_price'])
176                         {
177                                         $rep->TextCol(3, 7, $doc_Included . " " . $tax_item['tax_type_name'] .
178                                                 " (" . $tax_item['rate'] . "%) " . $doc_Amount . ":" . $DisplayTax, -2);
179                                 }
180                         else
181                         {
182                                         $rep->TextCol(3, 6, $tax_item['tax_type_name'] . " (" .
183                                                 $tax_item['rate'] . "%)", -2);
184                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
185                                 }
186                                 $rep->NewLine();
187                 }
188                 $rep->NewLine();
189                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
190                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
191                         $rep->Font('bold');
192                         $rep->TextCol(3, 6, $doc_TOTAL_INVOICE, - 2);
193                         $rep->TextCol(6, 7, $DisplayTotal, -2);
194                         $rep->Font();
195                         if ($email == 1)
196                         {
197                                 $myrow['dimension_id'] = $paylink; // helper for pmt link
198                                 if ($myrow['email'] == '')
199                                 {
200                                         $myrow['email'] = $branch['email'];
201                                         $myrow['DebtorName'] = $branch['br_name'];
202                                 }
203                                 $rep->End($email, $doc_Invoice_no . " " . $myrow['reference'], $myrow, $j);
204                         }
205                 }
206         }
207         if ($email == 0)
208                 $rep->End();
209 }
210
211 ?>