Items.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\Create;
  7. /**
  8. * Adminhtml invoice items grid
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Items extends \Magento\Sales\Block\Adminhtml\Items\AbstractItems
  14. {
  15. /**
  16. * Disable submit button
  17. *
  18. * @var bool
  19. */
  20. protected $_disableSubmitButton = false;
  21. /**
  22. * Sales data
  23. *
  24. * @var \Magento\Sales\Helper\Data
  25. */
  26. protected $_salesData;
  27. /**
  28. * @param \Magento\Backend\Block\Template\Context $context
  29. * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
  30. * @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
  31. * @param \Magento\Framework\Registry $registry
  32. * @param \Magento\Sales\Helper\Data $salesData
  33. * @param array $data
  34. */
  35. public function __construct(
  36. \Magento\Backend\Block\Template\Context $context,
  37. \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
  38. \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
  39. \Magento\Framework\Registry $registry,
  40. \Magento\Sales\Helper\Data $salesData,
  41. array $data = []
  42. ) {
  43. $this->_salesData = $salesData;
  44. parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data);
  45. }
  46. /**
  47. * Prepare child blocks
  48. *
  49. * @return $this
  50. */
  51. protected function _beforeToHtml()
  52. {
  53. $onclick = "submitAndReloadArea($('invoice_item_container'),'" . $this->getUpdateUrl() . "')";
  54. $this->addChild(
  55. 'update_button',
  56. \Magento\Backend\Block\Widget\Button::class,
  57. ['class' => 'update-button', 'label' => __('Update Qty\'s'), 'onclick' => $onclick]
  58. );
  59. $this->_disableSubmitButton = true;
  60. $submitButtonClass = ' disabled';
  61. foreach ($this->getInvoice()->getAllItems() as $item) {
  62. /**
  63. * @see bug #14839
  64. */
  65. if ($item->getQty()/* || $this->getSource()->getData('base_grand_total')*/) {
  66. $this->_disableSubmitButton = false;
  67. $submitButtonClass = '';
  68. break;
  69. }
  70. }
  71. if ($this->getOrder()->getForcedShipmentWithInvoice()) {
  72. $_submitLabel = __('Submit Invoice and Shipment');
  73. } else {
  74. $_submitLabel = __('Submit Invoice');
  75. }
  76. $this->addChild(
  77. 'submit_button',
  78. \Magento\Backend\Block\Widget\Button::class,
  79. [
  80. 'label' => $_submitLabel,
  81. 'class' => 'save submit-button primary' . $submitButtonClass,
  82. 'onclick' => 'disableElements(\'submit-button\');$(\'edit_form\').submit()',
  83. 'disabled' => $this->_disableSubmitButton
  84. ]
  85. );
  86. return parent::_prepareLayout();
  87. }
  88. /**
  89. * Get is submit button disabled or not
  90. *
  91. * @return bool
  92. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  93. */
  94. public function getDisableSubmitButton()
  95. {
  96. return $this->_disableSubmitButton;
  97. }
  98. /**
  99. * Retrieve invoice order
  100. *
  101. * @return \Magento\Sales\Model\Order
  102. */
  103. public function getOrder()
  104. {
  105. return $this->getInvoice()->getOrder();
  106. }
  107. /**
  108. * Retrieve source
  109. *
  110. * @return \Magento\Sales\Model\Order\Invoice
  111. */
  112. public function getSource()
  113. {
  114. return $this->getInvoice();
  115. }
  116. /**
  117. * Retrieve invoice model instance
  118. *
  119. * @return \Magento\Sales\Model\Order\Invoice
  120. */
  121. public function getInvoice()
  122. {
  123. return $this->_coreRegistry->registry('current_invoice');
  124. }
  125. /**
  126. * Retrieve order totals block settings
  127. *
  128. * @return array
  129. */
  130. public function getOrderTotalData()
  131. {
  132. return [];
  133. }
  134. /**
  135. * Retrieve order totalbar block data
  136. *
  137. * @return array
  138. */
  139. public function getOrderTotalbarData()
  140. {
  141. $this->setPriceDataObject($this->getInvoice()->getOrder());
  142. $totalbarData = [];
  143. $totalbarData[] = [__('Paid Amount'), $this->displayPriceAttribute('amount_paid'), false];
  144. $totalbarData[] = [__('Refund Amount'), $this->displayPriceAttribute('amount_refunded'), false];
  145. $totalbarData[] = [__('Shipping Amount'), $this->displayPriceAttribute('shipping_captured'), false];
  146. $totalbarData[] = [__('Shipping Refund'), $this->displayPriceAttribute('shipping_refunded'), false];
  147. $totalbarData[] = [__('Order Grand Total'), $this->displayPriceAttribute('grand_total'), true];
  148. return $totalbarData;
  149. }
  150. /**
  151. * Format price
  152. *
  153. * @param float $price
  154. * @return string
  155. */
  156. public function formatPrice($price)
  157. {
  158. return $this->getInvoice()->getOrder()->formatPrice($price);
  159. }
  160. /**
  161. * Get update button html
  162. *
  163. * @return string
  164. */
  165. public function getUpdateButtonHtml()
  166. {
  167. return $this->getChildHtml('update_button');
  168. }
  169. /**
  170. * Get update url
  171. *
  172. * @return string
  173. */
  174. public function getUpdateUrl()
  175. {
  176. return $this->getUrl('sales/*/updateQty', ['order_id' => $this->getInvoice()->getOrderId()]);
  177. }
  178. /**
  179. * Check shipment availability for current invoice
  180. *
  181. * @return bool
  182. */
  183. public function canCreateShipment()
  184. {
  185. foreach ($this->getInvoice()->getAllItems() as $item) {
  186. if ($item->getOrderItem()->getQtyToShip()) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * Check if qty can be edited
  194. *
  195. * @return bool
  196. */
  197. public function canEditQty()
  198. {
  199. if ($this->getInvoice()->getOrder()->getPayment()->canCapture()) {
  200. return $this->getInvoice()->getOrder()->getPayment()->canCapturePartial();
  201. }
  202. return true;
  203. }
  204. /**
  205. * Check if capture operation is allowed in ACL
  206. *
  207. * @return bool
  208. */
  209. public function isCaptureAllowed()
  210. {
  211. return $this->_authorization->isAllowed('Magento_Sales::capture');
  212. }
  213. /**
  214. * Check if invoice can be captured
  215. *
  216. * @return bool
  217. */
  218. public function canCapture()
  219. {
  220. return $this->getInvoice()->canCapture();
  221. }
  222. /**
  223. * Check if gateway is associated with invoice order
  224. *
  225. * @return bool
  226. */
  227. public function isGatewayUsed()
  228. {
  229. return $this->getInvoice()->getOrder()->getPayment()->getMethodInstance()->isGateway();
  230. }
  231. /**
  232. * Check if new invoice emails can be sent
  233. *
  234. * @return bool
  235. */
  236. public function canSendInvoiceEmail()
  237. {
  238. return $this->_salesData->canSendNewInvoiceEmail($this->getOrder()->getStore()->getId());
  239. }
  240. }