1
|
|
|
<?php namespace App\Repositories; |
2
|
|
|
|
3
|
|
|
use App\Exceptions\RepositoryException; |
4
|
|
|
use App\Models\CashSnapshots; |
5
|
|
|
use Auth; |
6
|
|
|
use DB; |
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Database\Eloquent\Collection; |
9
|
|
|
use Session; |
10
|
|
|
use Validator; |
11
|
|
|
|
12
|
|
|
class SnapshotRepository extends Repository |
13
|
|
|
{ |
14
|
|
|
public function getModelName() |
15
|
|
|
{ |
16
|
|
|
return 'App\Models\CashSnapshots'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
View Code Duplication |
public function all() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
try { |
22
|
|
|
return $this->model->where('group_id', '=', Session::get('groupID')) |
23
|
|
|
->orderBy('time', 'desc') |
24
|
|
|
->get(); |
25
|
|
|
} catch (Exception $e) { |
26
|
|
|
throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function history() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
try { |
33
|
|
|
return $this->model->where('group_id', '=', Session::get('groupID')) |
34
|
|
|
->where('is_closed', true) |
35
|
|
|
->orderBy('time', 'desc') |
36
|
|
|
->get(); |
37
|
|
|
} catch (Exception $e) { |
38
|
|
|
throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function get($id) |
43
|
|
|
{ |
44
|
|
|
$this->validateID($id); |
45
|
|
|
|
46
|
|
|
$snapshot = NULL; |
47
|
|
|
try { |
48
|
|
|
$snapshot = $this->model->where('group_id', '=', Session::get('groupID')) |
49
|
|
|
->where('cs_id', '=', $id) |
50
|
|
|
->first(); |
51
|
|
|
} catch (Exception $e) { |
52
|
|
|
throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($snapshot == NULL) |
56
|
|
|
throw new RepositoryException('Snapshot with ID ' . $id . ' not found', RepositoryException::RESOURCE_NOT_FOUND); |
57
|
|
|
|
58
|
|
|
return $snapshot; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function getNext($id) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->validateID($id); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$snapshot = $this->model->where('cs_id', '>', $id)->orderBy('cs_id')->firstOrFail(); |
67
|
|
|
} catch (Exception $e) { |
68
|
|
|
throw new RepositoryException('Could not retrieve next record', RepositoryException::DATABASE_ERROR); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $snapshot; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function store(array $data) |
75
|
|
|
{ |
76
|
|
|
$this->validate($data); |
77
|
|
|
|
78
|
|
|
$snapshot = new CashSnapshots(); |
79
|
|
|
|
80
|
|
|
$snapshot->snapshot_title = $data['title']; |
|
|
|
|
81
|
|
|
$snapshot->description = $data['description']; |
|
|
|
|
82
|
|
|
$snapshot->amount = $data['amount']; |
|
|
|
|
83
|
|
|
$snapshot->time = date('Y-m-d H:i:s'); |
|
|
|
|
84
|
|
|
$snapshot->group_id = Session::get('groupID'); |
|
|
|
|
85
|
|
|
$snapshot->user_id = Auth::id(); |
|
|
|
|
86
|
|
|
$snapshot->is_closed = false; |
|
|
|
|
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
|
90
|
|
|
$this->closeLastSnapshot(); |
91
|
|
|
|
92
|
|
|
$snapshot->save(); |
93
|
|
|
$snapshot->id = DB::getPdo()->lastInsertId(); |
|
|
|
|
94
|
|
|
} catch (Exception $e) { |
95
|
|
|
throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $snapshot; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function current() |
102
|
|
|
{ |
103
|
|
|
$snapshot = NULL; |
104
|
|
|
try { |
105
|
|
|
$snapshot = $this->model->where('group_id', '=', Session::get('groupID')) |
106
|
|
|
->where('is_closed', '=', false) |
107
|
|
|
->orderBy('time', 'desc') |
108
|
|
|
->first(); |
109
|
|
|
} catch (Exception $e) { |
110
|
|
|
throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($snapshot == NULL) |
114
|
|
|
throw new RepositoryException('Current snapshot not found', RepositoryException::RESOURCE_NOT_FOUND); |
115
|
|
|
|
116
|
|
|
return $snapshot; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function closeLastSnapshot() |
120
|
|
|
{ |
121
|
|
|
$lastSnapshot = $this->model->where('group_id', '=', Session::get('groupID')) |
122
|
|
|
->where('is_closed', '=', false) |
123
|
|
|
->orderBy('time', 'desc') |
124
|
|
|
->first(); |
125
|
|
|
|
126
|
|
|
if ($lastSnapshot == NULL) |
127
|
|
|
return; |
128
|
|
|
|
129
|
|
|
$this->updatePredictedAmount($lastSnapshot->cs_id, true); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function updatePredictedAmount($snapshotID, $closeSnapshot=false) |
133
|
|
|
{ |
134
|
|
|
try { |
135
|
|
|
$snapshot = $this->model->findOrFail($snapshotID); |
136
|
|
|
} catch (Exception $e) { |
137
|
|
|
throw new RepositoryException('Could not find snapshot', RepositoryException::DATABASE_ERROR); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($snapshot->is_closed) { |
141
|
|
|
throw new RepositoryException('Could not update closed snapshot', RepositoryException::RESOURCE_DENIED); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$details = $this->model->findOrFail($snapshotID) |
145
|
|
|
->details() |
146
|
|
|
->get(); |
147
|
|
|
|
148
|
|
|
$predictedAmount = $snapshot->amount; |
149
|
|
|
foreach ($details as $detail) { |
150
|
|
|
|
151
|
|
|
$predictedAmount += $detail->sum; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$snapshot->predicted_amount = $predictedAmount; |
155
|
|
|
|
156
|
|
|
if ($closeSnapshot) { |
157
|
|
|
$snapshot->is_closed = true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
try { |
161
|
|
|
$snapshot->save(); |
162
|
|
|
} catch (Exception $e) { |
163
|
|
|
throw new RepositoryException('Could not update snapshot', RepositoryException::DATABASE_ERROR); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function APIFormat(Collection $object) |
168
|
|
|
{ |
169
|
|
|
$responseArray = []; |
170
|
|
|
foreach ($object as $record) { |
171
|
|
|
array_push($responseArray, $this->formatRecord($record)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $responseArray; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function formatRecord($object) |
178
|
|
|
{ |
179
|
|
|
|
180
|
|
|
$formatted = new stdClass(); |
181
|
|
|
|
182
|
|
|
$formatted->id = intval($object->cs_id); |
183
|
|
|
$formatted->user = intval($object->user_id); |
184
|
|
|
$formatted->amount = floatval($object->amount); |
185
|
|
|
|
186
|
|
|
$formatted->title = $object->snapshot_title; |
187
|
|
|
$formatted->description = $object->description; |
188
|
|
|
$formatted->time = $object->time; |
189
|
|
|
|
190
|
|
|
if ($object->predicted_amount == NULL) { |
191
|
|
|
$formatted->predicted = NULL; |
192
|
|
|
} else { |
193
|
|
|
$formatted->predicted = floatval($object->predicted_amount); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$formatted->closed = (bool)$object->is_closed; |
197
|
|
|
|
198
|
|
|
return $formatted; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function validate(array $data) |
202
|
|
|
{ |
203
|
|
|
$rules = CashSnapshots::$validationRules; |
204
|
|
|
|
205
|
|
|
$validator = Validator::make($data, $rules); |
206
|
|
|
|
207
|
|
|
if ($validator->fails()) { |
208
|
|
|
throw new RepositoryException('Validation failed', RepositoryException::VALIDATION_FAILED); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
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.