first upload all files
This commit is contained in:
140
Modules/Tax/Entities/TaxClass.php
Normal file
140
Modules/Tax/Entities/TaxClass.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?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;
|
||||
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';
|
||||
|
||||
/**
|
||||
* 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'];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['label'];
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
{
|
||||
static::saved(function ($taxClass) {
|
||||
$taxClass->saveRates(request('rates', []));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tag list.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function list()
|
||||
{
|
||||
return Cache::tags('tax_classes')->rememberForever(md5('tax_classes.list:' . locale()), function () {
|
||||
return self::all()->sortBy('label')->pluck('label', 'id');
|
||||
});
|
||||
}
|
||||
|
||||
public function findTaxRate($addresses)
|
||||
{
|
||||
return $this->taxRates()
|
||||
->findByAddress($this->determineAddress($addresses))
|
||||
->first();
|
||||
}
|
||||
|
||||
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 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 = [])
|
||||
{
|
||||
return $this->taxRates()
|
||||
->pluck('id')
|
||||
->diff(array_pluck($rates, 'id'));
|
||||
}
|
||||
}
|
||||
15
Modules/Tax/Entities/TaxClassTranslation.php
Normal file
15
Modules/Tax/Entities/TaxClassTranslation.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Tax\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\TranslationModel;
|
||||
|
||||
class TaxClassTranslation extends TranslationModel
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['label'];
|
||||
}
|
||||
128
Modules/Tax/Entities/TaxRate.php
Normal file
128
Modules/Tax/Entities/TaxRate.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Tax\Entities;
|
||||
|
||||
use Modules\Support\Money;
|
||||
use Modules\Order\Entities\Order;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Order\Entities\OrderTax;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class TaxRate extends Model
|
||||
{
|
||||
use Translatable, SoftDeletes;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = ['start_date', 'end_date', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $translatedAttributes = ['name'];
|
||||
|
||||
public function orders()
|
||||
{
|
||||
return $this->belongsToMany(Order::class, 'order_taxes')
|
||||
->using(OrderTax::class)
|
||||
->as('order_tax')
|
||||
->withPivot('amount');
|
||||
}
|
||||
|
||||
public function scopeFindByAddress($query, $address)
|
||||
{
|
||||
$address = $this->mergeDefaultAddress($address);
|
||||
|
||||
$query->whereIn('country', [$address['country'], '*'])
|
||||
->whereIn('state', [$address['state'], '*'])
|
||||
->whereIn('city', [$address['city'], '*'])
|
||||
->whereIn('zip', [$address['zip'], '*'])
|
||||
->orderByRaw($this->rawOrderClause(), [
|
||||
$address['country'],
|
||||
$address['state'],
|
||||
$address['city'],
|
||||
$address['zip'],
|
||||
])
|
||||
->orderBy('position');
|
||||
}
|
||||
|
||||
private function mergeDefaultAddress($address)
|
||||
{
|
||||
return $address += ['country' => null, 'state' => null, 'city' => null, 'zip' => null];
|
||||
}
|
||||
|
||||
private function rawOrderClause()
|
||||
{
|
||||
return "(
|
||||
CASE WHEN country = ? THEN 1 ELSE 0 END +
|
||||
CASE WHEN state = ? THEN 1 ELSE 0 END +
|
||||
CASE WHEN city = ? THEN 1 ELSE 0 END +
|
||||
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);
|
||||
}
|
||||
}
|
||||
15
Modules/Tax/Entities/TaxRateTranslation.php
Normal file
15
Modules/Tax/Entities/TaxRateTranslation.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Tax\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\TranslationModel;
|
||||
|
||||
class TaxRateTranslation extends TranslationModel
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name'];
|
||||
}
|
||||
Reference in New Issue
Block a user