Exemplo n.º 1
0
 /**
  * @return ResponseInterface|\Magento\Backend\Model\View\Result\Forward
  */
 public function executeInternal()
 {
     /** @see \Magento\Sales\Controller\Adminhtml\Order\Invoice */
     $creditmemoId = $this->getRequest()->getParam('creditmemo_id');
     if ($creditmemoId) {
         $creditmemo = $this->creditmemoRepository->get($creditmemoId);
         if ($creditmemo) {
             $pdf = $this->_objectManager->create(
                 'Magento\Sales\Model\Order\Pdf\Creditmemo'
             )->getPdf(
                 [$creditmemo]
             );
             $date = $this->_objectManager->get('Magento\Framework\Stdlib\DateTime\DateTime')->date('Y-m-d_H-i-s');
             return $this->_fileFactory->create(
                 'creditmemo' . $date . '.pdf',
                 $pdf->render(),
                 DirectoryList::VAR_DIR,
                 'application/pdf'
             );
         }
     } else {
         $resultForward = $this->resultForwardFactory->create();
         $resultForward->forward('noroute');
         return $resultForward;
     }
 }
Exemplo n.º 2
0
    /**
     * Print Creditmemo Action
     *
     * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
     */
    public function executeInternal()
    {
        $creditmemoId = (int)$this->getRequest()->getParam('creditmemo_id');
        if ($creditmemoId) {
            $creditmemo = $this->creditmemoRepository->get($creditmemoId);
            $order = $creditmemo->getOrder();
        } else {
            $orderId = (int)$this->getRequest()->getParam('order_id');
            $order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId);
        }

        if ($this->orderAuthorization->canView($order)) {
            $this->_coreRegistry->register('current_order', $order);
            if (isset($creditmemo)) {
                $this->_coreRegistry->register('current_creditmemo', $creditmemo);
            }
            /** @var \Magento\Framework\View\Result\Page $resultPage */
            $resultPage = $this->resultPageFactory->create();
            $resultPage->addHandle('print');
            return $resultPage;
        } else {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            if ($this->_objectManager->get('Magento\Customer\Model\Session')->isLoggedIn()) {
                $resultRedirect->setPath('*/*/history');
            } else {
                $resultRedirect->setPath('sales/guest/form');
            }
            return $resultRedirect;
        }
    }
 public function testLoadByCreditmemoId()
 {
     $this->loader->setCreditmemoId(1);
     $this->loader->setOrderId(1);
     $this->loader->setCreditmemo('test');
     $creditmemoMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo')->disableOriginalConstructor()->setMethods([])->getMock();
     $this->creditmemoRepositoryMock->expects($this->once())->method('get')->willReturn($creditmemoMock);
     $this->assertInstanceOf('Magento\\Sales\\Model\\Order\\Creditmemo', $this->loader->load());
 }
Exemplo n.º 4
0
 /**
  * Get options
  *
  * @return array
  */
 public function toOptionArray()
 {
     if ($this->options === null) {
         $this->options = [];
         /** @var \Magento\Framework\Phrase $state */
         foreach ($this->creditmemoRepository->create()->getStates() as $id => $state) {
             $this->options[] = ['value' => $id, 'label' => $state->render()];
         }
     }
     return $this->options;
 }
Exemplo n.º 5
0
 /**
  * Convert order object to creditmemo
  *
  * @param   \Magento\Sales\Model\Order $order
  * @return  \Magento\Sales\Model\Order\Creditmemo
  */
 public function toCreditmemo(\Magento\Sales\Model\Order $order)
 {
     $creditmemo = $this->creditmemoRepository->create();
     $creditmemo->setOrder($order)->setStoreId($order->getStoreId())->setCustomerId($order->getCustomerId())->setBillingAddressId($order->getBillingAddressId())->setShippingAddressId($order->getShippingAddressId());
     $this->_objectCopyService->copyFieldsetToTarget('sales_convert_order', 'to_cm', $order, $creditmemo);
     return $creditmemo;
 }
 /**
  * Run test notify method
  */
 public function testNotify()
 {
     $id = 123;
     $returnValue = 'return-value';
     $modelMock = $this->getMockForAbstractClass('Magento\\Sales\\Model\\AbstractModel', [], '', false);
     $this->creditmemoRepositoryMock->expects($this->once())->method('get')->with($id)->will($this->returnValue($modelMock));
     $this->creditmemoNotifierMock->expects($this->once())->method('notify')->with($modelMock)->will($this->returnValue($returnValue));
     $this->assertEquals($returnValue, $this->creditmemoService->notify($id));
 }
