View.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Adminhtml\Order\Creditmemo;
  7. /**
  8. * Adminhtml creditmemo view
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class View extends \Magento\Backend\Block\Widget\Form\Container
  15. {
  16. /**
  17. * Core registry
  18. *
  19. * @var \Magento\Framework\Registry
  20. */
  21. protected $_coreRegistry = null;
  22. /**
  23. * @param \Magento\Backend\Block\Widget\Context $context
  24. * @param \Magento\Framework\Registry $registry
  25. * @param array $data
  26. */
  27. public function __construct(
  28. \Magento\Backend\Block\Widget\Context $context,
  29. \Magento\Framework\Registry $registry,
  30. array $data = []
  31. ) {
  32. $this->_coreRegistry = $registry;
  33. parent::__construct($context, $data);
  34. }
  35. /**
  36. * Add & remove control buttons
  37. *
  38. * @return void
  39. */
  40. protected function _construct()
  41. {
  42. $this->_objectId = 'creditmemo_id';
  43. $this->_controller = 'adminhtml_order_creditmemo';
  44. $this->_mode = 'view';
  45. parent::_construct();
  46. $this->buttonList->remove('save');
  47. $this->buttonList->remove('reset');
  48. $this->buttonList->remove('delete');
  49. if (!$this->getCreditmemo()) {
  50. return;
  51. }
  52. if ($this->getCreditmemo()->canCancel()) {
  53. $this->buttonList->add(
  54. 'cancel',
  55. [
  56. 'label' => __('Cancel'),
  57. 'class' => 'delete',
  58. 'onclick' => 'setLocation(\'' . $this->getCancelUrl() . '\')'
  59. ]
  60. );
  61. }
  62. if ($this->_isAllowedAction('Magento_Sales::emails')) {
  63. $this->addButton(
  64. 'send_notification',
  65. [
  66. 'label' => __('Send Email'),
  67. 'class' => 'send-email',
  68. 'onclick' => 'confirmSetLocation(\'' . __(
  69. 'Are you sure you want to send a credit memo email to customer?'
  70. ) . '\', \'' . $this->getEmailUrl() . '\')'
  71. ]
  72. );
  73. }
  74. if ($this->getCreditmemo()->canRefund()) {
  75. $this->buttonList->add(
  76. 'refund',
  77. [
  78. 'label' => __('Refund'),
  79. 'class' => 'refund',
  80. 'onclick' => 'setLocation(\'' . $this->getRefundUrl() . '\')'
  81. ]
  82. );
  83. }
  84. if ($this->getCreditmemo()->canVoid()) {
  85. $this->buttonList->add(
  86. 'void',
  87. [
  88. 'label' => __('Void'),
  89. 'class' => 'void',
  90. 'onclick' => 'setLocation(\'' . $this->getVoidUrl() . '\')'
  91. ]
  92. );
  93. }
  94. if ($this->getCreditmemo()->getId()) {
  95. $this->buttonList->add(
  96. 'print',
  97. [
  98. 'label' => __('Print'),
  99. 'class' => 'print',
  100. 'onclick' => 'setLocation(\'' . $this->getPrintUrl() . '\')'
  101. ]
  102. );
  103. }
  104. }
  105. /**
  106. * Retrieve creditmemo model instance
  107. *
  108. * @return \Magento\Sales\Model\Order\Creditmemo
  109. */
  110. public function getCreditmemo()
  111. {
  112. return $this->_coreRegistry->registry('current_creditmemo');
  113. }
  114. /**
  115. * Retrieve text for header
  116. *
  117. * @return \Magento\Framework\Phrase
  118. */
  119. public function getHeaderText()
  120. {
  121. if ($this->getCreditmemo()->getEmailSent()) {
  122. $emailSent = __('The credit memo email was sent.');
  123. } else {
  124. $emailSent = __('The credit memo email wasn\'t sent.');
  125. }
  126. return __(
  127. 'Credit Memo #%1 | %3 | %2 (%4)',
  128. $this->getCreditmemo()->getIncrementId(),
  129. $this->formatDate(
  130. $this->_localeDate->date(new \DateTime($this->getCreditmemo()->getCreatedAt())),
  131. \IntlDateFormatter::MEDIUM,
  132. true
  133. ),
  134. $this->getCreditmemo()->getStateName(),
  135. $emailSent
  136. );
  137. }
  138. /**
  139. * Retrieve back url
  140. *
  141. * @return string
  142. */
  143. public function getBackUrl()
  144. {
  145. return $this->getUrl(
  146. 'sales/order/view',
  147. [
  148. 'order_id' => $this->getCreditmemo() ? $this->getCreditmemo()->getOrderId() : null,
  149. 'active_tab' => 'order_creditmemos'
  150. ]
  151. );
  152. }
  153. /**
  154. * Retrieve capture url
  155. *
  156. * @return string
  157. */
  158. public function getCaptureUrl()
  159. {
  160. return $this->getUrl('sales/*/capture', ['creditmemo_id' => $this->getCreditmemo()->getId()]);
  161. }
  162. /**
  163. * Retrieve void url
  164. *
  165. * @return string
  166. */
  167. public function getVoidUrl()
  168. {
  169. return $this->getUrl('sales/*/void', ['creditmemo_id' => $this->getCreditmemo()->getId()]);
  170. }
  171. /**
  172. * Retrieve cancel url
  173. *
  174. * @return string
  175. */
  176. public function getCancelUrl()
  177. {
  178. return $this->getUrl('sales/*/cancel', ['creditmemo_id' => $this->getCreditmemo()->getId()]);
  179. }
  180. /**
  181. * Retrieve email url
  182. *
  183. * @return string
  184. */
  185. public function getEmailUrl()
  186. {
  187. return $this->getUrl(
  188. 'sales/*/email',
  189. [
  190. 'creditmemo_id' => $this->getCreditmemo()->getId(),
  191. 'order_id' => $this->getCreditmemo()->getOrderId()
  192. ]
  193. );
  194. }
  195. /**
  196. * Retrieve print url
  197. *
  198. * @return string
  199. */
  200. public function getPrintUrl()
  201. {
  202. return $this->getUrl('sales/*/print', ['creditmemo_id' => $this->getCreditmemo()->getId()]);
  203. }
  204. /**
  205. * Update 'back' button url
  206. *
  207. * @param bool $flag
  208. * @return \Magento\Backend\Block\Widget\Container|$this
  209. */
  210. public function updateBackButtonUrl($flag)
  211. {
  212. if ($flag) {
  213. if ($this->getCreditmemo()->getBackUrl()) {
  214. return $this->buttonList->update(
  215. 'back',
  216. 'onclick',
  217. 'setLocation(\'' . $this->getCreditmemo()->getBackUrl() . '\')'
  218. );
  219. }
  220. return $this->buttonList->update(
  221. 'back',
  222. 'onclick',
  223. 'setLocation(\'' . $this->getUrl('sales/creditmemo/') . '\')'
  224. );
  225. }
  226. return $this;
  227. }
  228. /**
  229. * Check whether action is allowed
  230. *
  231. * @param string $resourceId
  232. * @return bool
  233. */
  234. public function _isAllowedAction($resourceId)
  235. {
  236. return $this->_authorization->isAllowed($resourceId);
  237. }
  238. }