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