-
Notifications
You must be signed in to change notification settings - Fork 378
/
Product.php
287 lines (244 loc) · 7.86 KB
/
Product.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.
namespace App\Models\Store;
use App\Exceptions\InsufficientStockException;
use Carbon\Carbon;
/**
* @property bool $allow_multiple
* @property \Carbon\Carbon|null $available_until
* @property float $base_shipping
* @property float|null $cost
* @property \Carbon\Carbon $created_at
* @property string|null $custom_class
* @property \Carbon\Carbon|null $deleted_at
* @property string|null $description
* @property int $display_order
* @property bool $enabled
* @property string|null $header_description
* @property string|null $header_image
* @property string|null $image
* @property string|null $images_json
* @property self $masterProduct
* @property int|null $master_product_id
* @property int $max_quantity
* @property string $name
* @property float $next_shipping
* @property \Illuminate\Database\Eloquent\Collection $notificationRequests NotificationRequest
* @property int $product_id
* @property bool $promoted
* @property string|null $shopify_id
* @property int|null $stock
* @property string|null $type_mappings_json
* @property \Carbon\Carbon|null $updated_at
* @property \Illuminate\Database\Eloquent\Collection $variations static
* @property int|null $weight
*/
class Product extends Model
{
const BUTTON_DISABLED = [self::SUPPORTER_TAG_NAME, self::USERNAME_CHANGE];
const REDIRECT_PLACEHOLDER = 'redirect';
const SUPPORTER_TAG_NAME = 'supporter-tag';
const USERNAME_CHANGE = 'username-change';
protected $primaryKey = 'product_id';
protected $casts = [
'available_until' => 'datetime',
'cost' => 'float',
'base_shipping' => 'float',
'next_shipping' => 'float',
'promoted' => 'boolean',
'enabled' => 'boolean',
'allow_multiple' => 'boolean',
];
private $images;
private $types;
public function masterProduct()
{
return $this->belongsTo(static::class, 'master_product_id');
}
public function variations()
{
return $this->hasMany(static::class, 'master_product_id');
}
public function notificationRequests()
{
return $this->hasMany(NotificationRequest::class);
}
public function isRedirectPlaceholder()
{
return $this->custom_class === static::REDIRECT_PLACEHOLDER;
}
public function inStock($quantity = 1, $includeVariations = false)
{
$inStock = $this->stock === null || $this->stock >= $quantity;
if ($inStock === false && $includeVariations === true) {
$inStock = ($this->masterProduct ?? $this)
->variations
->contains(function ($variation) use ($quantity) {
return $variation->inStock($quantity);
});
}
return $inStock;
}
public function getHeaderImageAttribute($value)
{
if ($this->masterProduct) {
return $this->masterProduct->header_image;
} else {
return $value;
}
}
public function getHeaderDescriptionAttribute($value)
{
if ($this->masterProduct) {
return $this->masterProduct->header_description;
} else {
return $value;
}
}
public function getDescriptionAttribute($value)
{
if ($this->masterProduct) {
return $this->masterProduct->description;
} else {
return $value;
}
}
public function isAvailable(): bool
{
return $this->enabled
&& ($this->available_until === null ? true : $this->available_until->isFuture());
}
public function typeMappings()
{
if ($this->masterProduct) {
return $this->masterProduct->typeMappings();
} else {
return json_decode($this->type_mappings_json, true);
}
}
public function images()
{
if (!$this->images_json && $this->masterProduct) {
return $this->masterProduct->images();
} else {
if (!$this->images && $this->images_json) {
$this->images = json_decode($this->images_json, true);
}
return $this->images ?? [];
}
}
public function requiresShipping()
{
return $this->weight !== null;
}
public function scopeAvailable($query)
{
return $query
->where('enabled', true)
->where(function ($q) {
return $q->whereNull('available_until')->orWhere('available_until', '>=', Carbon::now());
});
}
public function scopeNotAvailable($query)
{
return $query->where('available_until', '<', Carbon::now());
}
public function scopeListing($query)
{
return $query
->available()
->where('master_product_id', null)
->with('masterProduct')
->with('variations')
->orderBy('promoted', 'desc')
->orderBy('display_order', 'desc');
}
public function scopeCustomClass($query, $name)
{
return $query
->where('custom_class', $name);
}
public function scopeEnabled($query)
{
return $query->where('enabled', true);
}
public function scopeHasShipping($query)
{
return $query->whereNotNull('weight');
}
/**
* Returns the Shopify product variant GraphQL gid for this Product, null if it is not a Shopify item.
* This is currently implemented as convenience for checking the gid matches the one from the Storefront API.
*
* @return string|null
*/
public function getShopifyVariantGid(): ?string
{
return $this->isShopify() ? base64_encode("gid://shopify/ProductVariant/{$this->shopify_id}") : null;
}
public function isShopify(): bool
{
return $this->shopify_id !== null;
}
public function productsInRange()
{
if (!($mappings = $this->typeMappings())) {
return [];
}
return self::whereIn('product_id', array_keys($mappings))->get();
}
public function release($quantity)
{
if (
$this->stock === null
// stock may have been directly updated to 0.
// TODO: should count reservations and available stock separately or something.
|| $this->stock <= 0
) {
return;
}
$this->incrementInstance('stock', $quantity);
}
public function reserve($quantity)
{
if ($this->stock === null) {
return;
}
$this->decrementInstance('stock', $quantity);
// operating under the assumtion that the caller will prevent concurrent updates.
if ($this->stock < 0) {
throw new InsufficientStockException();
}
}
public function types()
{
$mappings = $this->typeMappings();
if ($mappings === null) {
return;
}
if ($this->types !== null) {
return $this->types;
}
$currentMapping = $mappings[strval($this->product_id)];
$this->types = [];
foreach ($mappings as $product_id => $mapping) {
foreach ($mapping as $type => $value) {
if (!isset($this->types[$type])) {
$this->types[$type] = [];
}
$mappingDiff = array_diff_assoc($mapping, $currentMapping);
if ((count($mappingDiff) === 0) || (count($mappingDiff) === 1 && isset($mappingDiff[$type]))) {
$this->types[$type][$value] = intval($product_id);
}
}
}
return $this->types;
}
public function url(): string
{
return $this->isRedirectPlaceholder()
? $this->description
: route('store.products.show', $this);
}
}