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