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