Multiply contacts spport added.
[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
28 //----------------------------------------------------------------------------------------------------
29
30 print_po();
31
32 //----------------------------------------------------------------------------------------------------
33 function get_po($order_no)
34 {
35         $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,  "
36                 .TB_PREF."suppliers.supp_account_no,
37                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."suppliers.payment_terms, ".TB_PREF."locations.location_name,
38                 ".TB_PREF."suppliers.address, ".TB_PREF."suppliers.contact
39                 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
40                 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
41                 AND ".TB_PREF."locations.loc_code = into_stock_location
42                 AND ".TB_PREF."purch_orders.order_no = ".db_escape($order_no);
43         $result = db_query($sql, "The order cannot be retrieved");
44     return db_fetch($result);
45 }
46
47 function get_po_details($order_no)
48 {
49         $sql = "SELECT ".TB_PREF."purch_order_details.*, units
50                 FROM ".TB_PREF."purch_order_details
51                 LEFT JOIN ".TB_PREF."stock_master
52                 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
53                 WHERE order_no =".db_escape($order_no)." ";
54         $sql .= " ORDER BY po_detail_item";
55         return db_query($sql, "Retreive order Line Items");
56 }
57
58 function print_po()
59 {
60         global $path_to_root;
61
62         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
63
64         $from = $_POST['PARAM_0'];
65         $to = $_POST['PARAM_1'];
66         $currency = $_POST['PARAM_2'];
67         $email = $_POST['PARAM_3'];
68         $comments = $_POST['PARAM_4'];
69
70         if ($from == null)
71                 $from = 0;
72         if ($to == null)
73                 $to = 0;
74         $dec = user_price_dec();
75
76         $cols = array(4, 60, 225, 300, 340, 385, 450, 515);
77
78         // $headers in doctext.inc
79         $aligns = array('left', 'left', 'left', 'right', 'left', 'right', 'right');
80
81         $params = array('comments' => $comments);
82
83         $cur = get_company_Pref('curr_default');
84
85         if ($email == 0)
86         {
87                 $rep = new FrontReport(_('PURCHASE ORDER'), "PurchaseOrderBulk", user_pagesize());
88                 $rep->SetHeaderType('Header2');
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                 $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());
103                         $rep->SetHeaderType('Header2');
104                         $rep->currency = $cur;
105                         $rep->Font();
106                         $rep->title = _('PURCHASE ORDER');
107                         $rep->filename = "PurchaseOrder" . $i . ".pdf";
108                         $rep->Info($params, $cols, null, $aligns);
109                 }
110                 else
111                         $rep->title = _('PURCHASE ORDER');
112                 $contacts = get_supplier_contacts($myrow['supplier_id'], 'order');
113                 $rep->SetCommonData($myrow, null, $myrow, $baccount, ST_PURCHORDER, $contacts);
114                 $rep->NewPage();
115
116                 $result = get_po_details($i);
117                 $SubTotal = 0;
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                         $SubTotal += $Net;
135                         $dec2 = 0;
136                         $DisplayPrice = price_decimal_format($myrow2["unit_price"],$dec2);
137                         $DisplayQty = number_format2($myrow2["quantity_ordered"],get_qty_dec($myrow2['item_code']));
138                         $DisplayNet = number_format2($Net,$dec);
139                         //$rep->TextCol(0, 1,   $myrow2['item_code'], -2);
140                         $rep->TextCol(0, 2,     $myrow2['description'], -2);
141                         $rep->TextCol(2, 3,     sql2date($myrow2['delivery_date']), -2);
142                         $rep->TextCol(3, 4,     $DisplayQty, -2);
143                         $rep->TextCol(4, 5,     $myrow2['units'], -2);
144                         $rep->TextCol(5, 6,     $DisplayPrice, -2);
145                         $rep->TextCol(6, 7,     $DisplayNet, -2);
146                         $rep->NewLine(1);
147                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
148                                 $rep->NewPage();
149                 }
150                 if ($myrow['comments'] != "")
151                 {
152                         $rep->NewLine();
153                         $rep->TextColLines(1, 5, $myrow['comments'], -2);
154                 }
155                 $DisplaySubTot = number_format2($SubTotal,$dec);
156
157                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
158                 $linetype = true;
159                 $doctype = ST_PURCHORDER;
160                 include($path_to_root . "/reporting/includes/doctext.inc");
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                 $words = price_in_words($SubTotal, ST_PURCHORDER);
170                 if ($words != "")
171                 {
172                         $rep->NewLine(1);
173                         $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
174                 }       
175                 $rep->Font();
176                 if ($email == 1)
177                 {
178 //                      $myrow['contact_email'] = $myrow['email'];
179                         $myrow['DebtorName'] = $myrow['supp_name'];
180 //                      if ($myrow['contact'] != '') $myrow['DebtorName'] = $myrow['contact'];
181                         $myrow['reference'] = $myrow['order_no'];
182                         $rep->End($email, $doc_Order_no . " " . $myrow['reference'], $myrow);
183                 }
184         }
185         if ($email == 0)
186                 $rep->End();
187 }
188
189 ?>