/**
  * Add comment to creditmemo history
  *
  * @return \Magento\Framework\Controller\Result\Raw|\Magento\Framework\Controller\Result\Json
  */
 public function execute()
 {
     try {
         $this->getRequest()->setParam('creditmemo_id', $this->getRequest()->getParam('id'));
         $data = $this->getRequest()->getPost('comment');
         if (empty($data['comment'])) {
             throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a comment.'));
         }
         $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
         $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
         $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
         $creditmemo = $this->creditmemoLoader->load();
         $comment = $creditmemo->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']));
         $comment->save();
         $this->creditmemoCommentSender->send($creditmemo, !empty($data['is_customer_notified']), $data['comment']);
         $resultPage = $this->resultPageFactory->create();
         $response = $resultPage->getLayout()->getBlock('creditmemo_comments')->toHtml();
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $response = ['error' => true, 'message' => $e->getMessage()];
     } catch (\Exception $e) {
         $response = ['error' => true, 'message' => __('Cannot add new comment.')];
     }
     if (is_array($response)) {
         $resultJson = $this->resultJsonFactory->create();
         $resultJson->setData($response);
         return $resultJson;
     } else {
         $resultRaw = $this->resultRawFactory->create();
         $resultRaw->setContents($response);
         return $resultRaw;
     }
 }
