Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two invoices to the entire order #1464

Closed
kassner opened this issue Jul 9, 2015 · 3 comments
Closed

Two invoices to the entire order #1464

kassner opened this issue Jul 9, 2015 · 3 comments
Labels
bug report Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@kassner
Copy link
Contributor

kassner commented Jul 9, 2015

Trying a simple snippet:

$this->_objectManager->create('Magento\Sales\Model\Order')->load(1);

$order->prepareInvoice()->save();
$order->save();

$order->prepareInvoice()->save();
$order->save();

In such case, prepareInvoice generates an invoice with all items from the order. In the second run, the invoice is generated again with all items (even those that already were invoiced). The shipping amount is invoiced only in the first time.

Is this an expected behavior? I guess it should not allow to invoice more items than were ordered.

@vpelipenko vpelipenko added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development CS bug report labels Jul 10, 2015
@vpelipenko
Copy link
Contributor

We can call it a bug. But could you describe here business case? When can we face with such behavior?
Internal ticket: MAGETWO-39989

@kassner
Copy link
Contributor Author

kassner commented Jul 17, 2015

Sorry, I was calling it in the wrong way, need to register it before saving.

Code for future reference:

    protected function invoice(\Magento\Sales\Api\Data\OrderInterface $order)
    {
        if (!$order->canInvoice()) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('The order does not allow an invoice to be created.')
            );
        }

        /** @var \Magento\Sales\Model\Order\Invoice $invoice */
        $invoice = $this->_objectManager->create('Magento\Sales\Model\Service\Order', ['order' => $order])->prepareInvoice();

        if (!$invoice) {
            throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the invoice right now.'));
        }

        if (!$invoice->getTotalQty()) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('You can\'t create an invoice without products.')
            );
        }

        $invoice->register();

        $invoice->getOrder()->setCustomerNoteNotify(true);
        $invoice->getOrder()->setIsInProcess(true);

        $transactionSave = $this->_objectManager->create('Magento\Framework\DB\Transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder());

        $transactionSave->save();
    }

Thanks!

@kassner kassner closed this as completed Jul 17, 2015
magento-team pushed a commit that referenced this issue Sep 6, 2017
[EngCom] Public Pull Requests
 - MAGETWO-72350: Fix: Use all columns when running tests #10784
 - MAGETWO-72349: Fix: Move GitHub-specific documents into .github #10778
 - MAGETWO-72344: Enhancement: Configure preferred installation source in composer.json #10774
 - MAGETWO-72279: Always use https for Vimeo video's. #10768
@magento-engcom-team magento-engcom-team added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Jul 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests

3 participants