first upload all files

This commit is contained in:
NW
2023-06-11 13:14:03 +01:00
parent f14dbc52b5
commit c08b36d1b6
1705 changed files with 106852 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
<?php
namespace Modules\Option\Entities;
use Modules\Support\Eloquent\Model;
use Modules\Option\Admin\OptionTable;
use Modules\Support\Eloquent\Translatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class Option extends Model
{
use Translatable, SoftDeletes;
/**
* Available option types.
*
* @var array
*/
const TYPES = [
'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
*/
protected $with = ['translations', 'values'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['option', 'type', 'is_required', 'is_global', 'position'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'is_required' => 'boolean',
'is_global' => 'boolean',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* The attributes that are translatable.
*
* @var array
*/
protected $translatedAttributes = ['name'];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::saved(function ($option) {
if (request()->routeIs('admin.options.*')) {
$option->saveValues(request('values', []));
}
});
}
public function isFieldType()
{
return in_array($this->type, ['field', 'textarea', 'dropdown', 'radio', 'date', 'date_time', 'time']);
}
/**
* Get the values for the option.
*
* @return mixed
*/
public function values()
{
return $this->hasMany(OptionValue::class)->orderBy('position');
}
/**
* Scope a query to only include global options.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeGlobals($query)
{
return $query->where('is_global', true);
}
/**
* Get table data for the resource
*
* @return \Illuminate\Http\JsonResponse
*/
public function table()
{
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)
{
return $this->values()
->pluck('id')
->diff(array_pluck($values, 'id'));
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Modules\Option\Entities;
use Modules\Support\Eloquent\TranslationModel;
class OptionTranslation extends TranslationModel
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name'];
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Modules\Option\Entities;
use Modules\Support\Money;
use Modules\Support\Eloquent\Model;
use Modules\Product\Entities\Product;
use Modules\Support\Eloquent\Translatable;
class OptionValue extends Model
{
use Translatable;
/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['translations'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['price', 'price_type', 'position'];
/**
* The attributes that are translatable.
*
* @var array
*/
protected $translatedAttributes = ['label'];
public function getPriceAttribute($price)
{
if ($this->priceIsPercent()) {
return $price;
}
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)
{
$price = $this->priceForProduct($product);
if ($price->isZero()) {
return;
}
$formattedPrice = $price->convertToCurrentCurrency()->format();
if ($forSelectOption) {
return "+ {$formattedPrice}";
}
return "<span class='extra-price'>+ {$formattedPrice}</span>";
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Modules\Option\Entities;
use Modules\Support\Eloquent\TranslationModel;
class OptionValueTranslation extends TranslationModel
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['label'];
}