Document.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Ui\Component\DataProvider;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Sales\Model\Order\Invoice;
  9. use Magento\Customer\Api\GroupRepositoryInterface;
  10. use Magento\Framework\Api\AttributeValueFactory;
  11. /**
  12. * Class Document
  13. */
  14. class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\Document
  15. {
  16. /**
  17. * @var string
  18. */
  19. private static $stateAttributeCode = 'state';
  20. /**
  21. * @var string
  22. */
  23. private static $customerGroupAttributeCode = 'customer_group_id';
  24. /**
  25. * @var GroupRepositoryInterface
  26. */
  27. private $groupRepository;
  28. /**
  29. * Document constructor.
  30. * @param AttributeValueFactory $attributeValueFactory
  31. * @param GroupRepositoryInterface $groupRepository
  32. */
  33. public function __construct(
  34. AttributeValueFactory $attributeValueFactory,
  35. GroupRepositoryInterface $groupRepository
  36. ) {
  37. parent::__construct($attributeValueFactory);
  38. $this->groupRepository = $groupRepository;
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function getCustomAttribute($attributeCode)
  44. {
  45. switch ($attributeCode) {
  46. case self::$stateAttributeCode:
  47. $this->setStateValue();
  48. break;
  49. case self::$customerGroupAttributeCode:
  50. $this->setCustomerGroupValue();
  51. break;
  52. }
  53. return parent::getCustomAttribute($attributeCode);
  54. }
  55. /**
  56. * Update invoice state value
  57. * Method set text label instead id value
  58. * @return void
  59. */
  60. private function setStateValue()
  61. {
  62. $value = $this->getData(self::$stateAttributeCode);
  63. /** @var \Magento\Framework\Phrase $state */
  64. $state = Invoice::getStates()[$value];
  65. $this->setCustomAttribute(self::$stateAttributeCode, $state->getText());
  66. }
  67. /**
  68. * Update customer group value
  69. * Method set group code instead id value
  70. * @return void
  71. */
  72. private function setCustomerGroupValue()
  73. {
  74. $value = $this->getData(self::$customerGroupAttributeCode);
  75. try {
  76. $group = $this->groupRepository->getById($value);
  77. $this->setCustomAttribute(self::$customerGroupAttributeCode, $group->getCode());
  78. } catch (NoSuchEntityException $e) {
  79. $this->setCustomAttribute(self::$customerGroupAttributeCode, 'N/A');
  80. }
  81. }
  82. }