1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: okaufmann |
5
|
|
|
* Date: 22.10.2016 |
6
|
|
|
* Time: 14:53. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace App\Services\Mmex; |
10
|
|
|
|
11
|
|
|
use App\Models\Category; |
12
|
|
|
use App\Models\User; |
13
|
|
|
use Log; |
14
|
|
|
use Spatie\MediaLibrary\Media; |
15
|
|
|
|
16
|
|
|
class ClientApiService |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
20
|
|
|
*/ |
21
|
|
|
public function getTransactions(User $user) |
22
|
|
|
{ |
23
|
|
|
$transactions = $user->transactions; |
24
|
|
|
|
25
|
|
|
return $transactions; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function deleteAccounts(User $user) |
29
|
|
|
{ |
30
|
|
|
// TODO: find better method than where hack |
31
|
|
|
$user->accounts()->where('id', '>', 0)->delete(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function importBankAccounts(User $user, $postData) |
35
|
|
|
{ |
36
|
|
|
Log::debug('MmexController.importBankAccounts(), $accounts', [$postData->Accounts]); |
37
|
|
|
foreach ($postData->Accounts as $account) { |
38
|
|
|
$user->accounts()->create([ |
39
|
|
|
'name' => $account->AccountName, |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function deletePayees(User $user) |
45
|
|
|
{ |
46
|
|
|
// TODO: find better method than where hack |
47
|
|
|
$user->payees()->where('id', '>', 0)->delete(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function importPayees(User $user, $postData) |
51
|
|
|
{ |
52
|
|
|
Log::debug('MmexController.importPayees(), $payees', [$postData->Payees]); |
53
|
|
|
|
54
|
|
|
foreach ($postData->Payees as $payeeData) { |
55
|
|
|
$existingPayee = $user->payees()->onlyTrashed()->where('name', $payeeData->PayeeName)->first(); |
56
|
|
|
|
57
|
|
|
if ($existingPayee) { |
58
|
|
|
$existingPayee->restore(); |
59
|
|
|
$payee = $existingPayee; |
60
|
|
|
} else { |
61
|
|
|
$payee = $user->payees()->create([ |
62
|
|
|
'name' => $payeeData->PayeeName, |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$categoryName = $payeeData->DefCateg; |
67
|
|
|
$subCategoryName = $payeeData->DefSubCateg; |
68
|
|
|
|
69
|
|
|
if (!empty($categoryName)) { |
70
|
|
|
$category = $this->createOrGetCategory($user, $categoryName); |
71
|
|
|
|
72
|
|
|
if (!empty($subCategoryName)) { |
73
|
|
|
$category = $this->createOrGetSubCategory($user, $category, $subCategoryName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$category->defaultForPayees()->save($payee); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$user->payees()->onlyTrashed()->forceDelete(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function deleteCategories(User $user) |
84
|
|
|
{ |
85
|
|
|
// TODO: find better method than where hack |
86
|
|
|
$user->categories()->where('id', '>', 0)->delete(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function importCategories(User $user, $postData) |
90
|
|
|
{ |
91
|
|
|
$categories = collect($postData->Categories); |
92
|
|
|
|
93
|
|
|
$grouped = $categories->groupBy('CategoryName'); |
94
|
|
|
|
95
|
|
|
foreach ($grouped as $categoryName => $subCategories) { |
96
|
|
|
$category = $this->createOrGetCategory($user, $categoryName); |
97
|
|
|
|
98
|
|
|
foreach ($subCategories as $subCategory) { |
99
|
|
|
$this->createOrGetSubCategory($user, $category, $subCategory->SubCategoryName); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$user->categories()->onlyTrashed()->forceDelete(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function deleteTransactions(User $user, $transactionId) |
107
|
|
|
{ |
108
|
|
|
$user->transactions()->whereId($transactionId)->delete(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param User $user |
113
|
|
|
* @param $fileName |
114
|
|
|
* |
115
|
|
|
* @return Media |
116
|
|
|
*/ |
117
|
|
|
public function getAttachment(User $user, $fileName) |
118
|
|
|
{ |
119
|
|
|
// extract transaction |
120
|
|
|
$fileNameParts = explode('_', $fileName); |
121
|
|
|
$transactionId = $fileNameParts[1]; |
122
|
|
|
$transaction = $user->transactions()->findOrFail($transactionId); |
123
|
|
|
|
124
|
|
|
// get attachment of transaction |
125
|
|
|
$media = $transaction->getMedia('attachments')->first(function ($item) use ($fileName) { |
126
|
|
|
return $item->file_name == $fileName; |
127
|
|
|
}); |
128
|
|
|
|
129
|
|
|
return $media; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param User $user |
134
|
|
|
* @param $name |
135
|
|
|
* |
136
|
|
|
* @return Category |
137
|
|
|
*/ |
138
|
|
|
private function createOrGetCategory(User $user, $name) |
139
|
|
|
{ |
140
|
|
|
$existingCategory = $user->categories()->withTrashed()->whereName($name)->first(); |
141
|
|
|
|
142
|
|
|
if ($existingCategory) { |
143
|
|
|
$existingCategory->restore(); |
144
|
|
|
|
145
|
|
|
return $existingCategory; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$newCategory = $user->categories()->create([ |
149
|
|
|
'name' => $name, |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
return $newCategory; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function createOrGetSubCategory(User $user, Category $parentCategory, $name) |
156
|
|
|
{ |
157
|
|
|
$existingCategory = $user->categories()->withTrashed()->whereHas('parentCategory')->whereName($name)->first(); |
158
|
|
|
|
159
|
|
|
if ($existingCategory) { |
160
|
|
|
$existingCategory->restore(); |
161
|
|
|
|
162
|
|
|
return $existingCategory; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$newCategory = $user->categories()->create([ |
166
|
|
|
'name' => $name, |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
$parentCategory->categories()->save($newCategory); |
170
|
|
|
|
171
|
|
|
return $newCategory; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|