|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Product; |
|
6
|
|
|
use App\Involved; |
|
7
|
|
|
use App\Lot; |
|
8
|
|
|
use Carbon; |
|
9
|
|
|
use Illuminate\Support\Facades\Auth; |
|
10
|
|
|
|
|
11
|
|
|
class InvolvedRepository extends Repository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @return Involved |
|
15
|
|
|
*/ |
|
16
|
|
|
public function getModel() |
|
17
|
|
|
{ |
|
18
|
|
|
return new Involved(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Involve the product offer |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $data |
|
25
|
|
|
* @param Product $product |
|
26
|
|
|
* @return Involved |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
public function create(array $data, $product) |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->getModel() |
|
32
|
|
|
->create([ |
|
33
|
|
|
'user_id' => \Auth::id(), |
|
34
|
|
|
'product_id' => $product->id, |
|
|
|
|
|
|
35
|
|
|
'lot_id' => $product->lot_id, |
|
|
|
|
|
|
36
|
|
|
'price_id' => $data['select_product'], |
|
37
|
|
|
'color_id' => $data['color_product'], |
|
38
|
|
|
'size_id'=>$data['sizes_product'], |
|
39
|
|
|
'count' => isset($data['count']) ? $data['count'] : 1 |
|
40
|
|
|
]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Update involve status. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Involved $involve |
|
47
|
|
|
* @param array $data |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
|
|
public function update(Involved $involve, array $data) |
|
51
|
|
|
{ |
|
52
|
|
|
$involved = $involve->fill([ |
|
53
|
|
|
'active' => (isset($data['active'])) ? $data['active'] : $involve->active, |
|
|
|
|
|
|
54
|
|
|
'count' => (isset($data['count'])) ? $data['count'] : $involve->count |
|
|
|
|
|
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
$involved->save(); |
|
58
|
|
|
|
|
59
|
|
|
return $involved; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Check if auth-cate user is involved to this product. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Product $product |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function checkIfAuthInvolved(Product $product) |
|
69
|
|
|
{ |
|
70
|
|
|
$model = $this->getModelByUserAndProduct($product); |
|
71
|
|
|
|
|
72
|
|
|
return (bool) $model; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $data |
|
77
|
|
|
* @param $product |
|
78
|
|
|
* @return Involved|InvolvedRepository |
|
79
|
|
|
*/ |
|
80
|
|
|
public function createOrUpdate(array $data, $product) |
|
81
|
|
|
{ |
|
82
|
|
|
if($this->checkIfAuthInvolved($product)) |
|
83
|
|
|
return $this->update($product, $data); |
|
84
|
|
|
|
|
85
|
|
|
return $this->create($data, $product); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param $product |
|
90
|
|
|
* @param null $user |
|
91
|
|
|
* @param bool $active |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public function getModelByUserAndProduct($product, $user = null, $active = true) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
return $this->getModel() |
|
|
|
|
|
|
97
|
|
|
->where('user_id', ($user) ? $user : Auth::id()) |
|
98
|
|
|
->where('product_id', $product->id) |
|
99
|
|
|
->active($active) |
|
100
|
|
|
->first(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $product |
|
105
|
|
|
* @param bool $active |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
public function getModelsByUserAndProduct($product, $active = true) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
return $this->getModel() |
|
|
|
|
|
|
111
|
|
|
->where('user_id', Auth::id()) |
|
112
|
|
|
->where('product_id', $product->id) |
|
113
|
|
|
->active($active) |
|
114
|
|
|
->get(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Count rules. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function countRules() |
|
123
|
|
|
{ |
|
124
|
|
|
return 'numeric|min:1'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $product |
|
129
|
|
|
* @return int |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getInvolveTimesProduct($product) |
|
132
|
|
|
{ |
|
133
|
|
|
return count($this->getModelsByUserAndProduct($product, true)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getCountSelled($id) |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->getModel() |
|
|
|
|
|
|
139
|
|
|
->where('product_id', $id) |
|
140
|
|
|
->active() |
|
141
|
|
|
->sum('count'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getInvolvedProductforUser() { |
|
145
|
|
|
|
|
146
|
|
|
return $this->getModel() |
|
|
|
|
|
|
147
|
|
|
->active() |
|
148
|
|
|
->where('user_id',\Auth::user()->id) |
|
149
|
|
|
->get(); |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} |
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@propertyannotation 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.