1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TransferController.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\Api\V1\Controllers\Search; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use FireflyIII\Api\V1\Controllers\Controller; |
26
|
|
|
use FireflyIII\Api\V1\Requests\Search\TransferRequest; |
27
|
|
|
use FireflyIII\Exceptions\FireflyException; |
28
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface; |
29
|
|
|
use FireflyIII\Support\Search\TransferSearch; |
30
|
|
|
use FireflyIII\Transformers\TransactionGroupTransformer; |
31
|
|
|
use FireflyIII\User; |
32
|
|
|
use Illuminate\Http\JsonResponse; |
33
|
|
|
use Illuminate\Http\Request; |
34
|
|
|
use Illuminate\Http\Response; |
35
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
36
|
|
|
use League\Fractal\Resource\Collection as FractalCollection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class TransferController |
40
|
|
|
*/ |
41
|
|
|
class TransferController extends Controller |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @param Request $request |
45
|
|
|
* |
46
|
|
|
* @return JsonResponse|Response |
47
|
|
|
* @throws FireflyException |
48
|
|
|
*/ |
49
|
|
|
public function search(TransferRequest $request) |
50
|
|
|
{ |
51
|
|
|
// configure transfer search to search for a > b |
52
|
|
|
$search = app(TransferSearch::class); |
53
|
|
|
$search->setSource($request->get('source')); |
|
|
|
|
54
|
|
|
$search->setDestination($request->get('destination')); |
|
|
|
|
55
|
|
|
$search->setAmount($request->get('amount')); |
|
|
|
|
56
|
|
|
$search->setDescription($request->get('description')); |
|
|
|
|
57
|
|
|
$search->setDate($request->get('date')); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$left = $search->search(); |
60
|
|
|
|
61
|
|
|
// configure transfer search to search for b > a |
62
|
|
|
$search->setSource($request->get('destination')); |
63
|
|
|
$search->setDestination($request->get('source')); |
64
|
|
|
$search->setAmount($request->get('amount')); |
65
|
|
|
$search->setDescription($request->get('description')); |
66
|
|
|
$search->setDate($request->get('date')); |
67
|
|
|
|
68
|
|
|
$right = $search->search(); |
69
|
|
|
|
70
|
|
|
// add parameters to URL: |
71
|
|
|
$this->parameters->set('source', $request->get('source')); |
72
|
|
|
$this->parameters->set('destination', $request->get('destination')); |
73
|
|
|
$this->parameters->set('amount', $request->get('amount')); |
74
|
|
|
$this->parameters->set('description', $request->get('description')); |
75
|
|
|
$this->parameters->set('date', $request->get('date')); |
76
|
|
|
|
77
|
|
|
// get all journal ID's. |
78
|
|
|
$total = $left->merge($right)->unique('id')->pluck('id')->toArray(); |
79
|
|
|
if (0 === count($total)) { |
80
|
|
|
// forces search to be empty. |
81
|
|
|
$total = [-1]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// collector to return results. |
85
|
|
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
86
|
|
|
$manager = $this->getManager(); |
87
|
|
|
/** @var User $admin */ |
88
|
|
|
$admin = auth()->user(); |
89
|
|
|
|
90
|
|
|
// use new group collector: |
91
|
|
|
/** @var GroupCollectorInterface $collector */ |
92
|
|
|
$collector = app(GroupCollectorInterface::class); |
93
|
|
|
$collector |
94
|
|
|
->setUser($admin) |
95
|
|
|
// all info needed for the API: |
96
|
|
|
->withAPIInformation() |
97
|
|
|
// set page size: |
98
|
|
|
->setLimit($pageSize) |
99
|
|
|
// set page to retrieve |
100
|
|
|
->setPage(1) |
101
|
|
|
->setJournalIds($total); |
102
|
|
|
|
103
|
|
|
$paginator = $collector->getPaginatedGroups(); |
104
|
|
|
$paginator->setPath(route('api.v1.search.transfers') . $this->buildParams()); |
105
|
|
|
$transactions = $paginator->getCollection(); |
106
|
|
|
|
107
|
|
|
/** @var TransactionGroupTransformer $transformer */ |
108
|
|
|
$transformer = app(TransactionGroupTransformer::class); |
109
|
|
|
$transformer->setParameters($this->parameters); |
110
|
|
|
|
111
|
|
|
$resource = new FractalCollection($transactions, $transformer, 'transactions'); |
112
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
113
|
|
|
|
114
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
115
|
|
|
} |
116
|
|
|
} |