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