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