ctrlpanel/app/Models/ShopProduct.php

99 lines
2.2 KiB
PHP
Raw Normal View History

2021-06-05 09:26:32 +00:00
<?php
namespace App\Models;
use Hidehalo\Nanoid\Client;
use Illuminate\Database\Eloquent\Model;
use NumberFormatter;
2023-01-05 18:33:46 +00:00
use Spatie\Activitylog\LogOptions;
2021-06-05 09:26:32 +00:00
use Spatie\Activitylog\Traits\LogsActivity;
2022-05-30 09:07:10 +00:00
class ShopProduct extends Model
2021-06-05 09:26:32 +00:00
{
use LogsActivity;
2023-01-05 18:33:46 +00:00
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
-> logOnlyDirty()
-> logOnly(['*'])
-> dontSubmitEmptyLogs();
}
2021-06-05 09:26:32 +00:00
/**
* @var bool
*/
public $incrementing = false;
/**
* @var string[]
*/
protected $fillable = [
'type',
'price',
'description',
'display',
'currency_code',
'quantity',
'disabled',
2021-06-05 09:26:32 +00:00
];
public static function boot()
{
parent::boot();
2022-05-30 09:07:10 +00:00
static::creating(function (ShopProduct $shopProduct) {
2021-06-05 09:26:32 +00:00
$client = new Client();
2022-05-30 09:07:10 +00:00
$shopProduct->{$shopProduct->getKeyName()} = $client->generateId($size = 21);
2021-06-05 09:26:32 +00:00
});
}
/**
* @param mixed $value
* @param string $locale
* @return float
*/
public function formatToCurrency($value, $locale = 'en_US')
2021-06-05 09:26:32 +00:00
{
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
2021-11-05 08:00:18 +00:00
return $formatter->formatCurrency($value, $this->currency_code);
2021-06-05 09:26:32 +00:00
}
2021-11-05 16:38:05 +00:00
2021-11-07 17:02:53 +00:00
/**
* @description Returns the tax in % taken from the Configuration
*
* @return int
*/
2021-11-07 17:02:53 +00:00
public function getTaxPercent()
{
$tax = config('SETTINGS::PAYMENTS:SALES_TAX');
2021-11-07 17:02:53 +00:00
return $tax < 0 ? 0 : $tax;
2021-11-05 16:38:05 +00:00
}
2022-08-27 16:59:07 +00:00
public function getPriceAfterDiscount()
{
return number_format($this->price - ($this->price * PartnerDiscount::getDiscount() / 100), 2);
}
2021-11-07 17:02:53 +00:00
/**
* @description Returns the tax as Number
*
* @return float
*/
2021-11-07 17:02:53 +00:00
public function getTaxValue()
{
2022-08-27 16:59:07 +00:00
return number_format($this->getPriceAfterDiscount() * $this->getTaxPercent() / 100, 2);
2021-11-05 16:38:05 +00:00
}
2021-11-07 17:02:53 +00:00
/**
* @description Returns the full price of a Product including tax
*
* @return float
*/
2021-12-12 23:58:47 +00:00
public function getTotalPrice()
2021-11-07 17:02:53 +00:00
{
2022-08-27 16:59:07 +00:00
return number_format($this->getPriceAfterDiscount() + $this->getTaxValue(), 2);
2021-11-05 16:38:05 +00:00
}
}