Better layout. Copyright notes. Mailto links, outer table routines. (3. run)
[fa-stable.git] / purchasing / includes / ui / invoice_ui.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 //--------------------------------------------------------------------------------------------------
13
14 function copy_from_trans(&$supp_trans)
15 {
16         $_POST['Comments'] = $supp_trans->Comments;
17         $_POST['tran_date'] = $supp_trans->tran_date;
18         $_POST['due_date'] = $supp_trans->due_date;
19         $_POST['supp_reference'] = $supp_trans->supp_reference;
20         $_POST['reference'] = $supp_trans->reference;
21         $_POST['supplier_id'] = $supp_trans->supplier_id;
22 }
23
24 //--------------------------------------------------------------------------------------------------
25
26 function copy_to_trans(&$supp_trans)
27 {
28         $supp_trans->Comments = $_POST['Comments'];
29         $supp_trans->tran_date = $_POST['tran_date'];
30         $supp_trans->due_date = $_POST['due_date'];
31         $supp_trans->supp_reference = $_POST['supp_reference'];
32         $supp_trans->reference = $_POST['reference'];
33
34         $supp_trans->ov_amount = 0; /* for starters */
35         if (count($supp_trans->grn_items) > 0)
36         {
37                 foreach ( $supp_trans->grn_items as $grn)
38                 {
39                         $supp_trans->ov_amount += round(($grn->this_quantity_inv * $grn->chg_price),
40                           user_price_dec());
41                 }
42         }
43         if (count($supp_trans->gl_codes) > 0)
44         {
45                 foreach ( $supp_trans->gl_codes as $gl_line)
46                 {
47                         $supp_trans->ov_amount += $gl_line->amount;
48                 }
49         }
50 }
51
52 //--------------------------------------------------------------------------------------------------
53
54 function invoice_header(&$supp_trans)
55 {
56         global $Ajax, $table_style;
57         
58         // if vars have been lost, recopy
59         if (!isset($_POST['tran_date']))
60                 copy_from_trans($supp_trans);
61
62         start_outer_table("$table_style width=98%", 5);
63
64         table_section(1);
65
66     if (!isset($_POST['supplier_id']) && (get_global_supplier() != reserved_words::get_all()))
67         $_POST['supplier_id'] = get_global_supplier();
68
69         supplier_list_row(_("Supplier:"), 'supplier_id', $_POST['supplier_id'], false, true);
70
71         if ($supp_trans->supplier_id != $_POST['supplier_id'])
72         {
73                 // supplier has changed
74                 // delete all the order items - drastic but necessary because of
75                 // change of currency, etc
76                 $supp_trans->clear_items();
77                 read_supplier_details_to_trans($supp_trans, $_POST['supplier_id']);
78                 copy_from_trans($supp_trans);
79         }
80
81         if ($supp_trans->is_invoice)
82         ref_row(_("Reference:"), 'reference', '', references::get_next(20));
83     else
84         ref_row(_("Reference:"), 'reference', '', references::get_next(21));
85
86         text_row(_("Supplier's Ref.:"), 'supp_reference', $_POST['supp_reference'], 20, 20);
87
88         table_section(2, "33%");
89
90         date_row(_("Date") . ":", 'tran_date', '', null, 0, 0, 0, "", true);
91         if (isset($_POST['_tran_date_changed'])) {
92                 $Ajax->activate('_ex_rate');
93                 $supp_trans->tran_date = $_POST['tran_date'];
94                 get_duedate_from_terms($supp_trans);
95                 $_POST['due_date'] = $supp_trans->due_date;
96                 $Ajax->activate('due_date');
97         }
98
99     date_row(_("Due Date") . ":", 'due_date');
100
101     label_row(_("Terms:"), $supp_trans->terms_description);
102
103         table_section(3, "33%");
104
105         $supplier_currency = get_supplier_currency($supp_trans->supplier_id);
106
107         $company_currency = get_company_currency();
108
109         if ($supplier_currency != $company_currency)
110         {
111         label_row(_("Supplier's Currency:"), "<b>" . $supplier_currency . "</b>");
112                 exchange_rate_display($supplier_currency, $company_currency, $_POST['tran_date']);
113         }
114
115         label_row(_("Tax Group:"), $supp_trans->tax_description);
116
117         end_outer_table();
118 }
119
120 //--------------------------------------------------------------------------------------------------
121
122 function invoice_totals(&$supp_trans)
123 {
124         global $table_style, $table_style2;
125
126         copy_to_trans($supp_trans);
127
128     start_table("$table_style width=95%");
129         label_row(_("Sub-total:"), price_format( $supp_trans->ov_amount), "align=right", "align=right");
130
131     $taxes = $supp_trans->get_taxes($supp_trans->tax_group_id);
132     $tax_total = display_edit_tax_items($taxes, 1, 0); // tax_included==0 (we are the company)
133
134     $display_total = price_format($supp_trans->ov_amount + $tax_total);
135
136         if ($supp_trans->is_invoice)
137         label_row(_("Invoice Total:"), $display_total, "align=right style='font-weight:bold;'", "align=right style='font-weight:bold;'");
138     else
139                 label_row(_("Credit Note Total"),
140                         $display_total, "align=right style='font-weight:bold;color:red;'", "nowrap align=right style='font-weight:bold;color:red;'");
141
142     end_table(1);
143     start_table($table_style2);
144     textarea_row(_("Memo:"), "Comments", null, 50, 3);
145     end_table(1);
146 }
147
148 //--------------------------------------------------------------------------------------------------
149 function display_gl_controls(&$supp_trans, $k)
150 {
151         global $table_style;
152
153         $accs = get_supplier_accounts($supp_trans->supplier_id);
154         $_POST['gl_code'] = $accs['purchase_account'];
155
156         alt_table_row_color($k);
157         gl_all_accounts_list('gl_code', null, true, false, true);
158         $dim = get_company_pref('use_dimension');
159         if ($dim >= 1)
160                 dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
161         if ($dim > 1)
162                 dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
163         amount_cells(null, 'amount');
164         if ($dim < 1)   
165                 text_cells_ex(null, 'memo_', 35, 50, null, null, null, hidden('dimension_id', 0, false).hidden('dimension2_id', 0, false));
166         else if ($dim < 2)      
167                 text_cells_ex(null, 'memo_', 35, 50, null, null, null, hidden('dimension2_id', 0, false));
168         else    
169                 text_cells_ex(null, 'memo_', 35, 50, null, null, null);
170         submit_cells('AddGLCodeToTrans', _("Add"), "",
171                     _('Add GL Line'), true);
172         submit_cells('ClearFields', _("Reset"), "",
173                     _("Clear all GL entry fields"), true);
174         end_row();      
175 }
176
177 // $mode = 0 none at the moment
178 //               = 1 display on invoice/credit page
179 //               = 2 display on view invoice
180 //               = 3 display on view credit
181
182 function display_gl_items(&$supp_trans, $mode=0)
183 {
184         global $table_style, $path_to_root;
185
186     // if displaying in form, and no items, exit
187     if (($mode == 2 || $mode == 3) && count($supp_trans->gl_codes) == 0)
188         return 0;
189
190         start_outer_table("width=95%");
191
192         if ($supp_trans->is_invoice)
193                 $heading = _("GL Items for this Invoice");
194         else
195                 $heading = _("GL Items for this Credit Note");
196
197         vertical_space(); // ouer table
198
199         div_start('gl_items');
200         start_table("$table_style width=100%");
201
202         $dim = get_company_pref('use_dimension');
203         if ($dim == 2)
204         $th = array(_("Account"), _("Name"), _("Dimension")." 1", _("Dimension")." 2", _("Amount"), _("Memo"));
205         else if ($dim == 1)
206         $th = array(_("Account"), _("Name"), _("Dimension"), _("Amount"), _("Memo"));
207     else
208         $th = array(_("Account"), _("Name"), _("Amount"), _("Memo"));
209
210         if ($mode == 1)
211         {
212                 $th[] = "";
213                 $th[] = "";
214         }       
215         table_header($th);
216         $total_gl_value=0;
217         $i = $k = 0;
218
219         if (count($supp_trans->gl_codes) > 0)
220         {
221
222                 foreach ($supp_trans->gl_codes as $entered_gl_code)
223                 {
224
225                         alt_table_row_color($k);
226
227                         if ($mode == 3)
228                                 $entered_gl_code->amount = -$entered_gl_code->amount;
229
230                         label_cell($entered_gl_code->gl_code);
231                         label_cell($entered_gl_code->gl_act_name);
232
233                         if ($dim >= 1)
234                                 label_cell(get_dimension_string($entered_gl_code->gl_dim, true));
235                         if ($dim > 1)
236                                 label_cell(get_dimension_string($entered_gl_code->gl_dim2, true));
237
238                         amount_cell($entered_gl_code->amount, true);
239                         label_cell($entered_gl_code->memo_);
240
241                         if ($mode == 1)
242                         {
243                                 delete_button_cell("Delete2" . $entered_gl_code->Counter, _("Delete"),
244                                           _('Remove line from document'));
245                                 label_cell("");
246                         }       
247                         end_row();
248
249                         $total_gl_value += $entered_gl_code->amount;
250
251                         $i++;
252                         if ($i > 15)
253                         {
254                                 $i = 0;
255                                 table_header($th);
256                         }
257                 }
258
259         }
260         if ($mode == 1)
261                 display_gl_controls($supp_trans, $k);
262         $colspan = ($dim == 2 ? 4 : ($dim == 1 ? 3 : 2));
263         label_row(_("Total"), price_format($total_gl_value),
264                 "colspan=".$colspan." align=right", "nowrap align=right");
265
266         end_table();
267         div_end();
268
269     end_outer_table(0, false); // outertable
270         return $total_gl_value;
271 }
272
273 //--------------//-----------------------------------------------------------------------------------------
274
275 function display_grn_items_for_selection(&$supp_trans, $k)
276 {
277         global $table_style;
278
279         if ($supp_trans->is_invoice)
280                 $result = get_grn_items(0, $supp_trans->supplier_id, true);
281         else    
282                 $result = get_grn_items(0, $supp_trans->supplier_id, false, true);
283
284     if (db_num_rows($result) == 0)
285     {
286         return false;
287     }
288     
289     /*Set up a table to show the outstanding GRN items for selection */
290
291     while ($myrow = db_fetch($result))
292     {
293                 $grn_already_on_invoice = false;
294
295         foreach ($supp_trans->grn_items as $entered_grn)
296         {
297                 if ($entered_grn->id == $myrow["id"])
298                 {
299                         $grn_already_on_invoice = true;
300                 }
301         }
302         if ($grn_already_on_invoice == false)
303         {
304
305                         alt_table_row_color($k);
306
307                         $n = $myrow["id"];
308                 label_cell(get_trans_view_str(25, $myrow["grn_batch_id"]));
309                 label_cell($myrow["id"].
310                 hidden('qty_recd'.$n, $myrow["qty_recd"], false).
311                 hidden('item_code'.$n, $myrow["item_code"], false).
312                 hidden('item_description'.$n, $myrow["description"], false).
313                 hidden('prev_quantity_inv'.$n, $myrow['quantity_inv'], false).
314                 hidden('order_price'.$n, $myrow['unit_price'], false).
315                 hidden('std_cost_unit'.$n, $myrow['std_cost_unit'], false).
316                 hidden('po_detail_item'.$n, $myrow['po_detail_item'], false));
317                 label_cell(get_trans_view_str(systypes::po(), $myrow["purch_order_no"]));
318             label_cell($myrow["item_code"]);
319             label_cell($myrow["description"]);
320             label_cell(sql2date($myrow["delivery_date"]));
321             $dec = get_qty_dec($myrow["item_code"]);
322             qty_cell($myrow["qty_recd"], false, $dec);
323             qty_cell($myrow["quantity_inv"], false, $dec);
324             if ($supp_trans->is_invoice)
325                 qty_cells(null, 'this_quantity_inv'.$n, number_format2($myrow["qty_recd"] - $myrow["quantity_inv"], $dec), 
326                         null, null, $dec);
327             else                
328                 qty_cells(null, 'This_QuantityCredited'.$n, number_format2(max($myrow["quantity_inv"], 0), $dec), 
329                         null, null, $dec);
330             amount_cells(null, 'ChgPrice'.$n, price_format($myrow["unit_price"]));
331             if ($supp_trans->is_invoice)
332                 amount_cell(round($myrow["unit_price"] * ($myrow["qty_recd"] - $myrow["quantity_inv"]), user_price_dec()));
333             else        
334                 amount_cell(round($myrow["unit_price"] * max($myrow['quantity_inv'], 0), user_price_dec()));
335             if ($supp_trans->is_invoice)        
336                         submit_cells('grn_item_id'.$n, _("Add"), '', _("Add to Invoice"), true);
337                 else    
338                         submit_cells('grn_item_id'.$n, _("Add"), '', _("Add to Credit Note"), true);
339                 if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)       // Added 2008-10-18 by Joe Hunt. Only admins can remove GRNs
340                         submit_cells('void_item_id'.$n, _("Remove"), '', _("WARNING! Be careful with removal. The operation is executed immediately and cannot be undone !!!"), true);
341                         end_row();
342         }
343     }
344     return true;
345 }
346
347 //------------------------------------------------------------------------------------
348
349 // $mode = 0 none at the moment
350 //               = 1 display on invoice/credit page
351 //               = 2 display on view invoice
352 //               = 3 display on view credit
353
354 function display_grn_items(&$supp_trans, $mode=0)
355 {
356         global $table_style, $path_to_root;
357
358     $ret = true;
359     // if displaying in form, and no items, exit
360     if (($mode == 2  || $mode == 3) && count($supp_trans->grn_items) == 0)
361         return 0;
362
363         start_outer_table("width=95%");
364
365         $heading2 = "";
366         if ($mode == 1)
367         {
368                 if ($supp_trans->is_invoice)
369                 {
370                         $heading = _("Items Received Yet to be Invoiced");
371                 if ($_SESSION["wa_current_user"]->access == 2)  // Added 2008-10-18 by Joe Hunt. Only admins can remove GRNs
372                                 $heading2 = _("WARNING! Be careful with removal. The operation is executed immediately and cannot be undone !!!");
373                 }
374                 else
375                         $heading = _("Delivery Item Selected For Adding To A Supplier Credit Note");
376         }
377         else
378         {
379                 if ($supp_trans->is_invoice)
380                         $heading = _("Received Items Charged on this Invoice");
381                 else
382                         $heading = _("Received Items Credited on this Note");
383         }               
384         echo "<table width=100% >";
385         echo "<tr><td align=center><span class='headingtext'>$heading</span></td>";
386         if ($mode == 1)
387         {
388                 submit_cells('InvGRNAll', _("Add All Items"), "align=right",false,true);
389                 if ($heading2 != "")
390                 {
391                         echo "</tr><td>";
392                         display_note($heading2, 0, 0, "class='overduefg'");
393                         echo "</td>\n";
394                 }
395         }       
396         echo "</tr></table>";
397
398         vertical_space(); // outer table
399
400         div_start('grn_items');
401         start_table("$table_style width=100%");
402         if ($mode == 1)
403         {
404         $th = array(_("Delivery"), _("Sequence #"), _("P.O."), _("Item"), _("Description"),
405                 _("Received On"), _("Quantity Received"), _("Quantity Invoiced"),
406                 _("Qty Yet To Invoice"), _("Order Price"), _("Total"), "");
407         if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)       // Added 2008-10-18 by Joe Hunt. Only admins can remove GRNs
408                 $th[] = "";
409                 if (!$supp_trans->is_invoice)
410                 {
411                         $th[8] = _("Qty Yet To Credit");
412                 }
413     }
414     else
415                 $th = array(_("Delivery"), _("Item"), _("Description"),
416                         _("Quantity"), _("Price"), _("Line Value"));
417
418         table_header($th);
419     $total_grn_value = 0;
420     $i = $k = 0;
421
422         if (count($supp_trans->grn_items) > 0)
423         {
424
425         foreach ($supp_trans->grn_items as $entered_grn)
426         {
427
428                 alt_table_row_color($k);
429
430                         $grn_batch = get_grn_batch_from_item($entered_grn->id);
431                 label_cell(get_trans_view_str(25,$grn_batch));
432                 if ($mode == 1)
433                 {
434                                 label_cell($entered_grn->id);
435                                 label_cell(""); // PO
436                         }       
437                         label_cell($entered_grn->item_code);
438                         label_cell($entered_grn->item_description);
439             $dec = get_qty_dec($entered_grn->item_code);
440             if ($mode == 1)
441             {
442                 label_cell("");
443                                 qty_cell($entered_grn->qty_recd, false, $dec);
444                                 qty_cell($entered_grn->prev_quantity_inv, false, $dec);
445             }
446                         qty_cell(abs($entered_grn->this_quantity_inv), true, $dec);
447                         amount_cell($entered_grn->chg_price);
448                         amount_cell( round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv), user_price_dec()), true);
449
450                         if ($mode == 1)
451                         {
452                                 delete_button_cell("Delete" . $entered_grn->id, _("Edit"), _('Edit document line'));
453                                 if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)         
454                                         label_cell("");
455                         }       
456                         end_row();
457
458                 $total_grn_value += round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv),
459                            user_price_dec());
460
461                 $i++;
462                 if ($i > 15)
463                 {
464                         $i = 0;
465                         table_header($th);
466                 }
467         }
468     }
469         if ($mode == 1)
470         {
471                 $ret = display_grn_items_for_selection($supp_trans, $k);
472         $colspan = 10;
473         }
474         else
475                 $colspan = 5;
476         label_row(_("Total"), price_format($total_grn_value),
477                 "colspan=$colspan align=right", "nowrap align=right");
478     end_table();
479         if (!$ret)
480         {
481                 if ($supp_trans->is_invoice)
482                         display_note(_("There are no outstanding items received from this supplier that have not been invoiced by them."), 0, 1);
483                 else
484                 {
485                         display_note(_("There are no received items for the selected supplier that have been invoiced."));
486                         display_note(_("Credits can only be applied to invoiced items."), 0, 1);
487                 }
488         }       
489     div_end();
490
491     end_outer_table(0, false); // outertable
492         return $total_grn_value;
493 }
494
495 //--------------------------------------------------------------------------------------------------
496 function get_duedate_from_terms(&$supp_trans)
497 {
498         if (!is_date($supp_trans->tran_date))
499         {
500                 $supp_trans->tran_date = Today();
501         }
502         if (substr( $supp_trans->terms, 0, 1) == "1")
503         { /*Its a day in the following month when due */
504                 $supp_trans->due_date = add_days(end_month($supp_trans->tran_date), (int) substr( $supp_trans->terms,1));
505         }
506         else
507         { /*Use the Days Before Due to add to the invoice date */
508                 $supp_trans->due_date = add_days($supp_trans->tran_date, (int) substr( $supp_trans->terms,1));
509         }
510 }
511
512 //--------------------------------------------------------------------------------------------------
513
514 ?>