|
- <?php
-
- namespace Lc\CaracoleBundle\Definition\Field\Reduction;
-
- use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use Lc\CaracoleBundle\Context\MerchantContextTrait;
- use Lc\SovBundle\Definition\Field\AbstractFieldDefinition;
- use Lc\CaracoleBundle\Field\AssociationField;
- use Lc\CaracoleBundle\Model\Config\TaxRateModel;
- use Lc\CaracoleBundle\Model\Config\UnitModel;
- use Lc\CaracoleBundle\Repository\Product\ProductCategoryStore;
- use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore;
- use Lc\CaracoleBundle\Repository\User\GroupUserStore;
- use Lc\CaracoleBundle\Repository\User\UserStore;
- use Lc\SovBundle\Field\BooleanField;
- use Lc\SovBundle\Translation\TranslatorAdmin;
-
- class ReductionCatalogFieldDefinition extends AbstractFieldDefinition
- {
- use MerchantContextTrait;
-
- protected GroupUserStore $groupUserStore;
- protected UserStore $userStore;
- protected ProductFamilyStore $productFamilyStore;
- protected ProductCategoryStore $productCategoryStore;
-
- protected ?array $groupUserArray = null;
- protected ?array $usersArray= null;
- protected ?array $productFamilyArray= null;
- protected ?array $productCategoryArray= null;
-
- public function __construct(
- TranslatorAdmin $translatorAdmin,
- GroupUserStore $groupUserStore,
- UserStore $userStore,
- ProductFamilyStore $productFamilyStore,
- ProductCategoryStore $productCategoryStore
- )
- {
- parent::__construct($translatorAdmin);
- $this->groupUserStore = $groupUserStore;
- $this->userStore = $userStore;
- $this->productFamilyStore = $productFamilyStore;
- $this->productCategoryStore = $productCategoryStore;
- }
-
- public function configureIndex(): array
- {
-
- return [
- 'id',
- 'title',
- 'value',
- 'status',
- 'isDisplayed'
-
- ];
- }
-
- public function configureForm(): array
- {
- return [
- 'title',
- 'behaviorTaxRate',
- 'unit',
- 'value',
- 'permanent',
- 'dateStart',
- 'dateEnd',
- 'usersActive',
- 'users',
- 'groupUsersActive',
- 'groupUsers',
- 'productCategoriesActive',
- 'productCategories',
- 'productFamiliesActive',
- 'productFamilies',
- 'status',
- 'isDisplayed'
-
- ];
- }
-
- public function configureFields(): array
- {
- if(is_null($this->groupUserArray)) {
- $this->groupUserArray = $this->groupUserStore->setMerchant($this->merchant)->get();
- }
-
- if(is_null($this->usersArray)) {
- $this->usersArray = $this->userStore->setMerchant($this->merchant)->getJoinGroupUsers();
- }
-
- if(is_null($this->productFamilyArray)) {
- $this->productFamilyArray = $this->productFamilyStore->setMerchant($this->merchant)->get();
- }
-
- if(is_null($this->productCategoryArray)) {
- $this->productCategoryArray = $this->productCategoryStore->setMerchant($this->merchant)->get();
- }
-
- return [
- 'title' => TextField::new('title')->setSortable(true),
- 'behaviorTaxRate' => ChoiceField::new('behaviorTaxRate')
- ->setFormTypeOption('required', true)
- ->setFormTypeOption('empty_data', TaxRateModel::BEHAVIOR_TAX_RATE_INCLUDED)
- ->setChoices(
- $this->translatorAdmin->transChoices(
- TaxRateModel::getBehaviorTaxRateChoices(),
- 'TaxRate',
- 'behaviorTaxRate'
- )
- ),
- 'unit' => ChoiceField::new('unit')
- ->setFormTypeOption('expanded', true)
- ->setFormTypeOption('required', true)
- ->setChoices(
- $this->translatorAdmin->transChoices(
- UnitModel::getUnitAmountChoices(),
- 'Unit',
- 'unit'
- )
- ),
- 'value' => NumberField::new('value')->setTemplatePath('@LcCaracole/admin/reduction/field/amount.html.twig'),
- 'permanent' => BooleanField::new('permanent'),
- 'dateStart' => DateTimeField::new('dateStart'),
- 'dateEnd' => DateTimeField::new('dateEnd'),
- 'isDisplayed'=> BooleanField::new('isDisplayed'),
- 'groupUsersActive' => BooleanField::new('groupUsersActive')->setFormTypeOption('mapped', false),
- 'groupUsers' => AssociationField::new('groupUsers')
- ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
- ->setFormTypeOption('choices', $this->groupUserArray),
-
- 'usersActive' => BooleanField::new('usersActive')->setFormTypeOption('mapped', false),
-
- 'users' => AssociationField::new('users')
- ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
- ->setFormTypeOption('choices', $this->usersArray)
- ->setFormTypeOption(
- 'choice_attr',
- function ($choice, $key, $value) {
- $data = array();
- foreach ($choice->getGroupUsers() as $groupUser) {
- $data[] = '_' . $groupUser->getId() . '_';
- }
-
- return ['data-group-users' => json_encode($data)];
- },
- ),
-
-
- 'productCategoriesActive' => BooleanField::new('productCategoriesActive')->setFormTypeOption('mapped', false),
- 'productCategories' => AssociationField::new('productCategories')
- ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
- ->setFormTypeOption('choice_label',
- // @TODO : attention, code dupliqué de ProductCategoriesFilter
- function ($category) {
- $isOffline = '';
- if ($category->getStatus() != 1) {
- $isOffline = " [Hors ligne]";
- }
- $section = ' [' . $category->getSection()->getTitle() . ']';;
- return $category . $section . $isOffline;
- })
- ->setFormTypeOption('choices', $this->productCategoryArray),
-
-
- 'productFamiliesActive' => BooleanField::new('productFamiliesActive')->setFormTypeOption('mapped', false),
- 'productFamilies' => AssociationField::new('productFamilies')
- ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
- ->setFormTypeOption('choices', $this->productFamilyArray)
- ->setFormTypeOption(
- 'choice_attr',
- function ($choice, $key, $value) {
- $data = array();
- foreach ($choice->getProductCategories() as $category) {
- $data[] = '_' . $category->getId() . '_';
- }
- return [
- 'data-product-categories' => json_encode($data),
- 'data-supplier' => $choice->getSupplier()->getId()
- ];
- }
- ),
- 'productFamily' => AssociationField::new('productFamily')
- ->setFormTypeOption('choices', $this->productFamilyArray)
- ];
- }
- }
|