Passed
Push — master ( 40d218...d516cb )
by James
09:17 queued 11s
created

Amount::validAmount()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 4
nc 4
nop 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Amount.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 Amount
30
 */
31
class Amount extends AbstractTask
32
{
33
    /**
34
     * @param array $group
35
     *
36
     * @return array
37
     */
38
    public function process(array $group): array
39
    {
40
        foreach ($group['transactions'] as $index => $transaction) {
41
            $group['transactions'][$index] = $this->processAmount($transaction);
42
        }
43
44
        return $group;
45
    }
46
47
    /**
48
     * @param string $amount
49
     *
50
     * @return bool
51
     */
52
    private function validAmount(string $amount): bool
53
    {
54
        if ('' === $amount) {
55
            return false;
56
        }
57
        if ('0' === $amount) {
58
            return false;
59
        }
60
        if (0 === bccomp('0', $amount)) {
61
            return false;
62
        }
63
64
        return true;
65
    }
66
67
    /**
68
     * @param array $transaction
69
     *
70
     * @return array
71
     */
72
    private function processAmount(array $transaction): array
73
    {
74
        Log::debug(sprintf('Now at the start of processAmount("%s")', $transaction['amount']));
75
        $amount = null;
76
        if (null === $amount && $this->validAmount((string) $transaction['amount'])) {
77
            Log::debug('Transaction["amount"] value is not NULL, assume this is the correct value.');
78
            $amount = $transaction['amount'];
79
        }
80
81
        if (null === $amount && $this->validAmount((string) $transaction['amount_debit'])) {
82
            Log::debug(sprintf('Transaction["amount_debit"] value is not NULL ("%s"), assume this is the correct value.', $transaction['amount_debit']));
83
            $amount = $transaction['amount_debit'];
84
        }
85
86
        if (null === $amount && $this->validAmount((string) $transaction['amount_credit'])) {
87
            Log::debug(sprintf('Transaction["amount_credit"] value is not NULL ("%s"), assume this is the correct value.', $transaction['amount_credit']));
88
            $amount = $transaction['amount_credit'];
89
        }
90
91
        if (null === $amount && $this->validAmount((string) $transaction['amount_negated'])) {
92
            Log::debug(sprintf('Transaction["amount_negated"] value is not NULL ("%s"), assume this is the correct value.', $transaction['amount_negated']));
93
            $amount = $transaction['amount_negated'];
94
        }
95
96
        if (array_key_exists('amount_modifier', $transaction)) {
97
            $transaction['amount_modifier'] = (string) $transaction['amount_modifier'];
98
        }
99
        if (array_key_exists('foreign_amount', $transaction)) {
100
            $transaction['foreign_amount'] = (string) $transaction['foreign_amount'];
101
        }
102
        $amount = (string) $amount;
103
        if ('' === $amount) {
104
            Log::error('Amount is EMPTY. This will give problems further ahead.', $transaction);
105
106
            return $transaction;
107
        }
108
        // modify amount:
109
        $amount = bcmul($amount, $transaction['amount_modifier']);
110
111
        Log::debug(sprintf('Amount is now %s.', $amount));
112
113
        // modify foreign amount
114
        if (isset($transaction['foreign_amount']) && null !== $transaction['foreign_amount']) {
115
            $transaction['foreign_amount'] = bcmul($transaction['foreign_amount'], $transaction['amount_modifier']);
116
            Log::debug(sprintf('FOREIGN amount is now %s.', $transaction['foreign_amount']));
117
        }
118
119
        // unset those fields:
120
        unset($transaction['amount_credit'], $transaction['amount_debit'], $transaction['amount_negated']);
121
        $transaction['amount'] = $amount;
122
123
        return $transaction;
124
    }
125
126
    /**
127
     * Returns true if the task requires the default currency of the user.
128
     *
129
     * @return bool
130
     */
131
    public function requiresTransactionCurrency(): bool
132
    {
133
        return false;
134
    }
135
136
    /**
137
     * Returns true if the task requires the default account.
138
     *
139
     * @return bool
140
     */
141
    public function requiresDefaultAccount(): bool
142
    {
143
        return false;
144
    }
145
}
146