View.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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\Invoice;
  7. /**
  8. * Adminhtml invoice create
  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. * Admin session
  18. *
  19. * @var \Magento\Backend\Model\Auth\Session
  20. */
  21. protected $_session;
  22. /**
  23. * Core registry
  24. *
  25. * @var \Magento\Framework\Registry
  26. */
  27. protected $_coreRegistry = null;
  28. /**
  29. * Backend session
  30. *
  31. * @var \Magento\Backend\Model\Auth\Session
  32. */
  33. protected $_backendSession;
  34. /**
  35. * @param \Magento\Backend\Block\Widget\Context $context
  36. * @param \Magento\Backend\Model\Auth\Session $backendSession
  37. * @param \Magento\Framework\Registry $registry
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Backend\Block\Widget\Context $context,
  42. \Magento\Backend\Model\Auth\Session $backendSession,
  43. \Magento\Framework\Registry $registry,
  44. array $data = []
  45. ) {
  46. $this->_backendSession = $backendSession;
  47. $this->_coreRegistry = $registry;
  48. parent::__construct($context, $data);
  49. }
  50. /**
  51. * Constructor
  52. *
  53. * @return void
  54. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  55. * @SuppressWarnings(PHPMD.NPathComplexity)
  56. */
  57. protected function _construct()
  58. {
  59. $this->_objectId = 'invoice_id';
  60. $this->_controller = 'adminhtml_order_invoice';
  61. $this->_mode = 'view';
  62. $this->_session = $this->_backendSession;
  63. parent::_construct();
  64. $this->buttonList->remove('save');
  65. $this->buttonList->remove('reset');
  66. $this->buttonList->remove('delete');
  67. if (!$this->getInvoice()) {
  68. return;
  69. }
  70. if ($this->_isAllowedAction(
  71. 'Magento_Sales::cancel'
  72. ) && $this->getInvoice()->canCancel() && !$this->_isPaymentReview()
  73. ) {
  74. $this->buttonList->add(
  75. 'cancel',
  76. [
  77. 'label' => __('Cancel'),
  78. 'class' => 'delete',
  79. 'onclick' => 'setLocation(\'' . $this->getCancelUrl() . '\')'
  80. ]
  81. );
  82. }
  83. if ($this->_isAllowedAction('Magento_Sales::emails')) {
  84. $this->addButton(
  85. 'send_notification',
  86. [
  87. 'label' => __('Send Email'),
  88. 'class' => 'send-email',
  89. 'onclick' => 'confirmSetLocation(\'' . __(
  90. 'Are you sure you want to send an invoice email to customer?'
  91. ) . '\', \'' . $this->getEmailUrl() . '\')'
  92. ]
  93. );
  94. }
  95. $orderPayment = $this->getInvoice()->getOrder()->getPayment();
  96. if ($this->_isAllowedAction('Magento_Sales::creditmemo') && $this->getInvoice()->getOrder()->canCreditmemo()) {
  97. if ($orderPayment->canRefundPartialPerInvoice() &&
  98. $this->getInvoice()->canRefund() &&
  99. $orderPayment->getAmountPaid() > $orderPayment->getAmountRefunded() ||
  100. $orderPayment->canRefund() && !$this->getInvoice()->getIsUsedForRefund()
  101. ) {
  102. $this->buttonList->add(
  103. 'capture',
  104. [ // capture?
  105. 'label' => __('Credit Memo'),
  106. 'class' => 'credit-memo',
  107. 'onclick' => 'setLocation(\'' . $this->getCreditMemoUrl() . '\')'
  108. ]
  109. );
  110. }
  111. }
  112. if ($this->_isAllowedAction(
  113. 'Magento_Sales::capture'
  114. ) && $this->getInvoice()->canCapture() && !$this->_isPaymentReview()
  115. ) {
  116. $this->buttonList->add(
  117. 'capture',
  118. [
  119. 'label' => __('Capture'),
  120. 'class' => 'capture',
  121. 'onclick' => 'setLocation(\'' . $this->getCaptureUrl() . '\')'
  122. ]
  123. );
  124. }
  125. if ($this->getInvoice()->canVoid()) {
  126. $this->buttonList->add(
  127. 'void',
  128. [
  129. 'label' => __('Void'),
  130. 'class' => 'void',
  131. 'onclick' => 'setLocation(\'' . $this->getVoidUrl() . '\')'
  132. ]
  133. );
  134. }
  135. if ($this->getInvoice()->getId()) {
  136. $this->buttonList->add(
  137. 'print',
  138. [
  139. 'label' => __('Print'),
  140. 'class' => 'print',
  141. 'onclick' => 'setLocation(\'' . $this->getPrintUrl() . '\')'
  142. ]
  143. );
  144. }
  145. }
  146. /**
  147. * Check whether order is under payment review
  148. *
  149. * @return bool
  150. */
  151. protected function _isPaymentReview()
  152. {
  153. $order = $this->getInvoice()->getOrder();
  154. return $order->canReviewPayment() || $order->canFetchPaymentReviewUpdate();
  155. }
  156. /**
  157. * Retrieve invoice model instance
  158. *
  159. * @return \Magento\Sales\Model\Order\Invoice
  160. */
  161. public function getInvoice()
  162. {
  163. return $this->_coreRegistry->registry('current_invoice');
  164. }
  165. /**
  166. * Get header text
  167. *
  168. * @return \Magento\Framework\Phrase
  169. */
  170. public function getHeaderText()
  171. {
  172. if ($this->getInvoice()->getEmailSent()) {
  173. $emailSent = __('The invoice email was sent.');
  174. } else {
  175. $emailSent = __('The invoice email wasn\'t sent.');
  176. }
  177. return __(
  178. 'Invoice #%1 | %2 | %4 (%3)',
  179. $this->getInvoice()->getIncrementId(),
  180. $this->getInvoice()->getStateName(),
  181. $emailSent,
  182. $this->formatDate(
  183. $this->_localeDate->date(new \DateTime($this->getInvoice()->getCreatedAt())),
  184. \IntlDateFormatter::MEDIUM,
  185. true
  186. )
  187. );
  188. }
  189. /**
  190. * Get back url
  191. *
  192. * @return string
  193. */
  194. public function getBackUrl()
  195. {
  196. return $this->getUrl(
  197. 'sales/order/view',
  198. [
  199. 'order_id' => $this->getInvoice() ? $this->getInvoice()->getOrderId() : null,
  200. 'active_tab' => 'order_invoices'
  201. ]
  202. );
  203. }
  204. /**
  205. * Get capture url
  206. *
  207. * @return string
  208. */
  209. public function getCaptureUrl()
  210. {
  211. return $this->getUrl('sales/*/capture', ['invoice_id' => $this->getInvoice()->getId()]);
  212. }
  213. /**
  214. * Get void url
  215. *
  216. * @return string
  217. */
  218. public function getVoidUrl()
  219. {
  220. return $this->getUrl('sales/*/void', ['invoice_id' => $this->getInvoice()->getId()]);
  221. }
  222. /**
  223. * Get cancel url
  224. *
  225. * @return string
  226. */
  227. public function getCancelUrl()
  228. {
  229. return $this->getUrl('sales/*/cancel', ['invoice_id' => $this->getInvoice()->getId()]);
  230. }
  231. /**
  232. * Get email url
  233. *
  234. * @return string
  235. */
  236. public function getEmailUrl()
  237. {
  238. return $this->getUrl(
  239. 'sales/*/email',
  240. ['order_id' => $this->getInvoice()->getOrder()->getId(), 'invoice_id' => $this->getInvoice()->getId()]
  241. );
  242. }
  243. /**
  244. * Get credit memo url
  245. *
  246. * @return string
  247. */
  248. public function getCreditMemoUrl()
  249. {
  250. return $this->getUrl(
  251. 'sales/order_creditmemo/start',
  252. ['order_id' => $this->getInvoice()->getOrder()->getId(), 'invoice_id' => $this->getInvoice()->getId()]
  253. );
  254. }
  255. /**
  256. * Get print url
  257. *
  258. * @return string
  259. */
  260. public function getPrintUrl()
  261. {
  262. return $this->getUrl('sales/*/print', ['invoice_id' => $this->getInvoice()->getId()]);
  263. }
  264. /**
  265. * Update back button url
  266. *
  267. * @param bool $flag
  268. * @return $this
  269. */
  270. public function updateBackButtonUrl($flag)
  271. {
  272. if ($flag) {
  273. if ($this->getInvoice()->getBackUrl()) {
  274. return $this->buttonList->update(
  275. 'back',
  276. 'onclick',
  277. 'setLocation(\'' . $this->getInvoice()->getBackUrl() . '\')'
  278. );
  279. }
  280. return $this->buttonList->update(
  281. 'back',
  282. 'onclick',
  283. 'setLocation(\'' . $this->getUrl('sales/invoice/') . '\')'
  284. );
  285. }
  286. return $this;
  287. }
  288. /**
  289. * Check whether is allowed action
  290. *
  291. * @param string $resourceId
  292. * @return bool
  293. */
  294. protected function _isAllowedAction($resourceId)
  295. {
  296. return $this->_authorization->isAllowed($resourceId);
  297. }
  298. }