¨4.0.1¨
This commit is contained in:
@@ -7,6 +7,7 @@ use Modules\Support\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class TaxClass extends Model
|
||||
@@ -16,21 +17,24 @@ class TaxClass extends Model
|
||||
const SHIPPING_ADDRESS = 'shipping_address';
|
||||
const BILLING_ADDRESS = 'billing_address';
|
||||
const STORE_ADDRESS = 'store_address';
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['label'];
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
@@ -38,12 +42,19 @@ class TaxClass extends Model
|
||||
*/
|
||||
protected $dates = ['start_date', 'end_date', 'deleted_at'];
|
||||
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
* Get tag list.
|
||||
*
|
||||
* @var array
|
||||
* @return Collection
|
||||
*/
|
||||
public $translatedAttributes = ['label'];
|
||||
public static function list()
|
||||
{
|
||||
return Cache::tags('tax_classes')->rememberForever(md5('tax_classes.list:' . locale()), function () {
|
||||
return self::all()->sortBy('label')->pluck('label', 'id');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
@@ -57,18 +68,30 @@ class TaxClass extends Model
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tag list.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function list()
|
||||
|
||||
public function saveRates($rates = [])
|
||||
{
|
||||
return Cache::tags('tax_classes')->rememberForever(md5('tax_classes.list:' . locale()), function () {
|
||||
return self::all()->sortBy('label')->pluck('label', 'id');
|
||||
});
|
||||
$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');
|
||||
}
|
||||
|
||||
|
||||
public function findTaxRate($addresses)
|
||||
{
|
||||
return $this->taxRates()
|
||||
@@ -76,6 +99,7 @@ class TaxClass extends Model
|
||||
->first();
|
||||
}
|
||||
|
||||
|
||||
public function determineAddress($addresses)
|
||||
{
|
||||
if ($this->based_on === self::SHIPPING_ADDRESS) {
|
||||
@@ -100,36 +124,18 @@ class TaxClass extends Model
|
||||
return [];
|
||||
}
|
||||
|
||||
public function taxRates()
|
||||
{
|
||||
return $this->hasMany(TaxRate::class)->orderBy('position');
|
||||
}
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
|
||||
|
||||
public function table()
|
||||
{
|
||||
return new AdminTable($this->newQuery());
|
||||
}
|
||||
|
||||
public function saveRates($rates = [])
|
||||
{
|
||||
$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]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function getDeleteCandidates($rates = [])
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user