I have used the code
if ($order->getInvoiceCollection()->count()) {
$_invoices = $order->getInvoiceCollection();
if ($_invoices) {
foreach ($_invoices as $invoice) {
$invoice->delete();
}
}
}
This is deleting invoice, but invoice button was not reappearing although invoice is deleted.
So, I have go through the database table and turns out order_items
has item_invoiced
column, I have used this code to set item invoiced value to 0
$items = $order->getAllItems();
foreach ($items as $k => $item) {
echo $item->getId() . "\n";
$item->setQtyInvoiced(0);
$item->save();
}
After this code invoice button is appearing, but when I am creating invoice it is setting subtotal to negative like -1000