您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

164 行
6.7KB

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