Example #1
0
 /**
  * Convert order object to invoice
  *
  * @param   \Magento\Sales\Model\Order $order
  * @return  \Magento\Sales\Model\Order\Invoice
  */
 public function toInvoice(\Magento\Sales\Model\Order $order)
 {
     $invoice = $this->invoiceRepository->create();
     $invoice->setOrder($order)->setStoreId($order->getStoreId())->setCustomerId($order->getCustomerId())->setBillingAddressId($order->getBillingAddressId())->setShippingAddressId($order->getShippingAddressId());
     $this->_objectCopyService->copyFieldsetToTarget('sales_convert_order', 'to_invoice', $order, $invoice);
     return $invoice;
 }
Example #2
0
 /**
  * Get options
  *
  * @return array
  */
 public function toOptionArray()
 {
     if ($this->options === null) {
         $this->options = [];
         /** @var \Magento\Framework\Phrase $state */
         foreach ($this->invoiceRepository->create()->getStates() as $id => $state) {
             $this->options[] = ['value' => $id, 'label' => $state->render()];
         }
     }
     return $this->options;
 }
Example #3
0
 /**
  * Process relations for CreditMemo
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @throws \Exception
  * @return void
  */
 public function processRelation(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Creditmemo $object */
     if ($object->getState() == \Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED) {
         $this->prepareOrder($object);
         if ($object->getInvoice()) {
             $this->prepareInvoice($object);
             $this->invoiceRepository->save($object->getInvoice());
         }
         $this->preparePayment($object);
         $this->orderRepository->save($object->getOrder());
     }
 }
 /**
  * Run test setVoid method
  */
 public function testSetVoid()
 {
     $id = 145;
     $returnValue = true;
     $invoiceMock = $this->getMock('Magento\\Sales\\Model\\Order\\Invoice', ['void'], [], '', false);
     $this->repositoryMock->expects($this->once())->method('get')->with($id)->will($this->returnValue($invoiceMock));
     $invoiceMock->expects($this->once())->method('void')->will($this->returnValue($returnValue));
     $this->assertTrue($this->invoiceService->setVoid($id));
 }
Example #5
0
 public function testExecuteNoInvoice()
 {
     $invoiceId = 2;
     $this->requestMock->expects($this->once())->method('getParam')->with('invoice_id')->will($this->returnValue($invoiceId));
     $this->invoiceRepository->expects($this->once())->method('get')->willReturn(null);
     $resultForward = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\Forward')->disableOriginalConstructor()->setMethods([])->getMock();
     $resultForward->expects($this->once())->method('forward')->with('noroute')->will($this->returnSelf());
     $this->resultForwardFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultForward));
     $this->assertSame($resultForward, $this->controller->execute());
 }
 /**
  * @param \Magento\Sales\Model\Order $order
  * @return $this|bool
  */
 protected function _initInvoice($order)
 {
     $invoiceId = $this->getInvoiceId();
     if ($invoiceId) {
         $invoice = $this->invoiceRepository->get($invoiceId);
         $invoice->setOrder($order);
         if ($invoice->getId()) {
             return $invoice;
         }
     }
     return false;
 }
 /**
  * Process invoice or credit memo
  *
  * @param Queue $queue
  * @return \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface
  * @throws \Exception
  */
 protected function getProcessingEntity(Queue $queue)
 {
     // Check to see which type of entity we are processing
     if ($queue->getEntityTypeCode() === Queue::ENTITY_TYPE_CODE_INVOICE) {
         try {
             /* @var $invoice \Magento\Sales\Api\Data\InvoiceInterface */
             $invoice = $this->invoiceRepository->get($queue->getEntityId());
             if ($invoice->getEntityId()) {
                 return $invoice;
             } else {
                 $message = __('Invoice not found: (EntityId: %1, IncrementId: %2)', $queue->getEntityId(), $queue->getIncrementId());
                 // Update the queue record
                 $this->failQueueProcessing($queue, $message);
                 throw new \Exception($message);
             }
         } catch (NoSuchEntityException $e) {
             /* @var $message \Magento\Framework\Phrase */
             $message = __('Queue ID: %1 - Invoice not found: (EntityId: %2, IncrementId: %3)', $queue->getId(), $queue->getEntityId(), $queue->getIncrementId());
             // Update the queue record
             $this->failQueueProcessing($queue, $message);
             throw new NoSuchEntityException($message, $e);
         } catch (\Exception $e) {
             $message = __('Unexpected Exception getProcessingEntity() invoiceRepository->get(): ') . "\n" . $e->getMessage() . "\n" . $queue->getMessage();
             // Update the queue record
             $this->failQueueProcessing($queue, $message);
             throw new \Exception($message);
         }
     } elseif ($queue->getEntityTypeCode() === Queue::ENTITY_TYPE_CODE_CREDITMEMO) {
         try {
             /* @var $creditmemo \Magento\Sales\Api\Data\CreditmemoInterface */
             $creditmemo = $this->creditmemoRepository->get($queue->getEntityId());
             if ($creditmemo->getEntityId()) {
                 return $creditmemo;
             } else {
                 $message = __('Credit Memo not found: (EntityId: %1, IncrementId: %2)', $queue->getEntityId(), $queue->getIncrementId());
                 // Update the queue record
                 $this->failQueueProcessing($queue, $message);
                 throw new \Exception($message);
             }
         } catch (\Exception $e) {
             $message = __('ERROR getProcessingEntity() creditmemoRepository->get(): ') . "\n" . $e->getMessage() . "\n" . $queue->getMessage();
             // Update the queue record
             $this->failQueueProcessing($queue, $message);
             throw $e;
         }
     } else {
         $message = __('Unknown Entity Type Code for processing (%1)', $queue->getEntityTypeCode());
         // Update the queue record
         $this->failQueueProcessing($queue, $message);
         throw new \Exception();
     }
 }
