0

I need to remove/undo a wrong credit memo. Can anyone confirm that the following code is correct/save to achieve this?

UPDATE `sales_flat_order` SET
`base_discount_refunded`=null,
`base_shipping_refunded`=null,
`base_shipping_tax_refunded`=null,
`base_subtotal_refunded`=null,
`base_tax_refunded`=null,
`base_total_offline_refunded`=null,
`base_total_online_refunded`=null,
`base_total_refunded`=null,
`discount_refunded`=null,
`shipping_refunded`=null,
`shipping_tax_refunded`=null,
`subtotal_refunded`=null,
`tax_refunded`=null,
`total_offline_refunded`=null,
`total_online_refunded`=null,
`total_refunded`=null,
`hidden_tax_refunded`=null,
`base_hidden_tax_refunded`=null
WHERE entity_id=71508

UPDATE `sales_flat_creditmemo` SET
`adjustment_positive`=0,
`base_shipping_tax_amount`=0,
`base_discount_amount`=0,
`grand_total`=0,
`base_adjustment_negative`=0,
`base_subtotal_incl_tax`=0,
`shipping_amount`=0,
`subtotal_incl_tax`=0,
`adjustment_negative`=0,
`base_shipping_amount`=0,
`base_adjustment`=0,
`base_subtotal`=0,
`discount_amount`=0,
`subtotal`=0,
`adjustment`=0,
`base_grand_total`=0,
`base_adjustment_positive`=0,
`base_tax_amount`=0,
`shipping_tax_amount`=0,
`tax_amount`=0,
`hidden_tax_amount`=0,
`base_hidden_tax_amount`=0,
`shipping_hidden_tax_amount`=0,
`base_shipping_hidden_tax_amnt`=0,
`shipping_incl_tax`=0,
`base_shipping_incl_tax`=0,
`state`=3
WHERE order_id=71508

UPDATE `sales_flat_creditmemo_grid` SET
`grand_total`=0,
`base_grand_total`=0,
`state`=3
WHERE order_id=71508

2 Answers 2

1
+50

Undo a wrong credit memo is not easy. Its defined within your order and the payments of that order.

Magento nests a lot of different things when it comes down to accounting.

Now when you reaalllyy want to do it the SQL command it accurate as far as i can see. I don't see anything you missed.

That was your question right? Just if something is wrong with your SQL command.

You can also do this by code if you want to know how.

Update :

Since i don't know how good you can code or what you need to know i will start with explaning the basics. Magento is devided in a Theme and Modules. Modules you can find in the app/code folder or the vendor/ folder.

The app/code folder is when you want to create your own module. If you require a module with composer (ssh command : composer require ....) it will appear in the vendor folder.

Also all the magento files (magento is build on modules only) you will find in vendor/magento.

Now that you know that we want to create a module. I will not explain that part since this has been explained 1000 times before : http://devdocs.magento.com/videos/fundamentals/create-a-new-module/

When you made that you want to add something that you can call that will perform an action. Lets say we want to make a page where we want to fill in a number and it deletes the credit memo for us.

We can make pages in modules with controllers. This is done within the app/code/Vendor/Namespace/Controller folder.

http://devdocs.magento.com/videos/fundamentals/create-a-new-page/

You can follow this link to see how its made exactly. Now within this controller you have an execute function. You can make an input field where you put the credit memo number in and use a form submit to trigger this page (<form action="url.extension/newpage">) and within this execute you can do a number of things.

I suggest you use a new fille in your controller and put that new file in your Model folder. Within that fille you can use the Magento\Sales\Model\Order\Creditmemo (lets give this variable $creditmemo) to adjust the credit memo settings. You will need to use Magento\Sales\Api\CreditmemoRepositoryInterface to getlist() with searchcriteria to find your Creditmemo and then use the ->save($creditmemo) function to save it or ->delete() to delete it.

The same logics is applied to Orders. I will show you how i do it with orders (and you will need to update this field to).

How to use the getlist() with search functions you can find in my answer here : Search by color

