¨4.0.1¨

This commit is contained in:
¨NW¨
2023-12-03 14:07:47 +00:00
parent c08b36d1b6
commit f35052522d
1112 changed files with 43019 additions and 24987 deletions

View File

@@ -4,6 +4,7 @@ namespace Modules\Option\Entities;
use Modules\Support\Eloquent\Model;
use Modules\Option\Admin\OptionTable;
use Illuminate\Database\Eloquent\Builder;
use Modules\Support\Eloquent\Translatable;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -20,8 +21,7 @@ class Option extends Model
'field', 'textarea', 'dropdown', 'checkbox', 'checkbox_custom',
'radio', 'radio_custom', 'multiple_select', 'date', 'date_time', 'time',
];
/**
/*
* The relations to eager load on every query.
*
* @var array
@@ -59,6 +59,7 @@ class Option extends Model
*/
protected $translatedAttributes = ['name'];
/**
* Perform any actions required after the model boots.
*
@@ -73,11 +74,34 @@ class Option extends Model
});
}
public function isFieldType()
/**
* Save values for the option.
*
* @param array $values
*
* @return void
*/
public function saveValues($values = [])
{
return in_array($this->type, ['field', 'textarea', 'dropdown', 'radio', 'date', 'date_time', 'time']);
$ids = $this->getDeleteCandidates($values);
if ($ids->isNotEmpty()) {
$this->values()->whereIn('id', $ids)->delete();
}
$counter = 0;
foreach (array_reset_index($values) as $attributes) {
$attributes += ['position' => ++$counter];
$this->values()->updateOrCreate([
'id' => array_get($attributes, 'id'),
], $attributes);
}
}
/**
* Get the values for the option.
*
@@ -88,49 +112,36 @@ class Option extends Model
return $this->hasMany(OptionValue::class)->orderBy('position');
}
public function isFieldType()
{
return in_array($this->type, ['field', 'textarea', 'dropdown', 'radio', 'date', 'date_time', 'time']);
}
/**
* Scope a query to only include global options.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
* @param Builder $query
*
* @return Builder
*/
public function scopeGlobals($query)
{
return $query->where('is_global', true);
}
/**
* Get table data for the resource
*
* @return \Illuminate\Http\JsonResponse
* @return OptionTable
*/
public function table()
public function table(): OptionTable
{
return new OptionTable($this->newQuery()->globals());
}
/**
* Save values for the option.
*
* @param array $values
* @return void
*/
public function saveValues($values = [])
{
$ids = $this->getDeleteCandidates($values);
if ($ids->isNotEmpty()) {
$this->values()->whereIn('id', $ids)->delete();
}
foreach (array_reset_index($values) as $index => $attributes) {
$attributes += ['position' => $index];
$this->values()->updateOrCreate([
'id' => array_get($attributes, 'id'),
], $attributes);
}
}
private function getDeleteCandidates($values)
{

View File

@@ -32,6 +32,7 @@ class OptionValue extends Model
*/
protected $translatedAttributes = ['label'];
public function getPriceAttribute($price)
{
if ($this->priceIsPercent()) {
@@ -41,29 +42,12 @@ class OptionValue extends Model
return Money::inDefaultCurrency($price);
}
public function priceIsPercent()
{
return $this->price_type === 'percent';
}
public function priceIsFixed()
{
return $this->price_type === 'fixed';
}
public function priceForProduct(Product $product)
{
if ($this->priceIsFixed()) {
return $this->price;
}
return $this->getPercentOf($product->selling_price->amount());
}
private function getPercentOf($productPrice)
{
return Money::inDefaultCurrency(($this->price / 100) * $productPrice);
}
public function formattedPriceForProduct(Product $product, $forSelectOption = false)
{
@@ -81,4 +65,28 @@ class OptionValue extends Model
return "<span class='extra-price'>+ {$formattedPrice}</span>";
}
public function priceForProduct(Product $product)
{
if ($this->priceIsFixed()) {
return $this->price;
}
return $this->getPercentOf(($product->variant ?? $product)
->selling_price
->amount());
}
public function priceIsFixed()
{
return $this->price_type === 'fixed';
}
private function getPercentOf($productPrice)
{
return Money::inDefaultCurrency(($this->price / 100) * $productPrice);
}
}