1
|
|
|
<?php namespace App\Repositories; |
2
|
|
|
|
3
|
|
|
use App\Exceptions\RepositoryException; |
4
|
|
|
use App\Models\SnapshotDetails; |
5
|
|
|
use Session; |
6
|
|
|
use Exception; |
7
|
|
|
use Auth; |
8
|
|
|
use Validator; |
9
|
|
|
|
10
|
|
|
class SnapshotDetailsRepository extends Repository { |
11
|
|
|
|
12
|
|
|
public function getModelName() |
13
|
|
|
{ |
14
|
|
|
return 'App\Models\SnapshotDetails'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
View Code Duplication |
public function get($id) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->validateID($id); |
20
|
|
|
|
21
|
|
|
try { |
22
|
|
|
return $this->model->findOrFail($id); |
23
|
|
|
} |
24
|
|
|
catch(\Exception $e) { |
25
|
|
|
throw new RepositoryException('Could not retrieve snapshot detail', RepositoryException::DATABASE_ERROR); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function fromSnapshot($id) |
30
|
|
|
{ |
31
|
|
|
$this->validateID($id); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
return $this->model->group( Session::get('groupID') ) |
35
|
|
|
->leftJoin('users', 'snapshot_details.user_id', '=', 'users.user_id') |
36
|
|
|
->where('snapshot_details.cs_id', '=', $id) |
37
|
|
|
->get(); |
38
|
|
|
} |
39
|
|
|
catch(Exception $e) { |
40
|
|
|
throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function APIFormat($object) |
45
|
|
|
{ |
46
|
|
|
if( !is_object($object) ) |
47
|
|
|
return null; |
48
|
|
|
|
49
|
|
|
if( get_class($object) === 'SnapshotDetails' ) |
50
|
|
|
return $this->formatRecord( $object ); |
51
|
|
|
|
52
|
|
|
$responseArray = []; |
53
|
|
|
foreach ($object as $record) { |
54
|
|
|
|
55
|
|
|
array_push($responseArray, $this->formatRecord( $record )); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $responseArray; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function store(array $data) |
62
|
|
|
{ |
63
|
|
|
$this->validate($data); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$detail = new SnapshotDetails(); |
67
|
|
|
|
68
|
|
|
$detail->type = $data['type']; |
|
|
|
|
69
|
|
|
$detail->sum = floatval($data['sum']); |
|
|
|
|
70
|
|
|
$detail->timed = date('Y-m-d G:i:s', $data['time']); |
|
|
|
|
71
|
|
|
$detail->user_id = Auth::id(); |
|
|
|
|
72
|
|
|
$detail->cs_id = $data['cs_id']; |
|
|
|
|
73
|
|
|
|
74
|
|
|
if( $data['type']==='SALE' ) { |
75
|
|
|
$detail->sale_id = $data['sale_id']; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
if( isset($data['comment']) ) { |
78
|
|
|
$detail->comment = $data['comment']; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$detail->save(); |
82
|
|
|
} |
83
|
|
|
catch(\Exception $e) { |
84
|
|
|
throw new RepositoryException('Could not save snapshot detail in database', RepositoryException::DATABASE_ERROR); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function delete($id) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$this->validateID($id); |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
$this->model->destroy($id); |
94
|
|
|
} |
95
|
|
|
catch(\Exception $e) { |
96
|
|
|
throw new RepositoryException('Could not delete snapshot detail', RepositoryException::DATABASE_ERROR); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function validate(array $data) |
101
|
|
|
{ |
102
|
|
|
// If it's a sale, it must be positive. Otherwise, it's a cash operation and it can't be 0 |
103
|
|
|
$sumExtraValidation = ( $data['type']==='SALE' ) ? '|min:0' : '|not_in:0'; |
104
|
|
|
|
105
|
|
|
$validator = Validator::make($data, |
106
|
|
|
[ |
107
|
|
|
'type' => 'required|in:SALE,CASH', |
108
|
|
|
'sum' => 'required|numeric'.$sumExtraValidation, |
109
|
|
|
'time' => 'required|integer|min:0', |
110
|
|
|
'sale_id' => 'sometimes|integer|min:0', |
111
|
|
|
'cs_id' => 'required|integer|min:0', |
112
|
|
|
'comment' => '' |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
if( $validator->fails() ) { |
117
|
|
|
throw new RepositoryException('Cash snapshot detail validation failed', RepositoryException::VALIDATION_FAILED); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function formatRecord($object) { |
122
|
|
|
|
123
|
|
|
$formatted = new stdClass(); |
124
|
|
|
|
125
|
|
|
$formatted->id = intval($object->csd_id); |
126
|
|
|
$formatted->user = intval($object->user_id); |
127
|
|
|
$formatted->sale = intval($object->sale_id); |
128
|
|
|
$formatted->sum = floatval($object->sum); |
129
|
|
|
|
130
|
|
|
$formatted->type = $object->type; |
131
|
|
|
$formatted->comment = $object->comment; |
132
|
|
|
$formatted->time = $object->timed; |
133
|
|
|
|
134
|
|
|
return $formatted; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
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.