Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / reporting / rep209.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
13 $page_security = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?
14         'SA_SUPPTRANSVIEW' : 'SA_SUPPBULKREP';
15 // ----------------------------------------------------------------
16 // $ Revision:  2.0 $
17 // Creator:     Joe Hunt
18 // date_:       2005-05-19
19 // Title:       Purchase Orders
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 . "/includes/db/crm_contacts_db.inc");
27 include_once($path_to_root . "/taxes/tax_calc.inc");
28
29 //----------------------------------------------------------------------------------------------------
30
31 print_po();
32
33 //----------------------------------------------------------------------------------------------------
34 function get_supp_po($order_no)
35 {
36         $sql = "SELECT po.*, supplier.supp_name, supplier.supp_account_no,supplier.tax_included,
37                 supplier.gst_no AS tax_id,
38                 supplier.curr_code, supplier.payment_terms, loc.location_name,
39                 supplier.address, supplier.contact, supplier.tax_group_id
40                 FROM ".TB_PREF."purch_orders po,"
41                         .TB_PREF."suppliers supplier,"
42                         .TB_PREF."locations loc
43                 WHERE po.supplier_id = supplier.supplier_id
44                 AND loc.loc_code = into_stock_location
45                 AND po.order_no = ".db_escape($order_no);
46         $result = db_query($sql, "The order cannot be retrieved");
47     return db_fetch($result);
48 }
49
50 function get_po_details($order_no)
51 {
52         $sql = "SELECT poline.*, units
53                 FROM ".TB_PREF."purch_order_details poline
54                         LEFT JOIN ".TB_PREF."stock_master item ON poline.item_code=item.stock_id
55                 WHERE order_no =".db_escape($order_no)." ";
56         $sql .= " ORDER BY po_detail_item";
57         return db_query($sql, "Retreive order Line Items");
58 }
59
60 function print_po()
61 {
62         global $path_to_root, $SysPrefs;
63
64         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
65
66         $from = $_POST['PARAM_0'];
67         $to = $_POST['PARAM_1'];
68         $currency = $_POST['PARAM_2'];
69         $email = $_POST['PARAM_3'];
70         $comments = $_POST['PARAM_4'];
71         $orientation = $_POST['PARAM_5'];
72
73         if (!$from || !$to) return;
74
75         $orientation = ($orientation ? 'L' : 'P');
76         $dec = user_price_dec();
77
78         $cols = array(4, 60, 225, 300, 340, 385, 450, 515);
79
80         // $headers in doctext.inc
81         $aligns = array('left', 'left', 'left', 'right', 'left', 'right', 'right');
82
83         $params = array('comments' => $comments);
84
85         $cur = get_company_Pref('curr_default');
86
87         if ($email == 0)
88                 $rep = new FrontReport(_('PURCHASE ORDER'), "PurchaseOrderBulk", user_pagesize(), 9, $orientation);
89     if ($orientation == 'L')
90         recalculate_cols($cols);
91
92         for ($i = $from; $i <= $to; $i++)
93         {
94                 $myrow = get_supp_po($i);
95                 if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
96                         continue;
97                 }
98                 $baccount = get_default_bank_account($myrow['curr_code']);
99                 $params['bankaccount'] = $baccount['id'];
100
101                 if ($email == 1)
102                 {
103                         $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
104                         $rep->title = _('PURCHASE ORDER');
105                         $rep->filename = "PurchaseOrder" . $i . ".pdf";
106                 }       
107                 $rep->currency = $cur;
108                 $rep->Font();
109                 $rep->Info($params, $cols, null, $aligns);
110
111                 $contacts = get_supplier_contacts($myrow['supplier_id'], 'order');
112                 $rep->SetCommonData($myrow, null, $myrow, $baccount, ST_PURCHORDER, $contacts);
113                 $rep->SetHeaderType('Header2');
114                 $rep->NewPage();
115
116                 $result = get_po_details($i);
117                 $SubTotal = 0;
118                 $items = $prices = array();
119                 while ($myrow2=db_fetch($result))
120                 {
121                         $data = get_purchase_data($myrow['supplier_id'], $myrow2['item_code']);
122                         if ($data !== false)
123                         {
124                                 if ($data['supplier_description'] != "")
125                                         $myrow2['description'] = $data['supplier_description'];
126                                 if ($data['suppliers_uom'] != "")
127                                         $myrow2['units'] = $data['suppliers_uom'];
128                                 if ($data['conversion_factor'] != 1)
129                                 {
130                                         $myrow2['unit_price'] = round2($myrow2['unit_price'] * $data['conversion_factor'], user_price_dec());
131                                         $myrow2['quantity_ordered'] = round2($myrow2['quantity_ordered'] / $data['conversion_factor'], user_qty_dec());
132                                 }
133                         }
134                         $Net = round2(($myrow2["unit_price"] * $myrow2["quantity_ordered"]), user_price_dec());
135                         $prices[] = $Net;
136                         $items[] = $myrow2['item_code'];
137                         $SubTotal += $Net;
138                         $dec2 = 0;
139                         $DisplayPrice = price_decimal_format($myrow2["unit_price"],$dec2);
140                         $DisplayQty = number_format2($myrow2["quantity_ordered"],get_qty_dec($myrow2['item_code']));
141                         $DisplayNet = number_format2($Net,$dec);
142                         if ($SysPrefs->show_po_item_codes()) {
143                                 $rep->TextCol(0, 1,     $myrow2['item_code'], -2);
144                                 $rep->TextCol(1, 2,     $myrow2['description'], -2);
145                         } else
146                                 $rep->TextCol(0, 2,     $myrow2['description'], -2);
147                         $rep->TextCol(2, 3,     sql2date($myrow2['delivery_date']), -2);
148                         $rep->TextCol(3, 4,     $DisplayQty, -2);
149                         $rep->TextCol(4, 5,     $myrow2['units'], -2);
150                         $rep->TextCol(5, 6,     $DisplayPrice, -2);
151                         $rep->TextCol(6, 7,     $DisplayNet, -2);
152                         $rep->NewLine(1);
153                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
154                                 $rep->NewPage();
155                 }
156                 if ($myrow['comments'] != "")
157                 {
158                         $rep->NewLine();
159                         $rep->TextColLines(1, 4, $myrow['comments'], -2);
160                 }
161                 $DisplaySubTot = number_format2($SubTotal,$dec);
162
163                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
164                 $doctype = ST_PURCHORDER;
165
166                 $rep->TextCol(3, 6, _("Sub-total"), -2);
167                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
168                 $rep->NewLine();
169
170                 $tax_items = get_tax_for_items($items, $prices, 0,
171                   $myrow['tax_group_id'], $myrow['tax_included'],  null, TCA_LINES);
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                 $DisplayTotal = number_format2($SubTotal, $dec);
209                 $rep->Font('bold');
210                 $rep->TextCol(3, 6, _("TOTAL PO"), - 2);
211                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
212                 $words = price_in_words($SubTotal, ST_PURCHORDER);
213                 if ($words != "")
214                 {
215                         $rep->NewLine(1);
216                         $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
217                 }
218                 $rep->Font();
219                 if ($email == 1)
220                 {
221                         $myrow['DebtorName'] = $myrow['supp_name'];
222
223                         if ($myrow['reference'] == "")
224                                 $myrow['reference'] = $myrow['order_no'];
225                         $rep->End($email);
226                 }
227         }
228         if ($email == 0)
229                 $rep->End();
230 }
231