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