1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: okaufmann |
5
|
|
|
* Date: 22.10.2016 |
6
|
|
|
* Time: 14:53. |
7
|
|
|
*/ |
8
|
|
|
namespace App\Services; |
9
|
|
|
|
10
|
|
|
use App\Models\Account; |
11
|
|
|
use App\Models\Category; |
12
|
|
|
use App\Models\Payee; |
13
|
|
|
use App\Models\Transaction; |
14
|
|
|
use App\Transformers\TransactionTransformer; |
15
|
|
|
use Log; |
16
|
|
|
|
17
|
|
|
class MmexService |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var TransactionTransformer |
21
|
|
|
*/ |
22
|
|
|
private $transactionTransformer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* MmexService constructor. |
26
|
|
|
* @param TransactionTransformer $transactionTransformer |
27
|
|
|
*/ |
28
|
|
|
public function __construct(TransactionTransformer $transactionTransformer) |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
$this->transactionTransformer = $transactionTransformer; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function getTransactions() |
36
|
|
|
{ |
37
|
|
|
$transactions = Transaction::all()->all(); |
38
|
|
|
|
39
|
|
|
return $this->transactionTransformer->transformCollection($transactions); |
40
|
|
|
// example |
41
|
|
|
return [ |
|
|
|
|
42
|
|
|
0 => [ |
43
|
|
|
"ID" => 1, |
44
|
|
|
"Date" => "2016-10-07", |
45
|
|
|
"Account" => "Another Account", |
46
|
|
|
"ToAccount" => "None", |
47
|
|
|
"Status" => "R", |
48
|
|
|
"Type" => "Zahlung", |
49
|
|
|
"Payee" => "Migros", |
50
|
|
|
"Category" => "Einkauf", |
51
|
|
|
"SubCategory" => "Etwas anderes", |
52
|
|
|
"Amount" => "123", |
53
|
|
|
"Notes" => "Das ist ein \r\nMeeeeehrzeiliger \r\nText", |
54
|
|
|
"Attachments" => "Transaction_1_Attach1.png;Transaction_1_Attach2.jpg" |
55
|
|
|
] |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function deleteAccounts() |
60
|
|
|
{ |
61
|
|
|
// TODO: find better method than where hack |
62
|
|
|
Account::where('id', '>', 0)->delete(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function importBankAccounts($postData) |
66
|
|
|
{ |
67
|
|
|
Log::debug('MmexController.importBankAccounts(), $accounts', [$postData->Accounts]); |
68
|
|
|
foreach ($postData->Accounts as $account) { |
69
|
|
|
Account::create([ |
70
|
|
|
'name' => $account->AccountName, |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function deletePayees() |
76
|
|
|
{ |
77
|
|
|
// TODO: find better method than where hack |
78
|
|
|
Payee::where('id', '>', 0)->delete(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function importPayees($postData) |
82
|
|
|
{ |
83
|
|
|
Log::debug('MmexController.importPayees(), $payees', [$postData->Payees]); |
84
|
|
|
|
85
|
|
|
foreach ($postData->Payees as $payee) { |
86
|
|
|
$categoryName = $payee->DefCateg; |
87
|
|
|
$subCategoryName = $payee->DefSubCateg; |
88
|
|
|
|
89
|
|
|
$category = Category::where('name', $subCategoryName)->whereHas('parentCategory', function ($query) use ($categoryName) { |
90
|
|
|
$query->where('name', $categoryName); |
91
|
|
|
})->first(); |
92
|
|
|
|
93
|
|
|
if ($category) { |
94
|
|
|
$category->defaultForPayees()->create([ |
95
|
|
|
'name' => $payee->PayeeName, |
96
|
|
|
]); |
97
|
|
|
} else { |
98
|
|
|
// ignore default/last category and just create entry |
99
|
|
|
Payee::create([ |
100
|
|
|
'name' => $payee->PayeeName, |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function deleteCategories() |
107
|
|
|
{ |
108
|
|
|
// TODO: find better method than where hack |
109
|
|
|
Category::where('id', '>', 0)->delete(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function importCategories($postData) |
113
|
|
|
{ |
114
|
|
|
$categories = collect($postData->Categories); |
115
|
|
|
|
116
|
|
|
$grouped = $categories->groupBy('CategoryName'); |
117
|
|
|
|
118
|
|
|
foreach ($grouped as $categoryName => $subCategories) { |
119
|
|
|
$category = $this->createOrGetCategory($categoryName); |
120
|
|
|
|
121
|
|
|
foreach ($subCategories as $subCategory) { |
122
|
|
|
$this->createOrGetSubCategory($category, $subCategory->SubCategoryName); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
private function createOrGetCategory($name) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$existingCategory = Category::whereName($name)->first(); |
130
|
|
|
|
131
|
|
|
if ($existingCategory) { |
132
|
|
|
return $existingCategory; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$newCategory = Category::create([ |
136
|
|
|
'name' => $name, |
137
|
|
|
]); |
138
|
|
|
|
139
|
|
|
return $newCategory; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
private function createOrGetSubCategory(Category $parentCategory, $name) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$existingCategory = Category::whereName($name)->first(); |
145
|
|
|
|
146
|
|
|
if ($existingCategory) { |
147
|
|
|
return $existingCategory; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$newCategory = $parentCategory->subCategories()->create([ |
151
|
|
|
'name' => $name, |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
return $newCategory; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function deleteTransactions($transactionId) |
158
|
|
|
{ |
159
|
|
|
Transaction::whereId($transactionId)->delete(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.