Exemplo n.º 7
0
    /**
     * @covers \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\PrintAction::executeInternal
     */
    public function testExecute()
    {
        $creditmemoId = 2;
        $date = '2015-01-19_13-03-45';
        $fileName = 'creditmemo2015-01-19_13-03-45.pdf';
        $fileContents = 'pdf0123456789';
        $this->prepareTestExecute($creditmemoId);

        $this->objectManagerMock->expects($this->any())
            ->method('create')
            ->willReturnMap(
                [
                    ['Magento\Sales\Model\Order\Creditmemo', [], $this->creditmemoMock],
                    ['Magento\Sales\Model\Order\Pdf\Creditmemo', [], $this->creditmemoPdfMock]
                ]
            );
        $this->creditmemoRepositoryMock->expects($this->once())
            ->method('get')
            ->with($creditmemoId)
            ->willReturn($this->creditmemoMock);
        $this->creditmemoPdfMock->expects($this->once())
            ->method('getPdf')
            ->with([$this->creditmemoMock])
            ->willReturn($this->pdfMock);
        $this->objectManagerMock->expects($this->once())
            ->method('get')
            ->with('Magento\Framework\Stdlib\DateTime\DateTime')
            ->willReturn($this->dateTimeMock);
        $this->dateTimeMock->expects($this->once())
            ->method('date')
            ->with('Y-m-d_H-i-s')
            ->willReturn($date);
        $this->pdfMock->expects($this->once())
            ->method('render')
            ->willReturn($fileContents);
        $this->fileFactoryMock->expects($this->once())
            ->method('create')
            ->with(
                $fileName,
                $fileContents,
                \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
                'application/pdf'
            )
            ->willReturn($this->responseMock);

        $this->assertInstanceOf(
            'Magento\Framework\App\ResponseInterface',
            $this->printAction->executeInternal()
        );
    }
 /**
  * Prepare creditmemo to refund and save it.
  *
  * @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo
  * @param bool $offlineRequested
  * @return \Magento\Sales\Api\Data\CreditmemoInterface
  */
 public function refund(\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo, $offlineRequested = false)
 {
     $this->validateForRefund($creditmemo);
     $creditmemo->setState(\Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED);
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getQty() > 0) {
             $item->register();
         } else {
             $item->isDeleted(true);
         }
     }
     $creditmemo->setDoTransaction(!$offlineRequested);
     $this->eventManager->dispatch('sales_order_creditmemo_refund', ['creditmemo' => $creditmemo]);
     $this->creditmemoRepository->save($creditmemo);
     return $creditmemo;
 }
Exemplo n.º 9
0
 /**
  * Constructor
  *
  * @param ContextInterface $context
  * @param UiComponentFactory $uiComponentFactory
  * @param CreditmemoRepositoryInterface $creditmemoRepository
  * @param array $components
  * @param array $data
  */
 public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, CreditmemoRepositoryInterface $creditmemoRepository, array $components = [], array $data = [])
 {
     $this->states = $creditmemoRepository->create()->getStates();
     parent::__construct($context, $uiComponentFactory, $components, $data);
 }
Exemplo n.º 10
0
 /**
  * Notify user
  *
  * @param int $id
  * @return bool
  */
 public function notify($id)
 {
     return $this->creditmemoNotifier->notify($this->creditmemoRepository->get($id));
 }
Exemplo n.º 11
0
 /**
  * Return option array
  *
  * @return array
  */
 public function toOptionArray()
 {
     return $this->creditmemoRepository->create()->getStates();
 }