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.

128 líneas
4.7KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\Credit;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  18. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  19. use Lc\CaracoleBundle\Container\Credit\CreditHistoryContainer;
  20. use Lc\CaracoleBundle\Container\User\UserMerchantContainer;
  21. use Lc\CaracoleBundle\Controller\AbstractAdminController;
  22. use Lc\CaracoleBundle\Definition\ActionDefinition;
  23. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  24. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  25. use Lc\CaracoleBundle\Solver\Credit\CreditHistorySolver;
  26. use Lc\CaracoleBundle\Solver\Order\OrderPaymentSolver;
  27. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  28. use Lc\SovBundle\Translation\TranslatorAdmin;
  29. abstract class CreditHistoryAdminController extends AbstractAdminController
  30. {
  31. public function getRepositoryQuery(): RepositoryQueryInterface
  32. {
  33. return $this->get(CreditHistoryContainer::class)->getRepositoryQuery();
  34. }
  35. public function overrideGlobalActions(?ActionCollection $actions): void
  36. {
  37. parent::overrideGlobalActions($actions);
  38. if ($actions) {
  39. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  40. $creditControllerFqcn = $this->getControllerFqcnByInterface(CreditHistoryInterface::class);
  41. foreach ($actions as $action) {
  42. $url = $adminUrlGenerator
  43. ->setController($creditControllerFqcn)
  44. ->setAction(ActionDefinition::NEW)
  45. ->set('userMerchantId', $this->getUserMerchant()->getId())
  46. ->generateUrl();
  47. $action->setLinkUrl($url);
  48. }
  49. }
  50. }
  51. public function createEntity(string $entityFqcn)
  52. {
  53. return $this->get(CreditHistoryContainer::class)->getFactory()->createBase($this->getUserMerchant());
  54. }
  55. protected function getUserMerchant(): UserMerchantInterface
  56. {
  57. $request = $this->get(AdminContextProvider::class)->getContext()->getRequest();
  58. return $this->get(UserMerchantContainer::class)->getStore()->getOneById(
  59. $request->get('userMerchantId')
  60. );
  61. }
  62. public function configureActions(Actions $actions): Actions
  63. {
  64. $actions = parent::configureActions($actions);
  65. $actions->disable(ActionDefinition::DELETE, ActionDefinition::EDIT, ActionDefinition::DUPLICATE);
  66. return $actions;
  67. }
  68. public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
  69. {
  70. if ($this->get(CreditHistoryContainer::class)->getBuilder()->save($entityInstance)) {
  71. $this->addFlashTranslator('success', 'saveCreditHistory');
  72. } else {
  73. $this->addFlashTranslator('error', 'saveCreditHistory');
  74. }
  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 configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
  82. {
  83. $responseParameters = parent::configureResponseParameters($responseParameters);
  84. $responseParameters->set('user_merchant', $this->getUserMerchant());
  85. return $responseParameters;
  86. }
  87. public function configureFields(string $pageName): iterable
  88. {
  89. return $this->getCreditHistoryContainer()->getFieldDefinition()->getFields($pageName);
  90. }
  91. public function createIndexRepositoryQuery(
  92. SearchDto $searchDto,
  93. EntityDto $entityDto,
  94. FieldCollection $fields,
  95. FilterCollection $filters
  96. ): RepositoryQueryInterface {
  97. $repositoryQuery = parent::createIndexRepositoryQuery(
  98. $searchDto,
  99. $entityDto,
  100. $fields,
  101. $filters
  102. );
  103. $repositoryQuery->filterByUserMerchant($this->getUserMerchant());
  104. return $repositoryQuery;
  105. }
  106. }