Merged changes from main trunk up to 2.2.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 = $_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
27 //----------------------------------------------------------------------------------------------------
28
29 print_sales_orders();
30
31 $print_as_quote = 0;
32
33 function print_sales_orders()
34 {
35         global $path_to_root, $print_as_quote;
36
37         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
38
39         $from = $_POST['PARAM_0'];
40         $to = $_POST['PARAM_1'];
41         $currency = $_POST['PARAM_2'];
42         $email = $_POST['PARAM_3'];
43         $print_as_quote = $_POST['PARAM_4'];
44         $comments = $_POST['PARAM_5'];
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
59         $cur = get_company_Pref('curr_default');
60
61         if ($email == 0)
62         {
63                 if ($print_as_quote == 0)
64                         $rep = new FrontReport(_("SALES ORDER"), "SalesOrderBulk", user_pagesize());
65                 else
66                         $rep = new FrontReport(_("QUOTE"), "QuoteBulk", user_pagesize());
67                 $rep->currency = $cur;
68                 $rep->Font();
69                 $rep->Info($params, $cols, null, $aligns);
70         }
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());
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, ST_SALESORDER);
98
99                 $result = get_sales_order_details($i, ST_SALESORDER);
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, ST_SALESORDER);
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 = ST_SALESORDER;
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                 if ($myrow['tax_included'] == 0)
157                         $rep->TextCol(3, 6, $doc_TOTAL_ORDER, - 2);
158                 else    
159                         $rep->TextCol(3, 6, $doc_TOTAL_ORDER2, - 2);
160                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
161                 $rep->Font();
162                 if ($email == 1)
163                 {
164                         if ($myrow['contact_email'] == '')
165                         {
166                                 $myrow['contact_email'] = $branch['email'];
167                                 if ($myrow['contact_email'] == '')
168                                         $myrow['contact_email'] = $myrow['master_email'];
169                                 $myrow['DebtorName'] = $branch['br_name'];
170                         }
171                         //$myrow['reference'] = $i;
172                         $rep->End($email, $doc_Invoice_no . " " . $i, $myrow);
173                 }
174         }
175         if ($email == 0)
176                 $rep->End();
177 }
178
179 ?>