Passed
Push — master ( a02f75...ed4f6d )
by James
03:30 queued 12s
created

IngDescription::removeIngDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * IngDescription.php
4
 * Copyright (c) 2020 [email protected]
5
 *
6
 * This file is part of the Firefly III CSV importer
7
 * (https://github.com/firefly-iii/csv-importer).
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace App\Services\CSV\Specifics;
26
27
/**
28
 * Class IngDescription.
29
 *
30
 * Parses the description from CSV files for Ing bank accounts.
31
 *
32
 * With Mutation 'InternetBankieren', 'Overschrijving', 'Verzamelbetaling' and
33
 * 'Incasso' the Name of Opposing account the Opposing IBAN number are in the
34
 * Description. This class will remove them, and add Name in description by
35
 * 'Betaalautomaat' so those are easily recognizable
36
 */
37
class IngDescription implements SpecificInterface
38
{
39
    /** @var array The current row. */
40
    public $row;
41
42
    /**
43
     * Description of the current specific.
44
     *
45
     * @return string
46
     * @codeCoverageIgnore
47
     */
48
    public static function getDescription(): string
49
    {
50
        return 'specifics.ing_descr';
51
    }
52
53
    /**
54
     * Name of the current specific.
55
     *
56
     * @return string
57
     * @codeCoverageIgnore
58
     */
59
    public static function getName(): string
60
    {
61
        return 'specifics.ing_name';
62
    }
63
64
    /**
65
     * Run the specific code.
66
     *
67
     * @param array $row
68
     *
69
     * @return array
70
     *
71
     */
72
    public function run(array $row): array
73
    {
74
        $this->row = array_values($row);
75
        array_push($this->row); // New column for "Valutadatum"
0 ignored issues
show
Bug introduced by
The call to array_push() has too few arguments starting with var. ( Ignorable by Annotation )

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

75
        /** @scrutinizer ignore-call */ 
76
        array_push($this->row); // New column for "Valutadatum"

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...
76
        if (count($this->row) >= 8) {                    // check if the array is correct
77
            switch ($this->row[4]) {                     // Get value for the mutation type
78
                case 'GT':                               // InternetBankieren
79
                case 'OV':                               // Overschrijving
80
                case 'VZ':                               // Verzamelbetaling
81
                case 'IC':                               // Incasso
82
                case 'DV':                               // Divers
83
                    $this->removeIBANIngDescription();   // Remove "IBAN:", because it is already at "Tegenrekening"
84
                    $this->removeNameIngDescription();   // Remove "Naam:", because it is already at "Naam/ Omschrijving"
85
                    $this->removeIngDescription();       // Remove "Omschrijving", but not the value from description
86
                    $this->moveValutadatumDescription(); // Move "Valutadatum" from description to new column
87
                    $this->MoveSavingsAccount();         // Move savings account number and name
88
                    break;
89
                case 'BA':                              // Betaalautomaat
90
                    $this->moveValutadatumDescription(); // Move "Valutadatum" from description to new column
91
                    $this->addNameIngDescription();
92
                    break;
93
            }
94
        }
95
96
        return $this->row;
97
    }
98
99
    /**
100
     * Add the Opposing name from cell 1 in the description for Betaalautomaten
101
     * Otherwise the description is only: 'Pasvolgnr:<nr> <date> Transactie:<NR> Term:<nr>'.
102
     */
103
    protected function addNameIngDescription(): void
104
    {
105
        $this->row[8] = $this->row[1] . ' ' . $this->row[8];
106
    }
107
108
    /**
109
     * Remove "Omschrijving" (and NOT its value) from the description.
110
     */
111
    protected function removeIngDescription(): void
112
    {
113
        $this->row[8] = preg_replace('/Omschrijving: /', '', $this->row[8]);
114
    }
115
116
    /**
117
     * Remove IBAN number out of the  description
118
     * Default description of Description is: Naam: <OPPOS NAME> Omschrijving: <DESCRIPTION> IBAN: <OPPOS IBAN NR>.
119
     */
120
    protected function removeIBANIngDescription(): void
121
    {
122
        // Try replace the iban number with nothing. The IBAN nr is found in the third column
123
        $this->row[8] = preg_replace('/\sIBAN:\s' . $this->row[3] . '/', '', $this->row[8]);
124
    }
125
126
    /**
127
     * Remove "Naam" (and its value) from the description.
128
     */
129
    protected function removeNameIngDescription(): void
130
    {
131
        $this->row[8] = preg_replace('/Naam:.*?([a-zA-Z\/]+:)/', '$1', $this->row[8]);
132
    }
133
134
    /**
135
     * Move "Valutadatum" from the description to new column.
136
     */
137
    protected function moveValutadatumDescription(): void
138
    {
139
        $matches = array();
140
        preg_match('/Valutadatum: ([0-9-]+)/', $this->row[8], $matches);
141
        $this->row[9] = date("Ymd", strtotime($matches[1]));
142
        $this->row[8] = preg_replace('/Valutadatum: [0-9-]+/', '', $this->row[8]);
143
    }
144
145
    /**
146
     * Move savings account number to column 1 and name to column 3.
147
     */
148
    private function MoveSavingsAccount(): void
149
    {
150
        $matches = array();
151
        if ('' === (string)$this->row[3]) {
152
            if (preg_match('/(Naar|Van) (.*rekening) ([0-9]+)/', $this->row[8], $matches)) {
153
                $matches[3] = sprintf("%010d", $matches[3]);
154
                $this->row[1] = $matches[2]; // Savings account name
155
                $this->row[3] = $matches[3]; // Savings account number
156
                $this->row[8] = preg_replace('/(Naar|Van) (.*rekening) ([0-9]+)/', '', $this->row[8]); // Remove the savings account content from description
157
            } elseif (preg_match('/(Naar|Van) (.*rekening) ([0-9]+)/', $this->row[1], $matches)) {
158
                $matches[3] = sprintf("%010d", $matches[3]);
159
                $this->row[1] = $matches[2]; // Savings account name
160
                $this->row[3] = $matches[3]; // Savings account number
161
            }
162
        }
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function runOnHeaders(array $headers): array
169
    {
170
        return $headers;
171
    }
172
}
173