Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

162 rindas
6.6KB

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