1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TransferSearch.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace FireflyIII\Support\Search; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use Carbon\Carbon; |
26
|
|
|
use FireflyIII\Models\Account; |
27
|
|
|
use FireflyIII\Models\AccountType; |
28
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
29
|
|
|
use FireflyIII\User; |
30
|
|
|
use Illuminate\Database\Query\JoinClause; |
31
|
|
|
use Illuminate\Support\Collection; |
32
|
|
|
use InvalidArgumentException; |
33
|
|
|
use Log; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class TransferSearch |
37
|
|
|
*/ |
38
|
|
|
class TransferSearch implements GenericSearchInterface |
39
|
|
|
{ |
40
|
|
|
/** @var AccountRepositoryInterface */ |
41
|
|
|
private $accountRepository; |
42
|
|
|
/** @var string */ |
43
|
|
|
private $amount; |
44
|
|
|
/** @var Carbon */ |
45
|
|
|
private $date; |
46
|
|
|
/** @var string */ |
47
|
|
|
private $description; |
48
|
|
|
/** @var Account */ |
49
|
|
|
private $destination; |
50
|
|
|
/** @var Account */ |
51
|
|
|
private $source; |
52
|
|
|
/** @var array */ |
53
|
|
|
private $types; |
54
|
|
|
|
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
$this->accountRepository = app(AccountRepositoryInterface::class); |
58
|
|
|
$this->types = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return Collection |
63
|
|
|
*/ |
64
|
|
|
public function search(): Collection |
65
|
|
|
{ |
66
|
|
|
/** @var User $user */ |
67
|
|
|
$user = auth()->user(); |
68
|
|
|
|
69
|
|
|
$query = $user->transactionJournals() |
70
|
|
|
->leftJoin( |
71
|
|
|
'transactions as source', static function (JoinClause $join) { |
72
|
|
|
$join->on('transaction_journals.id', '=', 'source.transaction_journal_id'); |
73
|
|
|
$join->where('source.amount', '<', '0'); |
74
|
|
|
} |
75
|
|
|
) |
76
|
|
|
->leftJoin( |
77
|
|
|
'transactions as destination', static function (JoinClause $join) { |
78
|
|
|
$join->on('transaction_journals.id', '=', 'destination.transaction_journal_id'); |
79
|
|
|
$join->where('destination.amount', '>', '0'); |
80
|
|
|
} |
81
|
|
|
) |
82
|
|
|
->where('source.account_id', $this->source->id) |
83
|
|
|
->where('destination.account_id', $this->destination->id) |
84
|
|
|
->where('transaction_journals.description', $this->description) |
85
|
|
|
->where('destination.amount', $this->amount) |
86
|
|
|
->where('transaction_journals.date', $this->date->format('Y-m-d 00:00:00')) |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
return $query->get(['transaction_journals.id', 'transaction_journals.transaction_group_id']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $amount |
94
|
|
|
*/ |
95
|
|
|
public function setAmount(string $amount): void |
96
|
|
|
{ |
97
|
|
|
$this->amount = $amount; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $date |
102
|
|
|
*/ |
103
|
|
|
public function setDate(string $date): void |
104
|
|
|
{ |
105
|
|
|
try { |
106
|
|
|
$carbon = Carbon::createFromFormat('Y-m-d', $date); |
107
|
|
|
} catch (InvalidArgumentException $e) { |
108
|
|
|
Log::error($e->getMessage()); |
109
|
|
|
$carbon = Carbon::now(); |
110
|
|
|
} |
111
|
|
|
$this->date = $carbon; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $description |
116
|
|
|
*/ |
117
|
|
|
public function setDescription(string $description): void |
118
|
|
|
{ |
119
|
|
|
$this->description = $description; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $destination |
124
|
|
|
*/ |
125
|
|
|
public function setDestination(string $destination): void |
126
|
|
|
{ |
127
|
|
|
if (is_numeric($destination)) { |
128
|
|
|
$this->destination = $this->accountRepository->findNull((int)$destination); |
129
|
|
|
} |
130
|
|
|
if (null === $this->destination) { |
131
|
|
|
$this->destination = $this->accountRepository->findByName($destination, $this->types); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $source |
137
|
|
|
*/ |
138
|
|
|
public function setSource(string $source): void |
139
|
|
|
{ |
140
|
|
|
if (is_numeric($source)) { |
141
|
|
|
$this->source = $this->accountRepository->findNull((int)$source); |
142
|
|
|
} |
143
|
|
|
if (null === $this->source) { |
144
|
|
|
$this->source = $this->accountRepository->findByName($source, $this->types); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |