¨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 = [])
|
||||
{
|
||||
|
||||
@@ -13,20 +13,24 @@ class TaxRate extends Model
|
||||
{
|
||||
use Translatable, SoftDeletes;
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['name'];
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['translations'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['country', 'state', 'city', 'zip', 'rate', 'position'];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
@@ -34,12 +38,6 @@ class TaxRate extends Model
|
||||
*/
|
||||
protected $dates = ['start_date', 'end_date', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['name'];
|
||||
|
||||
public function orders()
|
||||
{
|
||||
@@ -49,6 +47,7 @@ class TaxRate extends Model
|
||||
->withPivot('amount');
|
||||
}
|
||||
|
||||
|
||||
public function scopeFindByAddress($query, $address)
|
||||
{
|
||||
$address = $this->mergeDefaultAddress($address);
|
||||
@@ -66,11 +65,69 @@ class TaxRate extends Model
|
||||
->orderBy('position');
|
||||
}
|
||||
|
||||
|
||||
public function getCountryAttribute($country)
|
||||
{
|
||||
return $country === '*' ? null : $country;
|
||||
}
|
||||
|
||||
|
||||
public function setCountryAttribute($country)
|
||||
{
|
||||
$this->attributes['country'] = $country ?: '*';
|
||||
}
|
||||
|
||||
|
||||
public function getStateAttribute($state)
|
||||
{
|
||||
return $state === '*' ? null : $state;
|
||||
}
|
||||
|
||||
|
||||
public function setStateAttribute($state)
|
||||
{
|
||||
$this->attributes['state'] = $state ?: '*';
|
||||
}
|
||||
|
||||
|
||||
public function getCityAttribute($city)
|
||||
{
|
||||
return $city === '*' ? null : $city;
|
||||
}
|
||||
|
||||
|
||||
public function setCityAttribute($city)
|
||||
{
|
||||
$this->attributes['city'] = $city ?: '*';
|
||||
}
|
||||
|
||||
|
||||
public function getZipAttribute($zip)
|
||||
{
|
||||
return $zip === '*' ? null : $zip;
|
||||
}
|
||||
|
||||
|
||||
public function setZipAttribute($zip)
|
||||
{
|
||||
$this->attributes['zip'] = $zip ?: '*';
|
||||
}
|
||||
|
||||
|
||||
public function getTotalAttribute($total)
|
||||
{
|
||||
return Money::inDefaultCurrency($total);
|
||||
}
|
||||
|
||||
|
||||
private function mergeDefaultAddress($address)
|
||||
{
|
||||
return $address += ['country' => null, 'state' => null, 'city' => null, 'zip' => null];
|
||||
$address += ['country' => null, 'state' => null, 'city' => null, 'zip' => null];
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
|
||||
private function rawOrderClause()
|
||||
{
|
||||
return "(
|
||||
@@ -80,49 +137,4 @@ class TaxRate extends Model
|
||||
CASE WHEN zip = ? THEN 1 ELSE 0 END
|
||||
) DESC";
|
||||
}
|
||||
|
||||
public function getCountryAttribute($country)
|
||||
{
|
||||
return $country === '*' ? null : $country;
|
||||
}
|
||||
|
||||
public function setCountryAttribute($country)
|
||||
{
|
||||
$this->attributes['country'] = $country ?: '*';
|
||||
}
|
||||
|
||||
public function getStateAttribute($state)
|
||||
{
|
||||
return $state === '*' ? null : $state;
|
||||
}
|
||||
|
||||
public function setStateAttribute($state)
|
||||
{
|
||||
$this->attributes['state'] = $state ?: '*';
|
||||
}
|
||||
|
||||
public function getCityAttribute($city)
|
||||
{
|
||||
return $city === '*' ? null : $city;
|
||||
}
|
||||
|
||||
public function setCityAttribute($city)
|
||||
{
|
||||
$this->attributes['city'] = $city ?: '*';
|
||||
}
|
||||
|
||||
public function getZipAttribute($zip)
|
||||
{
|
||||
return $zip === '*' ? null : $zip;
|
||||
}
|
||||
|
||||
public function setZipAttribute($zip)
|
||||
{
|
||||
$this->attributes['zip'] = $zip ?: '*';
|
||||
}
|
||||
|
||||
public function getTotalAttribute($total)
|
||||
{
|
||||
return Money::inDefaultCurrency($total);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user