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 //--------------------------------------------------------------------------------------------------
14 function copy_from_trans(&$supp_trans)
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;
24 //--------------------------------------------------------------------------------------------------
26 function copy_to_trans(&$supp_trans)
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'];
34 $supp_trans->ov_amount = 0; /* for starters */
35 if (count($supp_trans->grn_items) > 0)
37 foreach ( $supp_trans->grn_items as $grn)
39 $supp_trans->ov_amount += round(($grn->this_quantity_inv * $grn->chg_price),
43 if (count($supp_trans->gl_codes) > 0)
45 foreach ( $supp_trans->gl_codes as $gl_line)
47 $supp_trans->ov_amount += $gl_line->amount;
52 //--------------------------------------------------------------------------------------------------
54 function invoice_header(&$supp_trans)
56 global $Ajax, $table_style;
58 // if vars have been lost, recopy
59 if (!isset($_POST['tran_date']))
60 copy_from_trans($supp_trans);
62 start_outer_table("$table_style width=98%", 5);
66 if (!isset($_POST['supplier_id']) && (get_global_supplier() != reserved_words::get_all()))
67 $_POST['supplier_id'] = get_global_supplier();
69 supplier_list_row(_("Supplier:"), 'supplier_id', $_POST['supplier_id'], false, true);
71 if ($supp_trans->supplier_id != $_POST['supplier_id'])
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);
81 if ($supp_trans->is_invoice)
82 ref_row(_("Reference:"), 'reference', '', references::get_next(20));
84 ref_row(_("Reference:"), 'reference', '', references::get_next(21));
86 text_row(_("Supplier's Ref.:"), 'supp_reference', $_POST['supp_reference'], 20, 20);
88 table_section(2, "33%");
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');
99 date_row(_("Due Date") . ":", 'due_date');
101 label_row(_("Terms:"), $supp_trans->terms_description);
103 table_section(3, "33%");
105 $supplier_currency = get_supplier_currency($supp_trans->supplier_id);
107 $company_currency = get_company_currency();
109 if ($supplier_currency != $company_currency)
111 label_row(_("Supplier's Currency:"), "<b>" . $supplier_currency . "</b>");
112 exchange_rate_display($supplier_currency, $company_currency, $_POST['tran_date']);
115 label_row(_("Tax Group:"), $supp_trans->tax_description);
120 //--------------------------------------------------------------------------------------------------
122 function invoice_totals(&$supp_trans)
124 global $table_style, $table_style2;
126 copy_to_trans($supp_trans);
128 start_table("$table_style width=95%");
129 label_row(_("Sub-total:"), price_format( $supp_trans->ov_amount), "align=right", "align=right");
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)
134 $display_total = price_format($supp_trans->ov_amount + $tax_total);
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;'");
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;'");
143 start_table($table_style2);
144 textarea_row(_("Memo:"), "Comments", null, 50, 3);
148 //--------------------------------------------------------------------------------------------------
149 function display_gl_controls(&$supp_trans, $k)
153 $accs = get_supplier_accounts($supp_trans->supplier_id);
154 $_POST['gl_code'] = $accs['purchase_account'];
156 alt_table_row_color($k);
157 gl_all_accounts_list('gl_code', null, true, false, true);
158 $dim = get_company_pref('use_dimension');
160 dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
162 dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
163 amount_cells(null, 'amount');
165 text_cells_ex(null, 'memo_', 35, 50, null, null, null, hidden('dimension_id', 0, false).hidden('dimension2_id', 0, false));
167 text_cells_ex(null, 'memo_', 35, 50, null, null, null, hidden('dimension2_id', 0, false));
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);
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
182 function display_gl_items(&$supp_trans, $mode=0)
184 global $table_style, $path_to_root;
186 // if displaying in form, and no items, exit
187 if (($mode == 2 || $mode == 3) && count($supp_trans->gl_codes) == 0)
190 start_outer_table("width=95%");
192 if ($supp_trans->is_invoice)
193 $heading = _("GL Items for this Invoice");
195 $heading = _("GL Items for this Credit Note");
197 display_heading($heading);
199 vertical_space(); // ouer table
201 div_start('gl_items');
202 start_table("$table_style width=100%");
204 $dim = get_company_pref('use_dimension');
206 $th = array(_("Account"), _("Name"), _("Dimension")." 1", _("Dimension")." 2", _("Amount"), _("Memo"));
208 $th = array(_("Account"), _("Name"), _("Dimension"), _("Amount"), _("Memo"));
210 $th = array(_("Account"), _("Name"), _("Amount"), _("Memo"));
221 if (count($supp_trans->gl_codes) > 0)
224 foreach ($supp_trans->gl_codes as $entered_gl_code)
227 alt_table_row_color($k);
230 $entered_gl_code->amount = -$entered_gl_code->amount;
232 label_cell($entered_gl_code->gl_code);
233 label_cell($entered_gl_code->gl_act_name);
236 label_cell(get_dimension_string($entered_gl_code->gl_dim, true));
238 label_cell(get_dimension_string($entered_gl_code->gl_dim2, true));
240 amount_cell($entered_gl_code->amount, true);
241 label_cell($entered_gl_code->memo_);
245 delete_button_cell("Delete2" . $entered_gl_code->Counter, _("Delete"),
246 _('Remove line from document'));
251 $total_gl_value += $entered_gl_code->amount;
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");
271 end_outer_table(0, false); // outertable
272 return $total_gl_value;
275 //--------------//-----------------------------------------------------------------------------------------
277 function display_grn_items_for_selection(&$supp_trans, $k)
281 if ($supp_trans->is_invoice)
282 $result = get_grn_items(0, $supp_trans->supplier_id, true);
284 $result = get_grn_items(0, $supp_trans->supplier_id, false, true);
286 if (db_num_rows($result) == 0)
291 /*Set up a table to show the outstanding GRN items for selection */
293 while ($myrow = db_fetch($result))
295 $grn_already_on_invoice = false;
297 foreach ($supp_trans->grn_items as $entered_grn)
299 if ($entered_grn->id == $myrow["id"])
301 $grn_already_on_invoice = true;
304 if ($grn_already_on_invoice == false)
307 alt_table_row_color($k);
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),
330 qty_cells(null, 'This_QuantityCredited'.$n, number_format2(max($myrow["quantity_inv"], 0), $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()));
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);
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);
349 //------------------------------------------------------------------------------------
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
356 function display_grn_items(&$supp_trans, $mode=0)
358 global $table_style, $path_to_root;
361 // if displaying in form, and no items, exit
362 if (($mode == 2 || $mode == 3) && count($supp_trans->grn_items) == 0)
365 start_outer_table("width=95%");
370 if ($supp_trans->is_invoice)
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 !!!");
377 $heading = _("Delivery Item Selected For Adding To A Supplier Credit Note");
381 if ($supp_trans->is_invoice)
382 $heading = _("Received Items Charged on this Invoice");
384 $heading = _("Received Items Credited on this Note");
387 start_outer_table("width=100%");
389 display_heading($heading);
396 display_note($heading2, 0, 0, "class='overduefg'");
398 echo "</td><td align='right'>";
399 submit('InvGRNAll', _("Add All Items"), true, false,true);
402 end_outer_table(0, false);
404 div_start('grn_items');
405 start_table("$table_style width=100%");
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
413 if (!$supp_trans->is_invoice)
415 $th[8] = _("Qty Yet To Credit");
419 $th = array(_("Delivery"), _("Item"), _("Description"),
420 _("Quantity"), _("Price"), _("Line Value"));
423 $total_grn_value = 0;
426 if (count($supp_trans->grn_items) > 0)
429 foreach ($supp_trans->grn_items as $entered_grn)
432 alt_table_row_color($k);
434 $grn_batch = get_grn_batch_from_item($entered_grn->id);
435 label_cell(get_trans_view_str(25,$grn_batch));
438 label_cell($entered_grn->id);
439 label_cell(""); // PO
441 label_cell($entered_grn->item_code);
442 label_cell($entered_grn->item_description);
443 $dec = get_qty_dec($entered_grn->item_code);
447 qty_cell($entered_grn->qty_recd, false, $dec);
448 qty_cell($entered_grn->prev_quantity_inv, false, $dec);
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);
456 delete_button_cell("Delete" . $entered_grn->id, _("Edit"), _('Edit document line'));
457 if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)
462 $total_grn_value += round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv),
475 $ret = display_grn_items_for_selection($supp_trans, $k);
480 label_row(_("Total"), price_format($total_grn_value),
481 "colspan=$colspan align=right", "nowrap align=right");
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);
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);
495 end_outer_table(0, false); // outertable
496 return $total_grn_value;
499 //--------------------------------------------------------------------------------------------------
500 function get_duedate_from_terms(&$supp_trans)
502 if (!is_date($supp_trans->tran_date))
504 $supp_trans->tran_date = Today();
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));
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));
516 //--------------------------------------------------------------------------------------------------