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.

192 lines
7.6KB

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