1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Accounts.php |
5
|
|
|
* Copyright (c) 2020 [email protected] |
6
|
|
|
* |
7
|
|
|
* This file is part of the Firefly III CSV importer |
8
|
|
|
* (https://github.com/firefly-iii/csv-importer). |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace App\Services\Import\Task; |
25
|
|
|
|
26
|
|
|
use Log; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class EmptyAccounts |
30
|
|
|
* |
31
|
|
|
* A very simple task that makes sure that if the source of a deposit |
32
|
|
|
* or the destination of a withdrawal is empty, it will be set to "(no name)". |
33
|
|
|
*/ |
34
|
|
|
class EmptyAccounts extends AbstractTask |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $group |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function process(array $group): array |
43
|
|
|
{ |
44
|
|
|
Log::debug('Now in EmptyAccounts::process()'); |
45
|
|
|
$total = count($group['transactions']); |
46
|
|
|
foreach ($group['transactions'] as $index => $transaction) { |
47
|
|
|
Log::debug(sprintf('Now processing transaction %d of %d', $index + 1, $total)); |
48
|
|
|
$group['transactions'][$index] = $this->processTransaction($transaction); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $group; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns true if the task requires the default account. |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function requiresDefaultAccount(): bool |
60
|
|
|
{ |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns true if the task requires the default currency of the user. |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function requiresTransactionCurrency(): bool |
70
|
|
|
{ |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string|null $sourceType |
76
|
|
|
* @param string|null $destinationType |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
private function determineType(?string $sourceType, ?string $destinationType): string |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
Log::debug(sprintf('Now in determineType("%s", "%s")', $sourceType, $destinationType)); |
83
|
|
|
if (null === $sourceType && null === $destinationType) { |
84
|
|
|
Log::debug('Return withdrawal, both are NULL'); |
85
|
|
|
|
86
|
|
|
return 'withdrawal'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// if source is a asset and dest is NULL, its a withdrawal |
90
|
|
|
if ('asset' === $sourceType && null === $destinationType) { |
91
|
|
|
Log::debug('Return withdrawal, source is asset'); |
92
|
|
|
|
93
|
|
|
return 'withdrawal'; |
94
|
|
|
} |
95
|
|
|
// if destination is asset and source is NULL, its a deposit |
96
|
|
|
if (null === $sourceType && 'asset' === $destinationType) { |
97
|
|
|
Log::debug('Return deposit, dest is asset'); |
98
|
|
|
|
99
|
|
|
return 'deposit'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$key = sprintf('transaction_types.account_to_transaction.%s.%s', $sourceType, $destinationType); |
103
|
|
|
$type = config($key); |
104
|
|
|
$value = $type ?? 'withdrawal'; |
105
|
|
|
Log::debug(sprintf('Check config for "%s" and found "%s". Returning "%s"', $key, $type, $value)); |
106
|
|
|
|
107
|
|
|
return $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $transaction |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private function processTransaction(array $transaction): array |
116
|
|
|
{ |
117
|
|
|
Log::debug('Now in EmptyAccounts::processTransaction()'); |
118
|
|
|
|
119
|
|
|
if ('withdrawal' === $transaction['type']) { |
120
|
|
|
$destName = $transaction['destination_name'] ?? ''; |
121
|
|
|
$destId = (int) ($transaction['destination_id'] ?? 0); |
122
|
|
|
if ('' === $destName && 0 === $destId) { |
123
|
|
|
Log::debug('Destination name and ID of withdrawal are empty, set to "(no name)".'); |
124
|
|
|
$transaction['destination_name'] = '(no name)'; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
if ('deposit' === $transaction['type']) { |
128
|
|
|
$sourceName = $transaction['source_name'] ?? ''; |
129
|
|
|
$sourceId = (int) ($transaction['source_id'] ?? 0); |
130
|
|
|
if ('' === $sourceName && 0 === $sourceId) { |
131
|
|
|
Log::debug('Source name and ID of deposit are empty, set to "(no name)".'); |
132
|
|
|
$transaction['source_name'] = '(no name)'; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $transaction; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.