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\Account; |
12
|
|
|
use App\Models\Category; |
13
|
|
|
use App\Models\Payee; |
14
|
|
|
use App\Models\Transaction; |
15
|
|
|
use Log; |
16
|
|
|
|
17
|
|
|
class ClientApiService |
18
|
|
|
{ |
19
|
|
|
public function getTransactions() |
20
|
|
|
{ |
21
|
|
|
$transactions = Transaction::all(); |
22
|
|
|
|
23
|
|
|
return $transactions; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function deleteAccounts() |
27
|
|
|
{ |
28
|
|
|
// TODO: find better method than where hack |
29
|
|
|
Account::where('id', '>', 0)->delete(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function importBankAccounts($postData) |
33
|
|
|
{ |
34
|
|
|
Log::debug('MmexController.importBankAccounts(), $accounts', [$postData->Accounts]); |
35
|
|
|
foreach ($postData->Accounts as $account) { |
36
|
|
|
Account::create([ |
37
|
|
|
'name' => $account->AccountName, |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function deletePayees() |
43
|
|
|
{ |
44
|
|
|
// TODO: find better method than where hack |
45
|
|
|
Payee::where('id', '>', 0)->delete(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function importPayees($postData) |
49
|
|
|
{ |
50
|
|
|
Log::debug('MmexController.importPayees(), $payees', [$postData->Payees]); |
51
|
|
|
|
52
|
|
|
foreach ($postData->Payees as $payee) { |
53
|
|
|
$categoryName = $payee->DefCateg; |
54
|
|
|
$subCategoryName = $payee->DefSubCateg; |
55
|
|
|
|
56
|
|
|
$category = Category::where('name', $subCategoryName)->whereHas('parentCategory', function ($query) use ($categoryName) { |
57
|
|
|
$query->where('name', $categoryName); |
58
|
|
|
})->first(); |
59
|
|
|
|
60
|
|
|
if ($category) { |
61
|
|
|
$category->defaultForPayees()->create([ |
62
|
|
|
'name' => $payee->PayeeName, |
63
|
|
|
]); |
64
|
|
|
} else { |
65
|
|
|
// ignore default/last category and just create entry |
66
|
|
|
Payee::create([ |
67
|
|
|
'name' => $payee->PayeeName, |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function deleteCategories() |
74
|
|
|
{ |
75
|
|
|
// TODO: find better method than where hack |
76
|
|
|
Category::where('id', '>', 0)->delete(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function importCategories($postData) |
80
|
|
|
{ |
81
|
|
|
$categories = collect($postData->Categories); |
82
|
|
|
|
83
|
|
|
$grouped = $categories->groupBy('CategoryName'); |
84
|
|
|
|
85
|
|
|
foreach ($grouped as $categoryName => $subCategories) { |
86
|
|
|
$category = $this->createOrGetCategory($categoryName); |
87
|
|
|
|
88
|
|
|
foreach ($subCategories as $subCategory) { |
89
|
|
|
$this->createOrGetSubCategory($category, $subCategory->SubCategoryName); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
private function createOrGetCategory($name) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$existingCategory = Category::whereName($name)->first(); |
97
|
|
|
|
98
|
|
|
if ($existingCategory) { |
99
|
|
|
return $existingCategory; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$newCategory = Category::create([ |
103
|
|
|
'name' => $name, |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
return $newCategory; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
private function createOrGetSubCategory(Category $parentCategory, $name) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$existingCategory = Category::whereName($name)->first(); |
112
|
|
|
|
113
|
|
|
if ($existingCategory) { |
114
|
|
|
return $existingCategory; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$newCategory = $parentCategory->subCategories()->create([ |
118
|
|
|
'name' => $name, |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
return $newCategory; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function deleteTransactions($transactionId) |
125
|
|
|
{ |
126
|
|
|
Transaction::whereId($transactionId)->delete(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.