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

172 行
7.1KB

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