1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Lot; |
6
|
|
|
use App\Vendor; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
|
9
|
|
|
class LotRepository extends Repository |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @return Lot |
13
|
|
|
*/ |
14
|
|
|
public function getModel() |
15
|
|
|
{ |
16
|
|
|
return new Lot(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create plain lot for attached items to him. |
21
|
|
|
* |
22
|
|
|
* @param Vendor $vendor |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
|
|
public function createDraft(Vendor $vendor) |
26
|
|
|
{ |
27
|
|
|
return $this->getModel() |
28
|
|
|
->create([ |
29
|
|
|
'vendor_id' => $vendor->id |
|
|
|
|
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function find($slug) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
if (is_numeric($slug)) |
36
|
|
|
return $this->getModel() |
|
|
|
|
37
|
|
|
->whereId((int) $slug) |
38
|
|
|
->first(); |
39
|
|
|
|
40
|
|
|
return $this->getModel() |
|
|
|
|
41
|
|
|
->whereSlug($slug) |
42
|
|
|
->first(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function statusChange(){ |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add empty lot or modify just existed (drafted).. |
51
|
|
|
* |
52
|
|
|
* @param $vendor |
53
|
|
|
* @return Lot|mixed |
54
|
|
|
*/ |
55
|
|
|
public function addLot($vendor) |
56
|
|
|
{ |
57
|
|
|
if($lot = $this->getDraftedLot($vendor)) |
58
|
|
|
return $lot; |
59
|
|
|
|
60
|
|
|
return $this->createDraft($vendor); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get drafted lot |
65
|
|
|
* |
66
|
|
|
* @param $vendor|null |
67
|
|
|
* @return Lot $Lot|null |
68
|
|
|
*/ |
69
|
|
|
public function getDraftedLot($vendor = null) |
70
|
|
|
{ |
71
|
|
|
$query = $this->getModel(); |
72
|
|
|
|
73
|
|
|
if($vendor) |
74
|
|
|
$query = $query->where('vendor_id', $vendor->id); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$lot = $query->drafted()->first(); |
77
|
|
|
|
78
|
|
|
return ($lot) ? $lot : null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get user's lots. |
83
|
|
|
* |
84
|
|
|
* @param $user |
85
|
|
|
* @param $perPage |
86
|
|
|
* @return \Illuminate\Support\Collection|null |
87
|
|
|
*/ |
88
|
|
|
public function userLots($user, $perPage = 5) |
89
|
|
|
{ |
90
|
|
|
$model = self::getModel(); |
91
|
|
|
|
92
|
|
|
$vendors = []; |
93
|
|
|
|
94
|
|
|
$user->vendors()->active()->get() |
95
|
|
|
->each(function($vendor) use (&$vendors){ |
96
|
|
|
$vendors[] = $vendor->id; |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
$lots = $this->getModel() |
|
|
|
|
100
|
|
|
->whereIn('vendor_id', $vendors) |
101
|
|
|
->where('status', '!=', $model::STATUS_DELETED) |
102
|
|
|
->orderBy('expire_date', 'desc') |
103
|
|
|
->orderBy('status', 'asc') |
104
|
|
|
->paginate($perPage); |
105
|
|
|
|
106
|
|
|
return ($lots->count()) ? $lots : null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function userLotsPendingComision($user,$lotId=null) |
110
|
|
|
{ |
111
|
|
|
|
112
|
|
|
$model = self::getModel(); |
113
|
|
|
|
114
|
|
|
$vendors = []; |
115
|
|
|
$user->vendors()->active()->get() |
116
|
|
|
->each(function($vendor) use (&$vendors){ |
117
|
|
|
$vendors[] = $vendor->id; |
118
|
|
|
}); |
119
|
|
|
$query = $this->getModel()->where('verify_status', $model::STATUS_VERIFY_PENDING); |
|
|
|
|
120
|
|
|
if (!empty($vendors)) { |
121
|
|
|
$query->whereIn('vendor_id', $vendors); |
122
|
|
|
} |
123
|
|
|
if ($lotId != null) { |
124
|
|
|
$query->where('id','!=', $lotId); |
125
|
|
|
} |
126
|
|
|
$sum = $query->sum('comision'); |
127
|
|
|
return $sum ? (int)$sum : 0; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Delete lot. |
132
|
|
|
* |
133
|
|
|
* @param $lot |
134
|
|
|
*/ |
135
|
|
|
public function delete($lot) |
136
|
|
|
{ |
137
|
|
|
$model = self::getModel(); |
138
|
|
|
if($lot->status !== $model::STATUS_DELETED) |
139
|
|
|
{ |
140
|
|
|
$lot->status = $model::STATUS_DELETED; |
141
|
|
|
|
142
|
|
|
$lot->save(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Convert string date to \Carbon/Carbon timestamp. |
148
|
|
|
* |
149
|
|
|
* @param $date |
150
|
|
|
* @return static |
151
|
|
|
*/ |
152
|
|
|
public function dateToTimestamp($date) |
153
|
|
|
{ |
154
|
|
|
$dates = $this->reformatDateString($date); |
155
|
|
|
|
156
|
|
|
return Carbon::createFromDate($dates['y'], $dates['m'], $dates['d']); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Reformat date. |
161
|
|
|
* |
162
|
|
|
* @param $date |
163
|
|
|
* @param string $delimiter |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function reformatDateString($date, $delimiter = '.') |
167
|
|
|
{ |
168
|
|
|
$datas = explode($delimiter, $date); |
169
|
|
|
|
170
|
|
|
if(count($datas) == 3) { |
171
|
|
|
$new_date['d'] = $datas[0]; |
|
|
|
|
172
|
|
|
$new_date['m'] = $datas[1]; |
173
|
|
|
$new_date['y'] = $datas[2]; |
174
|
|
|
} else { |
175
|
|
|
$now = Carbon::now(); |
176
|
|
|
$new_date['d'] = $now->day; |
|
|
|
|
177
|
|
|
$new_date['m'] = $now->month; |
178
|
|
|
$new_date['y'] = $now->year; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $new_date; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function save($lot, array $data) |
185
|
|
|
{ |
186
|
|
|
$lot->fill([ |
187
|
|
|
'name' => isset($data['name']) ? $data['name'] : $lot->present()->renderDraftedName(), |
188
|
|
|
'category_id' => isset($data['category']) ? $data['category'] : null, |
189
|
|
|
'currency_id' => isset($data['currency']) ? (int)$data['currency'] : null, |
190
|
|
|
'description' => isset($data['description']) ? $data['description'] : null, |
191
|
|
|
'yield_amount' => isset($data['yield_amount']) ? $data['yield_amount'] : null, |
192
|
|
|
'public_date' => isset($data['public_date']) ? $this->dateToTimestamp($data['public_date']) : Carbon::now()->addDays(1), |
193
|
|
|
'expire_date' => isset($data['expirate_date']) ? $this->dateToTimestamp($data['expirate_date']) :Carbon::now()->addDays(5), |
194
|
|
|
'comision' => isset($data['comision']) ? $data['comision'] : 0, |
195
|
|
|
'description_delivery' => isset($data['description_delivery']) ? $data['description_delivery'] : null, |
196
|
|
|
'description_payment' => isset($data['description_payment']) ? $data['description_payment'] : null, |
197
|
|
|
])->save(); |
198
|
|
|
return $lot; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Change category. |
204
|
|
|
* |
205
|
|
|
* @param $lot |
206
|
|
|
* @param $category_id |
207
|
|
|
* |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
public function changeCategory($lot, $category_id) |
211
|
|
|
{ |
212
|
|
|
$lot->fill([ |
213
|
|
|
'category_id' => $category_id |
214
|
|
|
])->save(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Check if user can to change category. |
219
|
|
|
* |
220
|
|
|
* @param Lot $lot |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function checkIfPossibleToChangeCategory(Lot $lot) |
224
|
|
|
{ |
225
|
|
|
if(! count($lot->products)) |
|
|
|
|
226
|
|
|
return true; |
227
|
|
|
|
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getLatestLot($limit = 10) |
232
|
|
|
{ |
233
|
|
|
return self::getModel() |
|
|
|
|
234
|
|
|
->orderBy('id','DESC') |
235
|
|
|
->active() |
236
|
|
|
->limit($limit) |
237
|
|
|
->get(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function getExpireSoon($paginate = 10) |
241
|
|
|
{ |
242
|
|
|
$query = $this->getModel() |
|
|
|
|
243
|
|
|
->select('lots.*') |
244
|
|
|
->where('lots.active', 1) |
245
|
|
|
->where('lots.expire_date', '>', Carbon::now()) |
246
|
|
|
->orderBy('lots.expire_date', self::ASC); |
247
|
|
|
|
248
|
|
|
return $query->paginate($paginate); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.