34252f3427a325df1302e5ec08a2986b195e02c9
[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->tran_date;
22         if ($cart->trans_type == ST_SUPPINVOICE)
23                 $_POST['due_date'] = $cart->due_date;
24     $_POST['supp_ref'] = $cart->supp_ref;
25     $_POST['ref'] = $cart->reference;
26         $_POST['Comments'] = $cart->Comments;
27     $_POST['StkLocation'] = $cart->Location;
28     $_POST['delivery_address'] = $cart->delivery_address;
29         $_POST['prep_amount'] = price_format($cart->prep_amount);
30         $_POST['_ex_rate'] = $cart->ex_rate;
31         $_POST['cash_account'] = $cart->cash_account;
32     $_POST['dimension'] = $cart->dimension;
33     $_POST['dimension2'] = $cart->dimension2;
34     foreach($cart->tax_overrides as $id => $value)
35             $_POST['mantax'][$id] = price_format($value);
36 }
37
38 function copy_to_cart()
39 {
40         $cart = &$_SESSION['PO'];
41
42         $cart->supplier_id = $_POST['supplier_id'];     
43         $cart->tran_date = $_POST['OrderDate'];
44         if ($cart->trans_type == ST_SUPPINVOICE)
45         {
46                 $cart->due_date = $_POST['due_date'];
47                 $cart->cash_account = get_post('cash_account');
48         }
49         $cart->reference = $_POST['ref'];
50         $cart->supp_ref = $_POST['supp_ref'];
51         $cart->Comments = $_POST['Comments'];   
52         $cart->Location = $_POST['StkLocation'];
53         $cart->delivery_address = $_POST['delivery_address'];
54         $cart->dimension = @$_POST['dimension'];
55         $cart->dimension2 =  @$_POST['dimension2'];
56         $cart->prep_amount = input_num('prep_amount', 0);
57         $cart->ex_rate = input_num('_ex_rate', null);
58
59     if (isset($_POST['mantax'])) {
60                 foreach($_POST['mantax'] as $id => $tax) {
61                         $cart->tax_overrides[$id] = user_numeric($_POST['mantax'][$id]); }
62         }
63 }
64
65 //---------------------------------------------------------------------------------------------------
66
67 function create_cart($trans_type, $trans_no)
68 {
69         global $Refs;
70
71         if (isset($_SESSION['PO']))
72                 unset ($_SESSION['PO']->line_items, $_SESSION['PO']);
73
74         $cart = new purch_order;
75         $cart->trans_type = $trans_type;
76
77         if ($trans_type == ST_SUPPRECEIVE && $trans_no != 0) {
78                 read_grn($trans_no, $cart);
79                 foreach($cart->line_items as &$line) {
80                         $line->qty_old = $line->quantity; // for concurrency checks
81                         $line->qty_received -= $line->quantity;
82                 }
83         } else {
84                 $cart->order_no = $trans_no;
85                 $_POST['OrderDate'] = new_doc_date();
86                 if (!is_date_in_fiscalyear($_POST['OrderDate']))
87                         $_POST['OrderDate'] = end_fiscalyear();
88                 $cart->due_date = $cart->tran_date = $_POST['OrderDate'];
89                 /* read in all the selected order into the Items cart  */
90                 if ($trans_no) {
91                         read_po($trans_no, $cart);
92                         $cart->order_no = $trans_no;
93                 } else
94                         $cart->reference = $Refs->get_next($trans_type, null,
95                                 array('supplier_id' => $cart->supplier_id, 'date' => get_post('OrderDate')));
96         }
97         $_SESSION['PO'] = &$cart;
98 }
99
100 //---------------------------------------------------------------------------------------------------
101
102 function display_po_header(&$order)
103 {
104         global $Ajax, $Refs;
105
106         $editable = ($order->order_no == 0);
107
108         start_outer_table(TABLESTYLE2, "width='80%'");
109
110         table_section(1);
111     if ($editable)
112     {
113         if (!isset($_POST['supplier_id']) && (get_global_supplier() != ALL_TEXT))
114                 $_POST['supplier_id'] = get_global_supplier();
115
116         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true, false, true);
117         }
118         else
119         {
120                 hidden('supplier_id', $order->supplier_id);
121                 label_row(_("Supplier:"), $order->supplier_name);
122     }
123
124         if ($order->supplier_id != get_post('supplier_id',-1)) {
125                 $old_supp = $order->supplier_id;
126                 get_supplier_details_to_order($order, $_POST['supplier_id']); 
127             get_duedate_from_terms($order);
128                 $_POST['due_date'] = $order->due_date;
129
130                 // supplier default price update
131                 foreach ($order->line_items as $line_no=>$item) {
132                         $line = &$order->line_items[$line_no];
133                         $line->price =  get_purchase_price ($order->supplier_id, $line->stock_id);
134                         $line->quantity =
135                                 $line->quantity/get_purchase_conversion_factor ($old_supp, $line->stock_id)
136                                         *get_purchase_conversion_factor ($order->supplier_id, $line->stock_id);
137                 }
138             $Ajax->activate('items_table');
139             $Ajax->activate('due_date');
140         }
141         set_global_supplier($_POST['supplier_id']);
142
143         date_row($order->trans_type==ST_PURCHORDER ? _("Order Date:") :
144                 ($order->trans_type==ST_SUPPRECEIVE ? _("Delivery Date:") : _("Invoice Date:")),
145                 'OrderDate', '', true, 0, 0, 0, null, true);
146
147         if (isset($_POST['_OrderDate_changed'])) {
148                 $order->tran_date = $_POST['OrderDate'];
149             get_duedate_from_terms($order);
150             $_POST['due_date'] = $order->due_date;
151                 $Ajax->activate('due_date');
152         }
153         supplier_credit_row($order->supplier_id, $order->credit);
154
155         if (!is_company_currency($order->curr_code))
156         {
157                 label_row(_("Supplier Currency:"), $order->curr_code);
158                 exchange_rate_display(get_company_currency(), $order->curr_code,
159                         $_POST['OrderDate']);
160         }
161
162     if ($editable)
163     {
164         ref_row(_("Reference:"), 'ref', '', null, false, $order->trans_type, array('date'=> @$_POST['OrderDate']));
165     }
166     else
167     {
168         hidden('ref', $order->reference);
169         label_row(_("Reference:"), $order->reference);
170     }
171
172         table_section(2);
173
174         if ($order->trans_type==ST_SUPPINVOICE)
175                 date_row(_("Due Date:"), 'due_date', '', false, 0, 0, 0, null, true);
176
177         text_row(_("Supplier's Reference:"), 'supp_ref', null, 20, 60);
178
179         if (get_company_pref('use_dimension'))
180                 dimensions_list_row(_('Dimension').':', 'dimension', null, true, _('Default'), false, 1);
181         if (get_company_pref('use_dimension') == 2)
182                 dimensions_list_row(_('Dimension 2').':', 'dimension2', null, true, _('Default'), false, 2);
183         locations_list_row(_("Receive Into:"), 'StkLocation', null, false, true, $order->fixed_asset); 
184
185         table_section(3);
186
187     if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
188         isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
189         $_POST['delivery_address'] == "")
190     {
191         /*If this is the first time the form loaded set up defaults */
192
193                 $loc_row = get_item_location(get_post('StkLocation'));
194         if ($loc_row)
195         {
196                 $_POST['delivery_address'] = $loc_row["delivery_address"];
197                         $Ajax->activate('delivery_address');
198                 $_SESSION['PO']->Location = $_POST['StkLocation'];
199                 $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
200
201         }
202         else
203         { /*The default location of the user is crook */
204                 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."));
205         }
206     }
207
208         textarea_row(_("Deliver to:"), 'delivery_address', $_POST['delivery_address'], 35, 4);
209
210         end_outer_table(); // outer table
211 }
212
213 //---------------------------------------------------------------------------------------------------
214
215 function display_po_items(&$order, $editable=true)
216 {
217     display_heading(_("Order Items"));
218
219     div_start('items_table');
220     start_table(TABLESTYLE, "width='80%'");
221
222         $new = $order->order_no == 0;
223
224         $th = array(_("Item Code"), _("Item Description"), _("Quantity"), _("Received"),
225                 _("Unit"), _("Required Delivery Date"), $order->tax_included ? _("Price after Tax") : _("Price before Tax"), _("Line Total"), "");
226
227         if ($order->trans_type != ST_PURCHORDER)
228                 array_remove($th, 5);
229         if ($new)
230                 array_remove($th, 3);
231
232         if (count($order->line_items)) $th[] = '';
233
234         table_header($th);
235
236         $id = find_submit('Edit');
237         $total = 0;
238         $k = 0;
239         foreach ($order->line_items as $line_no => $po_line)
240         {
241         $line_total =   round($po_line->quantity * $po_line->price,  user_price_dec());
242         if (!$editable || ($id != $line_no))
243                 {
244                 alt_table_row_color($k);
245                 label_cell($po_line->stock_id);
246                 label_cell($po_line->item_description);
247                 if ($order->fixed_asset)
248                     label_cell(1);
249                 else
250                 qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
251                         if (!$new)
252                     qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
253                 label_cell($po_line->units);
254                         if ($order->trans_type == ST_PURCHORDER)
255                     label_cell($po_line->req_del_date);
256                 amount_decimal_cell($po_line->price);
257             amount_cell($line_total);
258
259                         if ($editable)
260                         {
261                                         edit_button_cell("Edit$line_no", _("Edit"),
262                                           _('Edit document line'));
263                                         delete_button_cell("Delete$line_no", _("Delete"),
264                                                 _('Remove line from document'));
265                         }
266                 end_row();
267                 }
268                 else
269                 {
270                         po_item_controls($order, $k, $line_no);
271                 }
272                 $total += $line_total;
273     }
274
275         if ($id==-1 && $editable)
276                 po_item_controls($order, $k);
277
278         $colspan = count($th)-2;
279         if (count($order->line_items))
280                 $colspan--;
281
282         $display_sub_total = price_format($total);
283
284         label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right", 2);
285
286         $taxes = $order->get_taxes(input_num('freight_cost'));
287         
288         $tax_total = display_edit_tax_items($taxes, $colspan, $order->tax_included, 2, $order->trans_type==ST_SUPPINVOICE);
289
290         $display_total = price_format(($total + input_num('freight_cost') + $tax_total));
291
292         start_row();
293         label_cells(_("Amount Total"), $display_total, "colspan=$colspan align='right'","align='right'");
294         $order->trans_type == ST_SUPPINVOICE
295          ? submit_cells('update', _("Update"), "colspan=2 align='center'", _("Refresh"), true)
296                 : label_cell('', "colspan=2");
297         end_row();
298
299         end_table(1);
300         div_end();
301 }
302
303 //---------------------------------------------------------------------------------------------------
304
305 function display_po_summary(&$po, $is_self=false, $editable=false)
306 {
307     start_table(TABLESTYLE, "width='90%'");
308
309     start_row();
310     label_cells(_("Reference"), $po->reference, "class='tableheader2'");
311
312     label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
313
314     if (!is_company_currency($po->curr_code))
315         label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
316
317     if (!$is_self)
318     {
319         label_cells(_("Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
320                 "class='tableheader2'");
321     }
322         end_row();
323         start_row();
324     label_cells(_("Date"), $po->tran_date, "class='tableheader2'");
325
326     if ($editable)
327     {
328         if (!isset($_POST['Location']))
329                 $_POST['Location'] = $po->Location;
330         label_cell(_("Deliver Into Location"), "class='tableheader2'");
331         locations_list_cells(null, 'Location', $_POST['Location']);
332     }
333     else
334     {
335         label_cells(_("Deliver Into Location"), get_location_name($po->Location),
336                 "class='tableheader2'");
337     }
338
339     if ($po->supp_ref != "")
340         label_cells(_("Supplier's Reference"), $po->supp_ref, "class='tableheader2'");
341     end_row();
342
343     if (!$editable)
344         label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
345                 "colspan=9");
346
347         if (!$editable && ($po->prep_amount > 0))
348         {
349                 start_row();
350                 label_cells(_("Required Pre-Payment"), price_format($po->prep_amount), "class='tableheader2'");
351                 label_cells(_("Pre-Payments Allocated"), price_format($po->alloc), "class='tableheader2'");
352                 end_row();
353         }
354
355     if ($po->Comments != "")
356         label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
357                 "colspan=9");
358     end_table(1);
359 }
360
361 //--------------------------------------------------------------------------------
362
363 function po_item_controls(&$order, &$rowcounter, $line_no=-1)
364 {
365    global $Ajax, $SysPrefs;
366
367         alt_table_row_color($rowcounter);
368         $new = $order->order_no == 0;
369
370         $dec2 = 0;
371         $id = find_submit('Edit');
372         if (($id != -1) && $line_no == $id)
373         {
374
375                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
376                 $dec = get_qty_dec($_POST['stock_id']);
377                 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
378                 $_POST['price'] = price_decimal_format($order->line_items[$id]->price, $dec2);
379                 if ($order->trans_type == ST_PURCHORDER)
380                         $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
381
382                 $_POST['units'] = $order->line_items[$id]->units;
383                 $_POST['item_description'] = $order->line_items[$id]->item_description;
384
385                 hidden('stock_id', $_POST['stock_id']);
386                 label_cell($_POST['stock_id']);
387
388                 if ($order->line_items[$id]->descr_editable)
389                         text_cells(null,'item_description', null, 45, 150);
390                 else {
391                         hidden('item_description', $_POST['item_description']);
392                         label_cell($order->line_items[$id]->item_description); 
393                 }
394
395             $Ajax->activate('items_table');
396                 $qty_rcvd = $order->line_items[$id]->qty_received;
397         }
398         else
399         {
400
401                 // Manufactured item can be purchased
402                 if ($order->fixed_asset)
403                         stock_purchasable_fa_list_cells(null, 'stock_id', null, false, true, false, true, $order->line_items);
404                 else
405                         stock_items_list_cells(null, 'stock_id', null, false, true, false, true, array('editable' => 30, 'where'=>array("NOT no_purchase")));
406
407                 if (list_updated('stock_id')) {
408                             $Ajax->activate('price');
409                             $Ajax->activate('units');
410                             $Ajax->activate('qty');
411                             $Ajax->activate('req_del_date');
412                             $Ajax->activate('line_total');
413                 }
414         $item_info = get_item_edit_info($_POST['stock_id']);
415                 $_POST['units'] = $item_info["units"];
416
417                 $dec = $item_info["decimals"];
418                 $_POST['qty'] = number_format2(get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']), $dec);
419                 $_POST['price'] = price_decimal_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']), $dec2);
420                 if ($order->trans_type == ST_PURCHORDER)
421                         $_POST['req_del_date'] = add_days(Today(), $SysPrefs->default_receival_required_by());
422                 $qty_rcvd = '';
423         }
424
425         if ($order->fixed_asset) {
426                 label_cell(1, '', 'qty');
427                 hidden('qty', 1);
428         } else
429                 qty_cells(null, 'qty', null, null, null, $dec);
430
431         if (!$new)
432                 qty_cell($qty_rcvd, false, $dec);
433
434         label_cell($_POST['units'], '', 'units');
435         if ($order->trans_type == ST_PURCHORDER)
436                 date_cells(null, 'req_del_date', '', null, 0, 0, 0);
437         if ($qty_rcvd > 0)
438         {
439                 amount_decimal_cell($_POST['price']);
440                 hidden('price', $_POST['price']);
441         }       
442         else    
443                 amount_cells(null, 'price', null, null, null, $dec2);
444
445         $line_total = round(input_num('qty') * input_num('price'),  user_price_dec());
446         amount_cell($line_total, false, '','line_total');
447
448         if ($id!=-1)
449         {
450                 button_cell('UpdateLine', _("Update"),
451                                 _('Confirm changes'), ICON_UPDATE);
452                 button_cell('CancelUpdate', _("Cancel"),
453                                 _('Cancel changes'), ICON_CANCEL);
454                 hidden('line_no', $line_no);
455                 set_focus('qty');
456         }
457         else
458         {
459                 submit_cells('EnterLine', _("Add Item"), "colspan=2 align='center'",
460                     _('Add new item to document'), true);
461         }
462
463         end_row();
464 }
465
466 //---------------------------------------------------------------------------------------------------
467
468 function display_po_footer(&$trans)
469 {
470         global $Ajax;
471
472 start_table(TABLESTYLE2);
473
474 if ($trans->trans_type == ST_SUPPINVOICE) {
475         cash_accounts_list_row(_("Payment:"), 'cash_account', null, false, _('Delayed'));
476 } elseif ($trans->trans_type == ST_PURCHORDER)
477         amount_row(_("Pre-Payment Required:"), 'prep_amount');
478
479 textarea_row(_("Memo:"), 'Comments', null, 70, 4);
480
481 end_table();
482 }
483 //---------------------------------------------------------------------------------------------------
484
485
486