View.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /**
  3. * @category Magento
  4. * @package Magento_Sales
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Sales\Block\Adminhtml\Order;
  9. /**
  10. * Adminhtml sales order view
  11. * @api
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @since 100.0.2
  14. */
  15. class View extends \Magento\Backend\Block\Widget\Form\Container
  16. {
  17. /**
  18. * Block group
  19. *
  20. * @var string
  21. */
  22. protected $_blockGroup = 'Magento_Sales';
  23. /**
  24. * Core registry
  25. *
  26. * @var \Magento\Framework\Registry
  27. */
  28. protected $_coreRegistry = null;
  29. /**
  30. * Sales config
  31. *
  32. * @var \Magento\Sales\Model\Config
  33. */
  34. protected $_salesConfig;
  35. /**
  36. * Reorder helper
  37. *
  38. * @var \Magento\Sales\Helper\Reorder
  39. */
  40. protected $_reorderHelper;
  41. /**
  42. * @param \Magento\Backend\Block\Widget\Context $context
  43. * @param \Magento\Framework\Registry $registry
  44. * @param \Magento\Sales\Model\Config $salesConfig
  45. * @param \Magento\Sales\Helper\Reorder $reorderHelper
  46. * @param array $data
  47. */
  48. public function __construct(
  49. \Magento\Backend\Block\Widget\Context $context,
  50. \Magento\Framework\Registry $registry,
  51. \Magento\Sales\Model\Config $salesConfig,
  52. \Magento\Sales\Helper\Reorder $reorderHelper,
  53. array $data = []
  54. ) {
  55. $this->_reorderHelper = $reorderHelper;
  56. $this->_coreRegistry = $registry;
  57. $this->_salesConfig = $salesConfig;
  58. parent::__construct($context, $data);
  59. }
  60. /**
  61. * Constructor
  62. *
  63. * @return void
  64. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  65. * @SuppressWarnings(PHPMD.NPathComplexity)
  66. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  67. */
  68. protected function _construct()
  69. {
  70. $this->_objectId = 'order_id';
  71. $this->_controller = 'adminhtml_order';
  72. $this->_mode = 'view';
  73. parent::_construct();
  74. $this->removeButton('delete');
  75. $this->removeButton('reset');
  76. $this->removeButton('save');
  77. $this->setId('sales_order_view');
  78. $order = $this->getOrder();
  79. if (!$order) {
  80. return;
  81. }
  82. if ($this->_isAllowedAction('Magento_Sales::actions_edit') && $order->canEdit()) {
  83. $onclickJs = 'jQuery(\'#order_edit\').orderEditDialog({message: \''
  84. . $this->getEditMessage($order) . '\', url: \'' . $this->getEditUrl()
  85. . '\'}).orderEditDialog(\'showDialog\');';
  86. $this->addButton(
  87. 'order_edit',
  88. [
  89. 'label' => __('Edit'),
  90. 'class' => 'edit primary',
  91. 'onclick' => $onclickJs,
  92. 'data_attribute' => [
  93. 'mage-init' => '{"orderEditDialog":{}}',
  94. ]
  95. ]
  96. );
  97. }
  98. if ($this->_isAllowedAction('Magento_Sales::cancel') && $order->canCancel()) {
  99. $this->addButton(
  100. 'order_cancel',
  101. [
  102. 'label' => __('Cancel'),
  103. 'class' => 'cancel',
  104. 'id' => 'order-view-cancel-button',
  105. 'data_attribute' => [
  106. 'url' => $this->getCancelUrl()
  107. ]
  108. ]
  109. );
  110. }
  111. if ($this->_isAllowedAction('Magento_Sales::emails') && !$order->isCanceled()) {
  112. $message = __('Are you sure you want to send an order email to customer?');
  113. $this->addButton(
  114. 'send_notification',
  115. [
  116. 'label' => __('Send Email'),
  117. 'class' => 'send-email',
  118. 'onclick' => "confirmSetLocation('{$message}', '{$this->getEmailUrl()}')"
  119. ]
  120. );
  121. }
  122. if ($this->_isAllowedAction('Magento_Sales::creditmemo') && $order->canCreditmemo()) {
  123. $message = __(
  124. 'This will create an offline refund. ' .
  125. 'To create an online refund, open an invoice and create credit memo for it. Do you want to continue?'
  126. );
  127. $onClick = "setLocation('{$this->getCreditmemoUrl()}')";
  128. if ($order->getPayment()->getMethodInstance()->isGateway()) {
  129. $onClick = "confirmSetLocation('{$message}', '{$this->getCreditmemoUrl()}')";
  130. }
  131. $this->addButton(
  132. 'order_creditmemo',
  133. ['label' => __('Credit Memo'), 'onclick' => $onClick, 'class' => 'credit-memo']
  134. );
  135. }
  136. // invoice action intentionally
  137. if ($this->_isAllowedAction('Magento_Sales::invoice') && $order->canVoidPayment()) {
  138. $message = __('Are you sure you want to void the payment?');
  139. $this->addButton(
  140. 'void_payment',
  141. [
  142. 'label' => __('Void'),
  143. 'onclick' => "confirmSetLocation('{$message}', '{$this->getVoidPaymentUrl()}')"
  144. ]
  145. );
  146. }
  147. if ($this->_isAllowedAction('Magento_Sales::hold') && $order->canHold()) {
  148. $this->addButton(
  149. 'order_hold',
  150. [
  151. 'label' => __('Hold'),
  152. 'class' => __('hold'),
  153. 'id' => 'order-view-hold-button',
  154. 'data_attribute' => [
  155. 'url' => $this->getHoldUrl()
  156. ]
  157. ]
  158. );
  159. }
  160. if ($this->_isAllowedAction('Magento_Sales::unhold') && $order->canUnhold()) {
  161. $this->addButton(
  162. 'order_unhold',
  163. [
  164. 'label' => __('Unhold'),
  165. 'class' => __('unhold'),
  166. 'id' => 'order-view-unhold-button',
  167. 'data_attribute' => [
  168. 'url' => $this->getUnholdUrl()
  169. ]
  170. ]
  171. );
  172. }
  173. if ($this->_isAllowedAction('Magento_Sales::review_payment')) {
  174. if ($order->canReviewPayment()) {
  175. $message = __('Are you sure you want to accept this payment?');
  176. $this->addButton(
  177. 'accept_payment',
  178. [
  179. 'label' => __('Accept Payment'),
  180. 'onclick' => "confirmSetLocation('{$message}', '{$this->getReviewPaymentUrl('accept')}')"
  181. ]
  182. );
  183. $message = __('Are you sure you want to deny this payment?');
  184. $this->addButton(
  185. 'deny_payment',
  186. [
  187. 'label' => __('Deny Payment'),
  188. 'onclick' => "confirmSetLocation('{$message}', '{$this->getReviewPaymentUrl('deny')}')"
  189. ]
  190. );
  191. }
  192. if ($order->canFetchPaymentReviewUpdate()) {
  193. $this->addButton(
  194. 'get_review_payment_update',
  195. [
  196. 'label' => __('Get Payment Update'),
  197. 'onclick' => 'setLocation(\'' . $this->getReviewPaymentUrl('update') . '\')'
  198. ]
  199. );
  200. }
  201. }
  202. if ($this->_isAllowedAction('Magento_Sales::invoice') && $order->canInvoice()) {
  203. $_label = $order->getForcedShipmentWithInvoice() ? __('Invoice and Ship') : __('Invoice');
  204. $this->addButton(
  205. 'order_invoice',
  206. [
  207. 'label' => $_label,
  208. 'onclick' => 'setLocation(\'' . $this->getInvoiceUrl() . '\')',
  209. 'class' => 'invoice'
  210. ]
  211. );
  212. }
  213. if ($this->_isAllowedAction(
  214. 'Magento_Sales::ship'
  215. ) && $order->canShip() && !$order->getForcedShipmentWithInvoice()
  216. ) {
  217. $this->addButton(
  218. 'order_ship',
  219. [
  220. 'label' => __('Ship'),
  221. 'onclick' => 'setLocation(\'' . $this->getShipUrl() . '\')',
  222. 'class' => 'ship'
  223. ]
  224. );
  225. }
  226. if ($this->_isAllowedAction(
  227. 'Magento_Sales::reorder'
  228. ) && $this->_reorderHelper->isAllowed(
  229. $order->getStore()
  230. ) && $order->canReorderIgnoreSalable()
  231. ) {
  232. $this->addButton(
  233. 'order_reorder',
  234. [
  235. 'label' => __('Reorder'),
  236. 'onclick' => 'setLocation(\'' . $this->getReorderUrl() . '\')',
  237. 'class' => 'reorder'
  238. ]
  239. );
  240. }
  241. }
  242. /**
  243. * Retrieve order model object
  244. *
  245. * @return \Magento\Sales\Model\Order
  246. */
  247. public function getOrder()
  248. {
  249. return $this->_coreRegistry->registry('sales_order');
  250. }
  251. /**
  252. * Retrieve Order Identifier
  253. *
  254. * @return int
  255. */
  256. public function getOrderId()
  257. {
  258. return $this->getOrder() ? $this->getOrder()->getId() : null;
  259. }
  260. /**
  261. * Get header text
  262. *
  263. * @return \Magento\Framework\Phrase
  264. */
  265. public function getHeaderText()
  266. {
  267. $_extOrderId = $this->getOrder()->getExtOrderId();
  268. if ($_extOrderId) {
  269. $_extOrderId = '[' . $_extOrderId . '] ';
  270. } else {
  271. $_extOrderId = '';
  272. }
  273. return __(
  274. 'Order # %1 %2 | %3',
  275. $this->getOrder()->getRealOrderId(),
  276. $_extOrderId,
  277. $this->formatDate(
  278. $this->_localeDate->date(new \DateTime($this->getOrder()->getCreatedAt())),
  279. \IntlDateFormatter::MEDIUM,
  280. true
  281. )
  282. );
  283. }
  284. /**
  285. * URL getter
  286. *
  287. * @param string $params
  288. * @param array $params2
  289. * @return string
  290. */
  291. public function getUrl($params = '', $params2 = [])
  292. {
  293. $params2['order_id'] = $this->getOrderId();
  294. return parent::getUrl($params, $params2);
  295. }
  296. /**
  297. * Edit URL getter
  298. *
  299. * @return string
  300. */
  301. public function getEditUrl()
  302. {
  303. return $this->getUrl('sales/order_edit/start');
  304. }
  305. /**
  306. * Email URL getter
  307. *
  308. * @return string
  309. */
  310. public function getEmailUrl()
  311. {
  312. return $this->getUrl('sales/*/email');
  313. }
  314. /**
  315. * Cancel URL getter
  316. *
  317. * @return string
  318. */
  319. public function getCancelUrl()
  320. {
  321. return $this->getUrl('sales/*/cancel');
  322. }
  323. /**
  324. * Invoice URL getter
  325. *
  326. * @return string
  327. */
  328. public function getInvoiceUrl()
  329. {
  330. return $this->getUrl('sales/order_invoice/start');
  331. }
  332. /**
  333. * Credit memo URL getter
  334. *
  335. * @return string
  336. */
  337. public function getCreditmemoUrl()
  338. {
  339. return $this->getUrl('sales/order_creditmemo/start');
  340. }
  341. /**
  342. * Hold URL getter
  343. *
  344. * @return string
  345. */
  346. public function getHoldUrl()
  347. {
  348. return $this->getUrl('sales/*/hold');
  349. }
  350. /**
  351. * Unhold URL getter
  352. *
  353. * @return string
  354. */
  355. public function getUnholdUrl()
  356. {
  357. return $this->getUrl('sales/*/unhold');
  358. }
  359. /**
  360. * Ship URL getter
  361. *
  362. * @return string
  363. */
  364. public function getShipUrl()
  365. {
  366. return $this->getUrl('adminhtml/order_shipment/start');
  367. }
  368. /**
  369. * Comment URL getter
  370. *
  371. * @return string
  372. */
  373. public function getCommentUrl()
  374. {
  375. return $this->getUrl('sales/*/comment');
  376. }
  377. /**
  378. * Reorder URL getter
  379. *
  380. * @return string
  381. */
  382. public function getReorderUrl()
  383. {
  384. return $this->getUrl('sales/order_create/reorder');
  385. }
  386. /**
  387. * Payment void URL getter
  388. *
  389. * @return string
  390. */
  391. public function getVoidPaymentUrl()
  392. {
  393. return $this->getUrl('sales/*/voidPayment');
  394. }
  395. /**
  396. * Check permission for passed action
  397. *
  398. * @param string $resourceId
  399. * @return bool
  400. */
  401. protected function _isAllowedAction($resourceId)
  402. {
  403. return $this->_authorization->isAllowed($resourceId);
  404. }
  405. /**
  406. * Return back url for view grid
  407. *
  408. * @return string
  409. */
  410. public function getBackUrl()
  411. {
  412. if ($this->getOrder() && $this->getOrder()->getBackUrl()) {
  413. return $this->getOrder()->getBackUrl();
  414. }
  415. return $this->getUrl('sales/*/');
  416. }
  417. /**
  418. * Payment review URL getter
  419. *
  420. * @param string $action
  421. * @return string
  422. */
  423. public function getReviewPaymentUrl($action)
  424. {
  425. return $this->getUrl('sales/*/reviewPayment', ['action' => $action]);
  426. }
  427. /**
  428. * @param \Magento\Sales\Model\Order $order
  429. * @return \Magento\Framework\Phrase
  430. */
  431. protected function getEditMessage($order)
  432. {
  433. // see if order has non-editable products as items
  434. $nonEditableTypes = $this->getNonEditableTypes($order);
  435. if (!empty($nonEditableTypes)) {
  436. return __(
  437. 'This order contains (%1) items and therefore cannot be edited through the admin interface. ' .
  438. 'If you wish to continue editing, the (%2) items will be removed, ' .
  439. ' the order will be canceled and a new order will be placed.',
  440. implode(', ', $nonEditableTypes),
  441. implode(', ', $nonEditableTypes)
  442. );
  443. }
  444. return __('Are you sure? This order will be canceled and a new one will be created instead.');
  445. }
  446. /**
  447. * @param \Magento\Sales\Model\Order $order
  448. * @return array
  449. */
  450. protected function getNonEditableTypes($order)
  451. {
  452. return array_keys(
  453. $this->getOrder()->getResource()->aggregateProductsByTypes(
  454. $order->getId(),
  455. $this->_salesConfig->getAvailableProductTypes(),
  456. false
  457. )
  458. );
  459. }
  460. }