Пример #2
0
 /**
  * Add comment to creditmemo history
  *
  * @return void
  */
 public function execute()
 {
     try {
         $this->getRequest()->setParam('creditmemo_id', $this->getRequest()->getParam('id'));
         $data = $this->getRequest()->getPost('comment');
         if (empty($data['comment'])) {
             throw new \Magento\Framework\Model\Exception(__('The Comment Text field cannot be empty.'));
         }
         $this->_title->add(__('Credit Memos'));
         $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
         $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
         $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
         $creditmemo = $this->creditmemoLoader->load();
         $comment = $creditmemo->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']));
         $comment->save();
         $this->creditmemoSender->send($creditmemo, !empty($data['is_customer_notified']), $data['comment']);
         $this->_view->loadLayout();
         $response = $this->_view->getLayout()->getBlock('creditmemo_comments')->toHtml();
     } catch (\Magento\Framework\Model\Exception $e) {
         $response = array('error' => true, 'message' => $e->getMessage());
     } catch (\Exception $e) {
         $response = array('error' => true, 'message' => __('Cannot add new comment.'));
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
 /**
  * Creditmemo create page
  *
  * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Forward
  */
 public function execute()
 {
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $creditmemo = $this->creditmemoLoader->load();
     if ($creditmemo) {
         if ($comment = $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true)) {
             $creditmemo->setCommentText($comment);
         }
         $resultPage = $this->resultPageFactory->create();
         $resultPage->setActiveMenu('Magento_Sales::sales_order');
         $resultPage->getConfig()->getTitle()->prepend(__('Credit Memos'));
         if ($creditmemo->getInvoice()) {
             $resultPage->getConfig()->getTitle()->prepend(__("New Memo for #%1", $creditmemo->getInvoice()->getIncrementId()));
         } else {
             $resultPage->getConfig()->getTitle()->prepend(__("New Memo"));
         }
         return $resultPage;
     } else {
         $resultForward = $this->resultForwardFactory->create();
         $resultForward->forward('noroute');
         return $resultForward;
     }
 }
Пример #4
0
 /**
  * Cancel creditmemo action
  *
  * @return void
  */
 public function execute()
 {
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $creditmemo = $this->creditmemoLoader->load();
     if ($creditmemo) {
         try {
             $creditmemo->cancel();
             $transactionSave = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction');
             $transactionSave->addObject($creditmemo);
             $transactionSave->addObject($creditmemo->getOrder());
             if ($creditmemo->getInvoice()) {
                 $transactionSave->addObject($creditmemo->getInvoice());
             }
             $transactionSave->save();
             $this->messageManager->addSuccess(__('The credit memo has been canceled.'));
         } catch (\Magento\Framework\Model\Exception $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addError(__('You canceled the credit memo.'));
         }
         $this->_redirect('sales/*/view', array('creditmemo_id' => $creditmemo->getId()));
     } else {
         $this->_forward('noroute');
     }
 }
Пример #5
0
 /**
  * Void creditmemo action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Backend\Model\View\Result\Forward
  */
 public function execute()
 {
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $creditmemo = $this->creditmemoLoader->load();
     if ($creditmemo) {
         try {
             $creditmemo->void();
             $transactionSave = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction');
             $transactionSave->addObject($creditmemo);
             $transactionSave->addObject($creditmemo->getOrder());
             if ($creditmemo->getInvoice()) {
                 $transactionSave->addObject($creditmemo->getInvoice());
             }
             $transactionSave->save();
             $this->messageManager->addSuccess(__('You voided the credit memo.'));
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addError(__('We can\'t void the credit memo.'));
         }
         $resultRedirect = $this->resultRedirectFactory->create();
         $resultRedirect->setPath('sales/*/view', ['creditmemo_id' => $creditmemo->getId()]);
         return $resultRedirect;
     } else {
         $resultForward = $this->resultForwardFactory->create();
         $resultForward->forward('noroute');
         return $resultForward;
     }
 }
Пример #6
0
 /**
  * Update items qty action
  *
  * @return \Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\Result\Raw
  */
 public function executeInternal()
 {
     try {
         $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
         $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
         $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
         $this->creditmemoLoader->load();
         $resultPage = $this->resultPageFactory->create();
         $response = $resultPage->getLayout()->getBlock('order_items')->toHtml();
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $response = ['error' => true, 'message' => $e->getMessage()];
     } catch (\Exception $e) {
         $response = ['error' => true, 'message' => __('We can\'t update the item\'s quantity right now.')];
     }
     if (is_array($response)) {
         $resultJson = $this->resultJsonFactory->create();
         $resultJson->setData($response);
         return $resultJson;
     } else {
         $resultRaw = $this->resultRawFactory->create();
         $resultRaw->setContents($response);
         return $resultRaw;
     }
 }
Пример #7
0
 /**
  * Creditmemo information page
  *
  * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Forward
  */
 public function executeInternal()
 {
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $creditmemo = $this->creditmemoLoader->load();
     if ($creditmemo) {
         $resultPage = $this->resultPageFactory->create();
         $resultPage->getLayout()->getBlock('sales_creditmemo_view')
             ->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
         $resultPage->setActiveMenu('Magento_Sales::sales_creditmemo');
         if ($creditmemo->getInvoice()) {
             $resultPage->getConfig()->getTitle()->prepend(
                 __("View Memo for #%1", $creditmemo->getInvoice()->getIncrementId())
             );
         } else {
             $resultPage->getConfig()->getTitle()->prepend(__("View Memo"));
         }
         return $resultPage;
     } else {
         $resultForward = $this->resultForwardFactory->create();
         $resultForward->forward('noroute');
         return $resultForward;
     }
 }
Пример #8
0
 /**
  * Create pdf for current creditmemo
  *
  * @return ResponseInterface|\Magento\Backend\Model\View\Result\Forward
  */
 public function execute()
 {
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $this->creditmemoLoader->load();
     return parent::execute();
 }
Пример #9
0
 /**
  * Create pdf for current creditmemo
  *
  * @return ResponseInterface|void
  */
 public function execute()
 {
     $this->_title->add(__('Credit Memos'));
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $this->creditmemoLoader->load();
     parent::execute();
 }
Пример #10
0
 /**
  * Save creditmemo
  * We can save only new creditmemo. Existing creditmemos are not editable
  *
  * @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Backend\Model\View\Result\Forward
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $resultRedirect = $this->resultRedirectFactory->create();
     $data = $this->getRequest()->getPost('creditmemo');
     if (!empty($data['comment_text'])) {
         $this->_getSession()->setCommentText($data['comment_text']);
     }
     try {
         $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
         $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
         $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
         $creditmemo = $this->creditmemoLoader->load();
         if ($creditmemo) {
             if (!$creditmemo->isValidGrandTotal()) {
                 throw new \Magento\Framework\Exception\LocalizedException(__('The credit memo\'s total must be positive.'));
             }
             if (!empty($data['comment_text'])) {
                 $creditmemo->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front']));
                 $creditmemo->setCustomerNote($data['comment_text']);
                 $creditmemo->setCustomerNoteNotify(isset($data['comment_customer_notify']));
             }
             if (isset($data['do_offline'])) {
                 //do not allow online refund for Refund to Store Credit
                 if (!$data['do_offline'] && !empty($data['refund_customerbalance_return_enable'])) {
                     throw new \Magento\Framework\Exception\LocalizedException(__('Cannot create online refund for Refund to Store Credit.'));
                 }
             }
             $creditmemoManagement = $this->_objectManager->create('Magento\\Sales\\Api\\CreditmemoManagementInterface');
             $creditmemoManagement->refund($creditmemo, (bool) $data['do_offline'], !empty($data['send_email']));
             if (!empty($data['send_email'])) {
                 $this->creditmemoSender->send($creditmemo);
             }
             $this->messageManager->addSuccess(__('You created the credit memo.'));
             $this->_getSession()->getCommentText(true);
             $resultRedirect->setPath('sales/order/view', ['order_id' => $creditmemo->getOrderId()]);
             return $resultRedirect;
         } else {
             $resultForward = $this->resultForwardFactory->create();
             $resultForward->forward('noroute');
             return $resultForward;
         }
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addError($e->getMessage());
         $this->_getSession()->setFormData($data);
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
         $this->messageManager->addError(__('We can\'t save the credit memo right now.'));
     }
     $resultRedirect->setPath('sales/*/new', ['_current' => true]);
     return $resultRedirect;
 }
