Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[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, $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         $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         $pictures = $SysPrefs->print_item_images_on_quote();
51         // If you want a larger image, then increase pic_height f.i.
52         // $SysPrefs->pic_height += 25;
53         
54         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
55
56         // $headers in doctext.inc
57         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
58
59         $params = array('comments' => $comments);
60
61         $cur = get_company_Pref('curr_default');
62
63         if ($email == 0)
64                 $rep = new FrontReport(_("SALES QUOTATION"), "SalesQuotationBulk", user_pagesize(), 9, $orientation);
65     if ($orientation == 'L')
66         recalculate_cols($cols);
67
68         for ($i = $from; $i <= $to; $i++)
69         {
70                 $myrow = get_sales_order_header($i, ST_SALESQUOTE);
71                 if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
72                         continue;
73                 }
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                 {
79                         $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
80                         if ($SysPrefs->print_invoice_no() == 1)
81                                 $rep->filename = "SalesQuotation" . $i . ".pdf";
82                         else    
83                                 $rep->filename = "SalesQuotation" . $myrow['reference'] . ".pdf";
84                 }
85                 $rep->currency = $cur;
86                 $rep->Font();
87                 $rep->Info($params, $cols, null, $aligns);
88
89                 $contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no'], true);
90                 $rep->SetCommonData($myrow, $branch, $myrow, $baccount, ST_SALESQUOTE, $contacts);
91                 $rep->SetHeaderType('Header2');
92                 $rep->NewPage();
93
94                 $result = get_sales_order_details($i, ST_SALESQUOTE);
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                         if (!empty($SysPrefs->prefs['long_description_invoice']) && !empty($myrow2['long_description']))
115                                 $rep->TextColLines(1, 2, $myrow2['long_description'], -2);
116                         $newrow = $rep->row;
117                         $rep->row = $oldrow;
118                         if ($Net != 0.0 || !is_service($myrow2['mb_flag']) || !$SysPrefs->no_zero_lines_amount())
119                         {
120                                 $rep->TextCol(2, 3,     $DisplayQty, -2);
121                                 $rep->TextCol(3, 4,     $myrow2['units'], -2);
122                                 $rep->TextCol(4, 5,     $DisplayPrice, -2);
123                                 $rep->TextCol(5, 6,     $DisplayDiscount, -2);
124                                 $rep->TextCol(6, 7,     $DisplayNet, -2);
125                         }
126                         $rep->row = $newrow;
127                         
128                         if ($pictures)
129                         {
130                                 $image = company_path(). "/images/" . item_img_name($myrow2['stk_code']) . ".jpg";
131                                 if (file_exists($image))
132                                 {
133                                         if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin)
134                                                 $rep->NewPage();
135                                         $rep->AddImage($image, $rep->cols[1], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height);
136                                         $rep->row -= $SysPrefs->pic_height;
137                                         $rep->NewLine();
138                                 }
139                         }
140                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
141                                 $rep->NewPage();
142                 }
143                 if ($myrow['comments'] != "")
144                 {
145                         $rep->NewLine();
146                         $rep->TextColLines(1, 3, $myrow['comments'], -2);
147                 }
148                 $DisplaySubTot = number_format2($SubTotal,$dec);
149
150                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
151                 $doctype = ST_SALESQUOTE;
152
153                 $rep->TextCol(3, 6, _("Sub-total"), -2);
154                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
155                 $rep->NewLine();
156                 if ($myrow['freight_cost'] != 0.0)
157                 {
158                         $DisplayFreight = number_format2($myrow["freight_cost"],$dec);
159                         $rep->TextCol(3, 6, _("Shipping"), -2);
160                         $rep->TextCol(6, 7,     $DisplayFreight, -2);
161                         $rep->NewLine();
162                 }       
163                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
164                 if ($myrow['tax_included'] == 0) {
165                         $rep->TextCol(3, 6, _("TOTAL ORDER EX VAT"), - 2);
166                         $rep->TextCol(6, 7,     $DisplayTotal, -2);
167                         $rep->NewLine();
168                 }
169
170                 $tax_items = get_tax_for_items($items, $prices, $myrow["freight_cost"],
171                   $myrow['tax_group_id'], $myrow['tax_included'],  null);
172                 $first = true;
173                 foreach($tax_items as $tax_item)
174                 {
175                         if ($tax_item['Value'] == 0)
176                                 continue;
177                         $DisplayTax = number_format2($tax_item['Value'], $dec);
178
179                         $tax_type_name = $tax_item['tax_type_name'];
180
181                         if ($myrow['tax_included'])
182                         {
183                                 if ($SysPrefs->alternative_tax_include_on_docs() == 1)
184                                 {
185                                         if ($first)
186                                         {
187                                                 $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
188                                                 $rep->TextCol(6, 7,     number_format2($tax_item['net_amount'], $dec), -2);
189                                                 $rep->NewLine();
190                                         }
191                                         $rep->TextCol(3, 6, $tax_type_name, -2);
192                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
193                                         $first = false;
194                                 }
195                                 else
196                                         $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . " " . _("Amount") . ": " . $DisplayTax, -2);
197                         }
198                         else
199                         {
200                                 $SubTotal += $tax_item['Value'];
201                                 $rep->TextCol(3, 6, $tax_type_name, -2);
202                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
203                         }
204                         $rep->NewLine();
205                 }
206
207                 $rep->NewLine();
208
209                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
210                 $rep->Font('bold');
211                 $rep->TextCol(3, 6, _("TOTAL ORDER VAT INCL."), - 2);
212                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
213                 $words = price_in_words($myrow["freight_cost"] + $SubTotal, ST_SALESQUOTE);
214                 if ($words != "")
215                 {
216                         $rep->NewLine(1);
217                         $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
218                 }       
219                 $rep->Font();
220                 if ($email == 1)
221                 {
222                         if ($SysPrefs->print_invoice_no() == 1)
223                                 $myrow['reference'] = $i;
224                         $rep->End($email);
225                 }
226         }
227         if ($email == 0)
228                 $rep->End();
229 }
230