Passed
Push — master ( 0dc3be...a707ad )
by Jan
05:21
created

PaymentOrderSEPAExporter::exportAuto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
/*
3
 * Copyright (C) 2020  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\Entity\BankAccount;
22
use App\Entity\PaymentOrder;
23
use App\Entity\SEPAExport;
24
use App\Exception\SEPAExportAutoModeNotPossible;
25
use App\Helpers\SEPAXML\SEPAXMLExportResult;
26
use Digitick\Sepa\DomBuilder\BaseDomBuilder;
27
use Digitick\Sepa\DomBuilder\DomBuilderFactory;
28
use Digitick\Sepa\PaymentInformation;
29
use Digitick\Sepa\TransferFile\CustomerCreditTransferFile;
30
use Digitick\Sepa\TransferInformation\CustomerCreditTransferInformation;
31
use Webmozart\Assert\Assert;
32
33
final class PaymentOrderSEPAExporter
34
{
35
    /** @var GroupHeaderHelper */
36
    private $group_header_helper;
37
38
    public function __construct(GroupHeaderHelper $group_header_helper, SEPAExportGroupAndSplitHelper $splitHelper)
0 ignored issues
show
Unused Code introduced by
The parameter $splitHelper is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function __construct(GroupHeaderHelper $group_header_helper, /** @scrutinizer ignore-unused */ SEPAExportGroupAndSplitHelper $splitHelper)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        $this->group_header_helper = $group_header_helper;
41
    }
42
43
    /**
44
     * Exports the given payment orders and automatically assign the used bank accounts.
45
     * For this the bankaccount of the associated departments are used, and the FSR-Kom bank account if a payment is an
46
     * FSR-Kom transaction.
47
     * @param  PaymentOrder[]  $payment_orders The payment orders which should be exported
48
     * @throws SEPAExportAutoModeNotPossible If an element has no assigned default bank account, then automatic mode is not possible
49
     * @return SEPAXMLExportResult
50
     */
51
    public function exportAuto(array $payment_orders): SEPAXMLExportResult
0 ignored issues
show
Unused Code introduced by
The parameter $payment_orders is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
    public function exportAuto(/** @scrutinizer ignore-unused */ array $payment_orders): SEPAXMLExportResult

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        //First we have to group the payment orders according to their bank accounts
54
55
56
57
        return new SEPAXMLExportResult();
0 ignored issues
show
Bug introduced by
The call to App\Helpers\SEPAXML\SEPA...rtResult::__construct() has too few arguments starting with sepa_exports. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        return /** @scrutinizer ignore-call */ new SEPAXMLExportResult();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
58
    }
59
60
61
    public function exportUsingGivenIBAN(array $payment_orders, string $debtor_iban, string $debtor_bic, string $debtor_account_name): SEPAExport
62
    {
63
        //We need IBAN without spaces
64
        $debtor_iban = str_replace(" ", "", $debtor_iban);
65
66
        $groupHeader = $this->group_header_helper->getGroupHeader($debtor_bic);
67
        $sepaFile = new CustomerCreditTransferFile($groupHeader);
68
69
        $payment = new PaymentInformation(
70
            md5(random_bytes(50)), //Use a random payment identifier ID
71
            $debtor_iban,
72
            $debtor_bic,
73
            $debtor_account_name
74
        );
75
76
        //Disable batch booking, as the commerzbank does not show details for "Sammelüberweisungen"
77
        $payment->setBatchBooking(false);
78
79
        //Add each payment order as transaction
80
        foreach ($payment_orders as $payment_order) {
81
            //Ensure that type is correct
82
            if (!$payment_order instanceof PaymentOrder) {
83
                throw new \InvalidArgumentException('$payment_orders must be an array of PaymentOrder elements!');
84
            }
85
86
            //Ensure that the ID is available
87
            if (!$payment_order->getId() === null) {
88
                throw new \InvalidArgumentException('A payment order that should be exported misses an ID. All payment orders must have been persisted!');
89
            }
90
91
            $transfer = new CustomerCreditTransferInformation(
92
                $payment_order->getAmount(),
93
                $payment_order->getBankInfo()->getIbanWithoutSpaces(),
94
                $payment_order->getBankInfo()->getAccountOwner()
95
            );
96
97
            //BIC is optional, only set it if it was set.
98
            if (!empty($payment_order->getBankInfo()->getBic())) {
99
                $transfer->setBic($payment_order->getBankInfo()->getBic());
100
            }
101
102
            //We use the ID String of the payment order as end to end reference
103
            $transfer->setEndToEndIdentification($payment_order->getIDString());
104
            //Set the reference ID of the payment order as
105
            $transfer->setRemittanceInformation($payment_order->getBankInfo()->getReference());
106
107
            $payment->addTransfer($transfer);
108
        }
109
110
        //Add payment infos to SEPA file
111
        $sepaFile->addPaymentInformation($payment);
112
113
        // We have to use the format 'pain.001.001.03'
114
        $domBuilder = DomBuilderFactory::createDomBuilder($sepaFile, 'pain.001.001.03');
115
116
        if (!$domBuilder instanceof BaseDomBuilder) {
0 ignored issues
show
introduced by
$domBuilder is always a sub-type of Digitick\Sepa\DomBuilder\BaseDomBuilder.
Loading history...
117
            throw new \RuntimeException('$domBuilder must be an BaseDomBuilder instance!');
118
        }
119
120
        //Create a temporary file with the XML content
121
        $xml_string = $domBuilder->asXml();
122
123
        //We use the format YYYYMMDDHHmmss_MsgID.xml
124
        $original_filename = sprintf("%s_%s.xml",
125
            (new \DateTime())->format('YmdHis'),
126
            $groupHeader->getMessageIdentification(),
127
        );
128
129
        return SEPAExport::createFromXMLString($xml_string, $original_filename);
130
    }
131
132
133
134
135
}