Print Long Item Description on Invoice. Feature. Company Setup Flag.
[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 $SysPrefs;
31
32         $ref = ($SysPrefs->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.trans_no BETWEEN ".db_escape($from)." AND ".db_escape($to)                 
40                 ." ORDER BY trans.tran_date, trans.$ref";
41
42         return db_query($sql, "Cant retrieve invoice range");
43 }
44
45 print_invoices();
46
47 //----------------------------------------------------------------------------------------------------
48
49 function print_invoices()
50 {
51         global $path_to_root, $SysPrefs;
52         
53         $show_this_payment = true; // include payments invoiced here in summary
54
55         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
56
57         $from = $_POST['PARAM_0'];
58         $to = $_POST['PARAM_1'];
59         $currency = $_POST['PARAM_2'];
60         $email = $_POST['PARAM_3'];
61         $pay_service = $_POST['PARAM_4'];
62         $comments = $_POST['PARAM_5'];
63         $customer = $_POST['PARAM_6'];
64         $orientation = $_POST['PARAM_7'];
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                         if (!exists_customer_trans(ST_SALESINVOICE, $row['trans_no']))
95                                 continue;
96                         $sign = 1;
97                         $myrow = get_customer_trans($row['trans_no'], ST_SALESINVOICE);
98
99                         if ($customer && $myrow['debtor_no'] != $customer) {
100                                 continue;
101                         }
102                         if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
103                                 continue;
104                         }
105                         $baccount = get_default_bank_account($myrow['curr_code']);
106                         $params['bankaccount'] = $baccount['id'];
107
108                         $branch = get_branch($myrow["branch_code"]);
109                         $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
110                         if ($email == 1)
111                         {
112                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
113                                 $rep->title = _('INVOICE');
114                                 $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
115                         }       
116                         $rep->currency = $cur;
117                         $rep->Font();
118                         $rep->Info($params, $cols, null, $aligns);
119
120                         $contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);
121                         $baccount['payment_service'] = $pay_service;
122                         $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts);
123                         $rep->SetHeaderType('Header2');
124                         $rep->NewPage();
125                         // calculate summary start row for later use
126                         $summary_start_row = $rep->bottomMargin + (15 * $rep->lineHeight);
127
128                         if ($rep->formData['prepaid'])
129                         {
130                                 $result = get_sales_order_invoices($myrow['order_']);
131                                 $prepayments = array();
132                                 while($inv = db_fetch($result))
133                                 {
134                                         $prepayments[] = $inv;
135                                         if ($inv['trans_no'] == $row['trans_no'])
136                                         break;
137                                 }
138
139                                 if (count($prepayments) > ($show_this_payment ? 0 : 1))
140                                         $summary_start_row += (count($prepayments)) * $rep->lineHeight;
141                                 else
142                                         unset($prepayments);
143                         }
144
145                         $result = get_customer_trans_details(ST_SALESINVOICE, $row['trans_no']);
146                         $SubTotal = 0;
147                         while ($myrow2=db_fetch($result))
148                         {
149                                 if ($myrow2["quantity"] == 0)
150                                         continue;
151
152                                 $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
153                                    user_price_dec());
154                                 $SubTotal += $Net;
155                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
156                         $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
157                         $DisplayNet = number_format2($Net,$dec);
158                         if ($myrow2["discount_percent"]==0)
159                                         $DisplayDiscount ="";
160                         else
161                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
162                                 $c=0;
163                                 $rep->TextCol($c++, $c, $myrow2['stock_id'], -2);
164                                 $oldrow = $rep->row;
165                                 $rep->TextColLines($c++, $c, $myrow2['StockDescription'], -2);
166                                 if (!empty($SysPrefs->prefs['long_description_invoice']) && !empty($myrow2['StockLongDescription']))
167                                 {
168                                         $c--;
169                                         $rep->TextColLines($c++, $c, $myrow2['StockLongDescription'], -2);
170                                 }
171                                 $newrow = $rep->row;
172                                 $rep->row = $oldrow;
173                                 if ($Net != 0.0 || !is_service($myrow2['mb_flag']) || !$SysPrefs->no_zero_lines_amount())
174                                 {
175                                         $rep->TextCol($c++, $c, $DisplayQty, -2);
176                                         $rep->TextCol($c++, $c, $myrow2['units'], -2);
177                                         $rep->TextCol($c++, $c, $DisplayPrice, -2);
178                                         $rep->TextCol($c++, $c, $DisplayDiscount, -2);
179                                         $rep->TextCol($c++, $c, $DisplayNet, -2);
180                                 }
181                                 $rep->row = $newrow;
182                                 //$rep->NewLine(1);
183                                 if ($rep->row < $summary_start_row)
184                                         $rep->NewPage();
185                         }
186
187                         $memo = get_comments_string(ST_SALESINVOICE, $row['trans_no']);
188                         if ($memo != "")
189                         {
190                                 $rep->NewLine();
191                                 $rep->TextColLines(1, 3, $memo, -2);
192                         }
193
194                         $DisplaySubTot = number_format2($SubTotal,$dec);
195
196                         // set to start of summary line:
197                 $rep->row = $summary_start_row;
198                         if (isset($prepayments))
199                         {
200                                 // Partial invoices table
201                                 $rep->TextCol(0, 3,_("Prepayments invoiced to this order up to day:"));
202                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
203                                 $rep->cols[2] -= 20;
204                                 $rep->aligns[2] = 'right';
205                                 $rep->NewLine(); $c = 0; $tot_pym=0;
206                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
207                                 $rep->TextCol($c++, $c, _("Date"));
208                                 $rep->TextCol($c++, $c, _("Invoice reference"));
209                                 $rep->TextCol($c++, $c, _("Amount"));
210
211                                 foreach ($prepayments as $invoice)
212                                 {
213                                         if ($show_this_payment || ($invoice['reference'] != $myrow['reference']))
214                                         {
215                                                 $rep->NewLine();
216                                                 $c = 0; $tot_pym += $invoice['prep_amount'];
217                                                 $rep->TextCol($c++, $c, sql2date($invoice['tran_date']));
218                                                 $rep->TextCol($c++, $c, $invoice['reference']);
219                                                 $rep->TextCol($c++, $c, number_format2($invoice['prep_amount'], $dec));
220                                         }
221                                         if ($invoice['reference']==$myrow['reference']) break;
222                                 }
223                                 $rep->TextCol(0, 3,     str_pad('', 150, '_'));
224                                 $rep->NewLine();
225                                 $rep->TextCol(1, 2,     _("Total payments:"));
226                                 $rep->TextCol(2, 3,     number_format2($tot_pym, $dec));
227                         }
228
229
230                         $doctype = ST_SALESINVOICE;
231                 $rep->row = $summary_start_row;
232                         $rep->cols[2] += 20;
233                         $rep->cols[3] += 20;
234                         $rep->aligns[3] = 'left';
235
236                         $rep->TextCol(3, 6, _("Sub-total"), -2);
237                         $rep->TextCol(6, 7,     $DisplaySubTot, -2);
238                         $rep->NewLine();
239                         if ($myrow['ov_freight'] != 0.0)
240                         {
241                                 $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);
242                                 $rep->TextCol(3, 6, _("Shipping"), -2);
243                                 $rep->TextCol(6, 7,     $DisplayFreight, -2);
244                                 $rep->NewLine();
245                         }       
246                         $tax_items = get_trans_tax_details(ST_SALESINVOICE, $row['trans_no']);
247                         $first = true;
248                 while ($tax_item = db_fetch($tax_items))
249                 {
250                         if ($tax_item['amount'] == 0)
251                                 continue;
252                         $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);
253
254                         if ($SysPrefs->suppress_tax_rates() == 1)
255                                 $tax_type_name = $tax_item['tax_type_name'];
256                         else
257                                 $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
258
259                         if ($myrow['tax_included'])
260                         {
261                                 if ($SysPrefs->alternative_tax_include_on_docs() == 1)
262                                 {
263                                         if ($first)
264                                         {
265                                                         $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
266                                                         $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
267                                                         $rep->NewLine();
268                                         }
269                                                 $rep->TextCol(3, 6, $tax_type_name, -2);
270                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
271                                                 $first = false;
272                                 }
273                                 else
274                                                 $rep->TextCol(3, 6, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
275                                 }
276                         else
277                         {
278                                         $rep->TextCol(3, 6, $tax_type_name, -2);
279                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
280                                 }
281                                 $rep->NewLine();
282                 }
283
284                 $rep->NewLine();
285                         $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
286                                 $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
287                         $rep->Font('bold');
288                         if (!$myrow['prepaid']) $rep->Font('bold');
289                                 $rep->TextCol(3, 6, $rep->formData['prepaid'] ? _("TOTAL ORDER VAT INCL.") : _("TOTAL INVOICE"), - 2);
290                         $rep->TextCol(6, 7, $DisplayTotal, -2);
291                         if ($rep->formData['prepaid'])
292                         {
293                                 $rep->NewLine();
294                                 $rep->Font('bold');
295                                 $rep->TextCol(3, 6, $rep->formData['prepaid']=='final' ? _("THIS INVOICE") : _("TOTAL INVOICE"), - 2);
296                                 $rep->TextCol(6, 7, number_format2($myrow['prep_amount'], $dec), -2);
297                         }
298                         $words = price_in_words($rep->formData['prepaid'] ? $myrow['prep_amount'] : $myrow['Total']
299                                 , array( 'type' => ST_SALESINVOICE, 'currency' => $myrow['curr_code']));
300                         if ($words != "")
301                         {
302                                 $rep->NewLine(1);
303                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
304                         }
305                         $rep->Font();
306                         if ($email == 1)
307                         {
308                                 $rep->End($email);
309                         }
310         }
311         if ($email == 0)
312                 $rep->End();
313 }
314