¨4.0.1¨
This commit is contained in:
@@ -6,6 +6,7 @@ use Modules\Support\Money;
|
||||
use Modules\Cart\Facades\Cart;
|
||||
use Modules\User\Entities\User;
|
||||
use Modules\Order\Entities\Order;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Coupon\Admin\CouponTable;
|
||||
@@ -72,6 +73,13 @@ class Coupon extends Model
|
||||
*/
|
||||
protected $translatedAttributes = ['name'];
|
||||
|
||||
|
||||
public static function findByCode($code)
|
||||
{
|
||||
return self::where(DB::raw('BINARY `code`'), $code)->first();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
@@ -86,16 +94,36 @@ class Coupon extends Model
|
||||
static::addActiveGlobalScope();
|
||||
}
|
||||
|
||||
public static function findByCode($code)
|
||||
|
||||
/**
|
||||
* Save associated relations for the coupon.
|
||||
*
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function saveRelations(array $attributes)
|
||||
{
|
||||
return self::where(DB::raw('BINARY `code`'), $code)->first();
|
||||
$this->syncProducts(array_get($attributes, 'products', []));
|
||||
$this->syncExcludeProducts(array_get($attributes, 'exclude_products', []));
|
||||
|
||||
$this->syncCategories(array_get($attributes, 'categories', []));
|
||||
$this->syncExcludeCategories(array_get($attributes, 'exclude_categories', []));
|
||||
}
|
||||
|
||||
|
||||
public function freeShipping()
|
||||
{
|
||||
return $this->free_shipping ? $this : (object) ['free_shipping' => 0];
|
||||
return $this->free_shipping ? $this : (object)['free_shipping' => 0];
|
||||
}
|
||||
|
||||
|
||||
public function invalid()
|
||||
{
|
||||
return !$this->valid();
|
||||
}
|
||||
|
||||
|
||||
public function valid()
|
||||
{
|
||||
if ($this->hasStartDate() && $this->hasEndDate()) {
|
||||
@@ -113,36 +141,13 @@ class Coupon extends Model
|
||||
return true;
|
||||
}
|
||||
|
||||
public function invalid()
|
||||
{
|
||||
return ! $this->valid();
|
||||
}
|
||||
|
||||
private function hasStartDate()
|
||||
{
|
||||
return ! is_null($this->start_date);
|
||||
}
|
||||
|
||||
private function hasEndDate()
|
||||
{
|
||||
return ! is_null($this->end_date);
|
||||
}
|
||||
|
||||
private function startDateIsValid()
|
||||
{
|
||||
return today() >= $this->start_date;
|
||||
}
|
||||
|
||||
private function endDateIsValid()
|
||||
{
|
||||
return today() <= $this->end_date;
|
||||
}
|
||||
|
||||
public function usageLimitReached($customerEmail = null)
|
||||
{
|
||||
return $this->perCouponUsageLimitReached() || $this->perCustomerUsageLimitReached($customerEmail);
|
||||
}
|
||||
|
||||
|
||||
public function perCouponUsageLimitReached()
|
||||
{
|
||||
if (is_null($this->usage_limit_per_coupon)) {
|
||||
@@ -152,6 +157,7 @@ class Coupon extends Model
|
||||
return $this->used >= $this->usage_limit_per_coupon;
|
||||
}
|
||||
|
||||
|
||||
public function perCustomerUsageLimitReached($customerEmail = null)
|
||||
{
|
||||
if ($this->couponHasNoUsageLimitForCustomers() ||
|
||||
@@ -169,15 +175,12 @@ class Coupon extends Model
|
||||
return $used >= $this->usage_limit_per_customer;
|
||||
}
|
||||
|
||||
private function couponHasNoUsageLimitForCustomers()
|
||||
|
||||
public function orders()
|
||||
{
|
||||
return is_null($this->usage_limit_per_customer);
|
||||
return $this->hasMany(Order::class)->withTrashed();
|
||||
}
|
||||
|
||||
private function userIsNotLoggedInWhenAddingCouponToCart($customerEmail = null)
|
||||
{
|
||||
return is_null($customerEmail) && auth()->guest();
|
||||
}
|
||||
|
||||
public function didNotSpendTheRequiredAmount()
|
||||
{
|
||||
@@ -188,6 +191,7 @@ class Coupon extends Model
|
||||
return Cart::subTotal()->lessThan($this->minimum_spend);
|
||||
}
|
||||
|
||||
|
||||
public function spentMoreThanMaximumAmount()
|
||||
{
|
||||
if (is_null($this->maximum_spend)) {
|
||||
@@ -197,6 +201,7 @@ class Coupon extends Model
|
||||
return Cart::subTotal()->greaterThan($this->maximum_spend);
|
||||
}
|
||||
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->belongsToMany(Product::class, 'coupon_products')
|
||||
@@ -204,6 +209,7 @@ class Coupon extends Model
|
||||
->wherePivot('exclude', false);
|
||||
}
|
||||
|
||||
|
||||
public function excludeProducts()
|
||||
{
|
||||
return $this->belongsToMany(Product::class, 'coupon_products')
|
||||
@@ -211,6 +217,7 @@ class Coupon extends Model
|
||||
->wherePivot('exclude', true);
|
||||
}
|
||||
|
||||
|
||||
public function categories()
|
||||
{
|
||||
return $this->belongsToMany(Category::class, 'coupon_categories')
|
||||
@@ -218,6 +225,7 @@ class Coupon extends Model
|
||||
->wherePivot('exclude', false);
|
||||
}
|
||||
|
||||
|
||||
public function excludeCategories()
|
||||
{
|
||||
return $this->belongsToMany(Category::class, 'coupon_categories')
|
||||
@@ -225,10 +233,6 @@ class Coupon extends Model
|
||||
->wherePivot('exclude', true);
|
||||
}
|
||||
|
||||
public function orders()
|
||||
{
|
||||
return $this->hasMany(Order::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function customers()
|
||||
{
|
||||
@@ -242,6 +246,7 @@ class Coupon extends Model
|
||||
)->withTrashed();
|
||||
}
|
||||
|
||||
|
||||
public function getValueAttribute($value)
|
||||
{
|
||||
if ($this->is_percent) {
|
||||
@@ -251,47 +256,72 @@ class Coupon extends Model
|
||||
return Money::inDefaultCurrency($value);
|
||||
}
|
||||
|
||||
|
||||
public function getMinimumSpendAttribute($minimumSpend)
|
||||
{
|
||||
if (! is_null($minimumSpend)) {
|
||||
if (!is_null($minimumSpend)) {
|
||||
return Money::inDefaultCurrency($minimumSpend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getMaximumSpendAttribute($maximumSpend)
|
||||
{
|
||||
if (! is_null($maximumSpend)) {
|
||||
if (!is_null($maximumSpend)) {
|
||||
return Money::inDefaultCurrency($maximumSpend);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getTotalAttribute($total)
|
||||
{
|
||||
return Money::inDefaultCurrency($total);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get table data for the resource
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function table()
|
||||
{
|
||||
return new CouponTable($this->newQuery()->withoutGlobalScope('active'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save associated relations for the coupon.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return void
|
||||
*/
|
||||
public function saveRelations(array $attributes)
|
||||
{
|
||||
$this->syncProducts(array_get($attributes, 'products', []));
|
||||
$this->syncExcludeProducts(array_get($attributes, 'exclude_products', []));
|
||||
|
||||
$this->syncCategories(array_get($attributes, 'categories', []));
|
||||
$this->syncExcludeCategories(array_get($attributes, 'exclude_categories', []));
|
||||
private function hasStartDate()
|
||||
{
|
||||
return !is_null($this->start_date);
|
||||
}
|
||||
|
||||
|
||||
private function hasEndDate()
|
||||
{
|
||||
return !is_null($this->end_date);
|
||||
}
|
||||
|
||||
|
||||
private function startDateIsValid()
|
||||
{
|
||||
return today() >= $this->start_date;
|
||||
}
|
||||
|
||||
|
||||
private function endDateIsValid()
|
||||
{
|
||||
return today() <= $this->end_date;
|
||||
}
|
||||
|
||||
|
||||
private function couponHasNoUsageLimitForCustomers()
|
||||
{
|
||||
return is_null($this->usage_limit_per_customer);
|
||||
}
|
||||
|
||||
|
||||
private function userIsNotLoggedInWhenAddingCouponToCart($customerEmail = null)
|
||||
{
|
||||
return is_null($customerEmail) && auth()->guest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user