Пример #11
0
 /**
  * @param Creditmemo $dataObject
  * @return bool|\Magento\Sales\Model\Order\Creditmemo
  */
 public function getModel(Creditmemo $dataObject)
 {
     $this->creditmemoLoader->setOrderId($dataObject->getOrderId());
     $this->creditmemoLoader->setCreditmemoId($dataObject->getEntityId());
     $items = [];
     foreach ($dataObject->getItems() as $item) {
         $items[$item->getOrderItemId()] = ['qty' => $item->getQty()];
     }
     $creditmemo = ['items' => $items, 'shipping_amount' => $dataObject->getShippingAmount(), 'adjustment_positive' => $dataObject->getAdjustmentPositive(), 'adjustment_negative' => $dataObject->getAdjustmentNegative()];
     $this->creditmemoLoader->setCreditmemo($creditmemo);
     $this->creditmemoLoader->setInvoiceId($dataObject->getInvoiceId());
     return $this->creditmemoLoader->load();
 }
Пример #12
0
 public function testLoadByOrder()
 {
     $orderId = 1234;
     $invoiceId = 99;
     $qty = 1;
     $data = ['items' => [1 => ['qty' => $qty, 'back_to_stock' => true]]];
     $this->loader->setCreditmemoId(0);
     $this->loader->setOrderId($orderId);
     $this->loader->setCreditmemo($data);
     $this->loader->setInvoiceId($invoiceId);
     $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods([])->getMock();
     $orderMock->expects($this->once())->method('load')->willReturnSelf();
     $orderMock->expects($this->once())->method('getId')->willReturn($orderId);
     $orderMock->expects($this->once())->method('canCreditmemo')->willReturn(true);
     $this->orderFactoryMock->expects($this->once())->method('create')->willReturn($orderMock);
     $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->getMock();
     $invoiceMock->expects($this->any())->method('setOrder')->willReturnSelf();
     $invoiceMock->expects($this->any())->method('getId')->willReturn(1);
     $this->invoiceRepositoryMock->expects($this->once())->method('get')->willReturn($invoiceMock);
     $creditmemoMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo')->disableOriginalConstructor()->setMethods([])->getMock();
     $orderItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Item')->disableOriginalConstructor()->setMethods([])->getMock();
     $creditmemoItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo\\Item')->disableOriginalConstructor()->setMethods([])->getMock();
     $creditmemoItemMock->expects($this->any())->method('getOrderItem')->willReturn($orderItemMock);
     $items = [$creditmemoItemMock, $creditmemoItemMock, $creditmemoItemMock];
     $creditmemoMock->expects($this->any())->method('getAllItems')->willReturn($items);
     $data['qtys'] = [1 => $qty];
     $this->creditmemoFactoryMock->expects($this->any())->method('createByInvoice')->with($invoiceMock, $data)->willReturn($creditmemoMock);
     $this->assertEquals($creditmemoMock, $this->loader->load());
 }
 public function testLoadByOrder()
 {
     $qty = 1;
     $data = ['items' => [1 => ['qty' => $qty, 'back_to_stock' => true]]];
     $this->loader->setCreditmemoId(0);
     $this->loader->setOrderId(1);
     $this->loader->setCreditmemo($data);
     $this->loader->setInvoiceId(1);
     $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods([])->getMock();
     $orderMock->expects($this->once())->method('load')->willReturnSelf();
     $orderMock->expects($this->once())->method('getId')->willReturn(1);
     $orderMock->expects($this->once())->method('canCreditmemo')->willReturn(true);
     $this->orderFactoryMock->expects($this->once())->method('create')->willReturn($orderMock);
     $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->setMethods([])->getMock();
     $invoiceMock->expects($this->any())->method('load')->willReturnSelf();
     $invoiceMock->expects($this->any())->method('setOrder')->willReturnSelf();
     $invoiceMock->expects($this->any())->method('getId')->willReturn(1);
     $this->invoiceFactoryMock->expects($this->once())->method('create')->willReturn($invoiceMock);
     $serviceOrder = $this->getMockBuilder('Magento\\Sales\\Model\\Service\\Order')->disableOriginalConstructor()->setMethods([])->getMock();
     $creditmemoMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo')->disableOriginalConstructor()->setMethods([])->getMock();
     $orderItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Item')->disableOriginalConstructor()->setMethods([])->getMock();
     $creditmemoItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo\\Item')->disableOriginalConstructor()->setMethods([])->getMock();
     $creditmemoItemMock->expects($this->any())->method('getOrderItem')->willReturn($orderItemMock);
     $items = [$creditmemoItemMock, $creditmemoItemMock, $creditmemoItemMock];
     $creditmemoMock->expects($this->any())->method('getAllItems')->willReturn($items);
     $serviceOrder->expects($this->any())->method('prepareInvoiceCreditmemo')->willReturn($creditmemoMock);
     $this->serviceOrderFactoryMock->expects($this->once())->method('create')->willReturn($serviceOrder);
     $this->assertInstanceOf('Magento\\Sales\\Model\\Order\\Creditmemo', $this->loader->load());
 }
