0

I need to work with invoice, just after it's payed but already has increment_id.

Is there event after invoice save? Or which function generate increment_id, so i can create plugin on it.

Thanks.


PS: I tried those to observe those two events, but none of them gave me invoice with increment_id.

  • sales_order_invoice_register
  • sales_order_invoice_pay

2 Answers 2

0

Following class is responsible for generating increment_id

Magento/SalesSequence/Model/Sequence.php

/**
 * Retrieve next value
 *
 * @return string
 */
public function getNextValue()
{
    $this->connection->insert($this->meta->getSequenceTable(), []);
    $this->lastIncrementId = $this->connection->lastInsertId($this->meta->getSequenceTable());
    return $this->getCurrentValue();
}

So you can use plugin for any modification. Its global function for order, invoice, shipment, creditmemo.

You can pass registry param from

\Magento\SalesSequence\Model\Manager

As an example:

/**
 * Returns sequence for given entityType and store
 *
 * @param string $entityType
 * @param int $storeId
 * @return \Magento\Framework\DB\Sequence\SequenceInterface
 */
public function aroundGetSequence(
    \Magento\SalesSequence\Model\Manager $subject,
    \Closure $proceed,
    $entityType,
    $storeId
)
{
    // $entityType is 'order' or 'shipment' or 'invoice' or 'creditmemo'
    $this->_objectManager->get('Magento\Framework\Registry')->register('sr_entityType', $entityType);
    return $proceed($entityType, $storeId);
}

Now you get this registry param from getNextValue and modify your own way.

1
  • I don't actually need to edit it. I just need to get it to external SW, but without breaking functionality of other modules, that modify increment_id. I'm not sure if this is exactly what i can use. I will investigate this further, but anyway, thanks. Commented Feb 1, 2017 at 14:11
0

I have a similar requirement, I need to get invoice information after invoice save from admin.
I fulfil the requirement using the event observer.
app/code/Anshu/Customization/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_postdispatch_sales_order_invoice_save">
        <observer name="observer_admin_invoice_save" instance="Anshu\Customization\Observer\AnshuInvoiceSave" />
    </event>
</config>

app/code/Anshu/Customization/Observer/AdminInvoiceSave.php

<?php

namespace Anshu\Customization\Observer;

use Magento\Framework\Event\ObserverInterface;

class AnshuInvoiceSave implements ObserverInterface 
{
    /**
    * @var \Magento\Framework\Registry
    */

   protected $_registry;

    public function __construct(
        \Magento\Framework\Registry $registry
    )
    {
        $this->_registry = $registry;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
        $invoice = $this->getInvoiceObject();
        // My Customization
    }

    private function getInvoiceObject()
    {
        return $this->_registry->registry('current_invoice');
    }

} 

Check if it is helpful to you.
Magento version was 2.2.1

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.