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