|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\Couponables; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use MichaelRubel\Couponables\Exceptions\InvalidCouponException; |
|
10
|
|
|
use MichaelRubel\Couponables\Models\Contracts\CouponContract; |
|
11
|
|
|
use MichaelRubel\Couponables\Models\Contracts\CouponPivotContract; |
|
12
|
|
|
use MichaelRubel\Couponables\Services\Contracts\CouponServiceContract; |
|
13
|
|
|
use MichaelRubel\EnhancedContainer\Call; |
|
14
|
|
|
|
|
15
|
|
|
trait HasCoupons |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Polymorphic relation to the coupons. |
|
19
|
|
|
* |
|
20
|
|
|
* @return MorphToMany |
|
21
|
|
|
*/ |
|
22
|
14 |
|
public function coupons(): MorphToMany |
|
23
|
|
|
{ |
|
24
|
14 |
|
return $this->morphToMany(app(CouponContract::class), Str::singular( |
|
|
|
|
|
|
25
|
14 |
|
config('couponables.pivot_table', 'couponables') |
|
26
|
14 |
|
))->withPivot( |
|
27
|
14 |
|
call(CouponPivotContract::class)->getRedeemedAtColumn() |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Use the coupon. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $code |
|
35
|
|
|
* |
|
36
|
|
|
* @return CouponContract |
|
37
|
|
|
*/ |
|
38
|
17 |
|
public function redeemCoupon(string $code): CouponContract |
|
39
|
|
|
{ |
|
40
|
17 |
|
$service = call(CouponServiceContract::class); |
|
41
|
17 |
|
$proxy = call($service->verifyCoupon($code, $this)); |
|
42
|
|
|
|
|
43
|
13 |
|
$coupon = $proxy->getInternal(Call::INSTANCE); |
|
44
|
|
|
|
|
45
|
13 |
|
return $service->applyCoupon($coupon, $this); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Check if the coupon is already redeemed by the model at least once. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $code |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function isCouponRedeemed(string $code): bool |
|
56
|
|
|
{ |
|
57
|
4 |
|
$column = call(CouponContract::class) |
|
58
|
4 |
|
->getCodeColumn(); |
|
59
|
|
|
|
|
60
|
4 |
|
return $this->coupons() |
|
61
|
4 |
|
->where($column, $code) |
|
62
|
4 |
|
->exists(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Check if coupon with this code is already used. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $code |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function isCouponAlreadyUsed(string $code): bool |
|
73
|
|
|
{ |
|
74
|
3 |
|
return call($this)->isCouponRedeemed($code); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Check if the coupon is over limit for the model. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $code |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
* |
|
84
|
|
|
* @throws InvalidCouponException |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function isCouponOverLimit(string $code): bool |
|
87
|
|
|
{ |
|
88
|
1 |
|
$service = call(CouponServiceContract::class); |
|
89
|
1 |
|
$coupon = call($service->getCoupon($code)); |
|
90
|
|
|
|
|
91
|
1 |
|
return $coupon->isOverLimitFor($this); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|