Merged changes from master branch up to current state.
[fa-stable.git] / reporting / rep110.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:     Janusz Dobrwolski
17 // date_:       2008-01-14
18 // Title:       Print Delivery Notes
19 // draft version!
20 // ----------------------------------------------------------------
21 $path_to_root="..";
22
23 include_once($path_to_root . "/includes/session.inc");
24 include_once($path_to_root . "/includes/date_functions.inc");
25 include_once($path_to_root . "/includes/data_checks.inc");
26 include_once($path_to_root . "/sales/includes/sales_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_deliveries();
31
32 //----------------------------------------------------------------------------------------------------
33
34 function print_deliveries()
35 {
36         global $path_to_root, $SysPrefs;
37
38         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
39
40         $from = $_POST['PARAM_0'];
41         $to = $_POST['PARAM_1'];
42         $email = $_POST['PARAM_2'];
43         $packing_slip = $_POST['PARAM_3'];
44         $comments = $_POST['PARAM_4'];
45         $orientation = $_POST['PARAM_5'];
46
47         if (!$from || !$to) return;
48
49         $orientation = ($orientation ? 'L' : 'P');
50         $dec = user_price_dec();
51
52         $fno = explode("-", $from);
53         $tno = explode("-", $to);
54         $from = min($fno[0], $tno[0]);
55         $to = max($fno[0], $tno[0]);
56
57         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
58
59         // $headers in doctext.inc
60         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
61
62         $params = array('comments' => $comments);
63
64         $cur = get_company_Pref('curr_default');
65
66         if ($email == 0)
67         {
68                 if ($packing_slip == 0)
69                         $rep = new FrontReport(_('DELIVERY'), "DeliveryNoteBulk", user_pagesize(), 9, $orientation);
70                 else
71                         $rep = new FrontReport(_('PACKING SLIP'), "PackingSlipBulk", user_pagesize(), 9, $orientation);
72         }
73     if ($orientation == 'L')
74         recalculate_cols($cols);
75         for ($i = $from; $i <= $to; $i++)
76         {
77                         if (!exists_customer_trans(ST_CUSTDELIVERY, $i))
78                                 continue;
79                         $myrow = get_customer_trans($i, ST_CUSTDELIVERY);
80                         $branch = get_branch($myrow["branch_code"]);
81                         $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER); // ?
82                         if ($email == 1)
83                         {
84                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
85                                 if ($packing_slip == 0)
86                                 {
87                                         $rep->title = _('DELIVERY NOTE');
88                                         $rep->filename = "Delivery" . $myrow['reference'] . ".pdf";
89                                 }
90                                 else
91                                 {
92                                         $rep->title = _('PACKING SLIP');
93                                         $rep->filename = "Packing_slip" . $myrow['reference'] . ".pdf";
94                                 }
95                         }
96                         $rep->currency = $cur;
97                         $rep->Font();
98                         $rep->Info($params, $cols, null, $aligns);
99
100                         $contacts = get_branch_contacts($branch['branch_code'], 'delivery', $branch['debtor_no'], true);
101                         $rep->SetCommonData($myrow, $branch, $sales_order, '', ST_CUSTDELIVERY, $contacts);
102                         $rep->SetHeaderType('Header2');
103                         $rep->NewPage();
104
105                         $result = get_customer_trans_details(ST_CUSTDELIVERY, $i);
106                         $SubTotal = 0;
107                         while ($myrow2=db_fetch($result))
108                         {
109                                 if ($myrow2["quantity"] == 0)
110                                         continue;
111
112                                 $Net = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
113                                    user_price_dec());
114                                 $SubTotal += $Net;
115                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
116                         $DisplayQty = number_format2($myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
117                         $DisplayNet = number_format2($Net,$dec);
118                         if ($myrow2["discount_percent"]==0)
119                                         $DisplayDiscount ="";
120                         else
121                                         $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
122                                 $rep->TextCol(0, 1,     $myrow2['stock_id'], -2);
123                                 $oldrow = $rep->row;
124                                 $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
125                                 $newrow = $rep->row;
126                                 $rep->row = $oldrow;
127                                 if ($Net != 0.0  || !is_service($myrow2['mb_flag']) || !$SysPrefs->no_zero_lines_amount())
128                                 {
129                                         $rep->TextCol(2, 3,     $DisplayQty, -2);
130                                         $rep->TextCol(3, 4,     $myrow2['units'], -2);
131                                         if ($packing_slip == 0)
132                                         {
133                                                 $rep->TextCol(4, 5,     $DisplayPrice, -2);
134                                                 $rep->TextCol(5, 6,     $DisplayDiscount, -2);
135                                                 $rep->TextCol(6, 7,     $DisplayNet, -2);
136                                         }
137                                 }
138                                 $rep->row = $newrow;
139                                 //$rep->NewLine(1);
140                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
141                                         $rep->NewPage();
142                         }
143
144                         $memo = get_comments_string(ST_CUSTDELIVERY, $i);
145                         if ($memo != "")
146                         {
147                                 $rep->NewLine();
148                                 $rep->TextColLines(1, 5, $memo, -2);
149                         }
150
151                         $DisplaySubTot = number_format2($SubTotal,$dec);
152                         $DisplayFreight = number_format2($myrow["ov_freight"],$dec);
153
154                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
155                         $doctype=ST_CUSTDELIVERY;
156                         if ($packing_slip == 0)
157                         {
158                                 $rep->TextCol(3, 6, _("Sub-total"), -2);
159                                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
160                                 $rep->NewLine();
161                                 $rep->TextCol(3, 6, _("Shipping"), -2);
162                                 $rep->TextCol(6, 7,     $DisplayFreight, -2);
163                                 $rep->NewLine();
164                                 $tax_items = get_trans_tax_details(ST_CUSTDELIVERY, $i);
165                                 $first = true;
166                         while ($tax_item = db_fetch($tax_items))
167                         {
168                                 if ($tax_item['amount'] == 0)
169                                         continue;
170                                 $DisplayTax = number_format2($tax_item['amount'], $dec);
171  
172                                         if ($SysPrefs->suppress_tax_rates() == 1)
173                                                 $tax_type_name = $tax_item['tax_type_name'];
174                                         else
175                                                 $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
176
177                                         if ($myrow['tax_included'])
178                                 {
179                                                 if ($SysPrefs->alternative_tax_include_on_docs() == 1)
180                                         {
181                                                 if ($first)
182                                                 {
183                                                                 $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
184                                                                 $rep->TextCol(6, 7,     number_format2($tax_item['net_amount'], $dec), -2);
185                                                                 $rep->NewLine();
186                                                 }
187                                                         $rep->TextCol(3, 6, $tax_type_name, -2);
188                                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
189                                                         $first = false;
190                                         }
191                                         else
192                                                         $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
193                                         }
194                                 else
195                                 {
196                                                 $rep->TextCol(3, 6, $tax_type_name, -2);
197                                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
198                                         }
199                                         $rep->NewLine();
200                         }
201                         $rep->NewLine();
202                                 $DisplayTotal = number_format2($myrow["ov_freight"] +$myrow["ov_freight_tax"] + $myrow["ov_gst"] +
203                                         $myrow["ov_amount"],$dec);
204                                 $rep->Font('bold');
205                                 $rep->TextCol(3, 6, _("TOTAL DELIVERY INCL. VAT"), - 2);
206                                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
207                                 $words = price_in_words($myrow['Total'], ST_CUSTDELIVERY);
208                                 if ($words != "")
209                                 {
210                                         $rep->NewLine(1);
211                                         $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
212                                 }       
213                                 $rep->Font();
214                         }       
215                         if ($email == 1)
216                         {
217                                 $rep->End($email);
218                         }
219         }
220         if ($email == 0)
221                 $rep->End();
222 }
223