Fixed problems with htmlspecialchars() function for not dupported encodings on newer...
[fa-stable.git] / reporting / rep109.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 Sales Orders
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 include_once($path_to_root . "/taxes/tax_calc.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_sales_orders();
31
32 function print_sales_orders()
33 {
34         global $path_to_root, $SysPrefs;
35
36         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
37
38         $from = $_POST['PARAM_0'];
39         $to = $_POST['PARAM_1'];
40         $currency = $_POST['PARAM_2'];
41         $email = $_POST['PARAM_3'];
42         $print_as_quote = $_POST['PARAM_4'];
43         $comments = $_POST['PARAM_5'];
44         $orientation = $_POST['PARAM_6'];
45
46         if (!$from || !$to) return;
47
48         $orientation = ($orientation ? 'L' : 'P');
49         $dec = user_price_dec();
50
51         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
52
53         // $headers in doctext.inc
54         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
55
56         $params = array('comments' => $comments);
57
58         $cur = get_company_Pref('curr_default');
59
60         if ($email == 0)
61         {
62
63                 if ($print_as_quote == 0)
64                         $rep = new FrontReport(_("SALES ORDER"), "SalesOrderBulk", user_pagesize(), 9, $orientation);
65                 else
66                         $rep = new FrontReport(_("QUOTE"), "QuoteBulk", user_pagesize(), 9, $orientation);
67         }
68     if ($orientation == 'L')
69         recalculate_cols($cols);
70
71         for ($i = $from; $i <= $to; $i++)
72         {
73                 $myrow = get_sales_order_header($i, ST_SALESORDER);
74                 $baccount = get_default_bank_account($myrow['curr_code']);
75                 $params['bankaccount'] = $baccount['id'];
76                 $branch = get_branch($myrow["branch_code"]);
77                 if ($email == 1)
78                         $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
79                 $rep->SetHeaderType('Header2');
80                 $rep->currency = $cur;
81                 $rep->Font();
82                 if ($print_as_quote == 1)
83                 {
84                         $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
85                         if ($print_as_quote == 1)
86                         {
87                                 $rep->title = _('QUOTE');
88                                 $rep->filename = "Quote" . $i . ".pdf";
89                         }
90                         else
91                         {
92                                 $rep->title = _("SALES ORDER");
93                                 $rep->filename = "SalesOrder" . $i . ".pdf";
94                         }
95                 }
96                 else
97                         $rep->title = ($print_as_quote==1 ? _("QUOTE") : _("SALES ORDER"));
98                 $rep->SetHeaderType('Header2');
99                 $rep->currency = $cur;
100                 $rep->Font();
101                 $rep->Info($params, $cols, null, $aligns);
102
103                 $contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no'], true);
104                 $rep->SetCommonData($myrow, $branch, $myrow, $baccount, ST_SALESORDER, $contacts);
105                 $rep->NewPage();
106
107                 $result = get_sales_order_details($i, ST_SALESORDER);
108                 $SubTotal = 0;
109                 $items = $prices = array();
110                 while ($myrow2=db_fetch($result))
111                 {
112                         $Net = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
113                            user_price_dec());
114                         $prices[] = $Net;
115                         $items[] = $myrow2['stk_code'];
116                         $SubTotal += $Net;
117                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
118                         $DisplayQty = number_format2($myrow2["quantity"],get_qty_dec($myrow2['stk_code']));
119                         $DisplayNet = number_format2($Net,$dec);
120                         if ($myrow2["discount_percent"]==0)
121                                 $DisplayDiscount ="";
122                         else
123                                 $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
124                         $rep->TextCol(0, 1,     $myrow2['stk_code'], -2);
125                         $oldrow = $rep->row;
126                         $rep->TextColLines(1, 2, $myrow2['description'], -2);
127                         $newrow = $rep->row;
128                         $rep->row = $oldrow;
129                         if ($Net != 0.0 || !is_service($myrow2['mb_flag']) || !$SysPrefs->no_zero_lines_amount())
130                         {
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                         }
137                         $rep->row = $newrow;
138                         //$rep->NewLine(1);
139                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
140                                 $rep->NewPage();
141                 }
142                 if ($myrow['comments'] != "")
143                 {
144                         $rep->NewLine();
145                         $rep->TextColLines(1, 5, $myrow['comments'], -2);
146                 }
147                 $DisplaySubTot = number_format2($SubTotal,$dec);
148                 $DisplayFreight = number_format2($myrow["freight_cost"],$dec);
149
150                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
151                 $doctype = ST_SALESORDER;
152
153                 $rep->TextCol(3, 6, _("Sub-total"), -2);
154                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
155                 $rep->NewLine();
156                 $rep->TextCol(3, 6, _("Shipping"), -2);
157                 $rep->TextCol(6, 7,     $DisplayFreight, -2);
158                 $rep->NewLine();
159
160                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
161                 if ($myrow['tax_included'] == 0) {
162                         $rep->TextCol(3, 6, _("TOTAL ORDER EX VAT"), - 2);
163                         $rep->TextCol(6, 7,     $DisplayTotal, -2);
164                         $rep->NewLine();
165                 }
166
167                 $tax_items = get_tax_for_items($items, $prices, $myrow["freight_cost"],
168                   $myrow['tax_group_id'], $myrow['tax_included'],  null);
169                 $first = true;
170                 foreach($tax_items as $tax_item)
171                 {
172                         if ($tax_item['Value'] == 0)
173                                 continue;
174                         $DisplayTax = number_format2($tax_item['Value'], $dec);
175
176                         $tax_type_name = $tax_item['tax_type_name'];
177
178                         if ($myrow['tax_included'])
179                         {
180                                 if ($SysPrefs->alternative_tax_include_on_docs() == 1)
181                                 {
182                                         if ($first)
183                                         {
184                                                 $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
185                                                 $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
186                                                 $rep->NewLine();
187                                         }
188                                         $rep->TextCol(3, 6, $tax_type_name, -2);
189                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
190                                         $first = false;
191                                 }
192                                 else
193                                         $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . " " . _("Amount"). ": " . $DisplayTax, -2);
194                         }
195                         else
196                         {
197                                 $SubTotal += $tax_item['Value'];
198                                 $rep->TextCol(3, 6, $tax_type_name, -2);
199                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
200                         }
201                         $rep->NewLine();
202                 }
203
204                 $rep->NewLine();
205
206                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
207                 $rep->Font('bold');
208                 $rep->TextCol(3, 6, _("TOTAL ORDER VAT INCL."), - 2);
209                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
210                 $words = price_in_words($myrow["freight_cost"] + $SubTotal, ST_SALESORDER);
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                         $rep->End($email);
220                 }
221         }
222         if ($email == 0)
223                 $rep->End();
224 }
225