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