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