Topic: Edit Supplier Invoice
I know supplier invoices shouldn't be edited, as this can easily make a mess in inventory (and COGS calculations). But, what about the case when it is the last transaction, so nothing would actually change if we make some correction (for example, we've found a typo when we've printed the invoice after it has been posted; or, we want just to edit a memo, or a product description, which wouldn't actually change anything...)?
The option to void such a transaction and type all over again I don't find very convenient. So, I've adapted the "supplier invioce form" so I can "open" some previously entered invoice, make corrections and repost it. At this stage, I actually void the previous invoice (with the date of that transaction), and post a new version of the invoice.
I know it is not the exact solution for the case above, but it is something I was able to do (seems to work for me, as much as I was able to test up to now...). To be able to achieve to actually edit the existing invoice and repost it (with the old ID), if it can be done safely (no transactions after it yet, or some insignificant changes that don't affect stock), I should have a more in-depth knowledge about FA (and, MySQL and PHP) - which unfortunately I don't. So, any help in this direction is more than welcome (what to check to be sure that it is the last transaction, and what would be the most convenient way to make modifications).
In any case, below is my code (modifications). Any feedback is more than welcome, in particular if I've done something I shouldn't (so I can introduce some inconsistency in the database using this).
=============================================================================================
--- gl/inquiry/journal_inquiry.php.ORIGINAL 2010-12-10 14:12:42.000000000 +0100
+++ gl/inquiry/journal_inquiry.php 2010-12-27 13:50:52.000000000 +0100
@@ -94,7 +94,7 @@ $editors = array(
ST_CUSTDELIVERY => "/sales/customer_delivery.php?ModifyDelivery=%d",
// 16=> Location Transfer,
// 17=> Inventory Adjustment,
-// 20=> Supplier Invoice,
+ ST_SUPPINVOICE => "/purchasing/supplier_invoice.php?ModifySI=Yes&trans_no=%d",
// 21=> Supplier Credit Note,
// 22=> Supplier Payment,
// 25=> Purchase Order Delivery,
--- purchasing/supplier_invoice.php.ORIGINAL 2010-12-10 14:12:44.000000000 +0100
+++ purchasing/supplier_invoice.php 2010-12-27 13:43:49.000000000 +0100
@@ -25,7 +25,64 @@ if ($use_popup_windows)
$js .= get_js_open_window(900, 500);
if ($use_date_picker)
$js .= get_js_date_picker();
-page(_($help_context = "Enter Supplier Invoice"), false, false, "", $js);
+
+if (isset($_GET['ModifySI'])) {
+ $help_context = "Modifying Supplier Invoice";
+ $error_msg = '';
+
+ if (isset($_GET["trans_no"])) {
+ $trans_no = $_GET["trans_no"];
+ }
+ elseif (isset($_POST["trans_no"])) {
+ $trans_no = $_POST["trans_no"];
+ }
+ else $trans_no = null;
+
+ if ($trans_no != null) {
+ $_SESSION['page_title'] = sprintf(_("Modifying Supplier Invoice # %d."), $trans_no);
+ if (is_closed_trans(ST_SUPPINVOICE, $trans_no)) {
+ $error_msg = _("The selected transaction was closed for edition.");
+ }
+ else {
+ $void_entry = get_voided_entry(ST_SUPPINVOICE, $trans_no);
+ if ($void_entry != null) {
+ $error_msg = _("The selected transaction has already been voided.");
+ }
+ else {
+ $result = get_gl_trans(ST_SUPPINVOICE, $trans_no);
+ if (db_num_rows($result) == 0) {
+ $error_msg = _("Cannot find Supplier Invoice # ") . $trans_no;
+ }
+ }
+ }
+ }
+ else {
+ $_SESSION['page_title'] = _("Modifying Supplier Invoice (with missing #).");
+ $error_msg = _("Supplier Invoice # is missing!");
+ }
+
+ if ($error_msg == '') {
+ $supp_trans = new supp_trans(ST_SUPPINVOICE);
+ read_supp_invoice($trans_no, ST_SUPPINVOICE, $supp_trans);
+ page($_SESSION['page_title'], false, false,'', $js);
+ $_SESSION['supp_trans'] = $supp_trans;
+ $_SESSION['inv_modify'] = true;
+ $_SESSION['old_trans'] = $trans_no;
+ $_SESSION['old_date'] = $supp_trans->tran_date;
+ }
+ else {
+ $_SESSION['inv_modify'] = false;
+ $_SESSION['old_trans'] = null;
+ $_SESSION['old_date'] = null;
+ page($_SESSION['page_title'], false, false,'', $js);
+ display_error($error_msg);
+ end_page(true);
+ exit;
+ }
+}
+else {
+ page(_($help_context = "Enter Supplier Invoice"), false, false, "", $js);
+}
//----------------------------------------------------------------------------------------
@@ -193,6 +250,27 @@ function check_data()
//--------------------------------------------------------------------------------------------------
+function handle_void_invoice()
+{
+
+ if (!exists_supp_trans(ST_SUPPINVOICE, $_SESSION['old_trans']))
+ return false;
+ if (!post_void_supp_trans(ST_SUPPINVOICE, $_SESSION['old_trans']))
+ return false;
+
+ $ret = void_transaction(ST_SUPPINVOICE, $_SESSION['old_trans'], $_SESSION['old_date'], 'Invoice Modification - Old (Voided) Version');
+
+ if ($ret)
+ {
+ display_notification_centered(_("Old invoice has been voided."));
+ }
+ else {
+ display_error(_("Old invoice cannot be voided."));
+ set_focus('trans_no');
+ }
+ return $ret;
+}
+
function handle_commit_invoice()
{
copy_to_trans($_SESSION['supp_trans']);
@@ -212,7 +290,15 @@ function handle_commit_invoice()
if (isset($_POST['PostInvoice']))
{
- handle_commit_invoice();
+ if ($_SESSION['inv_modify']) {
+ if (handle_void_invoice($_SESSION['old_trans'], $_SESSION['old_date'])) {
+ display_notification_centered(_("New invoice data:<br>") . print_r($_SESSION['supp_trans'], TRUE) . "<br>");
+ handle_commit_invoice();
+ }
+ }
+ else {
+ handle_commit_invoice();
+ }
}
function check_item_data($n)
=============================================================================================