*** empty log message ***
[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         display_heading($heading);
198
199         vertical_space(); // ouer table
200
201         div_start('gl_items');
202         start_table("$table_style width=100%");
203
204         $dim = get_company_pref('use_dimension');
205         if ($dim == 2)
206         $th = array(_("Account"), _("Name"), _("Dimension")." 1", _("Dimension")." 2", _("Amount"), _("Memo"));
207         else if ($dim == 1)
208         $th = array(_("Account"), _("Name"), _("Dimension"), _("Amount"), _("Memo"));
209     else
210         $th = array(_("Account"), _("Name"), _("Amount"), _("Memo"));
211
212         if ($mode == 1)
213         {
214                 $th[] = "";
215                 $th[] = "";
216         }       
217         table_header($th);
218         $total_gl_value=0;
219         $i = $k = 0;
220
221         if (count($supp_trans->gl_codes) > 0)
222         {
223
224                 foreach ($supp_trans->gl_codes as $entered_gl_code)
225                 {
226
227                         alt_table_row_color($k);
228
229                         if ($mode == 3)
230                                 $entered_gl_code->amount = -$entered_gl_code->amount;
231
232                         label_cell($entered_gl_code->gl_code);
233                         label_cell($entered_gl_code->gl_act_name);
234
235                         if ($dim >= 1)
236                                 label_cell(get_dimension_string($entered_gl_code->gl_dim, true));
237                         if ($dim > 1)
238                                 label_cell(get_dimension_string($entered_gl_code->gl_dim2, true));
239
240                         amount_cell($entered_gl_code->amount, true);
241                         label_cell($entered_gl_code->memo_);
242
243                         if ($mode == 1)
244                         {
245                                 delete_button_cell("Delete2" . $entered_gl_code->Counter, _("Delete"),
246                                           _('Remove line from document'));
247                                 label_cell("");
248                         }       
249                         end_row();
250
251                         $total_gl_value += $entered_gl_code->amount;
252
253                         $i++;
254                         if ($i > 15)
255                         {
256                                 $i = 0;
257                                 table_header($th);
258                         }
259                 }
260
261         }
262         if ($mode == 1)
263                 display_gl_controls($supp_trans, $k);
264         $colspan = ($dim == 2 ? 4 : ($dim == 1 ? 3 : 2));
265         label_row(_("Total"), price_format($total_gl_value),
266                 "colspan=".$colspan." align=right", "nowrap align=right");
267
268         end_table();
269         div_end();
270
271     end_outer_table(0, false); // outertable
272         return $total_gl_value;
273 }
274
275 //--------------//-----------------------------------------------------------------------------------------
276
277 function display_grn_items_for_selection(&$supp_trans, $k)
278 {
279         global $table_style;
280
281         if ($supp_trans->is_invoice)
282                 $result = get_grn_items(0, $supp_trans->supplier_id, true);
283         else    
284                 $result = get_grn_items(0, $supp_trans->supplier_id, false, true);
285
286     if (db_num_rows($result) == 0)
287     {
288         return false;
289     }
290     
291     /*Set up a table to show the outstanding GRN items for selection */
292
293     while ($myrow = db_fetch($result))
294     {
295                 $grn_already_on_invoice = false;
296
297         foreach ($supp_trans->grn_items as $entered_grn)
298         {
299                 if ($entered_grn->id == $myrow["id"])
300                 {
301                         $grn_already_on_invoice = true;
302                 }
303         }
304         if ($grn_already_on_invoice == false)
305         {
306
307                         alt_table_row_color($k);
308
309                         $n = $myrow["id"];
310                 label_cell(get_trans_view_str(25, $myrow["grn_batch_id"]));
311                 label_cell($myrow["id"].
312                 hidden('qty_recd'.$n, $myrow["qty_recd"], false).
313                 hidden('item_code'.$n, $myrow["item_code"], false).
314                 hidden('item_description'.$n, $myrow["description"], false).
315                 hidden('prev_quantity_inv'.$n, $myrow['quantity_inv'], false).
316                 hidden('order_price'.$n, $myrow['unit_price'], false).
317                 hidden('std_cost_unit'.$n, $myrow['std_cost_unit'], false).
318                 hidden('po_detail_item'.$n, $myrow['po_detail_item'], false));
319                 label_cell(get_trans_view_str(systypes::po(), $myrow["purch_order_no"]));
320             label_cell($myrow["item_code"]);
321             label_cell($myrow["description"]);
322             label_cell(sql2date($myrow["delivery_date"]));
323             $dec = get_qty_dec($myrow["item_code"]);
324             qty_cell($myrow["qty_recd"], false, $dec);
325             qty_cell($myrow["quantity_inv"], false, $dec);
326             if ($supp_trans->is_invoice)
327                 qty_cells(null, 'this_quantity_inv'.$n, number_format2($myrow["qty_recd"] - $myrow["quantity_inv"], $dec), 
328                         null, null, $dec);
329             else                
330                 qty_cells(null, 'This_QuantityCredited'.$n, number_format2(max($myrow["quantity_inv"], 0), $dec), 
331                         null, null, $dec);
332             amount_cells(null, 'ChgPrice'.$n, price_format($myrow["unit_price"]));
333             if ($supp_trans->is_invoice)
334                 amount_cell(round($myrow["unit_price"] * ($myrow["qty_recd"] - $myrow["quantity_inv"]), user_price_dec()));
335             else        
336                 amount_cell(round($myrow["unit_price"] * max($myrow['quantity_inv'], 0), user_price_dec()));
337             if ($supp_trans->is_invoice)        
338                         submit_cells('grn_item_id'.$n, _("Add"), '', _("Add to Invoice"), true);
339                 else    
340                         submit_cells('grn_item_id'.$n, _("Add"), '', _("Add to Credit Note"), true);
341                 if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)       // Added 2008-10-18 by Joe Hunt. Only admins can remove GRNs
342                         submit_cells('void_item_id'.$n, _("Remove"), '', _("WARNING! Be careful with removal. The operation is executed immediately and cannot be undone !!!"), true);
343                         end_row();
344         }
345     }
346     return true;
347 }
348
349 //------------------------------------------------------------------------------------
350
351 // $mode = 0 none at the moment
352 //               = 1 display on invoice/credit page
353 //               = 2 display on view invoice
354 //               = 3 display on view credit
355
356 function display_grn_items(&$supp_trans, $mode=0)
357 {
358         global $table_style, $path_to_root;
359
360     $ret = true;
361     // if displaying in form, and no items, exit
362     if (($mode == 2  || $mode == 3) && count($supp_trans->grn_items) == 0)
363         return 0;
364
365         start_outer_table("width=95%");
366
367         $heading2 = "";
368         if ($mode == 1)
369         {
370                 if ($supp_trans->is_invoice)
371                 {
372                         $heading = _("Items Received Yet to be Invoiced");
373                 if ($_SESSION["wa_current_user"]->access == 2)  // Added 2008-10-18 by Joe Hunt. Only admins can remove GRNs
374                                 $heading2 = _("WARNING! Be careful with removal. The operation is executed immediately and cannot be undone !!!");
375                 }
376                 else
377                         $heading = _("Delivery Item Selected For Adding To A Supplier Credit Note");
378         }
379         else
380         {
381                 if ($supp_trans->is_invoice)
382                         $heading = _("Received Items Charged on this Invoice");
383                 else
384                         $heading = _("Received Items Credited on this Note");
385         }               
386
387         start_outer_table("width=100%");
388
389         display_heading($heading);
390
391         if ($mode == 1)
392         {
393                 if ($heading2 != "")
394                 {
395                         //br();
396                         display_note($heading2, 0, 0, "class='overduefg'");
397                 }       
398                 echo "</td><td align='right'>";
399                 submit('InvGRNAll', _("Add All Items"), true, false,true);
400         }       
401
402         end_outer_table(0, false);
403
404         div_start('grn_items');
405         start_table("$table_style width=100%");
406         if ($mode == 1)
407         {
408         $th = array(_("Delivery"), _("Sequence #"), _("P.O."), _("Item"), _("Description"),
409                 _("Received On"), _("Quantity Received"), _("Quantity Invoiced"),
410                 _("Qty Yet To Invoice"), _("Order Price"), _("Total"), "");
411         if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)       // Added 2008-10-18 by Joe Hunt. Only admins can remove GRNs
412                 $th[] = "";
413                 if (!$supp_trans->is_invoice)
414                 {
415                         $th[8] = _("Qty Yet To Credit");
416                 }
417     }
418     else
419                 $th = array(_("Delivery"), _("Item"), _("Description"),
420                         _("Quantity"), _("Price"), _("Line Value"));
421
422         table_header($th);
423     $total_grn_value = 0;
424     $i = $k = 0;
425
426         if (count($supp_trans->grn_items) > 0)
427         {
428
429         foreach ($supp_trans->grn_items as $entered_grn)
430         {
431
432                 alt_table_row_color($k);
433
434                         $grn_batch = get_grn_batch_from_item($entered_grn->id);
435                 label_cell(get_trans_view_str(25,$grn_batch));
436                 if ($mode == 1)
437                 {
438                                 label_cell($entered_grn->id);
439                                 label_cell(""); // PO
440                         }       
441                         label_cell($entered_grn->item_code);
442                         label_cell($entered_grn->item_description);
443             $dec = get_qty_dec($entered_grn->item_code);
444             if ($mode == 1)
445             {
446                 label_cell("");
447                                 qty_cell($entered_grn->qty_recd, false, $dec);
448                                 qty_cell($entered_grn->prev_quantity_inv, false, $dec);
449             }
450                         qty_cell(abs($entered_grn->this_quantity_inv), true, $dec);
451                         amount_cell($entered_grn->chg_price);
452                         amount_cell( round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv), user_price_dec()), true);
453
454                         if ($mode == 1)
455                         {
456                                 delete_button_cell("Delete" . $entered_grn->id, _("Edit"), _('Edit document line'));
457                                 if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)         
458                                         label_cell("");
459                         }       
460                         end_row();
461
462                 $total_grn_value += round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv),
463                            user_price_dec());
464
465                 $i++;
466                 if ($i > 15)
467                 {
468                         $i = 0;
469                         table_header($th);
470                 }
471         }
472     }
473         if ($mode == 1)
474         {
475                 $ret = display_grn_items_for_selection($supp_trans, $k);
476         $colspan = 10;
477         }
478         else
479                 $colspan = 5;
480         label_row(_("Total"), price_format($total_grn_value),
481                 "colspan=$colspan align=right", "nowrap align=right");
482     end_table();
483         if (!$ret)
484         {
485                 if ($supp_trans->is_invoice)
486                         display_note(_("There are no outstanding items received from this supplier that have not been invoiced by them."), 0, 1);
487                 else
488                 {
489                         display_note(_("There are no received items for the selected supplier that have been invoiced."));
490                         display_note(_("Credits can only be applied to invoiced items."), 0, 1);
491                 }
492         }       
493     div_end();
494
495     end_outer_table(0, false); // outertable
496         return $total_grn_value;
497 }
498
499 //--------------------------------------------------------------------------------------------------
500 function get_duedate_from_terms(&$supp_trans)
501 {
502         if (!is_date($supp_trans->tran_date))
503         {
504                 $supp_trans->tran_date = Today();
505         }
506         if (substr( $supp_trans->terms, 0, 1) == "1")
507         { /*Its a day in the following month when due */
508                 $supp_trans->due_date = add_days(end_month($supp_trans->tran_date), (int) substr( $supp_trans->terms,1));
509         }
510         else
511         { /*Use the Days Before Due to add to the invoice date */
512                 $supp_trans->due_date = add_days($supp_trans->tran_date, (int) substr( $supp_trans->terms,1));
513         }
514 }
515
516 //--------------------------------------------------------------------------------------------------
517
518 ?>