|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\ImprovedSpec; |
|
6
|
|
|
use App\SpecPrice; |
|
7
|
|
|
use App\Lot; |
|
8
|
|
|
use App\Product; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
use Mockery\CountValidator\Exception; |
|
12
|
|
|
|
|
13
|
|
|
class ProductsRepository extends Repository |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @return Product |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getModel() |
|
19
|
|
|
{ |
|
20
|
|
|
return new Product(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get all published products |
|
25
|
|
|
* |
|
26
|
|
|
* @return mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getPublic() |
|
29
|
|
|
{ |
|
30
|
|
|
return self::getModel() |
|
|
|
|
|
|
31
|
|
|
->published() |
|
32
|
|
|
->get(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Find product by id/slug. |
|
37
|
|
|
* |
|
38
|
|
|
* @param $slug |
|
39
|
|
|
* @return Product |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function find($slug) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
if (is_numeric($slug)) |
|
44
|
|
|
return $this->getModel() |
|
|
|
|
|
|
45
|
|
|
->whereId((int) $slug) |
|
46
|
|
|
// ->whereIn('status', ['published', 'drafted', 'notverified', 'completed']) |
|
|
|
|
|
|
47
|
|
|
->first(); |
|
48
|
|
|
|
|
49
|
|
|
return $this->getModel() |
|
|
|
|
|
|
50
|
|
|
->whereSlug($slug) |
|
51
|
|
|
// ->whereIn('status', ['published', 'drafted', 'notverified', 'completed']) |
|
|
|
|
|
|
52
|
|
|
->first(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create product. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
* @return Product |
|
60
|
|
|
*/ |
|
61
|
|
|
public function create(array $data) |
|
62
|
|
|
{ |
|
63
|
|
|
return self::getModel() |
|
64
|
|
|
->create([ |
|
65
|
|
|
'vendor_id' => $data['vendor_id'], |
|
66
|
|
|
'name' => (isset($data['name']) ? $data['name'] : ''), |
|
67
|
|
|
'price' => (isset($data['price']) ? $data['price'] : ''), |
|
68
|
|
|
'sale' => (isset($data['sale'])) ? $data['sale'] : 0, |
|
69
|
|
|
'count' => (isset($data['count'])) ? $data['count'] : 1, |
|
70
|
|
|
'type' => (isset($data['type'])) ? $data['type'] : 'new', |
|
71
|
|
|
'status' => (isset($data['status'])) ? $data['status'] : 'drafted', |
|
72
|
|
|
'published_date' => (isset($data['published_date']) ? $data['published_date'] : Carbon::now()), |
|
73
|
|
|
'expiration_date' => (isset($data['expiration_date']) ? $data['expiration_date'] : Carbon::now()), |
|
74
|
|
|
'active' => (isset($data['active']) ? $data['active'] : 0) |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Update data. |
|
80
|
|
|
* |
|
81
|
|
|
* @param $product |
|
82
|
|
|
* @param $data |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
public function update($product, $data) |
|
86
|
|
|
{ |
|
87
|
|
|
if(! $product instanceof Model) |
|
88
|
|
|
throw new Exception('First argument MUST be an instance of '.Model::class); |
|
89
|
|
|
|
|
90
|
|
|
$product->fill([ |
|
91
|
|
|
'name' => (isset($data['name']) ? $data['name'] : $product->name), |
|
92
|
|
|
'price' => (isset($data['price']) ? $data['price'] : $product->price), |
|
93
|
|
|
'sale' => (isset($data['sale'])) ? $this->formatSale($data['sale']) : $product->sale, |
|
94
|
|
|
'count' => (isset($data['count'])) ? $data['count'] : $product->count, |
|
95
|
|
|
'description' => (isset($data['description'])) ? $data['description'] : $product->description, |
|
96
|
|
|
'type' => (isset($data['type'])) ? $data['type'] : 'new', |
|
97
|
|
|
'status' => ($product->status == 'drafted') ? 'notverified' : $product->status, |
|
98
|
|
|
'published_date' => (isset($data['published_date']) ? $this->dateToTimestamp($data['published_date']) : $product->published_date), |
|
99
|
|
|
'expiration_date' => (isset($data['expiration_date']) ? $this->dateToTimestamp($data['expiration_date']) : $product->published_date), |
|
100
|
|
|
'active' => 1 |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
$product->save(); |
|
104
|
|
|
|
|
105
|
|
|
return $product; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Reformat date. |
|
110
|
|
|
* |
|
111
|
|
|
* @param $date |
|
112
|
|
|
* @param string $delimiter |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
*/ |
|
115
|
|
|
public function reformatDateString($date, $delimiter = '.') |
|
116
|
|
|
{ |
|
117
|
|
|
$datas = explode($delimiter, $date); |
|
118
|
|
|
|
|
119
|
|
|
$new_date['d'] = $datas[0]; |
|
|
|
|
|
|
120
|
|
|
$new_date['m'] = $datas[1]; |
|
121
|
|
|
$new_date['y'] = $datas[2]; |
|
122
|
|
|
|
|
123
|
|
|
return $new_date; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Convert string date to \Carbon/Carbon timestamp. |
|
128
|
|
|
* |
|
129
|
|
|
* @param $date |
|
130
|
|
|
* @return static |
|
131
|
|
|
*/ |
|
132
|
|
|
public function dateToTimestamp($date) |
|
133
|
|
|
{ |
|
134
|
|
|
$dates = $this->reformatDateString($date); |
|
135
|
|
|
|
|
136
|
|
|
return Carbon::createFromDate($dates['y'], $dates['m'], $dates['d']); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Remove product row from table. |
|
141
|
|
|
* |
|
142
|
|
|
* @param $id |
|
143
|
|
|
* @return bool|null |
|
144
|
|
|
* @throws \Exception |
|
145
|
|
|
*/ |
|
146
|
|
|
public function delete($id) |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->find($id)->delete(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get random products. |
|
153
|
|
|
* |
|
154
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getSomeRandomProducts() |
|
157
|
|
|
{ |
|
158
|
|
|
$random_element_1 = rand(1, 2); |
|
159
|
|
|
$random_element_2 = rand(1, 2); |
|
160
|
|
|
$random_element_3 = rand(1, 2); |
|
161
|
|
|
|
|
162
|
|
|
$query = self::getModel()->select('*'); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
if ($random_element_1 == 1) { |
|
165
|
|
|
$query->published(); |
|
166
|
|
|
} else { |
|
167
|
|
|
$query->drafted(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($random_element_2 == 1) { |
|
171
|
|
|
$query->whereType('old'); |
|
172
|
|
|
} else { |
|
173
|
|
|
$query->whereType('new'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if ($random_element_3 == 1) { |
|
177
|
|
|
$query->whereBetween('sale', [1, 50]); |
|
178
|
|
|
} else { |
|
179
|
|
|
$query->whereBetween('sale', [51, 100]); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $query->get(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get drafted product by id. |
|
187
|
|
|
* |
|
188
|
|
|
* @param $id |
|
189
|
|
|
* @return mixed |
|
190
|
|
|
*/ |
|
191
|
|
|
public function findDrafted($id) |
|
192
|
|
|
{ |
|
193
|
|
|
return self::getModel() |
|
|
|
|
|
|
194
|
|
|
->whereId($id) |
|
195
|
|
|
->drafted() |
|
196
|
|
|
->first(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function getCount($id) |
|
200
|
|
|
{ |
|
201
|
|
|
return self::getModel() |
|
|
|
|
|
|
202
|
|
|
->whereId($id) |
|
203
|
|
|
->pluck('count') |
|
204
|
|
|
->first(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function countInLotProduct($id) |
|
208
|
|
|
{ |
|
209
|
|
|
return self::getModel() |
|
|
|
|
|
|
210
|
|
|
->where('lot_id',$id) |
|
211
|
|
|
->count(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function search($filters) |
|
215
|
|
|
{ |
|
216
|
|
|
|
|
217
|
|
|
$product = $filters['search']; |
|
218
|
|
|
|
|
219
|
|
|
if(isset($filters['category'])) |
|
220
|
|
|
$category = $filters['category']; |
|
221
|
|
|
|
|
222
|
|
|
if(empty($product) && (!isset($category))) |
|
223
|
|
|
return null; |
|
224
|
|
|
|
|
225
|
|
|
if(isset($category)) |
|
226
|
|
|
{ |
|
227
|
|
|
$query = $this->getModel() |
|
|
|
|
|
|
228
|
|
|
->select('products.*', 'categoryable.category_id') |
|
229
|
|
|
->where('products.name', 'like', '%'.$product.'%') |
|
230
|
|
|
->join('categoryable', 'products.id', '=', 'categoryable.categoryable_id') |
|
231
|
|
|
->where('categoryable.categoryable_type', get_class(self::getModel())) |
|
232
|
|
|
->where('categoryable.category_id', $category); |
|
233
|
|
|
} else |
|
234
|
|
|
{ |
|
235
|
|
|
$query = $this->getModel() |
|
|
|
|
|
|
236
|
|
|
->where('name', 'like', '%'.$product.'%'); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$query->where('products.active', 1); |
|
240
|
|
|
/*->whereIn('active', ['published', 'completed']);*/ |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
return $query->get(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Remove percent from sale if it exists. |
|
247
|
|
|
* |
|
248
|
|
|
* @param $sale |
|
249
|
|
|
* @return int |
|
250
|
|
|
*/ |
|
251
|
|
|
private function formatSale($sale) |
|
252
|
|
|
{ |
|
253
|
|
|
list($sale, $percent) = explode('%', $sale); |
|
|
|
|
|
|
254
|
|
|
|
|
255
|
|
|
return (int) $sale; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Get same products. |
|
260
|
|
|
* |
|
261
|
|
|
* @param mixed |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getSameProduct($id,$limit=10) |
|
264
|
|
|
{ |
|
265
|
|
|
$query = $this->getModel() |
|
|
|
|
|
|
266
|
|
|
->select('products.*') |
|
267
|
|
|
->where('sub_category_id',$id) |
|
268
|
|
|
->where('products.active', 1) |
|
269
|
|
|
/*->where('lots.expire_date', '>', Carbon::now())*/ |
|
|
|
|
|
|
270
|
|
|
->limit($limit); |
|
271
|
|
|
|
|
272
|
|
|
$query->join('lots', 'lots.id', '=', 'products.lot_id') |
|
273
|
|
|
->where('lots.verify_status', 'verified'); |
|
274
|
|
|
|
|
275
|
|
|
return $query->get(); |
|
276
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Get public latest created products. |
|
281
|
|
|
* |
|
282
|
|
|
* @param int $count |
|
283
|
|
|
* @return mixed |
|
284
|
|
|
*/ |
|
285
|
|
|
public function getPublicLatest($count = 8) |
|
286
|
|
|
{ |
|
287
|
|
|
return $this->getModel() |
|
|
|
|
|
|
288
|
|
|
->active() |
|
289
|
|
|
// ->published() // todo: on production back it. |
|
|
|
|
|
|
290
|
|
|
->orderBy('id', self::DESC) |
|
291
|
|
|
->take($count) |
|
292
|
|
|
->get(); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Get popular products. |
|
297
|
|
|
* |
|
298
|
|
|
* @return mixed |
|
299
|
|
|
*/ |
|
300
|
|
|
public function getFeaturedPublic($count = 8) |
|
301
|
|
|
{ |
|
302
|
|
|
return $this->getModel() |
|
|
|
|
|
|
303
|
|
|
// ->published() |
|
304
|
|
|
->featured() |
|
305
|
|
|
->active() |
|
306
|
|
|
->orderBy('id', self::DESC) |
|
307
|
|
|
->take($count) |
|
308
|
|
|
->get(); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Get expire soon products. |
|
313
|
|
|
* |
|
314
|
|
|
* @param int $count |
|
315
|
|
|
* @return mixed |
|
316
|
|
|
*/ |
|
317
|
|
|
public function getPublicExpireSoon($count = 8) |
|
318
|
|
|
{ |
|
319
|
|
|
return $this->getModel() |
|
|
|
|
|
|
320
|
|
|
// ->published() |
|
321
|
|
|
->active() |
|
322
|
|
|
// ->where('expiration_date', '>', Carbon::now()) |
|
|
|
|
|
|
323
|
|
|
// ->orderBy('expiration_date', self::ASC) |
|
324
|
|
|
->orderBy('id', self::ASC) |
|
325
|
|
|
->take($count) |
|
326
|
|
|
->get(); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Expire soon products. |
|
331
|
|
|
* |
|
332
|
|
|
* @param int $paginate |
|
333
|
|
|
* @return mixed |
|
334
|
|
|
*/ |
|
335
|
|
|
public function getExpireSoon($paginate = 10) |
|
336
|
|
|
{ |
|
337
|
|
|
$query = $this->getModel() |
|
|
|
|
|
|
338
|
|
|
->select('products.*') |
|
339
|
|
|
->where('products.active', 1) |
|
340
|
|
|
->where('lots.expire_date', '>', Carbon::now()) |
|
341
|
|
|
->orderBy('lots.expire_date', self::ASC); |
|
342
|
|
|
|
|
343
|
|
|
if(request()->get('name')) |
|
344
|
|
|
$query->orderBy('products.name', request()->get('name') == self::ASC ? self::ASC : self::DESC); |
|
345
|
|
|
|
|
346
|
|
|
if(request()->get('created_at')) |
|
347
|
|
|
$query->orderBy('products.created_at', request()->get('created_at') == self::ASC ? self::ASC : self::DESC); |
|
348
|
|
|
|
|
349
|
|
|
if(request()->get('price')) |
|
350
|
|
|
$query->orderBy('products.price', request()->get('price') == self::ASC ? self::ASC : self::DESC); |
|
351
|
|
|
|
|
352
|
|
|
$query->join('lots', 'lots.id', '=', 'products.lot_id') |
|
353
|
|
|
->where('lots.status', Lot::STATUS_COMPLETE) |
|
354
|
|
|
->where('lots.verify_status', Lot::STATUS_VERIFY_ACCEPTED); |
|
355
|
|
|
|
|
356
|
|
|
// return $query->orderBy('id', self::ASC) |
|
|
|
|
|
|
357
|
|
|
return $query->paginate($paginate); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* Create plain product for \App\Lot $lot |
|
362
|
|
|
* |
|
363
|
|
|
* @param Lot $lot |
|
364
|
|
|
* @return static |
|
365
|
|
|
*/ |
|
366
|
|
|
public function createPlain(Lot $lot) |
|
367
|
|
|
{ |
|
368
|
|
|
$product = self::getModel() |
|
369
|
|
|
->create([ |
|
370
|
|
|
'lot_id' => $lot->id, |
|
|
|
|
|
|
371
|
|
|
'uniqid' => substr(str_replace('.','',uniqid('00'.rand(),true)),0,10) |
|
372
|
|
|
|
|
373
|
|
|
]); |
|
374
|
|
|
/*$spec_price = SpecPrice::create([ |
|
|
|
|
|
|
375
|
|
|
'product_id' => $product->id |
|
376
|
|
|
]); |
|
377
|
|
|
ImprovedSpec::create([ |
|
378
|
|
|
'product_id' => $product->id, |
|
379
|
|
|
'price_spec_id' => $spec_price->id |
|
380
|
|
|
]);*/ |
|
381
|
|
|
|
|
382
|
|
|
return $product; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
public function saveProduct($product, array $data) |
|
386
|
|
|
{ |
|
387
|
|
|
$product->fill([ |
|
388
|
|
|
/*'name' => isset($data['name']) ? $data['name'] : null, |
|
|
|
|
|
|
389
|
|
|
'old_price' => isset($data['old_price']) ? $data['old_price'] : null, |
|
390
|
|
|
'price' => isset($data['price']) ? $data['price'] : null, |
|
391
|
|
|
'sale' => isset($data['sale']) ? $data['sale'] : null,*/ |
|
392
|
|
|
'sub_category_id' => isset($data['sub_category']) ? $data['sub_category'] : null |
|
393
|
|
|
]); |
|
394
|
|
|
|
|
395
|
|
|
$product->save(); |
|
396
|
|
|
|
|
397
|
|
|
return $product; |
|
398
|
|
|
} |
|
399
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.