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 += round2(($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, 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 start_outer_table("width=100%");
201 $qes = has_quick_entries(-1, 0);
204 echo "<div style='float:right;'>";
205 echo _("Quick Entry:")." ";
206 quick_entries_list('qid', null, 0, 1);
207 echo " "._("Amount")." ";
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'> ";
211 submit('go', _("Go"), true, false, true);
215 display_heading($heading);
217 end_outer_table(0, false);
219 vertical_space(); // ouer table
221 div_start('gl_items');
222 start_table("$table_style width=100%");
224 $dim = get_company_pref('use_dimension');
226 $th = array(_("Account"), _("Name"), _("Dimension")." 1", _("Dimension")." 2", _("Amount"), _("Memo"));
228 $th = array(_("Account"), _("Name"), _("Dimension"), _("Amount"), _("Memo"));
230 $th = array(_("Account"), _("Name"), _("Amount"), _("Memo"));
241 if (count($supp_trans->gl_codes) > 0)
244 foreach ($supp_trans->gl_codes as $entered_gl_code)
247 alt_table_row_color($k);
250 $entered_gl_code->amount = -$entered_gl_code->amount;
252 label_cell($entered_gl_code->gl_code);
253 label_cell($entered_gl_code->gl_act_name);
256 label_cell(get_dimension_string($entered_gl_code->gl_dim, true));
258 label_cell(get_dimension_string($entered_gl_code->gl_dim2, true));
260 amount_cell($entered_gl_code->amount, true);
261 label_cell($entered_gl_code->memo_);
265 delete_button_cell("Delete2" . $entered_gl_code->Counter, _("Delete"),
266 _('Remove line from document'));
271 $total_gl_value += $entered_gl_code->amount;
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");
291 end_outer_table(0, false); // outertable
292 return $total_gl_value;
295 //--------------//-----------------------------------------------------------------------------------------
297 function display_grn_items_for_selection(&$supp_trans, $k)
301 if ($supp_trans->is_invoice)
302 $result = get_grn_items(0, $supp_trans->supplier_id, true);
304 $result = get_grn_items(0, $supp_trans->supplier_id, false, true);
306 if (db_num_rows($result) == 0)
311 /*Set up a table to show the outstanding GRN items for selection */
313 while ($myrow = db_fetch($result))
315 $grn_already_on_invoice = false;
317 foreach ($supp_trans->grn_items as $entered_grn)
319 if ($entered_grn->id == $myrow["id"])
321 $grn_already_on_invoice = true;
324 if ($grn_already_on_invoice == false)
327 alt_table_row_color($k);
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),
350 qty_cells(null, 'This_QuantityCredited'.$n, number_format2(max($myrow["quantity_inv"], 0), $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()));
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);
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);
369 //------------------------------------------------------------------------------------
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
376 function display_grn_items(&$supp_trans, $mode=0)
378 global $table_style, $path_to_root;
381 // if displaying in form, and no items, exit
382 if (($mode == 2 || $mode == 3) && count($supp_trans->grn_items) == 0)
385 start_outer_table("width=95%");
390 if ($supp_trans->is_invoice)
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 !!!");
397 $heading = _("Delivery Item Selected For Adding To A Supplier Credit Note");
401 if ($supp_trans->is_invoice)
402 $heading = _("Received Items Charged on this Invoice");
404 $heading = _("Received Items Credited on this Note");
407 start_outer_table("width=100%");
409 display_heading($heading);
416 display_note($heading2, 0, 0, "class='overduefg'");
418 echo "</td><td width=10% align='right'>";
419 submit('InvGRNAll', _("Add All Items"), true, false,true);
422 end_outer_table(0, false);
424 div_start('grn_items');
425 start_table("$table_style width=100%");
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
433 if (!$supp_trans->is_invoice)
435 $th[8] = _("Qty Yet To Credit");
439 $th = array(_("Delivery"), _("Item"), _("Description"),
440 _("Quantity"), _("Price"), _("Line Value"));
443 $total_grn_value = 0;
446 if (count($supp_trans->grn_items) > 0)
449 foreach ($supp_trans->grn_items as $entered_grn)
452 alt_table_row_color($k);
454 $grn_batch = get_grn_batch_from_item($entered_grn->id);
455 label_cell(get_trans_view_str(25,$grn_batch));
458 label_cell($entered_grn->id);
459 label_cell(""); // PO
461 label_cell($entered_grn->item_code);
462 label_cell($entered_grn->item_description);
463 $dec = get_qty_dec($entered_grn->item_code);
467 qty_cell($entered_grn->qty_recd, false, $dec);
468 qty_cell($entered_grn->prev_quantity_inv, false, $dec);
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);
476 delete_button_cell("Delete" . $entered_grn->id, _("Edit"), _('Edit document line'));
477 if ($supp_trans->is_invoice && $_SESSION["wa_current_user"]->access == 2)
482 $total_grn_value += round2($entered_grn->chg_price * abs($entered_grn->this_quantity_inv),
495 $ret = display_grn_items_for_selection($supp_trans, $k);
500 label_row(_("Total"), price_format($total_grn_value),
501 "colspan=$colspan align=right", "nowrap align=right");
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);
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);
515 end_outer_table(0, false); // outertable
516 return $total_grn_value;
519 //--------------------------------------------------------------------------------------------------
520 function get_duedate_from_terms(&$supp_trans)
522 if (!is_date($supp_trans->tran_date))
524 $supp_trans->tran_date = Today();
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));
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));
536 //--------------------------------------------------------------------------------------------------