Currency filter not working in documents.
[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_po($order_no)
35 {
36         $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,  "
37                 .TB_PREF."suppliers.supp_account_no,".TB_PREF."suppliers.tax_included,".TB_PREF."suppliers.gst_no AS tax_id,
38                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."suppliers.payment_terms, ".TB_PREF."locations.location_name,
39                 ".TB_PREF."suppliers.address, ".TB_PREF."suppliers.contact, ".TB_PREF."suppliers.tax_group_id
40                 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
41                 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
42                 AND ".TB_PREF."locations.loc_code = into_stock_location
43                 AND ".TB_PREF."purch_orders.order_no = ".db_escape($order_no);
44         $result = db_query($sql, "The order cannot be retrieved");
45     return db_fetch($result);
46 }
47
48 function get_po_details($order_no)
49 {
50         $sql = "SELECT ".TB_PREF."purch_order_details.*, units
51                 FROM ".TB_PREF."purch_order_details
52                 LEFT JOIN ".TB_PREF."stock_master
53                 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
54                 WHERE order_no =".db_escape($order_no)." ";
55         $sql .= " ORDER BY po_detail_item";
56         return db_query($sql, "Retreive order Line Items");
57 }
58
59 function print_po()
60 {
61         global $path_to_root, $show_po_item_codes;
62
63         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
64
65         $from = $_POST['PARAM_0'];
66         $to = $_POST['PARAM_1'];
67         $currency = $_POST['PARAM_2'];
68         $email = $_POST['PARAM_3'];
69         $comments = $_POST['PARAM_4'];
70         $orientation = $_POST['PARAM_5'];
71
72         if (!$from || !$to) return;
73
74         $orientation = ($orientation ? 'L' : 'P');
75         $dec = user_price_dec();
76
77         $cols = array(4, 60, 225, 300, 340, 385, 450, 515);
78
79         // $headers in doctext.inc
80         $aligns = array('left', 'left', 'left', 'right', 'left', 'right', 'right');
81
82         $params = array('comments' => $comments);
83
84         $cur = get_company_Pref('curr_default');
85
86         if ($email == 0)
87                 $rep = new FrontReport(_('PURCHASE ORDER'), "PurchaseOrderBulk", user_pagesize(), 9, $orientation);
88     if ($orientation == 'L')
89         recalculate_cols($cols);
90
91         for ($i = $from; $i <= $to; $i++)
92         {
93                 $myrow = get_po($i);
94                 if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
95                         continue;
96                 }
97                 $baccount = get_default_bank_account($myrow['curr_code']);
98                 $params['bankaccount'] = $baccount['id'];
99
100                 if ($email == 1)
101                 {
102                         $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
103                         $rep->title = _('PURCHASE ORDER');
104                         $rep->filename = "PurchaseOrder" . $i . ".pdf";
105                 }       
106                 $rep->SetHeaderType('Header2');
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->NewPage();
114
115                 $result = get_po_details($i);
116                 $SubTotal = 0;
117                 $items = $prices = array();
118                 while ($myrow2=db_fetch($result))
119                 {
120                         $data = get_purchase_data($myrow['supplier_id'], $myrow2['item_code']);
121                         if ($data !== false)
122                         {
123                                 if ($data['supplier_description'] != "")
124                                         $myrow2['description'] = $data['supplier_description'];
125                                 if ($data['suppliers_uom'] != "")
126                                         $myrow2['units'] = $data['suppliers_uom'];
127                                 if ($data['conversion_factor'] != 1)
128                                 {
129                                         $myrow2['unit_price'] = round2($myrow2['unit_price'] * $data['conversion_factor'], user_price_dec());
130                                         $myrow2['quantity_ordered'] = round2($myrow2['quantity_ordered'] / $data['conversion_factor'], user_qty_dec());
131                                 }
132                         }
133                         $Net = round2(($myrow2["unit_price"] * $myrow2["quantity_ordered"]), user_price_dec());
134                         $prices[] = $Net;
135                         $items[] = $myrow2['item_code'];
136                         $SubTotal += $Net;
137                         $dec2 = 0;
138                         $DisplayPrice = price_decimal_format($myrow2["unit_price"],$dec2);
139                         $DisplayQty = number_format2($myrow2["quantity_ordered"],get_qty_dec($myrow2['item_code']));
140                         $DisplayNet = number_format2($Net,$dec);
141                         if ($show_po_item_codes) {
142                                 $rep->TextCol(0, 1,     $myrow2['item_code'], -2);
143                                 $rep->TextCol(1, 2,     $myrow2['description'], -2);
144                         } else
145                                 $rep->TextCol(0, 2,     $myrow2['description'], -2);
146                         $rep->TextCol(2, 3,     sql2date($myrow2['delivery_date']), -2);
147                         $rep->TextCol(3, 4,     $DisplayQty, -2);
148                         $rep->TextCol(4, 5,     $myrow2['units'], -2);
149                         $rep->TextCol(5, 6,     $DisplayPrice, -2);
150                         $rep->TextCol(6, 7,     $DisplayNet, -2);
151                         $rep->NewLine(1);
152                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
153                                 $rep->NewPage();
154                 }
155                 if ($myrow['comments'] != "")
156                 {
157                         $rep->NewLine();
158                         $rep->TextColLines(1, 5, $myrow['comments'], -2);
159                 }
160                 $DisplaySubTot = number_format2($SubTotal,$dec);
161
162                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
163                 $doctype = ST_PURCHORDER;
164
165                 $rep->TextCol(3, 6, _("Sub-total"), -2);
166                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
167                 $rep->NewLine();
168
169                 $tax_items = get_tax_for_items($items, $prices, 0,
170                   $myrow['tax_group_id'], $myrow['tax_included'],  null);
171                 $first = true;
172                 foreach($tax_items as $tax_item)
173                 {
174                         if ($tax_item['Value'] == 0)
175                                 continue;
176                         $DisplayTax = number_format2($tax_item['Value'], $dec);
177
178                         $tax_type_name = $tax_item['tax_type_name'];
179
180                         if ($myrow['tax_included'])
181                         {
182                                 if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
183                                 {
184                                         if ($first)
185                                         {
186                                                 $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
187                                                 $rep->TextCol(6, 7,     number_format2($sign*$tax_item['net_amount'], $dec), -2);
188                                                 $rep->NewLine();
189                                         }
190                                         $rep->TextCol(3, 6, $tax_type_name, -2);
191                                         $rep->TextCol(6, 7,     $DisplayTax, -2);
192                                         $first = false;
193                                 }
194                                 else
195                                         $rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . _("Amount") . ": " . $DisplayTax, -2);
196                         }
197                         else
198                         {
199                                 $SubTotal += $tax_item['Value'];
200                                 $rep->TextCol(3, 6, $tax_type_name, -2);
201                                 $rep->TextCol(6, 7,     $DisplayTax, -2);
202                         }
203                         $rep->NewLine();
204                 }
205
206                 $rep->NewLine();
207                 $DisplayTotal = number_format2($SubTotal, $dec);
208                 $rep->Font('bold');
209                 $rep->TextCol(3, 6, _("TOTAL PO"), - 2);
210                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
211                 $words = price_in_words($SubTotal, ST_PURCHORDER);
212                 if ($words != "")
213                 {
214                         $rep->NewLine(1);
215                         $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
216                 }
217                 $rep->Font();
218                 if ($email == 1)
219                 {
220                         $myrow['DebtorName'] = $myrow['supp_name'];
221
222                         if ($myrow['reference'] == "")
223                                 $myrow['reference'] = $myrow['order_no'];
224                         $rep->End($email);
225                 }
226         }
227         if ($email == 0)
228                 $rep->End();
229 }
230
231 ?>