Пример #14
0
 /**
  * Creditmemo information page
  *
  * @return void
  */
 public function execute()
 {
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $creditmemo = $this->creditmemoLoader->load();
     if ($creditmemo) {
         if ($creditmemo->getInvoice()) {
             $this->_title->add(__("View Memo for #%1", $creditmemo->getInvoice()->getIncrementId()));
         } else {
             $this->_title->add(__("View Memo"));
         }
         $this->_view->loadLayout();
         $this->_view->getLayout()->getBlock('sales_creditmemo_view')->updateBackButtonUrl($this->getRequest()->getParam('come_from'));
         $this->_setActiveMenu('Magento_Sales::sales_creditmemo');
         $this->_view->renderLayout();
     } else {
         $this->_forward('noroute');
     }
 }
Пример #15
0
 /**
  * Update items qty action
  *
  * @return void
  */
 public function execute()
 {
     try {
         $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
         $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
         $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
         $this->creditmemoLoader->load();
         $this->_view->loadLayout();
         $response = $this->_view->getLayout()->getBlock('order_items')->toHtml();
     } catch (\Magento\Framework\Model\Exception $e) {
         $response = array('error' => true, 'message' => $e->getMessage());
     } catch (\Exception $e) {
         $response = array('error' => true, 'message' => __('Cannot update the item\'s quantity.'));
     }
     if (is_array($response)) {
         $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response);
         $this->getResponse()->representJson($response);
     } else {
         $this->getResponse()->setBody($response);
     }
 }
