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