AbstractCreate.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Create;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Catalog\Pricing\Price\FinalPrice;
  10. /**
  11. * Adminhtml sales order create abstract block
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @SuppressWarnings(PHPMD.NumberOfChildren)
  15. */
  16. abstract class AbstractCreate extends \Magento\Backend\Block\Widget
  17. {
  18. /**
  19. * Session quote
  20. *
  21. * @var \Magento\Backend\Model\Session\Quote
  22. */
  23. protected $_sessionQuote;
  24. /**
  25. * Order create
  26. *
  27. * @var \Magento\Sales\Model\AdminOrder\Create
  28. */
  29. protected $_orderCreate;
  30. /**
  31. * @var PriceCurrencyInterface
  32. */
  33. protected $priceCurrency;
  34. /**
  35. * @param \Magento\Backend\Block\Template\Context $context
  36. * @param \Magento\Backend\Model\Session\Quote $sessionQuote
  37. * @param \Magento\Sales\Model\AdminOrder\Create $orderCreate
  38. * @param PriceCurrencyInterface $priceCurrency
  39. * @param array $data
  40. */
  41. public function __construct(
  42. \Magento\Backend\Block\Template\Context $context,
  43. \Magento\Backend\Model\Session\Quote $sessionQuote,
  44. \Magento\Sales\Model\AdminOrder\Create $orderCreate,
  45. PriceCurrencyInterface $priceCurrency,
  46. array $data = []
  47. ) {
  48. $this->priceCurrency = $priceCurrency;
  49. $this->_sessionQuote = $sessionQuote;
  50. $this->_orderCreate = $orderCreate;
  51. parent::__construct($context, $data);
  52. }
  53. /**
  54. * Retrieve create order model object
  55. *
  56. * @return \Magento\Sales\Model\AdminOrder\Create
  57. */
  58. public function getCreateOrderModel()
  59. {
  60. return $this->_orderCreate;
  61. }
  62. /**
  63. * Retrieve quote session object
  64. *
  65. * @return \Magento\Backend\Model\Session\Quote
  66. */
  67. protected function _getSession()
  68. {
  69. return $this->_sessionQuote;
  70. }
  71. /**
  72. * Retrieve quote model object
  73. *
  74. * @return \Magento\Quote\Model\Quote
  75. */
  76. public function getQuote()
  77. {
  78. return $this->_getSession()->getQuote();
  79. }
  80. /**
  81. * Retrieve customer identifier
  82. *
  83. * @return int
  84. */
  85. public function getCustomerId()
  86. {
  87. return $this->_getSession()->getCustomerId();
  88. }
  89. /**
  90. * Retrieve store model object
  91. *
  92. * @return \Magento\Store\Model\Store
  93. */
  94. public function getStore()
  95. {
  96. return $this->_getSession()->getStore();
  97. }
  98. /**
  99. * Retrieve store identifier
  100. *
  101. * @return int
  102. */
  103. public function getStoreId()
  104. {
  105. return $this->_getSession()->getStoreId();
  106. }
  107. /**
  108. * Retrieve formatted price
  109. *
  110. * @param float $value
  111. * @return string
  112. */
  113. public function formatPrice($value)
  114. {
  115. return $this->priceCurrency->format(
  116. $value,
  117. true,
  118. PriceCurrencyInterface::DEFAULT_PRECISION,
  119. $this->getStore()
  120. );
  121. }
  122. /**
  123. * @param Product $product
  124. * @return string
  125. */
  126. public function getItemPrice(Product $product)
  127. {
  128. $price = $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue();
  129. return $this->convertPrice($price);
  130. }
  131. /**
  132. * Convert price
  133. *
  134. * @param int|float $value
  135. * @param bool $format
  136. * @return string|int|float
  137. */
  138. public function convertPrice($value, $format = true)
  139. {
  140. return $format
  141. ? $this->priceCurrency->convertAndFormat(
  142. $value,
  143. true,
  144. PriceCurrencyInterface::DEFAULT_PRECISION,
  145. $this->getStore()
  146. )
  147. : $this->priceCurrency->convert($value, $this->getStore());
  148. }
  149. /**
  150. * If item is quote or wishlist we need to get product from it.
  151. *
  152. * @param $item
  153. *
  154. * @return Product
  155. */
  156. public function getProduct($item)
  157. {
  158. if ($item instanceof Product) {
  159. $product = $item;
  160. } else {
  161. $product = $item->getProduct();
  162. }
  163. return $product;
  164. }
  165. }