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; |
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 MmexService |
18
|
|
|
{ |
19
|
|
|
public function getTransactions() |
20
|
|
|
{ |
21
|
|
|
$transactions = Transaction::all(); |
22
|
|
|
|
23
|
|
|
return $transactions; |
24
|
|
|
// example |
|
|
|
|
25
|
|
|
// return [ |
26
|
|
|
// 0 => [ |
27
|
|
|
// 'ID' => 1, |
28
|
|
|
// 'Date' => '2016-10-07', |
29
|
|
|
// 'Account' => 'Another Account', |
30
|
|
|
// 'ToAccount' => 'None', |
31
|
|
|
// 'Status' => 'R', |
32
|
|
|
// 'Type' => 'Zahlung', |
33
|
|
|
// 'Payee' => 'Migros', |
34
|
|
|
// 'Category' => 'Einkauf', |
35
|
|
|
// 'SubCategory' => 'Etwas anderes', |
36
|
|
|
// 'Amount' => '123', |
37
|
|
|
// 'Notes' => "Das ist ein \r\nMeeeeehrzeiliger \r\nText", |
38
|
|
|
// 'Attachments' => 'Transaction_1_Attach1.png;Transaction_1_Attach2.jpg', |
39
|
|
|
// ], |
40
|
|
|
// ]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function deleteAccounts() |
44
|
|
|
{ |
45
|
|
|
// TODO: find better method than where hack |
46
|
|
|
Account::where('id', '>', 0)->delete(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function importBankAccounts($postData) |
50
|
|
|
{ |
51
|
|
|
Log::debug('MmexController.importBankAccounts(), $accounts', [$postData->Accounts]); |
52
|
|
|
foreach ($postData->Accounts as $account) { |
53
|
|
|
Account::create([ |
54
|
|
|
'name' => $account->AccountName, |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function deletePayees() |
60
|
|
|
{ |
61
|
|
|
// TODO: find better method than where hack |
62
|
|
|
Payee::where('id', '>', 0)->delete(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function importPayees($postData) |
66
|
|
|
{ |
67
|
|
|
Log::debug('MmexController.importPayees(), $payees', [$postData->Payees]); |
68
|
|
|
|
69
|
|
|
foreach ($postData->Payees as $payee) { |
70
|
|
|
$categoryName = $payee->DefCateg; |
71
|
|
|
$subCategoryName = $payee->DefSubCateg; |
72
|
|
|
|
73
|
|
|
$category = Category::where('name', $subCategoryName)->whereHas('parentCategory', function ($query) use ($categoryName) { |
74
|
|
|
$query->where('name', $categoryName); |
75
|
|
|
})->first(); |
76
|
|
|
|
77
|
|
|
if ($category) { |
78
|
|
|
$category->defaultForPayees()->create([ |
79
|
|
|
'name' => $payee->PayeeName, |
80
|
|
|
]); |
81
|
|
|
} else { |
82
|
|
|
// ignore default/last category and just create entry |
83
|
|
|
Payee::create([ |
84
|
|
|
'name' => $payee->PayeeName, |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function deleteCategories() |
91
|
|
|
{ |
92
|
|
|
// TODO: find better method than where hack |
93
|
|
|
Category::where('id', '>', 0)->delete(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function importCategories($postData) |
97
|
|
|
{ |
98
|
|
|
$categories = collect($postData->Categories); |
99
|
|
|
|
100
|
|
|
$grouped = $categories->groupBy('CategoryName'); |
101
|
|
|
|
102
|
|
|
foreach ($grouped as $categoryName => $subCategories) { |
103
|
|
|
$category = $this->createOrGetCategory($categoryName); |
104
|
|
|
|
105
|
|
|
foreach ($subCategories as $subCategory) { |
106
|
|
|
$this->createOrGetSubCategory($category, $subCategory->SubCategoryName); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
private function createOrGetCategory($name) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$existingCategory = Category::whereName($name)->first(); |
114
|
|
|
|
115
|
|
|
if ($existingCategory) { |
116
|
|
|
return $existingCategory; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$newCategory = Category::create([ |
120
|
|
|
'name' => $name, |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
return $newCategory; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
private function createOrGetSubCategory(Category $parentCategory, $name) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$existingCategory = Category::whereName($name)->first(); |
129
|
|
|
|
130
|
|
|
if ($existingCategory) { |
131
|
|
|
return $existingCategory; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$newCategory = $parentCategory->subCategories()->create([ |
135
|
|
|
'name' => $name, |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
return $newCategory; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function deleteTransactions($transactionId) |
142
|
|
|
{ |
143
|
|
|
Transaction::whereId($transactionId)->delete(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.