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