InvoiceDocumentFactory.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Sales\Api\Data\InvoiceCommentCreationInterface;
  8. use Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface;
  9. use Magento\Sales\Api\Data\InvoiceInterface;
  10. use Magento\Sales\Api\Data\InvoiceItemCreationInterface;
  11. use Magento\Sales\Api\Data\OrderInterface;
  12. use Magento\Sales\Model\Service\InvoiceService;
  13. /**
  14. * Class InvoiceDocumentFactory
  15. *
  16. * @api
  17. * @since 100.1.2
  18. */
  19. class InvoiceDocumentFactory
  20. {
  21. /**
  22. * @var InvoiceService
  23. */
  24. private $invoiceService;
  25. /**
  26. * InvoiceDocumentFactory constructor.
  27. * @param InvoiceService $invoiceService
  28. */
  29. public function __construct(
  30. InvoiceService $invoiceService
  31. ) {
  32. $this->invoiceService = $invoiceService;
  33. }
  34. /**
  35. * @param OrderInterface $order
  36. * @param array $items
  37. * @param InvoiceCommentCreationInterface|null $comment
  38. * @param bool|false $appendComment
  39. * @param InvoiceCreationArgumentsInterface|null $arguments
  40. * @return InvoiceInterface
  41. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  42. * @since 100.1.2
  43. */
  44. public function create(
  45. OrderInterface $order,
  46. $items = [],
  47. InvoiceCommentCreationInterface $comment = null,
  48. $appendComment = false,
  49. InvoiceCreationArgumentsInterface $arguments = null
  50. ) {
  51. $invoiceItems = $this->itemsToArray($items);
  52. $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems);
  53. if ($comment) {
  54. $invoice->addComment(
  55. $comment->getComment(),
  56. $appendComment,
  57. $comment->getIsVisibleOnFront()
  58. );
  59. }
  60. return $invoice;
  61. }
  62. /**
  63. * Convert Items To Array
  64. *
  65. * @param InvoiceItemCreationInterface[] $items
  66. * @return array
  67. */
  68. private function itemsToArray($items = [])
  69. {
  70. $invoiceItems = [];
  71. foreach ($items as $item) {
  72. $invoiceItems[$item->getOrderItemId()] = $item->getQty();
  73. }
  74. return $invoiceItems;
  75. }
  76. }