Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

166 lines
6.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\Credit;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\QueryBuilder;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  7. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  10. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  19. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  20. use Lc\CaracoleBundle\Container\Credit\CreditHistoryContainer;
  21. use Lc\CaracoleBundle\Container\User\UserMerchantContainer;
  22. use Lc\CaracoleBundle\Controller\AdminControllerTrait;
  23. use Lc\CaracoleBundle\Definition\ActionDefinition;
  24. use Lc\CaracoleBundle\Factory\Credit\CreditHistoryFactory;
  25. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  26. use Lc\CaracoleBundle\Model\Credit\CreditHistoryModel;
  27. use Lc\CaracoleBundle\Model\Order\OrderPaymentModel;
  28. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  29. use Lc\SovBundle\Controller\AbstractAdminController;
  30. use Lc\SovBundle\Translation\TranslatorAdmin;
  31. abstract class CreditHistoryAdminController extends AbstractAdminController
  32. {
  33. use AdminControllerTrait;
  34. public function overrideGlobalActions(?ActionCollection $actions): void
  35. {
  36. parent::overrideGlobalActions($actions);
  37. if ($actions) {
  38. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  39. $creditControllerFqcn = $this->getControllerFqcnByInterface(CreditHistoryInterface::class);
  40. foreach ($actions as $action) {
  41. $url = $adminUrlGenerator
  42. ->setController($creditControllerFqcn)
  43. ->setAction(ActionDefinition::NEW)
  44. ->set('userMerchantId', $this->getUserMerchant()->getId())
  45. ->generateUrl();
  46. $action->setLinkUrl($url);
  47. }
  48. }
  49. }
  50. public function createEntity(string $entityFqcn)
  51. {
  52. return $this->get(CreditHistoryContainer::class)->getFactory()->create();
  53. }
  54. public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
  55. {
  56. $entityInstance->setUserMerchant($this->getUserMerchant());
  57. parent::persistEntity($entityManager, $entityInstance); // TODO: Change the autogenerated stub
  58. }
  59. protected function getUserMerchant(): UserMerchantInterface
  60. {
  61. $request = $this->get(AdminContextProvider::class)->getContext()->getRequest();
  62. return $this->get(UserMerchantContainer::class)->getStore()->getOneById(
  63. $request->get('userMerchantId')
  64. );
  65. }
  66. public function configureActions(Actions $actions): Actions
  67. {
  68. $actions->disable(ActionDefinition::DELETE, ActionDefinition::EDIT);
  69. return parent::configureActions($actions); // TODO: Change the autogenerated stub
  70. }
  71. public function configureCrud(Crud $crud): Crud
  72. {
  73. $crud->overrideTemplate('crud/index', '@LcCaracole/admin/credit/index_credithistory.html.twig');
  74. return $crud;
  75. }
  76. public function configureFields(string $pageName): iterable
  77. {
  78. $translatorAdmin = $this->get(TranslatorAdmin::class);
  79. return [
  80. IdField::new('id')->hideOnForm(),
  81. // @TODO : transChoices
  82. ChoiceField::new('type')->setChoices(
  83. array(
  84. $translatorAdmin->transField(
  85. 'typeOptions.' . CreditHistoryModel::TYPE_CREDIT,
  86. 'CreditHistory'
  87. ) => CreditHistoryModel::TYPE_CREDIT,
  88. $translatorAdmin->transField(
  89. 'typeOptions.' . CreditHistoryModel::TYPE_DEBIT,
  90. 'CreditHistory'
  91. ) => CreditHistoryModel::TYPE_DEBIT,
  92. )
  93. ),
  94. MoneyField::new('amount')->setCurrency('EUR'),
  95. DateField::new('paidAt'),
  96. // @TODO : transChoices
  97. ChoiceField::new('meanPayment')->setChoices(
  98. array(
  99. $translatorAdmin->transField(
  100. 'meanPaymentOptions.' . OrderPaymentModel::MEAN_PAYMENT_CASH,
  101. 'CreditHistory'
  102. ) => OrderPaymentModel::MEAN_PAYMENT_CASH,
  103. $translatorAdmin->transField(
  104. 'meanPaymentOptions.' . OrderPaymentModel::MEAN_PAYMENT_CHEQUE,
  105. 'CreditHistory'
  106. ) => OrderPaymentModel::MEAN_PAYMENT_CHEQUE,
  107. $translatorAdmin->transField(
  108. 'meanPaymentOptions.' . OrderPaymentModel::MEAN_PAYMENT_CREDIT,
  109. 'CreditHistory'
  110. ) => OrderPaymentModel::MEAN_PAYMENT_CREDIT,
  111. $translatorAdmin->transField(
  112. 'meanPaymentOptions.' . OrderPaymentModel::MEAN_PAYMENT_CREDIT_CARD,
  113. 'CreditHistory'
  114. ) => OrderPaymentModel::MEAN_PAYMENT_CREDIT_CARD,
  115. $translatorAdmin->transField(
  116. 'meanPaymentOptions.' . OrderPaymentModel::MEAN_PAYMENT_TRANSFER,
  117. 'CreditHistory'
  118. ) => OrderPaymentModel::MEAN_PAYMENT_TRANSFER,
  119. )
  120. ),
  121. TextField::new('reference'),
  122. TextField::new('comment'),
  123. ];
  124. }
  125. public function createIndexQueryBuilder(
  126. SearchDto $searchDto,
  127. EntityDto $entityDto,
  128. FieldCollection $fields,
  129. FilterCollection $filters
  130. ): QueryBuilder {
  131. $queryBuilder = parent::createIndexQueryBuilder(
  132. $searchDto,
  133. $entityDto,
  134. $fields,
  135. $filters
  136. );
  137. $queryBuilder->andWhere('entity.userMerchant = :userMerchant');
  138. $queryBuilder->setParameter('userMerchant', $searchDto->getRequest()->get('userMerchantId'));
  139. return $queryBuilder;
  140. }
  141. }