¨4.0.1¨
This commit is contained in:
@@ -18,14 +18,18 @@ class FlashSale extends Model
|
||||
* @var self
|
||||
*/
|
||||
private static $active;
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['campaign_name'];
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['translations'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
@@ -33,46 +37,12 @@ class FlashSale extends Model
|
||||
*/
|
||||
protected $fillable = ['id'];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['campaign_name'];
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
{
|
||||
static::saved(function ($flashSale) {
|
||||
if (! empty(request()->has('products'))) {
|
||||
$flashSale->saveProducts(request('products'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the active flash sale.
|
||||
*
|
||||
* @param int $id
|
||||
* @return self
|
||||
*/
|
||||
public static function active()
|
||||
{
|
||||
if (is_callable(self::$active)) {
|
||||
return call_user_func(self::$active);
|
||||
}
|
||||
|
||||
return new self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an active flash sale campaign.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function activeCampaign($id)
|
||||
@@ -84,16 +54,30 @@ class FlashSale extends Model
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static function contains(Product $product)
|
||||
{
|
||||
return self::active()->products->contains($product);
|
||||
}
|
||||
|
||||
public static function pivot(Product $product)
|
||||
|
||||
/**
|
||||
* Get the active flash sale.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function active()
|
||||
{
|
||||
return self::active()->products->find($product)->pivot;
|
||||
if (is_callable(self::$active)) {
|
||||
return call_user_func(self::$active);
|
||||
}
|
||||
|
||||
return new self;
|
||||
}
|
||||
|
||||
|
||||
public static function remainingQty(Product $product)
|
||||
{
|
||||
$flashSaleProduct = self::pivot($product);
|
||||
@@ -101,13 +85,36 @@ class FlashSale extends Model
|
||||
return $flashSaleProduct->qty - $flashSaleProduct->sold;
|
||||
}
|
||||
|
||||
public function scopeWithEligibleProducts($query)
|
||||
|
||||
public static function pivot(Product $product)
|
||||
{
|
||||
$query->with(['products' => function ($query) {
|
||||
$query->forCard()->wherePivot('end_date', '>', now());
|
||||
}]);
|
||||
return self::active()->products->find($product)->pivot;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
{
|
||||
static::saved(function ($flashSale) {
|
||||
if (!empty(request()->has('products'))) {
|
||||
$flashSale->saveProducts(request('products'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public function saveProducts($products)
|
||||
{
|
||||
$this->products()->sync(
|
||||
$this->buildProductPivots($products)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->belongsToMany(Product::class, 'flash_sale_products')
|
||||
@@ -116,18 +123,27 @@ class FlashSale extends Model
|
||||
->orderBy('position');
|
||||
}
|
||||
|
||||
|
||||
public function scopeWithEligibleProducts($query)
|
||||
{
|
||||
$query->with(['products' => function ($query) {
|
||||
$query->forCard()->wherePivot('end_date', '<>', null);
|
||||
}]);
|
||||
}
|
||||
|
||||
|
||||
public function getPriceAttribute($price)
|
||||
{
|
||||
return Money::inDefaultCurrency($price);
|
||||
}
|
||||
|
||||
public function saveProducts($products)
|
||||
|
||||
public function table()
|
||||
{
|
||||
$this->products()->sync(
|
||||
$this->buildProductPivots($products)
|
||||
);
|
||||
return new AdminTable($this->query());
|
||||
}
|
||||
|
||||
|
||||
private function buildProductPivots($products)
|
||||
{
|
||||
return collect($products)->values()->mapWithKeys(function ($attributes, $index) {
|
||||
@@ -139,9 +155,4 @@ class FlashSale extends Model
|
||||
]];
|
||||
});
|
||||
}
|
||||
|
||||
public function table()
|
||||
{
|
||||
return new AdminTable($this->query());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user