Multiply contacts spport added.
[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;
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         $paylink = $_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
73         for ($i = $fno[0]; $i <= $tno[0]; $i++)
74         {
75                 for ($j = ST_SALESINVOICE; $j <= ST_CUSTCREDIT; $j++)
76                 {
77                         if (isset($_POST['PARAM_6']) && $_POST['PARAM_6'] != $j)
78                                 continue;
79                         if (!exists_customer_trans($j, $i))
80                                 continue;
81                         $sign = $j==ST_SALESINVOICE ? 1 : -1;
82                         $myrow = get_customer_trans($i, $j);
83                         $baccount = get_default_bank_account($myrow['curr_code']);
84                         $params['bankaccount'] = $baccount['id'];
85
86                         $branch = get_branch($myrow["branch_code"]);
87                         $branch['disable_branch'] = $paylink; // helper
88                         if ($j == ST_SALESINVOICE)
89                                 $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
90                         else
91                                 $sales_order = null;
92                         if ($email == 1)
93                         {
94                                 $rep = new FrontReport("", "", user_pagesize());
95                             $rep->SetHeaderType('Header2');
96                                 $rep->currency = $cur;
97                                 $rep->Font();
98                                 if ($j == ST_SALESINVOICE)
99                                 {
100                                         $rep->title = _('INVOICE');
101                                         $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
102                                 }
103                                 else
104                                 {
105                                         $rep->title = _('CREDIT NOTE');
106                                         $rep->filename = "CreditNote" . $myrow['reference'] . ".pdf";
107                                 }
108                                 $rep->Info($params, $cols, null, $aligns);
109                         }
110                         else
111                                 $rep->title = ($j == ST_SALESINVOICE) ? _('INVOICE') : _('CREDIT NOTE');
112                         $contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no']);
113                         $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, $j, $contacts);
114                         $rep->NewPage();
115
116                         $result = get_customer_trans_details($j, $i);
117                         $SubTotal = 0;
118                         while ($myrow2=db_fetch($result))
119                         {
120                                 if ($myrow2["quantity"] == 0)
121                                         continue;
122                                         
123                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
124                                    user_price_dec());
125                                 $SubTotal += $Net;
126                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
127                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
128                         $DisplayNet = number_format2($Net,$dec);
129                         if ($myrow2["discount_percent"]==0)
130                                         $DisplayDiscount ="";
131                         else
132                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
133                                 $rep->TextCol(0, 1,     $myrow2['stock_id'], -2);
134                                 $oldrow = $rep->row;
135                                 $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
136                                 $newrow = $rep->row;
137                                 $rep->row = $oldrow;
138                                 $rep->TextCol(2, 3,     $DisplayQty, -2);
139                                 $rep->TextCol(3, 4,     $myrow2['units'], -2);
140                                 $rep->TextCol(4, 5,     $DisplayPrice, -2);
141                                 $rep->TextCol(5, 6,     $DisplayDiscount, -2);
142                                 $rep->TextCol(6, 7,     $DisplayNet, -2);
143                                 $rep->row = $newrow;
144                                 //$rep->NewLine(1);
145                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
146                                         $rep->NewPage();
147                         }
148
149                         $comments = get_comments($j, $i);
150                         if ($comments && db_num_rows($comments))
151                         {
152                                 $rep->NewLine();
153                         while ($comment=db_fetch($comments))
154                                 $rep->TextColLines(0, 6, $comment['memo_'], -2);
155                         }
156
157                         $DisplaySubTot = number_format2($SubTotal,$dec);
158                         $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
159
160                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
161                         $linetype = true;
162                         $doctype = $j;
163                         include($path_to_root . "/reporting/includes/doctext.inc");
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                         $first = true;
173                 while ($tax_item = db_fetch($tax_items))
174                 {
175                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
176
177                         if ($tax_item['included_in_price'])
178                         {
179                                 if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
180                                 {
181                                         if ($first)
182                                         {
183                                                         $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
184                                                         $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
185                                                         $rep->NewLine();
186                                         }
187                                                 $rep->TextCol(3, 6, $tax_item['tax_type_name'] . " (" .
188                                                         $tax_item['rate'] . "%)", -2);
189                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
190                                                 $first = false;
191                                 }
192                                 else
193                                                 $rep->TextCol(3, 7, $doc_Included . " " . $tax_item['tax_type_name'] .
194                                                         " (" . $tax_item['rate'] . "%) " . $doc_Amount . ": " . $DisplayTax, -2);
195                                 }
196                         else
197                         {
198                                         $rep->TextCol(3, 6, $tax_item['tax_type_name'] . " (" .
199                                                 $tax_item['rate'] . "%)", -2);
200                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
201                                 }
202                                 $rep->NewLine();
203                 }
204                 $rep->NewLine();
205                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
206                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
207                         $rep->Font('bold');
208                         $rep->TextCol(3, 6, $doc_TOTAL_INVOICE, - 2);
209                         $rep->TextCol(6, 7, $DisplayTotal, -2);
210                         $words = price_in_words($myrow['Total'], $j);
211                         if ($words != "")
212                         {
213                                 $rep->NewLine(1);
214                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
215                         }       
216                         $rep->Font();
217                         if ($email == 1)
218                         {
219                                 $myrow['dimension_id'] = $paylink; // helper for pmt link
220                                 if ($myrow['email'] == '')
221                                 {
222                                         $myrow['email'] = $branch['email'];
223                                         $myrow['DebtorName'] = $branch['br_name'];
224                                 }
225                                 $rep->End($email, $doc_Invoice_no . " " . $myrow['reference'], $myrow, $j);
226                         }
227                 }
228         }
229         if ($email == 0)
230                 $rep->End();
231 }
232
233 ?>