Merged changes from main branch up to 2.1.3.
[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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Print Sales Orders
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/sales/includes/sales_db.inc");
25
26 //----------------------------------------------------------------------------------------------------
27
28 print_sales_orders();
29
30 $print_as_quote = 0;
31
32 function print_sales_orders()
33 {
34         global $path_to_root, $print_as_quote;
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         $bankaccount = $_POST['PARAM_3'];
42         $email = $_POST['PARAM_4'];
43         $print_as_quote = $_POST['PARAM_5'];
44         $comments = $_POST['PARAM_6'];
45
46         if ($from == null)
47                 $from = 0;
48         if ($to == null)
49                 $to = 0;
50         $dec = user_price_dec();
51
52         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
53
54         // $headers in doctext.inc
55         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
56
57         $params = array('comments' => $comments,
58                                         'bankaccount' => $bankaccount);
59
60         $baccount = get_bank_account($params['bankaccount']);
61         $cur = get_company_Pref('curr_default');
62
63         if ($email == 0)
64         {
65                 if ($print_as_quote == 0)
66                         $rep = new FrontReport(_("SALES ORDER"), "SalesOrderBulk", user_pagesize());
67                 else
68                         $rep = new FrontReport(_("QUOTE"), "QuoteBulk", user_pagesize());
69                 $rep->currency = $cur;
70                 $rep->Font();
71                 $rep->Info($params, $cols, null, $aligns);
72         }
73
74         for ($i = $from; $i <= $to; $i++)
75         {
76                 $myrow = get_sales_order_header($i);
77                 $branch = get_branch($myrow["branch_code"]);
78                 if ($email == 1)
79                 {
80                         $rep = new FrontReport("", "", user_pagesize());
81                         $rep->currency = $cur;
82                         $rep->Font();
83                         if ($print_as_quote == 1)
84                         {
85                                 $rep->title = _('QUOTE');
86                                 $rep->filename = "Quote" . $i . ".pdf";
87                         }
88                         else
89                         {
90                                 $rep->title = _("SALES ORDER");
91                                 $rep->filename = "SalesOrder" . $i . ".pdf";
92                         }
93                         $rep->Info($params, $cols, null, $aligns);
94                 }
95                 else
96                         $rep->title = ($print_as_quote==1 ? _("QUOTE") : _("SALES ORDER"));
97                 $rep->Header2($myrow, $branch, $myrow, $baccount, 9);
98
99                 $result = get_sales_order_details($i);
100                 $SubTotal = 0;
101                 while ($myrow2=db_fetch($result))
102                 {
103                         $Net = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
104                            user_price_dec());
105                         $SubTotal += $Net;
106                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
107                         $DisplayQty = number_format2($myrow2["quantity"],get_qty_dec($myrow2['stk_code']));
108                         $DisplayNet = number_format2($Net,$dec);
109                         if ($myrow2["discount_percent"]==0)
110                                 $DisplayDiscount ="";
111                         else
112                                 $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
113                         $rep->TextCol(0, 1,     $myrow2['stk_code'], -2);
114                         $oldrow = $rep->row;
115                         $rep->TextColLines(1, 2, $myrow2['description'], -2);
116                         $newrow = $rep->row;
117                         $rep->row = $oldrow;
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                         $rep->row = $newrow;
124                         //$rep->NewLine(1);
125                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
126                                 $rep->Header2($myrow, $branch, $myrow, $baccount, 9);
127                 }
128                 if ($myrow['comments'] != "")
129                 {
130                         $rep->NewLine();
131                         $rep->TextColLines(1, 5, $myrow['comments'], -2);
132                 }
133                 $DisplaySubTot = number_format2($SubTotal,$dec);
134                 $DisplayFreight = number_format2($myrow["freight_cost"],$dec);
135
136                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
137                 $linetype = true;
138                 $doctype = 9;
139                 if ($rep->currency != $myrow['curr_code'])
140                 {
141                         include($path_to_root . "/reporting/includes/doctext2.inc");
142                 }
143                 else
144                 {
145                         include($path_to_root . "/reporting/includes/doctext.inc");
146                 }
147
148                 $rep->TextCol(3, 6, $doc_Sub_total, -2);
149                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
150                 $rep->NewLine();
151                 $rep->TextCol(3, 6, $doc_Shipping, -2);
152                 $rep->TextCol(6, 7,     $DisplayFreight, -2);
153                 $rep->NewLine();
154                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
155                 $rep->Font('bold');
156                 $rep->TextCol(3, 6, $doc_TOTAL_ORDER, - 2);
157                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
158                 $rep->Font();
159                 if ($email == 1)
160                 {
161                         if ($myrow['contact_email'] == '')
162                         {
163                                 $myrow['contact_email'] = $branch['email'];
164                                 $myrow['DebtorName'] = $branch['br_name'];
165                         }
166                         $rep->End($email, $doc_Invoice_no . " " . $i, $myrow);
167                 }
168         }
169         if ($email == 0)
170                 $rep->End();
171 }
172
173 ?>