Update from usntable branch.
[fa-stable.git] / purchasing / includes / ui / po_ui.inc
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 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
13
14 // ------------------------------------------------------------------------------
15
16 function get_supplier_details_to_order(&$order, $supplier_id)
17 {
18         $sql = "SELECT curr_code,supp_name FROM ".TB_PREF."suppliers
19                 WHERE supplier_id = '$supplier_id'";
20         $result = db_query($sql, "The supplier details could not be retreived");
21
22         $myrow = db_fetch($result);
23
24         $order->curr_code = $_POST['curr_code'] = $myrow["curr_code"];
25         $order->supplier_name = $_POST['supplier_name'] = $myrow["supp_name"];
26         $order->supplier_id = $_POST['supplier_id'] = $supplier_id;
27 }
28
29 //---------------------------------------------------------------------------------------------------
30
31 function create_new_po()
32 {
33         if (isset($_SESSION['PO']))
34         {
35                 unset ($_SESSION['PO']->line_items);
36                 $_SESSION['PO']->lines_on_order = 0;
37                 unset ($_SESSION['PO']);
38         }
39
40         session_register("PO");
41
42         $_SESSION['PO'] = new purch_order;
43         $_POST['OrderDate'] = new_doc_date();
44         if (!is_date_in_fiscalyear($_POST['OrderDate']))
45                 $_POST['OrderDate'] = end_fiscalyear();
46         $_SESSION['PO']->orig_order_date = $_POST['OrderDate'];
47 }
48
49 //---------------------------------------------------------------------------------------------------
50
51 function display_po_header(&$order)
52 {
53         global $table_style2, $Ajax, $Refs;
54
55         $editable = ($order->order_no == 0);
56
57         start_outer_table("width=80% $table_style2");
58
59         table_section(1);
60     if ($editable)
61     {
62         if (!isset($_POST['supplier_id']) && (get_global_supplier() != ALL_TEXT))
63                 $_POST['supplier_id'] = get_global_supplier();
64
65         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true, false, true);
66         }
67         else
68         {
69                 hidden('supplier_id', $order->supplier_id);
70                 label_row(_("Supplier:"), $order->supplier_name);
71     }
72
73         if ($order->supplier_id != get_post('supplier_id',-1)) {
74                 get_supplier_details_to_order($order, $_POST['supplier_id']);
75                 // supplier default price update
76                 foreach ($order->line_items as $line_no=>$item) {
77                         $line = &$order->line_items[$line_no];
78                         $line->price =  get_purchase_price ($order->supplier_id, $_POST['stock_id']);
79                         $line->quantity =  get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']);
80                 }
81             $Ajax->activate('items_table');
82         }
83         set_global_supplier($_POST['supplier_id']);
84
85         if (!is_company_currency($order->curr_code))
86         {
87                 label_row(_("Supplier Currency:"), $order->curr_code);
88                 exchange_rate_display($order->curr_code, get_company_currency(),
89                         $_POST['OrderDate']);
90         }
91
92     if ($editable)
93     {
94         ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_PURCHORDER));
95     }
96     else
97     {
98         hidden('ref', $order->reference);
99         label_row(_("Reference:"), $order->reference);
100     }
101
102         table_section(2);
103
104         // check this out?????????
105         //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
106         //      $_POST['OrderDate'] = $order->orig_order_date;
107         //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
108         //{
109         //      $_POST['OrderDate'] = Today();
110         //      if (!is_date_in_fiscalyear($_POST['OrderDate']))
111         //              $_POST['OrderDate'] = end_fiscalyear();
112         //}
113         date_row(_("Order Date:"), 'OrderDate', '', true, 0, 0, 0, null, true);
114         if (isset($_POST['_OrderDate_changed'])) {
115                 $Ajax->activate('_ex_rate');
116         }
117
118         text_row(_("Supplier's Reference:"), 'Requisition', null, 16, 15);
119
120         echo "<tr><td>" . _("Receive Into:") . "</td>";
121         echo "<td>";
122     echo locations_list('StkLocation', null, false, true);
123         echo "</td></tr>";
124
125         table_section(3);
126
127     if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
128         isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
129         $_POST['delivery_address'] == "")
130     {
131         /*If this is the first time the form loaded set up defaults */
132
133         //$_POST['StkLocation'] = $_SESSION['UserStockLocation'];
134         $sql = "SELECT delivery_address, phone FROM ".TB_PREF."locations WHERE loc_code='" . $_POST['StkLocation'] . "'";
135         $result = db_query($sql,"could not get location info");
136
137         if (db_num_rows($result) == 1)
138         {
139                 $loc_row = db_fetch($result);
140                 $_POST['delivery_address'] = $loc_row["delivery_address"];
141                         $Ajax->activate('delivery_address');
142                 $_SESSION['PO']->Location = $_POST['StkLocation'];
143                 $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
144
145         }
146         else
147         { /*The default location of the user is crook */
148                 display_error(_("The default stock location set up for this user is not a currently defined stock location. Your system administrator needs to amend your user record."));
149         }
150     }
151
152         textarea_row(_("Deliver to:"), 'delivery_address', $_POST['delivery_address'], 35, 4);
153
154         end_outer_table(); // outer table
155 }
156
157 //---------------------------------------------------------------------------------------------------
158
159 function display_po_items(&$order, $editable=true)
160 {
161         global $table_style;
162
163     display_heading(_("Order Items"));
164
165     div_start('items_table');
166     start_table("$table_style width=80%");
167
168         $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
169                 _("Received"), _("Unit"),
170                 _("Required Delivery Date"), _("Price"), _("Line Total"), "");
171
172         if (count($order->line_items)) $th[] = '';
173         table_header($th);
174
175         $id = find_submit('Edit');
176         $total = 0;
177         $k = 0;
178         foreach ($order->line_items as $line_no => $po_line)
179         {
180
181                 if ($po_line->Deleted == false)
182                 {
183                 $line_total =   round($po_line->quantity * $po_line->price,  user_price_dec());
184                 if (!$editable || ($id != $line_no))
185                         {
186                         alt_table_row_color($k);
187                         label_cell($po_line->stock_id);
188                         label_cell($po_line->item_description);
189                 qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
190                 qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
191                         label_cell($po_line->units);
192                 label_cell($po_line->req_del_date);
193                         amount_decimal_cell($po_line->price);
194                 amount_cell($line_total);
195
196                 if ($editable)
197                 {
198                                         edit_button_cell("Edit$line_no", _("Edit"),
199                                           _('Edit document line'));
200                                         delete_button_cell("Delete$line_no", _("Delete"),
201                                           _('Remove line from document'));
202                 }
203                         end_row();
204                         }
205                         else
206                         {
207                                 po_item_controls($order, $po_line->stock_id);
208                         }
209                 $total += $line_total;
210                 }
211     }
212
213         if ($id==-1 && $editable)
214                 po_item_controls($order);
215
216     $display_total = price_format($total);
217     label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan=7 align=right",
218         "nowrap align=right", 2);
219
220         end_table(1);
221         div_end();
222 }
223
224 //---------------------------------------------------------------------------------------------------
225
226 function display_po_summary(&$po, $is_self=false, $editable=false)
227 {
228         global $table_style2;
229     start_table("$table_style2 width=90%");
230
231     start_row();
232     label_cells(_("Reference"), $po->reference, "class='tableheader2'");
233
234     label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
235
236     if (!is_company_currency($po->curr_code))
237         label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
238
239     if (!$is_self)
240     {
241         label_cells(_("Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
242                 "class='tableheader2'");
243     }
244         end_row();
245         start_row();
246     label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
247
248     if ($editable)
249     {
250         if (!isset($_POST['Location']))
251                 $_POST['Location'] = $po->Location;
252         label_cell(_("Deliver Into Location"), "class='tableheader2'");
253         locations_list_cells(null, 'Location', $_POST['Location']);
254     }
255     else
256     {
257         label_cells(_("Deliver Into Location"), get_location_name($po->Location),
258                 "class='tableheader2'");
259     }
260
261     if ($po->requisition_no != "")
262         label_cells(_("Supplier's Reference"), $po->requisition_no, "class='tableheader2'");
263     end_row();
264
265     if (!$editable)
266         label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
267                 "colspan=9");
268
269     if ($po->Comments != "")
270         label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
271                 "colspan=9");
272     end_table(1);
273 }
274
275 //--------------------------------------------------------------------------------
276
277 function po_item_controls(&$order, $stock_id=null)
278 {
279    global $Ajax;
280         start_row();
281
282         $dec2 = 0;
283         $id = find_submit('Edit');
284         if (($id != -1) && $stock_id != null)
285         {
286                 hidden('line_no', $id);
287
288                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
289                         $dec = get_qty_dec($_POST['stock_id']);
290                 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
291                 //$_POST['price'] = price_format($order->line_items[$id]->price);
292                 $_POST['price'] = price_decimal_format($order->line_items[$id]->price, $dec2);
293                 $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
294
295                 $_POST['units'] = $order->line_items[$id]->units;
296
297                 hidden('stock_id', $_POST['stock_id']);
298                 label_cell($_POST['stock_id']);
299                 label_cell($order->line_items[$id]->item_description);
300             $Ajax->activate('items_table');
301                 $qty_rcvd = $order->line_items[$id]->qty_received;
302         }
303         else
304         {
305                 hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
306
307                 stock_purchasable_items_list_cells(null, 'stock_id', null, false, false, true);
308                 if (list_updated('stock_id')) {
309                             $Ajax->activate('price');
310                             $Ajax->activate('units');
311                             $Ajax->activate('qty');
312                             $Ajax->activate('req_del_date');
313                             $Ajax->activate('line_total');
314                 }
315         $item_info = get_item_edit_info($_POST['stock_id']);
316                 $_POST['units'] = $item_info["units"];
317
318                 $dec = $item_info["decimals"];
319                 $_POST['qty'] = number_format2(get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']), $dec);
320                 //$_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
321                 $_POST['price'] = price_decimal_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']), $dec2);
322                 $_POST['req_del_date'] = add_days(Today(), 10);
323                 $qty_rcvd = '';
324         }
325
326         qty_cells(null, 'qty', null, null, null, $dec);
327         qty_cell($qty_rcvd, false, $dec);
328
329         label_cell($_POST['units'], '', 'units');
330         date_cells(null, 'req_del_date', '', null, 0, 0, 0);
331         amount_cells(null, 'price', null, null, null, $dec2);
332
333         //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
334         $line_total = round(input_num('qty') * input_num('price'),  user_price_dec());
335         amount_cell($line_total, false, '','line_total');
336
337         if ($id!=-1)
338         {
339                 button_cell('UpdateLine', _("Update"),
340                                 _('Confirm changes'), ICON_UPDATE);
341                 button_cell('CancelUpdate', _("Cancel"),
342                                 _('Cancel changes'), ICON_CANCEL);
343                 set_focus('qty');
344         }
345         else
346         {
347                 submit_cells('EnterLine', _("Add Item"), "colspan=2",
348                     _('Add new item to document'), true);
349         }
350
351         end_row();
352 }
353
354 //---------------------------------------------------------------------------------------------------
355
356
357
358 ?>