Passed
Push — master ( efefd6...187aa4 )
by James
09:45 queued 11s
created

IngDescription::moveDatumTijdValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
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
use Log;
28
/**
29
 * Class IngDescription.
30
 *
31
 * Parses the description from CSV files for Ing bank accounts.
32
 *
33
 * With Mutation 'InternetBankieren', 'Overschrijving', 'Verzamelbetaling' and
34
 * 'Incasso' the Name of Opposing account the Opposing IBAN number are in the
35
 * Description. This class will remove them, and add Name in description by
36
 * 'Betaalautomaat' so those are easily recognizable
37
 */
38
class IngDescription implements SpecificInterface
39
{
40
    /** @var array The current row. */
41
    public $row;
42
43
    /**
44
     * Description of the current specific.
45
     *
46
     * @return string
47
     * @codeCoverageIgnore
48
     */
49
    public static function getDescription(): string
50
    {
51
        return 'specifics.ing_descr';
52
    }
53
54
    /**
55
     * Name of the current specific.
56
     *
57
     * @return string
58
     * @codeCoverageIgnore
59
     */
60
    public static function getName(): string
61
    {
62
        return 'specifics.ing_name';
63
    }
64
65
    /**
66
     * Run the specific code.
67
     *
68
     * @param array $row
69
     *
70
     * @return array
71
     *
72
     */
73
    public function run(array $row): array
74
    {
75
        $this->row = array_values($row);
76
        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

76
        /** @scrutinizer ignore-call */ 
77
        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...
77
        if (count($this->row) >= 8) {                    // check if the array is correct
78
            switch ($this->row[4]) {                     // Get value for the mutation type
79
                case 'GT':                               // InternetBankieren
80
                case 'OV':                               // Overschrijving
81
                case 'VZ':                               // Verzamelbetaling
82
                case 'IC':                               // Incasso
83
                case 'DV':                               // Divers
84
                    $this->removeIBANIngDescription();   // Remove "IBAN:", because it is already at "Tegenrekening"
85
                    $this->removeNameIngDescription();   // Remove "Naam:", because it is already at "Naam/ Omschrijving"
86
                    $this->removeIngDescription();       // Remove "Omschrijving", but not the value from description
87
                    $this->moveValutadatumDescription(); // Move "Valutadatum" from description to new column
88
                    $this->MoveSavingsAccount();         // Move savings account number and name
89
                    $this->moveDatumTijdValue();         // Move datum + tijd
90
                    break;
91
                case 'BA':                              // Betaalautomaat
92
                    $this->moveValutadatumDescription(); // Move "Valutadatum" from description to new column
93
                    $this->addNameIngDescription();
94
                    break;
95
                default:
96
                    break;
97
            }
98
        }
99
100
        return $this->row;
101
    }
102
103
    /**
104
     * Add the Opposing name from cell 1 in the description for Betaalautomaten
105
     * Otherwise the description is only: 'Pasvolgnr:<nr> <date> Transactie:<NR> Term:<nr>'.
106
     */
107
    protected function addNameIngDescription(): void
108
    {
109
        $this->row[8] = sprintf('%s %s', $this->row[1] ?? '', $this->row[8] ?? '');
110
    }
111
112
    /**
113
     * Remove "Omschrijving" (and NOT its value) from the description.
114
     */
115
    protected function removeIngDescription(): void
116
    {
117
        Log::debug('Remove "Omschrijving:"');
118
        $this->row[8] = preg_replace('/Omschrijving: /', '', $this->row[8] ?? '');
119
    }
120
121
    /**
122
     * Remove IBAN number out of the  description
123
     * Default description of Description is: Naam: <OPPOS NAME> Omschrijving: <DESCRIPTION> IBAN: <OPPOS IBAN NR>.
124
     */
125
    protected function removeIBANIngDescription(): void
126
    {
127
        Log::debug('Remove IBAN.');
128
        // Try replace the iban number with nothing. The IBAN nr is found in the third column
129
        $this->row[8] = preg_replace('/\sIBAN:\s' . $this->row[3] . '/', '', $this->row[8]);
130
    }
131
132
    /**
133
     * Remove "Naam" (and its value) from the description.
134
     */
135
    protected function removeNameIngDescription(): void
136
    {
137
        Log::debug('Remove Naam.');
138
        $this->row[8] = preg_replace('/Naam:.*?([a-zA-Z\/]+:)/', '$1', $this->row[8]);
139
    }
140
141
    /**
142
     * Move "Valutadatum" from the description to new column.
143
     */
144
    protected function moveValutadatumDescription(): void
145
    {
146
        $matches = [];
147
        if (preg_match('/Valutadatum: ([0-9-]+)/', $this->row[8], $matches)) {
148
            $this->row[9] = date('Ymd', strtotime($matches[1]));
149
            $this->row[8] = preg_replace('/Valutadatum: [0-9-]+/', '', $this->row[8]);
150
        }
151
    }
152
153
    /**
154
     * Move "Datum/Tijd" from the description to new column.
155
     */
156
    protected function moveDatumTijdValue(): void
157
    {
158
        $matches = [];
159
        if (preg_match('/Datum\/Tijd: ([0-9- :]+)/', $this->row[8], $matches)) {
160
            $this->row[10] = date('Ymd H:i:s', strtotime($matches[1]));
161
            $this->row[8] = preg_replace('/Datum\/Tijd: ([0-9- :]+)/', '', $this->row[8]);
162
        }
163
    }
164
165
    /**
166
     * Move savings account number to column 1 and name to column 3.
167
     */
168
    private function MoveSavingsAccount(): void
169
    {
170
        $matches = [];
171
172
        if (preg_match('/(Naar|Van) (.*rekening) ([A-Za-z0-9]+)/', $this->row[8], $matches)) { // Search for saving acount at 'Mededelingen' column
173
            $this->row[1] .= ' ' . $matches[2] . ' ' . $matches[3]; // Current name + Saving acount name + Acount number
174
            if ('' === (string) $this->row[3]) { // if Saving account number does not yet exists
175
                $this->row[3] = $matches[3]; // Copy savings account number
176
            }
177
            $this->row[8] = preg_replace('/(Naar|Van) (.*rekening) ([A-Za-z0-9]+)/', '', $this->row[8]); // Remove the savings account content from description
178
        } elseif (preg_match('/(Naar|Van) (.*rekening) ([A-Za-z0-9]+)/', $this->row[1], $matches)) { // Search for saving acount at 'Naam / Omschrijving' column
179
            $this->row[1] = $matches[2] . ' ' . $matches[3];  // Saving acount name + Acount number
180
            if ('' === (string) $this->row[3]) { // if Saving account number does not yet exists
181
                $this->row[3] = $matches[3]; // Copy savings account number
182
            }
183
        }
184
185
        // if Saving account number exists
186
        if (('' !== (string) $this->row[3]) && !preg_match('/[A-Za-z]/', $this->row[3])) { // if Saving account number has no characters
187
            $this->row[3] = sprintf('%010d', $this->row[3]); // Make the number 10 digits
188
        }
189
    }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    public function runOnHeaders(array $headers): array
195
    {
196
        $headers[9] = 'Valutadatum';
197
        $headers[10] = 'Datum + tijd';
198
        return $headers;
199
    }
200
}
201