added price_offset_percent
This commit is contained in:
+44
-2
@@ -9,12 +9,49 @@ use Rooyesh\Wallet\models\Transaction;
|
||||
use Rooyesh\Wallet\models\Wallet;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class Product extends Model
|
||||
{
|
||||
use HasFactory,Selector,SoftDeletes;
|
||||
|
||||
private static ?bool $priceOffsetColumnExists = null;
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (Product $product) {
|
||||
if (!static::hasPriceOffsetColumn()) {
|
||||
unset($product->attributes['price_offset_percent']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function hasPriceOffsetColumn(): bool
|
||||
{
|
||||
if (static::$priceOffsetColumnExists !== null) {
|
||||
return static::$priceOffsetColumnExists;
|
||||
}
|
||||
|
||||
try {
|
||||
static::$priceOffsetColumnExists = Schema::hasColumn(
|
||||
(new static)->getTable(),
|
||||
'price_offset_percent'
|
||||
);
|
||||
} catch (\Throwable) {
|
||||
static::$priceOffsetColumnExists = false;
|
||||
}
|
||||
|
||||
return static::$priceOffsetColumnExists;
|
||||
}
|
||||
|
||||
public function getPriceOffsetPercentAttribute($value): float
|
||||
{
|
||||
if (!static::hasPriceOffsetColumn()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (float) ($value ?? 0);
|
||||
}
|
||||
protected $fillable = [
|
||||
'title', // The title of the record.
|
||||
'price', // The amount of the record.
|
||||
@@ -38,6 +75,9 @@ class Product extends Model
|
||||
'cash_fee_sell', // Add cash_fee_sell to fillable
|
||||
'percent_fee_buy', // Add percent_fee_buy to fillable
|
||||
'metal_id', //the material of metal (#gold or #silver)
|
||||
'price_offset_percent', // admin only: +/- % vs automatic reference (baked into price on sync)
|
||||
'price_profit',
|
||||
'bubble',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -54,6 +94,8 @@ class Product extends Model
|
||||
'percent_fee_buy' => 'float', // Cast to float.
|
||||
'cash_fee_buy' => 'int', // Cast to int or decimal if needed
|
||||
'cash_fee_sell' => 'int', // Cast to int or decimal if needed
|
||||
'price_offset_percent' => 'float',
|
||||
'price_profit' => 'int',
|
||||
];
|
||||
|
||||
static $METAL_ID = [
|
||||
|
||||
Reference in New Issue
Block a user