You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
7.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Definition\Field\Reduction;
  3. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  4. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  5. use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
  6. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  7. use Lc\CaracoleBundle\Context\MerchantContextTrait;
  8. use Lc\SovBundle\Definition\Field\AbstractFieldDefinition;
  9. use Lc\CaracoleBundle\Field\AssociationField;
  10. use Lc\CaracoleBundle\Model\Config\TaxRateModel;
  11. use Lc\CaracoleBundle\Model\Config\UnitModel;
  12. use Lc\CaracoleBundle\Repository\Product\ProductCategoryStore;
  13. use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore;
  14. use Lc\CaracoleBundle\Repository\User\GroupUserStore;
  15. use Lc\CaracoleBundle\Repository\User\UserStore;
  16. use Lc\SovBundle\Field\BooleanField;
  17. use Lc\SovBundle\Translation\TranslatorAdmin;
  18. class ReductionCatalogFieldDefinition extends AbstractFieldDefinition
  19. {
  20. use MerchantContextTrait;
  21. protected GroupUserStore $groupUserStore;
  22. protected UserStore $userStore;
  23. protected ProductFamilyStore $productFamilyStore;
  24. protected ProductCategoryStore $productCategoryStore;
  25. public function __construct(
  26. TranslatorAdmin $translatorAdmin,
  27. GroupUserStore $groupUserStore,
  28. UserStore $userStore,
  29. ProductFamilyStore $productFamilyStore,
  30. ProductCategoryStore $productCategoryStore
  31. )
  32. {
  33. parent::__construct($translatorAdmin);
  34. $this->groupUserStore = $groupUserStore;
  35. $this->userStore = $userStore;
  36. $this->productFamilyStore = $productFamilyStore;
  37. $this->productCategoryStore = $productCategoryStore;
  38. }
  39. public function configureIndex(): array
  40. {
  41. return [
  42. 'id',
  43. 'title',
  44. 'value',
  45. 'status',
  46. 'isDisplayed'
  47. ];
  48. }
  49. public function configureForm(): array
  50. {
  51. return [
  52. 'title',
  53. 'behaviorTaxRate',
  54. 'unit',
  55. 'value',
  56. 'permanent',
  57. 'dateStart',
  58. 'dateEnd',
  59. 'usersActive',
  60. 'users',
  61. 'groupUsersActive',
  62. 'groupUsers',
  63. 'productCategoriesActive',
  64. 'productCategories',
  65. 'productFamiliesActive',
  66. 'productFamilies',
  67. 'status',
  68. 'isDisplayed'
  69. ];
  70. }
  71. public function configureFields(): array
  72. {
  73. $groupUserArray = $this->groupUserStore->setMerchant($this->merchant)->get();
  74. $usersArray = $this->userStore->setMerchant($this->merchant)->getJoinGroupUsers();
  75. $productFamilyArray = $this->productFamilyStore->setMerchant($this->merchant)->get();
  76. $productCategoryArray = $this->productCategoryStore->setMerchant($this->merchant)->get();
  77. return [
  78. 'title' => TextField::new('title')->setSortable(true),
  79. 'behaviorTaxRate' => ChoiceField::new('behaviorTaxRate')
  80. ->setFormTypeOption('required', true)
  81. ->setFormTypeOption('empty_data', TaxRateModel::BEHAVIOR_TAX_RATE_INCLUDED)
  82. ->setChoices(
  83. $this->translatorAdmin->transChoices(
  84. TaxRateModel::getBehaviorTaxRateChoices(),
  85. 'TaxRate',
  86. 'behaviorTaxRate'
  87. )
  88. ),
  89. 'unit' => ChoiceField::new('unit')
  90. ->setFormTypeOption('expanded', true)
  91. ->setFormTypeOption('required', true)
  92. ->setChoices(
  93. $this->translatorAdmin->transChoices(
  94. UnitModel::getUnitAmountChoices(),
  95. 'Unit',
  96. 'unit'
  97. )
  98. ),
  99. 'value' => NumberField::new('value')->setTemplatePath('@LcCaracole/admin/reduction/field/amount.html.twig'),
  100. 'permanent' => BooleanField::new('permanent'),
  101. 'dateStart' => DateTimeField::new('dateStart'),
  102. 'dateEnd' => DateTimeField::new('dateEnd'),
  103. 'isDisplayed'=> BooleanField::new('isDisplayed'),
  104. 'groupUsersActive' => BooleanField::new('groupUsersActive')->setFormTypeOption('mapped', false),
  105. 'groupUsers' => AssociationField::new('groupUsers')
  106. ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
  107. ->setFormTypeOption('choices', $groupUserArray),
  108. 'usersActive' => BooleanField::new('usersActive')->setFormTypeOption('mapped', false),
  109. 'users' => AssociationField::new('users')
  110. ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
  111. ->setFormTypeOption('choices', $usersArray)
  112. ->setFormTypeOption(
  113. 'choice_attr',
  114. function ($choice, $key, $value) {
  115. $data = array();
  116. foreach ($choice->getGroupUsers() as $groupUser) {
  117. $data[] = '_' . $groupUser->getId() . '_';
  118. }
  119. return ['data-group-users' => json_encode($data)];
  120. },
  121. ),
  122. 'productCategoriesActive' => BooleanField::new('productCategoriesActive')->setFormTypeOption('mapped', false),
  123. 'productCategories' => AssociationField::new('productCategories')
  124. ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
  125. ->setFormTypeOption('choice_label',
  126. // @TODO : attention, code dupliqué de ProductCategoriesFilter
  127. function ($category) {
  128. $isOffline = '';
  129. if ($category->getStatus() != 1) {
  130. $isOffline = " [Hors ligne]";
  131. }
  132. $section = ' [' . $category->getSection()->getTitle() . ']';;
  133. return $category . $section . $isOffline;
  134. })
  135. ->setFormTypeOption('choices', $productCategoryArray),
  136. 'productFamiliesActive' => BooleanField::new('productFamiliesActive')->setFormTypeOption('mapped', false),
  137. 'productFamilies' => AssociationField::new('productFamilies')
  138. ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
  139. ->setFormTypeOption('choices', $productFamilyArray)
  140. ->setFormTypeOption(
  141. 'choice_attr',
  142. function ($choice, $key, $value) {
  143. $data = array();
  144. foreach ($choice->getProductCategories() as $category) {
  145. $data[] = '_' . $category->getId() . '_';
  146. }
  147. return [
  148. 'data-product-categories' => json_encode($data),
  149. 'data-supplier' => $choice->getSupplier()->getId()
  150. ];
  151. }
  152. ),
  153. 'productFamily' => AssociationField::new('productFamily')
  154. ->setFormTypeOption('choices', $productFamilyArray)
  155. ];
  156. }
  157. }