123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace App\Models;
- use Hidehalo\Nanoid\Client;
- use Illuminate\Database\Eloquent\Model;
- use NumberFormatter;
- use Spatie\Activitylog\LogOptions;
- use Spatie\Activitylog\Traits\LogsActivity;
- class ShopProduct extends Model
- {
- use LogsActivity;
- public function getActivitylogOptions(): LogOptions
- {
- return LogOptions::defaults()
- -> logOnlyDirty()
- -> logOnly(['*'])
- -> dontSubmitEmptyLogs();
- }
- /**
- * @var bool
- */
- public $incrementing = false;
- /**
- * @var string[]
- */
- protected $fillable = [
- 'type',
- 'price',
- 'description',
- 'display',
- 'currency_code',
- 'quantity',
- 'disabled',
- ];
- /**
- * @var string[]
- */
- protected $casts = [
- 'price' => 'float'
- ];
- public static function boot()
- {
- parent::boot();
- static::creating(function (ShopProduct $shopProduct) {
- $client = new Client();
- $shopProduct->{$shopProduct->getKeyName()} = $client->generateId($size = 21);
- });
- }
- /**
- * @param mixed $value
- * @param string $locale
- * @return float
- */
- public function formatToCurrency($value, $locale = 'en_US')
- {
- $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
- return $formatter->formatCurrency($value, $this->currency_code);
- }
- /**
- * @description Returns the tax in % taken from the Configuration
- *
- * @return int
- */
- public function getTaxPercent()
- {
- $tax = config('SETTINGS::PAYMENTS:SALES_TAX');
- return $tax < 0 ? 0 : $tax;
- }
- public function getPriceAfterDiscount()
- {
- $discountRate = PartnerDiscount::getDiscount() / 100;
- $discountedPrice = $this->price * (1 - $discountRate);
- return round($discountedPrice, 2);
- }
- /**
- * @description Returns the tax as Number
- *
- * @return float
- */
- public function getTaxValue()
- {
- $taxValue = $this->getPriceAfterDiscount() * $this->getTaxPercent() / 100;
- return round($taxValue, 2);
- }
- /**
- * @description Returns the full price of a Product including tax
- *
- * @return float
- */
- public function getTotalPrice()
- {
- $total = $this->getPriceAfterDiscount() + $this->getTaxValue();
- return round($total, 2);
- }
- }
|