0

Once i click the submit invoice button, a invoice will be created and i saw there are something has been changed in database, is it possible to revert a order into "not invoiced", so i can recreate the invoice again?

And i tested even if i delete a invoice in database, but i still can't to create a invoice for that order

--update--

ini_set('display_errors', 1);

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);

umask(0);
Mage::app('default');
//Mage::register('isSecureArea', 1);
//$invoiceId = $_GET['invoiceid'];          //put your invoice id - the autoincrement one not the long one.
//Mage::getModel('sales/order_invoice')->load($invoiceId)->delete();

  $orderId = 100000049; // this is an example 
  $order = Mage::getModel('sales/order')->load($orderId);
  $invoices = $order->getInvoiceCollection();
  var_dump($invoices);
  foreach ($invoices as $invoice){
        $invoice->delete();
  }
  • When loading by the increment id 100000049 then you can use the function loadByIncrementId on the order object. – David Manners May 17 '14 at 7:03
1

This is simple. This is code snippet as per your requirement.

<?php 
  $orderId = 101; // this is an example 
  $order = Mage::getModel('sales/order')->load($orderId);
  $invoices = $order->getInvoiceCollection();
  foreach ($invoices as $invoice){
     $invoice->delete();
  }
?>
| improve this answer | |
  • this is not work for me. – hkguile May 8 '14 at 3:16
  • at which place you put your code? – Keyul Shah May 8 '14 at 3:22
  • magento root directory – hkguile May 8 '14 at 3:23
  • i've post the script – hkguile May 8 '14 at 3:24
  • I can't see the script – Keyul Shah May 8 '14 at 4:06
1

In your case you are just deleting the invoice of the order but not the actual state of that order which is currently invoiced. To do that apart from deleting the invoice, you have to revert its state also. Try this:

$orderId = YOUR_ORDER_ID;
$order = Mage::getModel('sales/order')
                ->load($orderId);
$order->setState(Mage_Sales_Model_Order::STATE_NEW, true)->save();

This will revert the order to new state, after that you can create new invoice for it.

| improve this answer | |
0

I also have encounter with the same issue, and unfortunately answers above did not work for me. So I ended up comparing all fields in sales_flat_order and sales_flat_order_item tables before and after invoicing. Now I use the following SQL snippet to "revert" invoice for order:

SET @order_id = YOUR_ORDER_ID_HERE; -- <<< Put your order ID here

DELETE FROM sales_flat_invoice where order_id = @order_id;

UPDATE sales_flat_order
SET
    state = 'new',
    status = 'pending',
    base_discount_invoiced = NULL,
    base_shipping_invoiced = NULL,
    base_subtotal_invoiced = NULL,
    base_tax_invoiced = NULL,
    base_total_invoiced = NULL,
    base_total_invoiced_cost = NULL,
    discount_invoiced = NULL,
    shipping_invoiced = NULL,
    subtotal_invoiced = NULL,
    tax_invoiced = NULL,
    total_invoiced = NULL,
    forced_shipment_with_invoice = NULL,
    hidden_tax_invoiced = NULL,
    base_hidden_tax_invoiced = NULL,
    surcharge_invoiced = NULL,
    base_surcharge_invoiced = NULL,
    base_linked_amount_invoiced = NULL,
    linked_amount_invoiced = NULL,
    base_total_paid = NULL,
    total_paid = NULL,
    customer_note_notify = 1,
    base_total_due = NULL,
    total_due = NULL
WHERE entity_id = @order_id;

UPDATE sales_flat_order_item
SET
    qty_invoiced = NULL,
    tax_invoiced = NULL,
    base_tax_invoiced = NULL,
    discount_invoiced = NULL,
    row_invoiced = 0,
    base_row_invoiced = 0,
    locked_do_invoice = NULL,
    hidden_tax_invoiced = NULL,
    base_hidden_tax_invoiced = NULL
WHERE order_id = @order_id;

If you really need Magento PHP code, you can "wrap" the commands above the following way:

// assume you've already removed records from sales_flat_invoice and now need to "clean up" the order
// also assume you keep order ID in $orderId
$writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');
$orderTableName = Mage::getSingleton('core/resource')->getTableName('sales/order');
$orderItemTableName = Mage::getSingleton('core/resource')->getTableName('sales/order_item');

$query = "UPDATE $orderTableName " . <<<SQL_CODE
SET
    state = 'new',
    status = 'pending',
    base_discount_invoiced = NULL,
    base_shipping_invoiced = NULL,
    base_subtotal_invoiced = NULL,
    base_tax_invoiced = NULL,
    base_total_invoiced = NULL,
    base_total_invoiced_cost = NULL,
    discount_invoiced = NULL,
    shipping_invoiced = NULL,
    subtotal_invoiced = NULL,
    tax_invoiced = NULL,
    total_invoiced = NULL,
    forced_shipment_with_invoice = NULL,
    hidden_tax_invoiced = NULL,
    base_hidden_tax_invoiced = NULL,
    surcharge_invoiced = NULL,
    base_surcharge_invoiced = NULL,
    base_linked_amount_invoiced = NULL,
    linked_amount_invoiced = NULL,
    base_total_paid = NULL,
    total_paid = NULL,
    customer_note_notify = 1,
    base_total_due = NULL,
    total_due = NULL
WHERE entity_id =
SQL_CODE;
$query .= $orderId;
$writeConnection->query($query);

$query = "UPDATE $orderItemTableName " . <<<SQL_CODE
SET
    qty_invoiced = NULL,
    tax_invoiced = NULL,
    base_tax_invoiced = NULL,
    discount_invoiced = NULL,
    row_invoiced = 0,
    base_row_invoiced = 0,
    locked_do_invoice = NULL,
    hidden_tax_invoiced = NULL,
    base_hidden_tax_invoiced = NULL
WHERE order_id =
SQL_CODE;
$query .= $orderId;
$writeConnection->query($query);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.