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