Passed
Push — master ( cf0bfa...d7e9ff )
by Jan
05:15
created

SEPAExportCrudController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Admin\Field\VichyFileField;
6
use App\Admin\Filter\MoneyAmountFilter;
7
use App\Admin\Filter\ULIDFilter;
8
use App\Entity\SEPAExport;
9
use App\Services\SEPAExport\SEPAExportAdminHelper;
10
use Doctrine\ORM\EntityManagerInterface;
11
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
12
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
13
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
14
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
15
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
16
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
17
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
18
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
19
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
20
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
21
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
22
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
23
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
24
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
25
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
26
use EasyCorp\Bundle\EasyAdminBundle\Filter\DateTimeFilter;
27
use EasyCorp\Bundle\EasyAdminBundle\Filter\NumericFilter;
28
use EasyCorp\Bundle\EasyAdminBundle\Filter\TextFilter;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Contracts\Translation\TranslatorInterface;
31
32
class SEPAExportCrudController extends AbstractCrudController
33
{
34
    private $adminHelper;
35
    private $translator;
36
    private $entityManager;
37
38
    public function __construct(SEPAExportAdminHelper $adminHelper, TranslatorInterface $translator, EntityManagerInterface $entityManager)
39
    {
40
        $this->adminHelper = $adminHelper;
41
        $this->translator = $translator;
42
        $this->entityManager = $entityManager;
43
    }
44
45
    public function bookPaymentOrders(AdminContext $context): Response
46
    {
47
        /** @var SEPAExport $entity */
48
        $entity = $context->getEntity()->getInstance();
49
50
        $entity_must_be_booked = true;
51
52
        if(!$entity->isOpen()) {
53
            $entity_must_be_booked = false;
54
            $this->addFlash('error', 'sepa_export.flash.sepa_export_already_booked');
55
        }
56
57
        $not_factually = $this->adminHelper->getNotFactuallyCorrectPaymentOrders($entity);
58
        if (!empty($not_factually)) {
59
            $entity_must_be_booked = false;
60
            $this->addFlash('warning', $this->translator->trans('sepa_export.flash.sepa_export_payments_not_factually_checked', ['%payment_orders%' => $this->adminHelper->getPaymentOrdersFlashText($not_factually)]));
61
        }
62
63
        $not_mathematically = $this->adminHelper->getNotMathematicallyCorrectPaymentOrders($entity);
64
        if (!empty($not_mathematically)) {
65
            $entity_must_be_booked = false;
66
            $this->addFlash('warning', $this->translator->trans('sepa_export.flash.sepa_export_payments_not_mathematically_checked', ['%payment_orders%' => $this->adminHelper->getPaymentOrdersFlashText($not_mathematically)]));
67
        }
68
69
        $already_booked = $this->adminHelper->getAlreadyBookedPaymentOrders($entity);
70
        if (!empty($already_booked)) {
71
            $entity_must_be_booked = false;
72
            $this->addFlash('warning', $this->translator->trans('sepa_export.flash.sepa_export_payments_already_booked', ['%payment_orders%' => $this->adminHelper->getPaymentOrdersFlashText($already_booked)]));
73
        }
74
75
        if ($entity_must_be_booked) {
76
            $entity->setIsBooked();
77
            //Book all associated payment orders
78
            foreach ($entity->getAssociatedPaymentOrders() as $paymentOrder) {
79
                $paymentOrder->setBookingDate(new \DateTime());
80
            }
81
            $this->entityManager->flush();
82
            $this->addFlash('success', 'sepa_export.flash.sepa_export_books_success');
83
        }
84
85
        return $this->redirect($context->getReferrer() ?? '/admin');
86
    }
87
88
    public static function getEntityFqcn(): string
89
    {
90
        return SEPAExport::class;
91
    }
92
93
    public function configureCrud(Crud $crud): Crud
94
    {
95
        return $crud
96
            ->setEntityLabelInSingular('sepa_export.label')
97
            ->showEntityActionsInlined()
98
            ->setSearchFields(['description', 'initiator_iban', 'initiator_bic', 'comment', 'sepa_message_id'])
99
            ->setEntityLabelInPlural('sepa_export.labelp');
100
    }
101
102
    public function configureFilters(Filters $filters): Filters
103
    {
104
        return $filters
105
            ->add(NumericFilter::new('number_of_payments', 'sepa_export.number_of_payments'))
106
            ->add(MoneyAmountFilter::new('total_sum', 'sepa_export.total_sum'))
107
            ->add(TextFilter::new('description', 'sepa_export.description'))
108
            ->add(TextFilter::new('initiator_iban', 'sepa_export.initiator_iban'))
109
            ->add(TextFilter::new('initiator_bic', 'sepa_export.initiator_bic'))
110
            ->add(TextFilter::new('sepa_message_id', 'sepa_export.message_id'))
111
            ->add(DateTimeFilter::new('creation_date', 'creation_date'))
112
            ->add(DateTimeFilter::new('last_modified', 'last_modified'))
113
            ->add(DateTimeFilter::new('booking_date', 'sepa_export.booking_date'))
114
            ->add(ULIDFilter::new('group_ulid', 'sepa_export.group_ulid'))
115
            ;
116
    }
117
118
    public function configureActions(Actions $actions): Actions
119
    {
120
        $actions->setPermissions([
121
            Action::DETAIL => 'ROLE_SHOW_SEPA_EXPORTS',
122
            Action::INDEX => 'ROLE_SHOW_SEPA_EXPORTS',
123
            Action::EDIT => 'ROLE_EDIT_SEPA_EXPORTS',
124
            Action::DELETE => 'ROLE_EDIT_SEPA_EXPORTS',
125
        ]);
126
127
        $book_action = Action::new('bookPaymentOrders', 'sepa_export.action.book_payment_orders', 'fas fa-check')
128
            ->linkToCrudAction('bookPaymentOrders')
129
            /*->displayIf(function (PaymentOrder $paymentOrder) {
130
                return $this->isGranted('ROLE_PO_FACTUALLY')
131
                    && $paymentOrder->isConfirmed()
132
                    && !$paymentOrder->isFactuallyCorrect()
133
                    && $paymentOrder->isMathematicallyCorrect();
134
            })*/
135
            ->setCssClass('btn btn-success');
136
            $actions->add(Crud::PAGE_DETAIL, $book_action);
137
138
        $actions->disable(Crud::PAGE_NEW);
139
        $actions->add(Crud::PAGE_INDEX, Action::DETAIL);
140
141
        return parent::configureActions($actions); // TODO: Change the autogenerated stub
142
    }
143
144
    public function configureFields(string $pageName): iterable
145
    {
146
        $xml_file = VichyFileField::new('xml_file', 'sepa_export.xml_file');
147
        $id = IdField::new('id', 'sepa_export.id');
148
        $number_of_payments = NumberField::new('number_of_payments', 'sepa_export.number_of_payments')
149
            ->setHelp('sepa_export.number_of_payments.help');;
150
        $initiator_bic = TextField::new('initiator_bic', 'sepa_export.initiator_bic')
151
            ->setHelp('sepa_export.initiator_bic.help');
152
        $initiator_iban = TextField::new('initiator_iban', 'sepa_export.initiator_iban')
153
            ->setHelp('sepa_export.initiator_iban.help');
154
        $booking_date = DateTimeField::new('booking_date', 'sepa_export.booking_date');
155
        $total_sum = MoneyField::new('total_sum', 'sepa_export.total_sum')
156
            ->setCurrency('EUR')
157
            ->setStoredAsCents(true)
158
            ->setHelp('sepa_export.total_sum.help');;
159
        $sepa_message_id = TextField::new('sepa_message_id', 'sepa_export.message_id')
160
            ->setHelp('sepa_export.message_id.help');
161
        $description = TextField::new('description', 'sepa_export.description');
162
        $comment = TextEditorField::new('comment', 'sepa_export.comment');
163
        $group_ulid = TextField::new('group_ulid', 'sepa_export.group_ulid')
164
            ->setHelp('sepa_export.group_ulid.help')
165
            ->setTemplatePath('admin/field/group_ulid.html.twig');
166
        $last_modified = DateTimeField::new('last_modified', 'last_modified');
167
        $creationDate = DateTimeField::new('creation_date', 'creation_date')->onlyOnDetail();
168
        $associated_payment_orders = AssociationField::new('associated_payment_orders')
169
            ->setTemplatePath('admin/field/payment_orders_association.html.twig')
170
            ->setCrudController(PaymentOrderCrudController::class);
171
172
173
        if (Crud::PAGE_INDEX === $pageName) {
174
            return [$id, $number_of_payments, $total_sum, $description, $initiator_iban, $booking_date];
175
        }
176
177
        if (Crud::PAGE_DETAIL === $pageName) {
178
            return [
179
                $xml_file,
180
181
                FormField::addPanel('sepa_export.infos')->collapsible(),
182
                $id,
183
                $number_of_payments,
184
                $total_sum,
185
                $initiator_iban,
186
                $initiator_bic,
187
                $description,
188
                $booking_date,
189
                $associated_payment_orders,
190
                $comment,
191
192
                FormField::addPanel('sepa_export.advanced')->collapsible(),
193
                $sepa_message_id,
194
                $group_ulid,
195
                $last_modified,
196
                $creationDate,
197
            ];
198
        }
199
200
        if (Crud::PAGE_EDIT === $pageName) {
201
            return [$comment];
202
        }
203
204
        throw new \LogicException("This should never be reached!");
205
    }
206
}
207