A couple of additional fixes to changed document reports.
[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;
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
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
62         $cur = get_company_Pref('curr_default');
63
64         if ($email == 0)
65         {
66                 $rep = new FrontReport(_('INVOICE'), "InvoiceBulk", user_pagesize());
67                 $rep->SetHeaderType('Header2');
68                 $rep->currency = $cur;
69                 $rep->Font();
70                 $rep->Info($params, $cols, null, $aligns);
71         }
72         for ($i = $fno[0]; $i <= $tno[0]; $i++)
73         {
74                         if (!exists_customer_trans(ST_SALESINVOICE, $i))
75                                 continue;
76                         $sign = 1;
77                         $myrow = get_customer_trans($i, ST_SALESINVOICE);
78                         $baccount = get_default_bank_account($myrow['curr_code']);
79                         $params['bankaccount'] = $baccount['id'];
80
81                         $branch = get_branch($myrow["branch_code"]);
82                         $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
83                         if ($email == 1)
84                         {
85                                 $rep = new FrontReport("", "", user_pagesize());
86                             $rep->SetHeaderType('Header2');
87                                 $rep->currency = $cur;
88                                 $rep->Font();
89                                 $rep->title = _('INVOICE');
90                                 $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
91                                 $rep->Info($params, $cols, null, $aligns);
92                         }
93                         else
94                                 $rep->title = _('INVOICE');
95                         $contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no']);
96                         $baccount['payment_service'] = $pay_service;
97                         $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts);
98                         $rep->NewPage();
99                         $result = get_customer_trans_details(ST_SALESINVOICE, $i);
100                         $SubTotal = 0;
101                         while ($myrow2=db_fetch($result))
102                         {
103                                 if ($myrow2["quantity"] == 0)
104                                         continue;
105
106                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
107                                    user_price_dec());
108                                 $SubTotal += $Net;
109                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
110                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
111                         $DisplayNet = number_format2($Net,$dec);
112                         if ($myrow2["discount_percent"]==0)
113                                         $DisplayDiscount ="";
114                         else
115                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
116                                 $rep->TextCol(0, 1,     $myrow2['stock_id'], -2);
117                                 $oldrow = $rep->row;
118                                 $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
119                                 $newrow = $rep->row;
120                                 $rep->row = $oldrow;
121                                 $rep->TextCol(2, 3,     $DisplayQty, -2);
122                                 $rep->TextCol(3, 4,     $myrow2['units'], -2);
123                                 $rep->TextCol(4, 5,     $DisplayPrice, -2);
124                                 $rep->TextCol(5, 6,     $DisplayDiscount, -2);
125                                 $rep->TextCol(6, 7,     $DisplayNet, -2);
126                                 $rep->row = $newrow;
127                                 //$rep->NewLine(1);
128                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
129                                         $rep->NewPage();
130                         }
131
132                         $comments = get_comments(ST_SALESINVOICE, $i);
133                         if ($comments && db_num_rows($comments))
134                         {
135                                 $rep->NewLine();
136                         while ($comment=db_fetch($comments))
137                                 $rep->TextColLines(0, 6, $comment['memo_'], -2);
138                         }
139
140                         $DisplaySubTot = number_format2($SubTotal,$dec);
141                         $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
142
143                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
144                         $doctype = ST_SALESINVOICE;
145
146                         $rep->TextCol(3, 6, _("Sub-total"), -2);
147                         $rep->TextCol(6, 7,     $DisplaySubTot, -2);
148                         $rep->NewLine();
149                         $rep->TextCol(3, 6, _("Shipping"), -2);
150                         $rep->TextCol(6, 7,     $DisplayFreight, -2);
151                         $rep->NewLine();
152                         $tax_items = get_trans_tax_details(ST_SALESINVOICE, $i);
153                         $first = true;
154                 while ($tax_item = db_fetch($tax_items))
155                 {
156                         if ($tax_item['amount'] == 0)
157                                 continue;
158                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
159                         
160                         if (isset($suppress_tax_rates) && $suppress_tax_rates == 1)
161                                 $tax_type_name = $tax_item['tax_type_name'];
162                         else
163                                 $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
164
165                         if ($tax_item['included_in_price'])
166                         {
167                                 if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
168                                 {
169                                         if ($first)
170                                         {
171                                                         $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
172                                                         $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
173                                                         $rep->NewLine();
174                                         }
175                                                 $rep->TextCol(3, 6, $tax_type_name, -2);
176                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
177                                                 $first = false;
178                                 }
179                                 else
180                                                 $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
181                                 }
182                         else
183                         {
184                                         $rep->TextCol(3, 6, $tax_type_name, -2);
185                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
186                                 }
187                                 $rep->NewLine();
188                 }
189
190                 $rep->NewLine();
191                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
192                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
193                         $rep->Font('bold');
194                         $rep->TextCol(3, 6, _("TOTAL INVOICE"), - 2);
195                         $rep->TextCol(6, 7, $DisplayTotal, -2);
196                         $words = price_in_words($myrow['Total'], ST_SALESINVOICE);
197                         if ($words != "")
198                         {
199                                 $rep->NewLine(1);
200                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
201                         }
202                         $rep->Font();
203                         if ($email == 1)
204                         {
205                                 $rep->End($email);
206                         }
207         }
208         if ($email == 0)
209                 $rep->End();
210 }
211
212 ?>