You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.8 KiB
PHTML

1 year ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
1 year ago
class LocationProfile extends Model
{
const EXPIRED_HOUR = 'Jam';
const EXPIRED_DAY = 'Hari';
const EXPIRED_WEEK = 'Minggu';
const EXPIRED_MONTH = 'Bulan';
const EXPIRED_UNIT = [
self::EXPIRED_HOUR,
self::EXPIRED_DAY,
self::EXPIRED_WEEK,
self::EXPIRED_MONTH,
];
protected $fillable = [
'location_id',
'name',
'quota',
'display_note',
'expired',
'expired_unit',
'description',
'min_stock',
'price',
'display_price',
'discount',
'price_poin',
'bonus_poin',
];
protected $appends = [
'display_expired',
'validate_price',
'validate_display_price',
'validate_discount',
];
protected static function booted(): void
{
static::creating(function (LocationProfile $model) {
$price = $model->display_price;
if ($model->discount > 0) {
$price = $price - ($price * ($model->discount / 100));
}
$model->price = $price;
});
static::updating(function (LocationProfile $model) {
$price = $model->display_price;
if ($model->discount > 0) {
$price = $price - ($price * ($model->discount / 100));
}
$model->price = $price;
});
}
private static $instance = [];
private static function getInstance()
{
if (count(self::$instance) == 0) {
self::$instance = [
'customer' => Customer::find(auth()->guard('customer')->id())
];
}
return self::$instance;
}
public function prices()
{
return $this->hasMany(LocationProfilePrice::class);
}
public function location()
{
return $this->belongsTo(Location::class);
}
public function vouchers()
{
return $this->hasMany(Voucher::class);
}
public function displayExpired(): Attribute
{
return Attribute::make(get: function () {
return $this->expired . ' ' . $this->expired_unit;
});
}
public function countVouchers(): Attribute
{
return Attribute::make(get: function () {
$unsoldCount = $this->vouchers()->where('is_sold', Voucher::UNSOLD)->count();
return [
'color' => $unsoldCount <= $this->min_stock ? 'bg-red-200 border-red-500' : 'bg-green-200 border-green-500',
'unsold_count' => $unsoldCount,
];
});
}
public function validatePrice(): Attribute
{
return Attribute::make(get: function () {
if ($this->prices->count() > 0) {
$price = $this->prices;
if (auth()->guard('customer')->check()) {
$customer = self::getInstance()['customer'];
return $price->where('customer_level_id', $customer->customer_level_id)
->value('price');
}
return $price->max('price');
}
return $this->price;
});
}
public function validateDisplayPrice(): Attribute
{
return Attribute::make(get: function () {
if ($this->prices->count() > 0) {
$price = $this->prices;
if (auth()->guard('customer')->check()) {
$customer = self::getInstance()['customer'];
return $price->where('customer_level_id', $customer->customer_level_id)
->value('display_price');
}
return $price->max('display_price');
}
return $this->display_price;
});
}
public function validateDiscount(): Attribute
{
return Attribute::make(get: function () {
if ($this->prices->count() > 0) {
$price = $this->prices;
if (auth()->guard('customer')->check()) {
$customer = self::getInstance()['customer'];
return $price->where('customer_level_id', $customer->customer_level_id)
->value('discount');
}
return $price->min('discount');
}
return $this->discount;
});
}
public function shuffle_unsold($limit)
{
$vouchers = Voucher::where([
['is_sold', '=', Voucher::UNSOLD],
['location_profile_id', '=', $this->id],
])
->limit($limit)
->get();
return $vouchers;
}
public function count_unsold()
{
$voucher = Voucher::where([
['is_sold', '=', Voucher::UNSOLD],
['location_profile_id', '=', $this->id],
])->count();
return $voucher;
}
1 year ago
}