2023-06-11 12:14:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Tax\Entities;
|
|
|
|
|
|
|
|
use Modules\Admin\Ui\AdminTable;
|
|
|
|
use Modules\Support\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Modules\Product\Entities\Product;
|
|
|
|
use Modules\Support\Eloquent\Translatable;
|
2023-12-03 14:07:47 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2023-06-11 12:14:03 +00:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
|
|
|
class TaxClass extends Model
|
|
|
|
{
|
|
|
|
use Translatable, SoftDeletes;
|
|
|
|
|
|
|
|
const SHIPPING_ADDRESS = 'shipping_address';
|
|
|
|
const BILLING_ADDRESS = 'billing_address';
|
|
|
|
const STORE_ADDRESS = 'store_address';
|
2023-12-03 14:07:47 +00:00
|
|
|
/**
|
|
|
|
* The attributes that are translatable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $translatedAttributes = ['label'];
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
|
|
|
* The relations to eager load on every query.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $with = ['translations'];
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = ['based_on'];
|
|
|
|
/**
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = ['start_date', 'end_date', 'deleted_at'];
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
2023-12-03 14:07:47 +00:00
|
|
|
* Get tag list.
|
2023-06-11 12:14:03 +00:00
|
|
|
*
|
2023-12-03 14:07:47 +00:00
|
|
|
* @return Collection
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
public static function list()
|
|
|
|
{
|
|
|
|
return Cache::tags('tax_classes')->rememberForever(md5('tax_classes.list:' . locale()), function () {
|
|
|
|
return self::all()->sortBy('label')->pluck('label', 'id');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::saved(function ($taxClass) {
|
|
|
|
$taxClass->saveRates(request('rates', []));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
|
|
|
public function saveRates($rates = [])
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
2023-12-03 14:07:47 +00:00
|
|
|
$ids = $this->getDeleteCandidates($rates);
|
|
|
|
|
|
|
|
if ($ids->isNotEmpty()) {
|
|
|
|
$this->taxRates()->whereIn('id', $ids)->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array_reset_index($rates) as $index => $rate) {
|
|
|
|
$this->taxRates()->updateOrCreate(
|
|
|
|
['id' => $rate['id']],
|
|
|
|
$rate + ['position' => $index]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function taxRates()
|
|
|
|
{
|
|
|
|
return $this->hasMany(TaxRate::class)->orderBy('position');
|
2023-06-11 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
public function findTaxRate($addresses)
|
|
|
|
{
|
|
|
|
return $this->taxRates()
|
|
|
|
->findByAddress($this->determineAddress($addresses))
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
public function determineAddress($addresses)
|
|
|
|
{
|
|
|
|
if ($this->based_on === self::SHIPPING_ADDRESS) {
|
|
|
|
return $addresses['shipping'] ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->based_on === self::BILLING_ADDRESS) {
|
|
|
|
return $addresses['billing'] ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->based_on === self::STORE_ADDRESS) {
|
|
|
|
return [
|
|
|
|
'address_1' => setting('store_address_1'),
|
|
|
|
'address_2' => setting('store_address_2'),
|
|
|
|
'city' => setting('store_city'),
|
|
|
|
'state' => setting('store_state'),
|
|
|
|
'zip' => setting('store_zip'),
|
|
|
|
'country' => setting('store_country'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function products()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Product::class);
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
public function table()
|
|
|
|
{
|
|
|
|
return new AdminTable($this->newQuery());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function getDeleteCandidates($rates = [])
|
|
|
|
{
|
|
|
|
return $this->taxRates()
|
|
|
|
->pluck('id')
|
|
|
|
->diff(array_pluck($rates, 'id'));
|
|
|
|
}
|
|
|
|
}
|