No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

163 líneas
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\Model\Credit\CreditHistoryInterface;
  22. use Lc\CaracoleBundle\Model\Credit\CreditHistoryModel;
  23. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  24. use Lc\SovBundle\Controller\AbstractAdminController;
  25. use Lc\SovBundle\Translation\TranslatorAdmin;
  26. abstract class CreditHistoryAdminController extends AbstractAdminController
  27. {
  28. use AdminControllerTrait;
  29. protected $em;
  30. protected $translatorAdmin;
  31. public function __construct(EntityManagerInterface $entityManager, TranslatorAdmin $translatorAdmin)
  32. {
  33. $this->em = $entityManager;
  34. $this->translatorAdmin = $translatorAdmin;
  35. }
  36. public function overrideGlobalActions(?ActionCollection $actions): void
  37. {
  38. parent::overrideGlobalActions($actions);
  39. if ($actions) {
  40. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  41. $creditControllerFqcn = $this->getControllerFqcnByInterface(CreditHistoryInterface::class);
  42. foreach ($actions as $action) {
  43. $url = $adminUrlGenerator
  44. ->setController($creditControllerFqcn)
  45. ->setAction(Action::NEW)
  46. ->set('userMerchantId', $this->getUserMerchant()->getId())
  47. ->generateUrl();
  48. $action->setLinkUrl($url);
  49. }
  50. }
  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. $userMerchant = $this->get('em')->getRepository(UserMerchantInterface::class)->find(
  61. $request->get('userMerchantId')
  62. );
  63. return $userMerchant;
  64. }
  65. public function configureActions(Actions $actions): Actions
  66. {
  67. $actions->disable(Action::DELETE, Action::EDIT);
  68. return parent::configureActions($actions); // TODO: Change the autogenerated stub
  69. }
  70. public function configureCrud(Crud $crud): Crud
  71. {
  72. $crud->overrideTemplate('crud/index', '@LcCaracole/admin/credit/index_credithistory.html.twig');
  73. return $crud;
  74. }
  75. public function configureFields(string $pageName): iterable
  76. {
  77. return [
  78. IdField::new('id')->hideOnForm(),
  79. ChoiceField::new('type')->setChoices(
  80. array(
  81. $this->translatorAdmin->transField(
  82. 'typeOptions.'.CreditHistoryModel::TYPE_CREDIT,
  83. 'CreditHistory'
  84. ) => CreditHistoryModel::TYPE_CREDIT,
  85. $this->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. $this->translatorAdmin->transField(
  96. 'meanPaymentOptions.'.CreditHistoryModel::MEAN_PAYMENT_CASH,
  97. 'CreditHistory'
  98. ) => CreditHistoryModel::MEAN_PAYMENT_CASH,
  99. $this->translatorAdmin->transField(
  100. 'meanPaymentOptions.'.CreditHistoryModel::MEAN_PAYMENT_CHEQUE,
  101. 'CreditHistory'
  102. ) => CreditHistoryModel::MEAN_PAYMENT_CHEQUE,
  103. $this->translatorAdmin->transField(
  104. 'meanPaymentOptions.'.CreditHistoryModel::MEAN_PAYMENT_CREDIT,
  105. 'CreditHistory'
  106. ) => CreditHistoryModel::MEAN_PAYMENT_CREDIT,
  107. $this->translatorAdmin->transField(
  108. 'meanPaymentOptions.'.CreditHistoryModel::MEAN_PAYMENT_CREDIT_CARD,
  109. 'CreditHistory'
  110. ) => CreditHistoryModel::MEAN_PAYMENT_CREDIT_CARD,
  111. $this->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. }