Now how i load , adjust and save an order with the repository interface : Notice how i use the interfaceFactory. This fille does not exist but is generated within your /generated or /var/generation folders and allows you to create a separate instance :

<?
namespace Vendor\Namespace\...

use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model;

public function __construct(
        OrderInterfaceFactory $orderinterface,
        OrderRepositoryInterface $orderrepository,
        Model\OrderFactory $order,
    )
    {
        $this->_magentoorder = $orderinterface;
        $this->_orderrepository = $orderrepository;
        $this->_order = $order;
    }

    public function UpdateOrders(){

$orderrep = $this->_orderrepository;
$order = $this->_order->create();
/**
* @var $order \Magento\Sales\Model\Order;
*
*/

$order
                                    ->setOrderCurrencyCode($ex_order['Currency'])
                                    ->setCreatedAt(preg_replace('/[^0-9.]+/', '', $ex_order['OrderDate']) / 1000)
                                    ->setStoreId(1)
                                    ->setBaseCurrencyCode($this->_storeManager->getStore(1)->getCurrentCurrencyCode())
                                    ->setStoreCurrencyCode($this->_storeManager->getStore(1)->getCurrentCurrencyCode())
                                    ->setIncrementId($ex_order['OrderNumber'])
                                    ->setCustomerId($customerid)
                                    ->setCustomerFirstname($customerfirstname)
                                    ->setCustomerLastname($customerlastname)
                                    ->setCustomerEmail($customeremail)
                                    ->setBillingAddress($billingaddress)
                                    ->setShippingAddress($deliveryaddress)
                                    ->setBaseDiscountAmount($ex_order['TotalExclVatDiscount'] * $orderrate)
                                    ->setDiscountAmount($ex_order['TotalExclVatDiscount'])
                                    ->setTaxAmount($ex_order['TotalInclVat'] - $ex_order['TotalExclVat'])
                                    ->setBaseSubtotal($ex_order['TotalExclVat'] * $orderrate)
                                    ->setSubtotal($ex_order['TotalExclVat'])
                                    ->setBaseGrandTotal($ex_order['TotalInclVat'] * $orderrate)
                                    ->setGrandTotal($ex_order['TotalInclVat'])
                                    ->setShippingInclTax(0)
                                    ->setShippingAmount(0)
                                    ->setBaseShippingAmount(0)
                                    ->setBaseShippingInclTax(0)
                                    ->setPayment($orderpayment);


                                    $orderrep->save($order);
}

Now again this is just an example how to work with those. You can do $orderep->getlist($searchcriteria); to get the order you want (same logics for credit memo's)

Now again i'm not sure how good your understanding of magento and php is so this all can be very confusing for you. I'm afraid explaining everything in detail will require me to write even more explenation about this. If you want you can always PM me so i can help you out or make it for you.

5
  • Thanks for your reply! If it can be done by code, I would really like to know how. Can you tell me how this can be achieved by code?
    – JGeer
    Commented Nov 3, 2017 at 7:29
  • Can i first know how familiar you are with magento coding? Just to know how 'detailed' it needs to be Commented Nov 3, 2017 at 12:34
  • Thanks, not very familiar. So if it is possible to give it a little detailed, that whould be great.
    – JGeer
    Commented Nov 3, 2017 at 16:18
  • I will update my answer this weekend. Kind of busy right now and its a lot of explenation Commented Nov 3, 2017 at 19:46
  • this is updated Commented Nov 6, 2017 at 3:04
4

One principle in accounting: don't change what you have done, add something to correct it.

The credit memo was created, to remove part of an earlier sale (and act as an accounting instruction / voucher). To undo a credit memo, issue antoher accounting instruction. In this case, that is to create a new order for what was on the credit memo. If no refund has been made, select to pay it by invoice and make a reference to the payment when you indicate the order as paid.

1
  • Thanks for your reply. I understand that, but we made a mistake, by creating a creditmemo for some products, that should not be. So I want to undo this for the specific order, instead of creating a new order etc. Because the main order was correct, only we created a credit by mistake.
    – JGeer
    Commented Oct 30, 2017 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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