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