|
1
|
|
|
<?php namespace App\Repositories; |
|
2
|
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
|
4
|
|
|
use Carbon\CarbonInterval; |
|
5
|
|
|
use App\Exceptions\RepositoryException; |
|
6
|
|
|
use App\Models\Sales; |
|
7
|
|
|
use Auth; |
|
8
|
|
|
use DB; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Session; |
|
11
|
|
|
use Validator; |
|
12
|
|
|
|
|
13
|
|
|
class SaleRepository extends Repository |
|
14
|
|
|
{ |
|
15
|
|
|
public function getModelName() |
|
16
|
|
|
{ |
|
17
|
|
|
return 'App\Models\Sales'; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function register(array $saleData) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->validate($saleData); |
|
23
|
|
|
|
|
24
|
|
|
try { |
|
25
|
|
|
$sale = new Sales(); |
|
26
|
|
|
|
|
27
|
|
|
$sale->group_id = Session::get('groupID'); |
|
|
|
|
|
|
28
|
|
|
$sale->user_id = Auth::id(); |
|
|
|
|
|
|
29
|
|
|
$sale->time = date('Y-m-d G:i:s', $saleData['time']); |
|
|
|
|
|
|
30
|
|
|
$sale->sum = floatval($saleData['sum']); |
|
|
|
|
|
|
31
|
|
|
$sale->paid = floatval($saleData['paid']); |
|
|
|
|
|
|
32
|
|
|
$sale->is_active = true; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
$sale->save(); |
|
35
|
|
|
|
|
36
|
|
|
return DB::getPdo()->lastInsertId(); |
|
37
|
|
|
} |
|
38
|
|
|
catch (Exception $e) |
|
39
|
|
|
{ |
|
40
|
|
|
throw new RepositoryException('Could not save sale in database: ' . $e->getMessage(), RepositoryException::DATABASE_ERROR); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function countByInterval(CarbonInterval $interval) |
|
45
|
|
|
{ |
|
46
|
|
|
$beginDate = Carbon::now()->sub($interval)->addHour(); |
|
47
|
|
|
|
|
48
|
|
|
if( $interval->d>0 ) |
|
49
|
|
|
{ |
|
50
|
|
|
$beginDate = $beginDate->startOfDay(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$intervals = []; |
|
54
|
|
|
$dateCounter = clone $beginDate; |
|
55
|
|
|
while($dateCounter <= Carbon::now()) |
|
56
|
|
|
{ |
|
57
|
|
|
if( $interval->h>0 ) |
|
58
|
|
|
{ |
|
59
|
|
|
$intervals[$dateCounter->hour] = 0; |
|
60
|
|
|
$dateCounter->addHour(1); |
|
61
|
|
|
} |
|
62
|
|
|
else |
|
63
|
|
|
{ |
|
64
|
|
|
$intervals[$dateCounter->day] = 0; |
|
65
|
|
|
$dateCounter->addDay(1); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
try |
|
70
|
|
|
{ |
|
71
|
|
|
$sales = $this->model->where('is_active', '=', true) |
|
72
|
|
|
->where('time', '>=', $beginDate) |
|
73
|
|
|
->get(); |
|
74
|
|
|
} |
|
75
|
|
|
catch(Exception $e) |
|
76
|
|
|
{ |
|
77
|
|
|
throw new RepositoryException('Could not get sale data', RepositoryException::DATABASE_ERROR); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
foreach($sales as $sale) |
|
81
|
|
|
{ |
|
82
|
|
|
$carbonTimestamp = Carbon::createFromFormat('Y-m-d H:m:s', $sale->time); |
|
83
|
|
|
|
|
84
|
|
|
if( $interval->h>0 ) |
|
85
|
|
|
{ |
|
86
|
|
|
$intervals[$carbonTimestamp->hour]++; |
|
87
|
|
|
} |
|
88
|
|
|
else |
|
89
|
|
|
{ |
|
90
|
|
|
$intervals[$carbonTimestamp->day]++; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $intervals; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function rankUsersByInterval(CarbonInterval $interval) |
|
98
|
|
|
{ |
|
99
|
|
|
$beginDate = Carbon::now()->sub($interval)->addHour(); |
|
100
|
|
|
|
|
101
|
|
|
if( $interval->d>0 ) |
|
102
|
|
|
{ |
|
103
|
|
|
$beginDate = $beginDate->startOfDay(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
try |
|
107
|
|
|
{ |
|
108
|
|
|
$userRank = $this->model->select(['firstname', 'lastname', DB::raw('COUNT(*) as count')]) |
|
109
|
|
|
->join('users', 'sales.user_id', '=', 'users.user_id') |
|
110
|
|
|
->where('sales.is_active', '=', true) |
|
111
|
|
|
->where('sales.time', '>=', $beginDate) |
|
112
|
|
|
->groupBy('sales.user_id') |
|
113
|
|
|
->orderBy('count', 'DESC') |
|
114
|
|
|
->take(10) |
|
115
|
|
|
->get(); |
|
116
|
|
|
} |
|
117
|
|
|
catch(Exception $e) |
|
118
|
|
|
{ |
|
119
|
|
|
throw new RepositoryException('Could not retrieve users', RepositoryException::DATABASE_ERROR); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $userRank; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
View Code Duplication |
public function validate(array $data) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$validator = Validator::make($data, |
|
128
|
|
|
[ |
|
129
|
|
|
'time' => 'required|integer|min:0', |
|
130
|
|
|
'sum' => 'required|numeric|min:0', |
|
131
|
|
|
'paid' => 'required|numeric|min:0' |
|
132
|
|
|
] |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
if ($validator->fails()) { |
|
136
|
|
|
throw new RepositoryException('Sale data validation failed', RepositoryException::VALIDATION_FAILED); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.