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