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