Switch to new access levels system
[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
27 //----------------------------------------------------------------------------------------------------
28
29 print_po();
30
31 //----------------------------------------------------------------------------------------------------
32 function get_po($order_no)
33 {
34         $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,  ".TB_PREF."suppliers.supp_account_no,
35                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."suppliers.payment_terms, ".TB_PREF."locations.location_name,
36                 ".TB_PREF."suppliers.email, ".TB_PREF."suppliers.address, ".TB_PREF."suppliers.contact
37                 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
38                 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
39                 AND ".TB_PREF."locations.loc_code = into_stock_location
40                 AND ".TB_PREF."purch_orders.order_no = " . $order_no;
41         $result = db_query($sql, "The order cannot be retrieved");
42     return db_fetch($result);
43 }
44
45 function get_po_details($order_no)
46 {
47         $sql = "SELECT ".TB_PREF."purch_order_details.*, units
48                 FROM ".TB_PREF."purch_order_details
49                 LEFT JOIN ".TB_PREF."stock_master
50                 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
51                 WHERE order_no =$order_no ";
52         $sql .= " ORDER BY po_detail_item";
53         return db_query($sql, "Retreive order Line Items");
54 }
55
56 function print_po()
57 {
58         global $path_to_root;
59
60         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
61
62         $from = $_POST['PARAM_0'];
63         $to = $_POST['PARAM_1'];
64         $currency = $_POST['PARAM_2'];
65         $bankaccount = $_POST['PARAM_3'];
66         $email = $_POST['PARAM_4'];
67         $comments = $_POST['PARAM_5'];
68
69         if ($from == null)
70                 $from = 0;
71         if ($to == null)
72                 $to = 0;
73         $dec = user_price_dec();
74
75         $cols = array(4, 60, 225, 300, 340, 385, 450, 515);
76
77         // $headers in doctext.inc
78         $aligns = array('left', 'left', 'left', 'right', 'left', 'right', 'right');
79
80         $params = array('comments' => $comments,
81                                         'bankaccount' => $bankaccount);
82
83         $baccount = get_bank_account($params['bankaccount']);
84         $cur = get_company_Pref('curr_default');
85
86         if ($email == 0)
87         {
88                 $rep = new FrontReport(_('PURCHASE ORDER'), "PurchaseOrderBulk", user_pagesize());
89                 $rep->currency = $cur;
90                 $rep->Font();
91                 $rep->Info($params, $cols, null, $aligns);
92         }
93
94         for ($i = $from; $i <= $to; $i++)
95         {
96                 $myrow = get_po($i);
97
98                 if ($email == 1)
99                 {
100                         $rep = new FrontReport("", "", user_pagesize());
101                         $rep->currency = $cur;
102                         $rep->Font();
103                         $rep->title = _('PURCHASE ORDER');
104                         $rep->filename = "PurchaseOrder" . $i . ".pdf";
105                         $rep->Info($params, $cols, null, $aligns);
106                 }
107                 else
108                         $rep->title = _('PURCHASE ORDER');
109                 $rep->Header2($myrow, null, $myrow, $baccount, 8);
110
111                 $result = get_po_details($i);
112                 $SubTotal = 0;
113                 while ($myrow2=db_fetch($result))
114                 {
115                         $data = get_purchase_data($myrow['supplier_id'], $myrow2['item_code']);
116                         if ($data !== false)
117                         {
118                                 if ($data['supplier_description'] != "")
119                                         $myrow2['description'] = $data['supplier_description'];
120                                 if ($data['suppliers_uom'] != "")
121                                         $myrow2['units'] = $data['suppliers_uom'];
122                                 if ($data['conversion_factor'] != 1)
123                                 {
124                                         $myrow2['unit_price'] = round2($myrow2['unit_price'] * $data['conversion_factor'], user_price_dec());
125                                         $myrow2['quantity_ordered'] = round2($myrow2['quantity_ordered'] / $data['conversion_factor'], user_qty_dec());
126                                 }
127                         }       
128                         $Net = round2(($myrow2["unit_price"] * $myrow2["quantity_ordered"]), user_price_dec());
129                         $SubTotal += $Net;
130                         $dec2 = 0;
131                         $DisplayPrice = price_decimal_format($myrow2["unit_price"],$dec2);
132                         $DisplayQty = number_format2($myrow2["quantity_ordered"],get_qty_dec($myrow2['item_code']));
133                         $DisplayNet = number_format2($Net,$dec);
134                         //$rep->TextCol(0, 1,   $myrow2['item_code'], -2);
135                         $rep->TextCol(0, 2,     $myrow2['description'], -2);
136                         $rep->TextCol(2, 3,     sql2date($myrow2['delivery_date']), -2);
137                         $rep->TextCol(3, 4,     $DisplayQty, -2);
138                         $rep->TextCol(4, 5,     $myrow2['units'], -2);
139                         $rep->TextCol(5, 6,     $DisplayPrice, -2);
140                         $rep->TextCol(6, 7,     $DisplayNet, -2);
141                         $rep->NewLine(1);
142                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
143                                 $rep->Header2($myrow, $branch, $myrow, $baccount, 8);
144                 }
145                 if ($myrow['comments'] != "")
146                 {
147                         $rep->NewLine();
148                         $rep->TextColLines(1, 5, $myrow['comments'], -2);
149                 }
150                 $DisplaySubTot = number_format2($SubTotal,$dec);
151
152                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
153                 $linetype = true;
154                 $doctype = 8;
155                 if ($rep->currency != $myrow['curr_code'])
156                 {
157                         include($path_to_root . "/reporting/includes/doctext2.inc");
158                 }
159                 else
160                 {
161                         include($path_to_root . "/reporting/includes/doctext.inc");
162                 }
163
164                 $rep->TextCol(3, 6, $doc_Sub_total, -2);
165                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
166                 $rep->NewLine();
167                 $DisplayTotal = number_format2($SubTotal, $dec);
168                 $rep->Font('bold');
169                 $rep->TextCol(3, 6, $doc_TOTAL_PO, - 2);
170                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
171                 $rep->Font();
172                 if ($email == 1)
173                 {
174                         $myrow['contact_email'] = $myrow['email'];
175                         $myrow['DebtorName'] = $myrow['supp_name'];
176                         if ($myrow['contact'] != '') $myrow['DebtorName'] = $myrow['contact'];
177                         $myrow['reference'] = $myrow['order_no'];
178                         $rep->End($email, $doc_Order_no . " " . $myrow['reference'], $myrow);
179                 }
180         }
181         if ($email == 0)
182                 $rep->End();
183 }
184
185 ?>