6e0307ea80e9df2b482e4df80715df2bc9f4daf1
[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
29 print_invoices();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function print_invoices()
34 {
35         global $path_to_root, $alternative_tax_include_on_docs, $suppress_tax_rates, $no_zero_lines_amount;
36         
37         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
38
39         $from = $_POST['PARAM_0'];
40         $to = $_POST['PARAM_1'];
41         $currency = $_POST['PARAM_2'];
42         $email = $_POST['PARAM_3'];
43         $pay_service = $_POST['PARAM_4'];
44         $comments = $_POST['PARAM_5'];
45         $customer = $_POST['PARAM_6'];
46         $orientation = $_POST['PARAM_7'];
47
48         if (!$from || !$to) return;
49
50         $orientation = ($orientation ? 'L' : 'P');
51         $dec = user_price_dec();
52
53         $fno = explode("-", $from);
54         $tno = explode("-", $to);
55         $from = min($fno[0], $tno[0]);
56         $to = max($fno[0], $tno[0]);
57
58         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
59
60         // $headers in doctext.inc
61         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
62
63         $params = array('comments' => $comments);
64
65         $cur = get_company_Pref('curr_default');
66
67         if ($email == 0)
68                 $rep = new FrontReport(_('INVOICE'), "InvoiceBulk", user_pagesize(), 9, $orientation);
69         if ($orientation == 'L')
70                 recalculate_cols($cols);
71         for ($i = $from; $i <= $to; $i++)
72         {
73                         if (!exists_customer_trans(ST_SALESINVOICE, $i))
74                                 continue;
75                         $sign = 1;
76                         $myrow = get_customer_trans($i, ST_SALESINVOICE);
77
78                         if($customer != ALL_TEXT && $myrow['debtor_no'] != $customer) {
79                                 continue;
80                         }
81                         if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
82                                 continue;
83                         }
84                         $baccount = get_default_bank_account($myrow['curr_code']);
85                         $params['bankaccount'] = $baccount['id'];
86
87                         $branch = get_branch($myrow["branch_code"]);
88                         $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
89                         if ($email == 1)
90                         {
91                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
92                                 $rep->title = _('INVOICE');
93                                 $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
94                         }       
95                         $rep->SetHeaderType('Header2');
96                         $rep->currency = $cur;
97                         $rep->Font();
98                         $rep->Info($params, $cols, null, $aligns);
99
100                         $contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);
101                         $baccount['payment_service'] = $pay_service;
102                         $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts);
103                         $rep->NewPage();
104                         $result = get_customer_trans_details(ST_SALESINVOICE, $i);
105                         $SubTotal = 0;
106                         while ($myrow2=db_fetch($result))
107                         {
108                                 if ($myrow2["quantity"] == 0)
109                                         continue;
110
111                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
112                                    user_price_dec());
113                                 $SubTotal += $Net;
114                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
115                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
116                         $DisplayNet = number_format2($Net,$dec);
117                         if ($myrow2["discount_percent"]==0)
118                                         $DisplayDiscount ="";
119                         else
120                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
121                                 $rep->TextCol(0, 1,     $myrow2['stock_id'], -2);
122                                 $oldrow = $rep->row;
123                                 $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
124                                 $newrow = $rep->row;
125                                 $rep->row = $oldrow;
126                                 if ($Net != 0.0 || !is_service($myrow2['mb_flag']) || !isset($no_zero_lines_amount) || $no_zero_lines_amount == 0)
127                                 {
128                                         $rep->TextCol(2, 3,     $DisplayQty, -2);
129                                         $rep->TextCol(3, 4,     $myrow2['units'], -2);
130                                         $rep->TextCol(4, 5,     $DisplayPrice, -2);
131                                         $rep->TextCol(5, 6,     $DisplayDiscount, -2);
132                                         $rep->TextCol(6, 7,     $DisplayNet, -2);
133                                 }       
134                                 $rep->row = $newrow;
135                                 //$rep->NewLine(1);
136                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
137                                         $rep->NewPage();
138                         }
139
140                         $memo = get_comments_string(ST_SALESINVOICE, $i);
141                         if ($memo != "")
142                         {
143                                 $rep->NewLine();
144                                 $rep->TextColLines(1, 5, $memo, -2);
145                         }
146
147                         $DisplaySubTot = number_format2($SubTotal,$dec);
148                         $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
149
150                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
151                         $doctype = ST_SALESINVOICE;
152
153                         $rep->TextCol(3, 6, _("Sub-total"), -2);
154                         $rep->TextCol(6, 7,     $DisplaySubTot, -2);
155                         $rep->NewLine();
156                         $rep->TextCol(3, 6, _("Shipping"), -2);
157                         $rep->TextCol(6, 7,     $DisplayFreight, -2);
158                         $rep->NewLine();
159                         $tax_items = get_trans_tax_details(ST_SALESINVOICE, $i);
160                         $first = true;
161                 while ($tax_item = db_fetch($tax_items))
162                 {
163                         if ($tax_item['amount'] == 0)
164                                 continue;
165                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
166                         
167                         if (isset($suppress_tax_rates) && $suppress_tax_rates == 1)
168                                 $tax_type_name = $tax_item['tax_type_name'];
169                         else
170                                 $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
171
172                         if ($tax_item['included_in_price'])
173                         {
174                                 if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
175                                 {
176                                         if ($first)
177                                         {
178                                                         $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
179                                                         $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
180                                                         $rep->NewLine();
181                                         }
182                                                 $rep->TextCol(3, 6, $tax_type_name, -2);
183                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
184                                                 $first = false;
185                                 }
186                                 else
187                                                 $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
188                                 }
189                         else
190                         {
191                                         $rep->TextCol(3, 6, $tax_type_name, -2);
192                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
193                                 }
194                                 $rep->NewLine();
195                 }
196
197                 $rep->NewLine();
198                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
199                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
200                         $rep->Font('bold');
201                         $rep->TextCol(3, 6, _("TOTAL INVOICE"), - 2);
202                         $rep->TextCol(6, 7, $DisplayTotal, -2);
203                         $words = price_in_words($myrow['Total'], ST_SALESINVOICE);
204                         if ($words != "")
205                         {
206                                 $rep->NewLine(1);
207                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
208                         }
209                         $rep->Font();
210                         if ($email == 1)
211                         {
212                                 $rep->End($email);
213                         }
214         }
215         if ($email == 0)
216                 $rep->End();
217 }
218
219 ?>