Info.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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\View;
  7. use Magento\Eav\Model\AttributeDataFactory;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Sales\Model\Order\Address;
  10. /**
  11. * @api
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @since 100.0.2
  14. */
  15. class Info extends \Magento\Sales\Block\Adminhtml\Order\AbstractOrder
  16. {
  17. /**
  18. * Customer service
  19. *
  20. * @var \Magento\Customer\Api\CustomerMetadataInterface
  21. */
  22. protected $metadata;
  23. /**
  24. * Group service
  25. *
  26. * @var \Magento\Customer\Api\GroupRepositoryInterface
  27. */
  28. protected $groupRepository;
  29. /**
  30. * Metadata element factory
  31. *
  32. * @var \Magento\Customer\Model\Metadata\ElementFactory
  33. */
  34. protected $_metadataElementFactory;
  35. /**
  36. * @var Address\Renderer
  37. */
  38. protected $addressRenderer;
  39. /**
  40. * Constructor
  41. *
  42. * @param \Magento\Backend\Block\Template\Context $context
  43. * @param \Magento\Framework\Registry $registry
  44. * @param \Magento\Sales\Helper\Admin $adminHelper
  45. * @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
  46. * @param \Magento\Customer\Api\CustomerMetadataInterface $metadata
  47. * @param \Magento\Customer\Model\Metadata\ElementFactory $elementFactory
  48. * @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
  49. * @param array $data
  50. */
  51. public function __construct(
  52. \Magento\Backend\Block\Template\Context $context,
  53. \Magento\Framework\Registry $registry,
  54. \Magento\Sales\Helper\Admin $adminHelper,
  55. \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
  56. \Magento\Customer\Api\CustomerMetadataInterface $metadata,
  57. \Magento\Customer\Model\Metadata\ElementFactory $elementFactory,
  58. \Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
  59. array $data = []
  60. ) {
  61. $this->groupRepository = $groupRepository;
  62. $this->metadata = $metadata;
  63. $this->_metadataElementFactory = $elementFactory;
  64. $this->addressRenderer = $addressRenderer;
  65. parent::__construct($context, $registry, $adminHelper, $data);
  66. }
  67. /**
  68. * Retrieve required options from parent
  69. *
  70. * @throws \Magento\Framework\Exception\LocalizedException
  71. * @return void
  72. */
  73. protected function _beforeToHtml()
  74. {
  75. if (!$this->getParentBlock()) {
  76. throw new \Magento\Framework\Exception\LocalizedException(
  77. __('Please correct the parent block for this block.')
  78. );
  79. }
  80. $this->setOrder($this->getParentBlock()->getOrder());
  81. foreach ($this->getParentBlock()->getOrderInfoData() as $key => $value) {
  82. $this->setDataUsingMethod($key, $value);
  83. }
  84. parent::_beforeToHtml();
  85. }
  86. /**
  87. * Get order store name
  88. *
  89. * @return null|string
  90. */
  91. public function getOrderStoreName()
  92. {
  93. if ($this->getOrder()) {
  94. $storeId = $this->getOrder()->getStoreId();
  95. if ($storeId === null) {
  96. $deleted = __(' [deleted]');
  97. return nl2br($this->getOrder()->getStoreName()) . $deleted;
  98. }
  99. $store = $this->_storeManager->getStore($storeId);
  100. $name = [$store->getWebsite()->getName(), $store->getGroup()->getName(), $store->getName()];
  101. return implode('<br/>', $name);
  102. }
  103. return null;
  104. }
  105. /**
  106. * Return name of the customer group.
  107. *
  108. * @return string
  109. */
  110. public function getCustomerGroupName()
  111. {
  112. if ($this->getOrder()) {
  113. $customerGroupId = $this->getOrder()->getCustomerGroupId();
  114. try {
  115. if ($customerGroupId !== null) {
  116. return $this->groupRepository->getById($customerGroupId)->getCode();
  117. }
  118. } catch (NoSuchEntityException $e) {
  119. return '';
  120. }
  121. }
  122. return '';
  123. }
  124. /**
  125. * Get URL to edit the customer.
  126. *
  127. * @return string
  128. */
  129. public function getCustomerViewUrl()
  130. {
  131. if ($this->getOrder()->getCustomerIsGuest() || !$this->getOrder()->getCustomerId()) {
  132. return '';
  133. }
  134. return $this->getUrl('customer/index/edit', ['id' => $this->getOrder()->getCustomerId()]);
  135. }
  136. /**
  137. * Get order view URL.
  138. *
  139. * @param int $orderId
  140. * @return string
  141. */
  142. public function getViewUrl($orderId)
  143. {
  144. return $this->getUrl('sales/order/view', ['order_id' => $orderId]);
  145. }
  146. /**
  147. * Find sort order for account data
  148. * Sort Order used as array key
  149. *
  150. * @param array $data
  151. * @param int $sortOrder
  152. * @return int
  153. */
  154. protected function _prepareAccountDataSortOrder(array $data, $sortOrder)
  155. {
  156. if (isset($data[$sortOrder])) {
  157. return $this->_prepareAccountDataSortOrder($data, $sortOrder + 1);
  158. }
  159. return $sortOrder;
  160. }
  161. /**
  162. * Return array of additional account data
  163. * Value is option style array
  164. *
  165. * @return array
  166. */
  167. public function getCustomerAccountData()
  168. {
  169. $accountData = [];
  170. $entityType = 'customer';
  171. /* @var \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute */
  172. foreach ($this->metadata->getAllAttributesMetadata($entityType) as $attribute) {
  173. if (!$attribute->isVisible() || $attribute->isSystem()) {
  174. continue;
  175. }
  176. $orderKey = sprintf('customer_%s', $attribute->getAttributeCode());
  177. $orderValue = $this->getOrder()->getData($orderKey);
  178. if ($orderValue != '') {
  179. $metadataElement = $this->_metadataElementFactory->create($attribute, $orderValue, $entityType);
  180. $value = $metadataElement->outputValue(AttributeDataFactory::OUTPUT_FORMAT_HTML);
  181. $sortOrder = $attribute->getSortOrder() + $attribute->isUserDefined() ? 200 : 0;
  182. $sortOrder = $this->_prepareAccountDataSortOrder($accountData, $sortOrder);
  183. $accountData[$sortOrder] = [
  184. 'label' => $attribute->getFrontendLabel(),
  185. 'value' => $this->escapeHtml($value, ['br']),
  186. ];
  187. }
  188. }
  189. ksort($accountData, SORT_NUMERIC);
  190. return $accountData;
  191. }
  192. /**
  193. * Get link to edit order address page
  194. *
  195. * @param \Magento\Sales\Model\Order\Address $address
  196. * @param string $label
  197. * @return string
  198. */
  199. public function getAddressEditLink($address, $label = '')
  200. {
  201. if ($this->_authorization->isAllowed('Magento_Sales::actions_edit')) {
  202. if (empty($label)) {
  203. $label = __('Edit');
  204. }
  205. $url = $this->getUrl('sales/order/address', ['address_id' => $address->getId()]);
  206. return '<a href="' . $this->escapeUrl($url) . '">' . $this->escapeHtml($label) . '</a>';
  207. }
  208. return '';
  209. }
  210. /**
  211. * Whether Customer IP address should be displayed on sales documents
  212. *
  213. * @return bool
  214. */
  215. public function shouldDisplayCustomerIp()
  216. {
  217. return !$this->_scopeConfig->isSetFlag(
  218. 'sales/general/hide_customer_ip',
  219. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  220. $this->getOrder()->getStoreId()
  221. );
  222. }
  223. /**
  224. * Check if is single store mode
  225. *
  226. * @return bool
  227. */
  228. public function isSingleStoreMode()
  229. {
  230. return $this->_storeManager->isSingleStoreMode();
  231. }
  232. /**
  233. * Get object created at date affected with object store timezone
  234. *
  235. * @param mixed $store
  236. * @param string $createdAt
  237. * @return \DateTime
  238. */
  239. public function getCreatedAtStoreDate($store, $createdAt)
  240. {
  241. return $this->_localeDate->scopeDate($store, $createdAt, true);
  242. }
  243. /**
  244. * Get timezone for store
  245. *
  246. * @param mixed $store
  247. * @return string
  248. */
  249. public function getTimezoneForStore($store)
  250. {
  251. return $this->_localeDate->getConfigTimezone(
  252. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  253. $store->getCode()
  254. );
  255. }
  256. /**
  257. * Get object created at date
  258. *
  259. * @param string $createdAt
  260. * @return \DateTime
  261. */
  262. public function getOrderAdminDate($createdAt)
  263. {
  264. return $this->_localeDate->date(new \DateTime($createdAt));
  265. }
  266. /**
  267. * Returns string with formatted address
  268. *
  269. * @param Address $address
  270. * @return null|string
  271. */
  272. public function getFormattedAddress(Address $address)
  273. {
  274. return $this->addressRenderer->format($address, 'html');
  275. }
  276. /**
  277. * @inheritdoc
  278. * @since 101.0.0
  279. */
  280. public function getChildHtml($alias = '', $useCache = true)
  281. {
  282. $layout = $this->getLayout();
  283. if ($alias || !$layout) {
  284. return parent::getChildHtml($alias, $useCache);
  285. }
  286. $childNames = $layout->getChildNames($this->getNameInLayout());
  287. $outputChildNames = array_diff($childNames, ['extra_customer_info']);
  288. $out = '';
  289. foreach ($outputChildNames as $childName) {
  290. $out .= $layout->renderElement($childName, $useCache);
  291. }
  292. return $out;
  293. }
  294. }