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

SEPAExportAdminHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 35
c 1
b 0
f 0
dl 0
loc 77
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getNotFactuallyCorrectPaymentOrders() 0 10 1
A __construct() 0 4 1
A getNotMathematicallyCorrectPaymentOrders() 0 10 1
A getPaymentOrdersFlashText() 0 11 2
A getAlreadyBookedPaymentOrders() 0 10 1
1
<?php
2
/*
3
 * Copyright (C)  2020-2022  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Services\SEPAExport;
20
21
use App\Controller\Admin\PaymentOrderCrudController;
22
use App\Entity\PaymentOrder;
23
use App\Entity\SEPAExport;
24
use Doctrine\ORM\EntityManagerInterface;
25
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
26
27
class SEPAExportAdminHelper
28
{
29
    private $entityManager;
30
    private $adminUrlGenerator;
31
32
    public function __construct(EntityManagerInterface $entityManager, AdminUrlGenerator $adminUrlGenerator)
33
    {
34
        $this->entityManager = $entityManager;
35
        $this->adminUrlGenerator = $adminUrlGenerator;
36
    }
37
38
    /**
39
     * Returns all payments order that were not marked as factually correct.
40
     * @param  SEPAExport  $SEPAExport
41
     * @return PaymentOrder[]
42
     */
43
    public function getNotFactuallyCorrectPaymentOrders(SEPAExport $SEPAExport): array
44
    {
45
        $qb = $this->entityManager->createQueryBuilder();
46
        return $qb->select('p')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->select('p')-...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
47
            ->from(PaymentOrder::class, 'p')
48
            ->leftJoin('p.associated_sepa_exports', 's')
49
            ->where('s = :sepa_export')
50
            ->andwhere('p.factually_correct = false')
51
            ->setParameter('sepa_export', $SEPAExport)
52
            ->getQuery()->getResult();
53
    }
54
55
    /**
56
     * Returns all payments order that were not marked as factually correct.
57
     * @param  SEPAExport  $SEPAExport
58
     * @return PaymentOrder[]
59
     */
60
    public function getNotMathematicallyCorrectPaymentOrders(SEPAExport $SEPAExport): array
61
    {
62
        $qb = $this->entityManager->createQueryBuilder();
63
        return $qb->select('p')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->select('p')-...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
64
            ->from(PaymentOrder::class, 'p')
65
            ->leftJoin('p.associated_sepa_exports', 's')
66
            ->where('s = :sepa_export')
67
            ->andwhere('p.mathematically_correct = false')
68
            ->setParameter('sepa_export', $SEPAExport)
69
            ->getQuery()->getResult();
70
    }
71
72
    /**
73
     * Returns all payments order that were not marked as factually correct.
74
     * @param  SEPAExport  $SEPAExport
75
     * @return PaymentOrder[]
76
     */
77
    public function getAlreadyBookedPaymentOrders(SEPAExport $SEPAExport): array
78
    {
79
        $qb = $this->entityManager->createQueryBuilder();
80
        return $qb->select('p')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->select('p')-...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
81
            ->from(PaymentOrder::class, 'p')
82
            ->leftJoin('p.associated_sepa_exports', 's')
83
            ->where('s = :sepa_export')
84
            ->andwhere('p.booking_date IS NOT null')
85
            ->setParameter('sepa_export', $SEPAExport)
86
            ->getQuery()->getResult();
87
    }
88
89
    /**
90
     * @param  PaymentOrder[]  $paymentOrders
91
     * @return string
92
     */
93
    public function getPaymentOrdersFlashText(array $paymentOrders): string
94
    {
95
        $tmp = "";
96
97
        foreach($paymentOrders as $paymentOrder) {
98
            $link = $this->adminUrlGenerator->setController(PaymentOrderCrudController::class)->setAction('detail')->setEntityId($paymentOrder->getId());
99
            $text = $paymentOrder->getIDString() . ': ' . $paymentOrder->getProjectName() . ' (' . $paymentOrder->getDepartment()->getName() . '), ' . $paymentOrder->getAmountString() . ' €';
100
            $tmp .= sprintf('<br><a href="%s">%s</a>', $link, $text);
101
        }
102
103
        return $tmp;
104
    }
105
106
}