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