2021-06-05 09:26:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Hidehalo\Nanoid\Client;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use NumberFormatter;
|
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
2021-11-05 16:38:05 +00:00
|
|
|
use App\Models\Configuration;
|
2021-06-05 09:26:32 +00:00
|
|
|
|
|
|
|
class PaypalProduct extends Model
|
|
|
|
{
|
|
|
|
use LogsActivity;
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
"type",
|
|
|
|
"price",
|
|
|
|
"description",
|
|
|
|
"display",
|
|
|
|
"currency_code",
|
|
|
|
"quantity",
|
|
|
|
"disabled",
|
|
|
|
];
|
|
|
|
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::creating(function (PaypalProduct $paypalProduct) {
|
|
|
|
$client = new Client();
|
|
|
|
|
|
|
|
$paypalProduct->{$paypalProduct->getKeyName()} = $client->generateId($size = 21);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-23 09:28:57 +00:00
|
|
|
/**
|
2021-11-07 17:02:53 +00:00
|
|
|
* @param float/int
|
2021-06-23 09:28:57 +00:00
|
|
|
* @param string $locale
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-11-07 17:02:53 +00:00
|
|
|
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
|
|
|
/**
|
|
|
|
* @desc Returns the tax in % or 0 if less than 0
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getTaxPercent()
|
|
|
|
{
|
2021-11-05 16:38:05 +00:00
|
|
|
$tax = Configuration::getValueByKey("SALES_TAX");
|
2021-11-07 17:02:53 +00:00
|
|
|
return $tax < 0 ? 0 : $tax;
|
2021-11-05 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 17:02:53 +00:00
|
|
|
/**
|
|
|
|
* @desc Returns the total tax value.
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getTaxValue()
|
|
|
|
{
|
2021-11-05 16:38:05 +00:00
|
|
|
return $this->price*$this->getTaxPercent()/100;
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:02:53 +00:00
|
|
|
/**
|
|
|
|
* @desc Returns the total price incl. tax
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getTotalPrice()
|
|
|
|
{
|
2021-11-05 16:38:05 +00:00
|
|
|
return $this->price+($this->getTaxValue());
|
|
|
|
}
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|