Example #8
0
 /**
  * @return void
  */
 public function testExecuteException()
 {
     $invoiceId = 2;
     $message = 'Invoice canceling error';
     $e = new \Exception($message);
     $this->requestMock->expects($this->once())->method('getParam')->with('invoice_id')->will($this->returnValue($invoiceId));
     $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->setMethods([])->getMock();
     $invoiceMock->expects($this->once())->method('cancel')->will($this->throwException($e));
     $this->messageManagerMock->expects($this->once())->method('addError')->with($message);
     $invoiceMock->expects($this->once())->method('getId')->will($this->returnValue($invoiceId));
     $this->invoiceRepository->expects($this->once())->method('get')->willReturn($invoiceMock);
     $resultRedirect = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\Redirect')->disableOriginalConstructor()->setMethods([])->getMock();
     $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
     $this->resultRedirectFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultRedirect));
     $this->assertSame($resultRedirect, $this->controller->execute());
 }
 /**
  * @inheritdoc
  */
 public function setVoid($id)
 {
     return (bool) $this->repository->get($id)->void();
 }
Example #10
0
 /**
  * Constructor
  *
  * @param ContextInterface $context
  * @param UiComponentFactory $uiComponentFactory
  * @param InvoiceRepositoryInterface $invoiceRepository
  * @param array $components
  * @param array $data
  */
 public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, InvoiceRepositoryInterface $invoiceRepository, array $components = [], array $data = [])
 {
     $this->states = $invoiceRepository->create()->getStates();
     parent::__construct($context, $uiComponentFactory, $components, $data);
 }
 /**
  * Return option array
  *
  * @return array
  */
 public function toOptionArray()
 {
     return $this->invoiceRepository->create()->getStates();
 }
Example #12
0
 /**
  * Load invoice by id
  *
  * @param int|null $invoiceId
  * @return \Magento\Sales\Api\Data\InvoiceInterface|null
  */
 protected function getInvoice($invoiceId)
 {
     if ($invoiceId === null) {
         return null;
     }
     try {
         return $this->invoiceRepository->get($invoiceId);
     } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
         return null;
     }
 }