Replaced the global variables for table styles to defined CSS classes.
[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 $Ajax, $Refs;
87
88         $editable = ($order->order_no == 0);
89
90         start_outer_table(TABLESTYLE2, "width=80%");
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     display_heading(_("Order Items"));
194
195     div_start('items_table');
196     start_table(TABLESTYLE, "width=80%");
197
198         $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
199                 _("Received"), _("Unit"),
200                 _("Required Delivery Date"), _("Price"), _("Line Total"), "");
201         if ($order->trans_type != ST_PURCHORDER)
202                 array_remove($th, 5);
203                 
204         if (count($order->line_items)) $th[] = '';
205         table_header($th);
206
207         $id = find_submit('Edit');
208         $total = 0;
209         $k = 0;
210         foreach ($order->line_items as $line_no => $po_line)
211         {
212         $line_total =   round($po_line->quantity * $po_line->price,  user_price_dec());
213         if (!$editable || ($id != $line_no))
214                 {
215                 alt_table_row_color($k);
216                 label_cell($po_line->stock_id);
217                 label_cell($po_line->item_description);
218             qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
219             qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
220                 label_cell($po_line->units);
221                         if ($order->trans_type == ST_PURCHORDER)
222                     label_cell($po_line->req_del_date);
223                 amount_decimal_cell($po_line->price);
224             amount_cell($line_total);
225
226                         if ($editable)
227                         {
228                                         edit_button_cell("Edit$line_no", _("Edit"),
229                                           _('Edit document line'));
230                                         delete_button_cell("Delete$line_no", _("Delete"),
231                                                 _('Remove line from document'));
232                         }
233                 end_row();
234                 }
235                 else
236                 {
237                         po_item_controls($order, $k, $line_no);
238                 }
239                 $total += $line_total;
240     }
241
242         if ($id==-1 && $editable)
243                 po_item_controls($order, $k);
244
245     $display_total = price_format($total);
246     label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan="
247         .(count($order->line_items) ? 7:6)." align=right",
248         "nowrap align=right", 2);
249
250         end_table(1);
251         div_end();
252 }
253
254 //---------------------------------------------------------------------------------------------------
255
256 function display_po_summary(&$po, $is_self=false, $editable=false)
257 {
258     start_table(TABLESTYLE, "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 ?>