After creating Sales Order - sending email: PDF without a name? Fixed.
[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, 'print_quote' => $print_as_quote);
57
58         $cur = get_company_Pref('curr_default');
59
60     if ($orientation == 'L')
61         recalculate_cols($cols);
62
63         for ($i = $from; $i <= $to; $i++)
64         {
65                 $myrow = get_sales_order_header($i, ST_SALESORDER);
66                 if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
67                         continue;
68                 }
69                 $baccount = get_default_bank_account($myrow['curr_code']);
70                 $params['bankaccount'] = $baccount['id'];
71                 $branch = get_branch($myrow["branch_code"]);
72
73         if ($i == $from || $email == 1)
74             $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
75         if ($print_as_quote == 1)
76         {
77             $rep->title = _('QUOTE');
78             $rep->filename = "Quote" . $i . ".pdf";
79         }
80         else
81         {
82             $rep->title = _("SALES ORDER");
83             $rep->filename = "SalesOrder" . $i . ".pdf";
84         }               
85                 $rep->SetHeaderType('Header2');
86                 $rep->currency = $cur;
87                 $rep->Font();
88                 $rep->Info($params, $cols, null, $aligns);
89
90                 $contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no'], true);
91                 $rep->SetCommonData($myrow, $branch, $myrow, $baccount, ST_SALESORDER, $contacts);
92                 $rep->NewPage();
93
94                 $result = get_sales_order_details($i, ST_SALESORDER);
95                 $SubTotal = 0;
96                 $items = $prices = array();
97                 while ($myrow2=db_fetch($result))
98                 {
99                         $Net = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
100                            user_price_dec());
101                         $prices[] = $Net;
102                         $items[] = $myrow2['stk_code'];
103                         $SubTotal += $Net;
104                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
105                         $DisplayQty = number_format2($myrow2["quantity"],get_qty_dec($myrow2['stk_code']));
106                         $DisplayNet = number_format2($Net,$dec);
107                         if ($myrow2["discount_percent"]==0)
108                                 $DisplayDiscount ="";
109                         else
110                                 $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
111                         $rep->TextCol(0, 1,     $myrow2['stk_code'], -2);
112                         $oldrow = $rep->row;
113                         $rep->TextColLines(1, 2, $myrow2['description'], -2);
114                         $newrow = $rep->row;
115                         $rep->row = $oldrow;
116                         if ($Net != 0.0 || !is_service($myrow2['mb_flag']) || !$SysPrefs->no_zero_lines_amount())
117                         {
118                                 $rep->TextCol(2, 3,     $DisplayQty, -2);
119                                 $rep->TextCol(3, 4,     $myrow2['units'], -2);
120                                 $rep->TextCol(4, 5,     $DisplayPrice, -2);
121                                 $rep->TextCol(5, 6,     $DisplayDiscount, -2);
122                                 $rep->TextCol(6, 7,     $DisplayNet, -2);
123                         }
124                         $rep->row = $newrow;
125                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
126                                 $rep->NewPage();
127                 }
128                 if ($myrow['comments'] != "")
129                 {
130                         $rep->NewLine();
131                         $rep->TextColLines(1, 3, $myrow['comments'], -2);
132                 }
133                 $DisplaySubTot = number_format2($SubTotal,$dec);
134
135                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
136                 $doctype = ST_SALESORDER;
137
138                 $rep->TextCol(3, 6, _("Sub-total"), -2);
139                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
140                 $rep->NewLine();
141                 if ($myrow['freight_cost'] != 0.0)
142                 {
143                         $DisplayFreight = number_format2($myrow["freight_cost"],$dec);
144                         $rep->TextCol(3, 6, _("Shipping"), -2);
145                         $rep->TextCol(6, 7,     $DisplayFreight, -2);
146                         $rep->NewLine();
147                 }       
148                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
149                 if ($myrow['tax_included'] == 0) {
150                         $rep->TextCol(3, 6, _("TOTAL ORDER EX VAT"), - 2);
151                         $rep->TextCol(6, 7,     $DisplayTotal, -2);
152                         $rep->NewLine();
153                 }
154
155                 $tax_items = get_tax_for_items($items, $prices, $myrow["freight_cost"],
156                   $myrow['tax_group_id'], $myrow['tax_included'],  null);
157                 $first = true;
158                 foreach($tax_items as $tax_item)
159                 {
160                         if ($tax_item['Value'] == 0)
161                                 continue;
162                         $DisplayTax = number_format2($tax_item['Value'], $dec);
163
164                         $tax_type_name = $tax_item['tax_type_name'];
165
166                         if ($myrow['tax_included'])
167                         {
168                                 if ($SysPrefs->alternative_tax_include_on_docs() == 1)
169                                 {
170                                         if ($first)
171                                         {
172                                                 $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
173                                                 $rep->TextCol(6, 7,     number_format2($tax_item['net_amount'], $dec), -2);
174                                                 $rep->NewLine();
175                                         }
176                                         $rep->TextCol(3, 6, $tax_type_name, -2);
177                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
178                                         $first = false;
179                                 }
180                                 else
181                                         $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . " " . _("Amount"). ": " . $DisplayTax, -2);
182                         }
183                         else
184                         {
185                                 $SubTotal += $tax_item['Value'];
186                                 $rep->TextCol(3, 6, $tax_type_name, -2);
187                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
188                         }
189                         $rep->NewLine();
190                 }
191
192                 $rep->NewLine();
193
194                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
195                 $rep->Font('bold');
196                 $rep->TextCol(3, 6, _("TOTAL ORDER VAT INCL."), - 2);
197                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
198                 $words = price_in_words($myrow["freight_cost"] + $SubTotal, ST_SALESORDER);
199                 if ($words != "")
200                 {
201                         $rep->NewLine(1);
202                         $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
203                 }       
204                 $rep->Font();
205         if ($i == $to || $email == 1)
206             $rep->End($email);
207                 
208         }
209 }
210