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