partial_invoice.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. use Magento\Sales\Api\InvoiceRepositoryInterface;
  8. use Magento\Sales\Api\TransactionRepositoryInterface;
  9. use Magento\Sales\Model\Order;
  10. use Magento\Sales\Model\Service\InvoiceService;
  11. use Magento\TestFramework\ObjectManager;
  12. /** @var Order $order */
  13. require __DIR__ . '/order.php';
  14. $objectManager = ObjectManager::getInstance();
  15. /** @var InvoiceService $invoiceService */
  16. $invoiceService = $objectManager->get(InvoiceService::class);
  17. $invoice = $invoiceService->prepareInvoice($order);
  18. $invoice->setIncrementId('100000002');
  19. $invoice->register();
  20. $items = $invoice->getAllItems();
  21. $item = array_pop($items);
  22. $item->setQty(1);
  23. $invoice->setTotalQty(1);
  24. $items = $order->getAllItems();
  25. /** @var \Magento\Sales\Api\Data\OrderItemInterface $item */
  26. $item = array_pop($items);
  27. $item->setQtyInvoiced(1);
  28. $invoice->collectTotals();
  29. /** @var InvoiceRepositoryInterface $invoiceRepository */
  30. $invoiceRepository = $objectManager->get(InvoiceRepositoryInterface::class);
  31. $invoice = $invoiceRepository->save($invoice);
  32. /** @var TransactionRepositoryInterface $transactionRepository */
  33. $transactionRepository = $objectManager->get(TransactionRepositoryInterface::class);
  34. $transaction = $transactionRepository->create();
  35. $transaction->setTxnType('capture');
  36. $transaction->setPaymentId($order->getPayment()->getEntityId());
  37. $transaction->setOrderId($order->getEntityId());
  38. $transactionRepository->save($transaction);