Form.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /**
  9. * Adminhtml sales order create form block
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Form extends \Magento\Sales\Block\Adminhtml\Order\Create\AbstractCreate
  15. {
  16. /**
  17. * Customer form factory
  18. *
  19. * @var \Magento\Customer\Model\Metadata\FormFactory
  20. */
  21. protected $_customerFormFactory;
  22. /**
  23. * Json encoder
  24. *
  25. * @var \Magento\Framework\Json\EncoderInterface
  26. */
  27. protected $_jsonEncoder;
  28. /**
  29. * Address service
  30. *
  31. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  32. */
  33. protected $customerRepository;
  34. /**
  35. * @var \Magento\Framework\Locale\CurrencyInterface
  36. */
  37. protected $_localeCurrency;
  38. /**
  39. * @var \Magento\Customer\Model\Address\Mapper
  40. */
  41. protected $addressMapper;
  42. /**
  43. * Initialize dependencies.
  44. *
  45. * @param \Magento\Backend\Block\Template\Context $context
  46. * @param \Magento\Backend\Model\Session\Quote $sessionQuote
  47. * @param \Magento\Sales\Model\AdminOrder\Create $orderCreate
  48. * @param PriceCurrencyInterface $priceCurrency
  49. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  50. * @param \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory
  51. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  52. * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
  53. * @param \Magento\Customer\Model\Address\Mapper $addressMapper
  54. * @param array $data
  55. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  56. */
  57. public function __construct(
  58. \Magento\Backend\Block\Template\Context $context,
  59. \Magento\Backend\Model\Session\Quote $sessionQuote,
  60. \Magento\Sales\Model\AdminOrder\Create $orderCreate,
  61. PriceCurrencyInterface $priceCurrency,
  62. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  63. \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory,
  64. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  65. \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
  66. \Magento\Customer\Model\Address\Mapper $addressMapper,
  67. array $data = []
  68. ) {
  69. $this->_jsonEncoder = $jsonEncoder;
  70. $this->_customerFormFactory = $customerFormFactory;
  71. $this->customerRepository = $customerRepository;
  72. $this->_localeCurrency = $localeCurrency;
  73. $this->addressMapper = $addressMapper;
  74. parent::__construct($context, $sessionQuote, $orderCreate, $priceCurrency, $data);
  75. }
  76. /**
  77. * Constructor
  78. *
  79. * @return void
  80. */
  81. protected function _construct()
  82. {
  83. parent::_construct();
  84. $this->setId('sales_order_create_form');
  85. }
  86. /**
  87. * Retrieve url for loading blocks
  88. *
  89. * @return string
  90. */
  91. public function getLoadBlockUrl()
  92. {
  93. return $this->getUrl('sales/*/loadBlock');
  94. }
  95. /**
  96. * Retrieve url for form submitting
  97. *
  98. * @return string
  99. */
  100. public function getSaveUrl()
  101. {
  102. return $this->getUrl('sales/*/save');
  103. }
  104. /**
  105. * Get customer selector display
  106. *
  107. * @return string
  108. */
  109. public function getCustomerSelectorDisplay()
  110. {
  111. $customerId = $this->getCustomerId();
  112. if ($customerId === null) {
  113. return 'block';
  114. }
  115. return 'none';
  116. }
  117. /**
  118. * Get store selector display
  119. *
  120. * @return string
  121. */
  122. public function getStoreSelectorDisplay()
  123. {
  124. $storeId = $this->getStoreId();
  125. $customerId = $this->getCustomerId();
  126. if ($customerId !== null && !$storeId) {
  127. return 'block';
  128. }
  129. return 'none';
  130. }
  131. /**
  132. * Get data selector display
  133. *
  134. * @return string
  135. */
  136. public function getDataSelectorDisplay()
  137. {
  138. $storeId = $this->getStoreId();
  139. $customerId = $this->getCustomerId();
  140. if ($customerId !== null && $storeId) {
  141. return 'block';
  142. }
  143. return 'none';
  144. }
  145. /**
  146. * Get order data jason
  147. *
  148. * @return string
  149. */
  150. public function getOrderDataJson()
  151. {
  152. $data = [];
  153. $this->_storeManager->setCurrentStore($this->getStoreId());
  154. if ($this->getCustomerId()) {
  155. $data['customer_id'] = $this->getCustomerId();
  156. $data['addresses'] = [];
  157. $addresses = $this->customerRepository->getById($this->getCustomerId())->getAddresses();
  158. foreach ($addresses as $address) {
  159. $addressForm = $this->_customerFormFactory->create(
  160. 'customer_address',
  161. 'adminhtml_customer_address',
  162. $this->addressMapper->toFlatArray($address)
  163. );
  164. $data['addresses'][$address->getId()] = $addressForm->outputData(
  165. \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON
  166. );
  167. }
  168. }
  169. if ($this->getStoreId() !== null) {
  170. $data['store_id'] = $this->getStoreId();
  171. $currency = $this->_localeCurrency->getCurrency($this->getStore()->getCurrentCurrencyCode());
  172. $symbol = $currency->getSymbol() ? $currency->getSymbol() : $currency->getShortName();
  173. $data['currency_symbol'] = $symbol;
  174. $data['shipping_method_reseted'] = !(bool)$this->getQuote()->getShippingAddress()->getShippingMethod();
  175. $data['payment_method'] = $this->getQuote()->getPayment()->getMethod();
  176. }
  177. $data['quote_id'] = $this->_sessionQuote->getQuoteId();
  178. return $this->_jsonEncoder->encode($data);
  179. }
  180. }