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