FleetCart/Modules/Tax/Entities/TaxRate.php

141 lines
3.2 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?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;
2023-12-03 14:07:47 +00:00
/**
* The attributes that are translatable.
*
* @var array
*/
public $translatedAttributes = ['name'];
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 = ['country', 'state', 'city', 'zip', 'rate', 'position'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['start_date', 'end_date', 'deleted_at'];
public function orders()
{
return $this->belongsToMany(Order::class, 'order_taxes')
->using(OrderTax::class)
->as('order_tax')
->withPivot('amount');
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
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');
}
public function getCountryAttribute($country)
{
return $country === '*' ? null : $country;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function setCountryAttribute($country)
{
$this->attributes['country'] = $country ?: '*';
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function getStateAttribute($state)
{
return $state === '*' ? null : $state;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function setStateAttribute($state)
{
$this->attributes['state'] = $state ?: '*';
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function getCityAttribute($city)
{
return $city === '*' ? null : $city;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function setCityAttribute($city)
{
$this->attributes['city'] = $city ?: '*';
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function getZipAttribute($zip)
{
return $zip === '*' ? null : $zip;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function setZipAttribute($zip)
{
$this->attributes['zip'] = $zip ?: '*';
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function getTotalAttribute($total)
{
return Money::inDefaultCurrency($total);
}
2023-12-03 14:07:47 +00:00
private function mergeDefaultAddress($address)
{
$address += ['country' => null, 'state' => null, 'city' => null, 'zip' => null];
return $address;
}
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";
}
2023-06-11 12:14:03 +00:00
}