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