Пример #16
0
 /**
  * Creditmemo create page
  *
  * @return void
  */
 public function execute()
 {
     $this->_title->add(__('Credit Memos'));
     $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
     $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
     $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
     $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
     $creditmemo = $this->creditmemoLoader->load();
     if ($creditmemo) {
         if ($creditmemo->getInvoice()) {
             $this->_title->add(__("New Memo for #%1", $creditmemo->getInvoice()->getIncrementId()));
         } else {
             $this->_title->add(__("New Memo"));
         }
         if ($comment = $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true)) {
             $creditmemo->setCommentText($comment);
         }
         $this->_view->loadLayout();
         $this->_setActiveMenu('Magento_Sales::sales_order');
         $this->_view->renderLayout();
     } else {
         $this->_forward('noroute');
     }
 }
Пример #17
0
 /**
  * Save creditmemo
  * We can save only new creditmemo. Existing creditmemos are not editable
  *
  * @return void
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost('creditmemo');
     if (!empty($data['comment_text'])) {
         $this->_getSession()->setCommentText($data['comment_text']);
     }
     try {
         $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
         $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
         $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
         $creditmemo = $this->creditmemoLoader->load();
         if ($creditmemo) {
             if (!$creditmemo->isValidGrandTotal()) {
                 throw new \Magento\Framework\Model\Exception(__('Credit memo\'s total must be positive.'));
             }
             $comment = '';
             if (!empty($data['comment_text'])) {
                 $creditmemo->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front']));
                 if (isset($data['comment_customer_notify'])) {
                     $comment = $data['comment_text'];
                 }
             }
             if (isset($data['do_refund'])) {
                 $creditmemo->setRefundRequested(true);
             }
             if (isset($data['do_offline'])) {
                 //do not allow online refund for Refund to Store Credit
                 if (!$data['do_offline'] && !empty($data['refund_customerbalance_return_enable'])) {
                     throw new \Magento\Framework\Model\Exception(__('Cannot create online refund for Refund to Store Credit.'));
                 }
                 $creditmemo->setOfflineRequested((bool) (int) $data['do_offline']);
             }
             $creditmemo->register();
             if (!empty($data['send_email'])) {
                 $creditmemo->setEmailSent(true);
             }
             $creditmemo->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
             $transactionSave = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction')->addObject($creditmemo)->addObject($creditmemo->getOrder());
             if ($creditmemo->getInvoice()) {
                 $transactionSave->addObject($creditmemo->getInvoice());
             }
             $transactionSave->save();
             $this->creditmemoSender->send($creditmemo, !empty($data['send_email']), $comment);
             $this->messageManager->addSuccess(__('You created the credit memo.'));
             $this->_getSession()->getCommentText(true);
             $this->_redirect('sales/order/view', array('order_id' => $creditmemo->getOrderId()));
             return;
         } else {
             $this->_forward('noroute');
             return;
         }
     } catch (\Magento\Framework\Model\Exception $e) {
         $this->messageManager->addError($e->getMessage());
         $this->_getSession()->setFormData($data);
     } catch (\Exception $e) {
         $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
         $this->messageManager->addError(__('Cannot save the credit memo.'));
     }
     $this->_redirect('sales/*/new', array('_current' => true));
 }