Items.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\Create;
  7. /**
  8. * Adminhtml credit memo items grid
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Items extends \Magento\Sales\Block\Adminhtml\Items\AbstractItems
  14. {
  15. /**
  16. * @var bool
  17. */
  18. protected $_canReturnToStock;
  19. /**
  20. * Sales data
  21. *
  22. * @var \Magento\Sales\Helper\Data
  23. */
  24. protected $_salesData;
  25. /**
  26. * @param \Magento\Backend\Block\Template\Context $context
  27. * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
  28. * @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
  29. * @param \Magento\Framework\Registry $registry
  30. * @param \Magento\Sales\Helper\Data $salesData
  31. * @param array $data
  32. */
  33. public function __construct(
  34. \Magento\Backend\Block\Template\Context $context,
  35. \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
  36. \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
  37. \Magento\Framework\Registry $registry,
  38. \Magento\Sales\Helper\Data $salesData,
  39. array $data = []
  40. ) {
  41. $this->_salesData = $salesData;
  42. parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data);
  43. }
  44. /**
  45. * Prepare child blocks
  46. *
  47. * @return $this
  48. */
  49. protected function _prepareLayout()
  50. {
  51. $onclick = "submitAndReloadArea($('creditmemo_item_container'),'" . $this->getUpdateUrl() . "')";
  52. $this->addChild(
  53. 'update_button',
  54. \Magento\Backend\Block\Widget\Button::class,
  55. ['label' => __('Update Qty\'s'), 'class' => 'update-button', 'onclick' => $onclick]
  56. );
  57. if ($this->getCreditmemo()->canRefund()) {
  58. if ($this->getCreditmemo()->getInvoice() && $this->getCreditmemo()->getInvoice()->getTransactionId()) {
  59. $this->addChild(
  60. 'submit_button',
  61. \Magento\Backend\Block\Widget\Button::class,
  62. [
  63. 'label' => __('Refund'),
  64. 'class' => 'save submit-button refund primary',
  65. 'onclick' => 'disableElements(\'submit-button\');submitCreditMemo()'
  66. ]
  67. );
  68. }
  69. $this->addChild(
  70. 'submit_offline',
  71. \Magento\Backend\Block\Widget\Button::class,
  72. [
  73. 'label' => __('Refund Offline'),
  74. 'class' => 'save submit-button primary',
  75. 'onclick' => 'disableElements(\'submit-button\');submitCreditMemoOffline()'
  76. ]
  77. );
  78. } else {
  79. $this->addChild(
  80. 'submit_button',
  81. \Magento\Backend\Block\Widget\Button::class,
  82. [
  83. 'label' => __('Refund Offline'),
  84. 'class' => 'save submit-button primary',
  85. 'onclick' => 'disableElements(\'submit-button\');submitCreditMemoOffline()'
  86. ]
  87. );
  88. }
  89. return parent::_prepareLayout();
  90. }
  91. /**
  92. * Retrieve invoice order
  93. *
  94. * @return \Magento\Sales\Model\Order
  95. */
  96. public function getOrder()
  97. {
  98. return $this->getCreditmemo()->getOrder();
  99. }
  100. /**
  101. * Retrieve source
  102. *
  103. * @return \Magento\Sales\Model\Order\Creditmemo
  104. */
  105. public function getSource()
  106. {
  107. return $this->getCreditmemo();
  108. }
  109. /**
  110. * Retrieve order totals block settings
  111. *
  112. * @return array
  113. */
  114. public function getOrderTotalData()
  115. {
  116. return [];
  117. }
  118. /**
  119. * Retrieve order total bar block data
  120. *
  121. * @return array
  122. */
  123. public function getOrderTotalbarData()
  124. {
  125. $this->setPriceDataObject($this->getOrder());
  126. $totalBarData = [];
  127. $totalBarData[] = [__('Paid Amount'), $this->displayPriceAttribute('total_invoiced'), false];
  128. $totalBarData[] = [__('Refund Amount'), $this->displayPriceAttribute('total_refunded'), false];
  129. $totalBarData[] = [__('Shipping Amount'), $this->displayPriceAttribute('shipping_invoiced'), false];
  130. $totalBarData[] = [__('Shipping Refund'), $this->displayPriceAttribute('shipping_refunded'), false];
  131. $totalBarData[] = [__('Order Grand Total'), $this->displayPriceAttribute('grand_total'), true];
  132. return $totalBarData;
  133. }
  134. /**
  135. * Retrieve credit memo model instance
  136. *
  137. * @return \Magento\Sales\Model\Order\Creditmemo
  138. */
  139. public function getCreditmemo()
  140. {
  141. return $this->_coreRegistry->registry('current_creditmemo');
  142. }
  143. /**
  144. * Check if allow to edit qty
  145. *
  146. * @return bool
  147. */
  148. public function canEditQty()
  149. {
  150. if ($this->getCreditmemo()->getOrder()->getPayment()->canRefund()) {
  151. return $this->getCreditmemo()->getOrder()->getPayment()->canRefundPartialPerInvoice();
  152. }
  153. return true;
  154. }
  155. /**
  156. * Get update button html
  157. *
  158. * @return string
  159. */
  160. public function getUpdateButtonHtml()
  161. {
  162. return $this->getChildHtml('update_button');
  163. }
  164. /**
  165. * Get update url
  166. *
  167. * @return string
  168. */
  169. public function getUpdateUrl()
  170. {
  171. return $this->getUrl(
  172. 'sales/*/updateQty',
  173. [
  174. 'order_id' => $this->getCreditmemo()->getOrderId(),
  175. 'invoice_id' => $this->getRequest()->getParam('invoice_id', null)
  176. ]
  177. );
  178. }
  179. /**
  180. * Whether to show 'Return to stock' column in creaditmemo grid
  181. *
  182. * @return bool
  183. */
  184. public function canReturnItemsToStock()
  185. {
  186. if ($this->_canReturnToStock === null) {
  187. $this->_canReturnToStock = $this->canReturnToStock();
  188. if ($this->_canReturnToStock) {
  189. $canReturnToStock = false;
  190. foreach ($this->getCreditmemo()->getAllItems() as $item) {
  191. $productId = $item->getOrderItem()->getProductId();
  192. $stockItem = $this->stockRegistry->getStockItem(
  193. $productId,
  194. $item->getOrderItem()->getStore()->getWebsiteId()
  195. );
  196. if ($stockItem->getManageStock()) {
  197. $canReturnToStock = true;
  198. $item->setCanReturnToStock($canReturnToStock);
  199. } else {
  200. $item->setCanReturnToStock(false);
  201. }
  202. }
  203. $this->_canReturnToStock = $canReturnToStock;
  204. $this->getCreditmemo()->getOrder()->setCanReturnToStock($this->_canReturnToStock);
  205. }
  206. }
  207. return $this->_canReturnToStock;
  208. }
  209. /**
  210. * Check allow to send new credit memo email
  211. *
  212. * @return bool
  213. */
  214. public function canSendCreditmemoEmail()
  215. {
  216. return $this->_salesData->canSendNewCreditmemoEmail($this->getOrder()->getStore()->getId());
  217. }
  218. }