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