Shipping Tax Card
This commit is contained in:
parent
da08e9ff37
commit
eb55b01b0d
13 changed files with 86 additions and 81 deletions
|
|
@ -59,8 +59,8 @@ class ShippingController extends Controller
|
|||
$shipping = false;
|
||||
$data = Request::all();
|
||||
|
||||
if($data['action'] == 'shipping'){
|
||||
if ($data['id'] == "new") {
|
||||
if($data['action'] === 'shipping'){
|
||||
if ($data['id'] === "new") {
|
||||
$shipping = new Shipping();
|
||||
$rules = array('name' => 'required');
|
||||
} else {
|
||||
|
|
@ -79,7 +79,7 @@ class ShippingController extends Controller
|
|||
$shipping->save();
|
||||
}
|
||||
|
||||
if($data['action'] == 'price'){
|
||||
if($data['action'] === 'price'){
|
||||
$shipping = Shipping::findOrFail($data['shipping_id']);
|
||||
$rules = array('price' => 'required');
|
||||
$ret = ['value' => $shipping];
|
||||
|
|
@ -87,7 +87,7 @@ class ShippingController extends Controller
|
|||
if ($validator->fails()) {
|
||||
return view('admin.shipping.edit', $ret)->withErrors($validator);
|
||||
}
|
||||
if ($data['id'] == "new") {
|
||||
if ($data['id'] === "new") {
|
||||
$price = ShippingPrice::create($data);
|
||||
} else {
|
||||
$price = ShippingPrice::findOrFail($data['id']);
|
||||
|
|
@ -99,7 +99,7 @@ class ShippingController extends Controller
|
|||
}
|
||||
|
||||
}
|
||||
if($data['action'] == 'country'){
|
||||
if($data['action'] === 'country'){
|
||||
$shipping = Shipping::findOrFail($data['shipping_id']);
|
||||
foreach($data['country_ids'] as $country_id){
|
||||
if(ShippingCountry::where('country_id', $country_id)->count() == 0){
|
||||
|
|
|
|||
|
|
@ -73,4 +73,7 @@ class Shipping extends Model
|
|||
public function prices(){
|
||||
return $this->hasMany('App\Models\ShippingPrice', 'shipping_id', 'id');
|
||||
}
|
||||
public function shipping_prices(){
|
||||
return $this->hasMany('App\Models\ShippingPrice', 'shipping_id', 'id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||
* @property int $id
|
||||
* @property int $shipping_id
|
||||
* @property float|null $price
|
||||
* @property float|null $tax
|
||||
* @property float|null $tax_rate
|
||||
* @property float|null $factor
|
||||
* @property float|null $total_from
|
||||
* @property float|null $total_to
|
||||
|
|
@ -40,7 +40,7 @@ class ShippingPrice extends Model
|
|||
protected $table = 'shipping_prices';
|
||||
|
||||
protected $fillable = [
|
||||
'shipping_id', 'price', 'tax', 'factor', 'total_from', 'total_to', 'weight_from', 'weight_to',
|
||||
'shipping_id', 'price', 'tax_rate', 'factor', 'total_from', 'total_to', 'weight_from', 'weight_to',
|
||||
];
|
||||
|
||||
public function shipping()
|
||||
|
|
@ -75,9 +75,9 @@ class ShippingPrice extends Model
|
|||
{
|
||||
$value = $this->_format_number($value);
|
||||
if ($value == "") {
|
||||
$this->attributes['tax'] = null;
|
||||
$this->attributes['tax_rate'] = null;
|
||||
} else {
|
||||
$this->attributes['tax'] = floatval(str_replace(',', '.', $value));
|
||||
$this->attributes['tax_rate'] = floatval(str_replace(',', '.', $value));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -115,15 +115,15 @@ class ShippingPrice extends Model
|
|||
return number_format($this->attributes['price'], 2, ',', '.');
|
||||
}
|
||||
|
||||
public function getFormattedTax()
|
||||
public function getFormattedTaxRate()
|
||||
{
|
||||
if ($this->attributes['tax'] === NULL) {
|
||||
return $this->attributes['tax'];
|
||||
if ($this->attributes['tax_rate'] === NULL) {
|
||||
return $this->attributes['tax_rate'];
|
||||
}
|
||||
if (\App::getLocale() == "en") {
|
||||
return number_format($this->attributes['tax'], 2, '.', ',');
|
||||
return number_format($this->attributes['tax_rate'], 2, '.', ',');
|
||||
}
|
||||
return number_format($this->attributes['tax'], 2, ',', '.');
|
||||
return number_format($this->attributes['tax_rate'], 2, ',', '.');
|
||||
}
|
||||
|
||||
public function getFormattedFactor()
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ use Illuminate\Support\Collection;
|
|||
|
||||
class Yard extends Cart
|
||||
{
|
||||
private $shipping = 0;
|
||||
private $shipping_price = 0;
|
||||
private $shipping_tax = 0;
|
||||
private $shipping_country_id = 0; //default de
|
||||
private $ysession;
|
||||
private $yinstance;
|
||||
|
|
@ -21,7 +22,11 @@ class Yard extends Cart
|
|||
$this->ysession = $session;
|
||||
$this->yinstance = sprintf('%s.%s', 'cart', 'shipping_extras');
|
||||
if($this->getShippingExtra('shipping_price')){
|
||||
$this->shipping = floatval($this->getShippingExtra('shipping_price'));
|
||||
$this->shipping_price = (float) ($this->getShippingExtra('shipping_price'));
|
||||
}
|
||||
|
||||
if($this->getShippingExtra('shipping_tax')){
|
||||
$this->shipping_tax = (float) ($this->getShippingExtra('shipping_tax'));
|
||||
}
|
||||
|
||||
if($this->getShippingExtra('shipping_country_id')){
|
||||
|
|
@ -32,20 +37,18 @@ class Yard extends Cart
|
|||
|
||||
if($this->shipping_country_id == 0){
|
||||
$shippingCountry = ShippingCountry::first();
|
||||
$this->shipping_country_id = $shippingCountry->id;
|
||||
|
||||
if($shippingCountry){
|
||||
$this->shipping_country_id = $shippingCountry->id;
|
||||
}
|
||||
}
|
||||
if($this->shipping == 0){
|
||||
if($this->shipping_price == 0){
|
||||
self::instance('shopping')->setShippingCountryWithPrice($this->shipping_country_id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function getTaxRate()
|
||||
{
|
||||
|
||||
return config('cart.tax');
|
||||
|
||||
}
|
||||
|
||||
public function putShippingExtra($key, $value){
|
||||
|
|
@ -53,7 +56,6 @@ class Yard extends Cart
|
|||
$content = $this->getYContent();
|
||||
$content->put($key, $value);
|
||||
$this->ysession->put($this->yinstance, $content);
|
||||
|
||||
}
|
||||
|
||||
public function getShippingExtra($key){
|
||||
|
|
@ -113,24 +115,26 @@ class Yard extends Cart
|
|||
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
|
||||
$shipping = $shippingCountry->shipping;
|
||||
|
||||
if(intval($this->weight()) == 0){
|
||||
$price = $shipping->prices->first();
|
||||
$price->price = 0;
|
||||
if($this->weight() == 0){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
$shipping_price->price = 0;
|
||||
}else{
|
||||
//first by price
|
||||
$price = $this->shippingPriceByTotal($shipping->prices, floatval($this->total(2, '.', ',')));
|
||||
$shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', ''));
|
||||
//sec by weight
|
||||
if(!$price){
|
||||
$price = $this->shippingPriceByWeight($shipping->prices, intval($this->weight()));
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
|
||||
}
|
||||
//default
|
||||
if(!$price){
|
||||
$price = $shipping->prices->first();
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
}
|
||||
}
|
||||
if($price){
|
||||
$this->shipping = floatval($price->price);
|
||||
$this->putShippingExtra('shipping_price', $this->shipping);
|
||||
if($shipping_price){
|
||||
$this->shipping_price = $shipping_price->price;
|
||||
$this->shipping_tax = $shipping_price->tax_rate;
|
||||
$this->putShippingExtra('shipping_price', $this->shipping_price);
|
||||
$this->putShippingExtra('shipping_tax', $this->shipping_tax);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -155,9 +159,6 @@ class Yard extends Cart
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param null $decimals
|
||||
* @param null $decimalPoint
|
||||
|
|
@ -166,26 +167,24 @@ class Yard extends Cart
|
|||
*/
|
||||
public function shipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->shipping, $decimals, $decimalPoint, $thousandSeperator);
|
||||
return $this->numberFormat($this->shipping_price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
//
|
||||
private function shippingTax($taxRate = -19){
|
||||
return ($this->shipping * ($taxRate / 100));
|
||||
private function shippingTax($decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$shippingTax = $this->shipping_price / (100 + $this->shipping_tax) * $this->shipping_tax;
|
||||
return $this->numberFormat($shippingTax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
private function shippingSub($taxRate){
|
||||
return ($this->shipping + $this->shippingTax($taxRate));
|
||||
private function subShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$subShipping = $this->shipping_price - $this->shippingTax($decimals, $decimalPoint, $thousandSeperator);
|
||||
return $this->numberFormat($subShipping, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function subtotalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$subtotal = $this->subtotal(2, '.', '') + $this->shipping;
|
||||
//$taxRate =config('cart.tax');
|
||||
/*$total = (float) ($this->total(2, '.', '')) + $this->shipping;
|
||||
$totalTax = $total/ (100 + $taxRate) * $taxRate;*/
|
||||
$subtotal = $this->subtotal(2, '.', '') + $this->subShipping(2, '.', '');
|
||||
return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
|
@ -193,21 +192,17 @@ class Yard extends Cart
|
|||
public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$totalTax = $this->tax(2, '.', '');
|
||||
/* $taxRate =config('cart.tax');
|
||||
$total = (float) ($this->total(2, '.', '')) + $this->shipping;
|
||||
$totalTax = $total/ (100 + $taxRate) * $taxRate; */
|
||||
return $this->numberFormat($totalTax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
$shippingTax = $this->shippingTax(2, '.', '');
|
||||
return $this->numberFormat(($totalTax+$shippingTax), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$total = (float) ($this->total(2, '.', '')) + $this->shipping;
|
||||
$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
|
||||
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the total price of the items in the cart.
|
||||
*
|
||||
|
|
@ -220,7 +215,7 @@ class Yard extends Cart
|
|||
{
|
||||
$content = $this->getContent();
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->options->weight ? intval($cartItem->options->weight*$cartItem->qty) : 0);
|
||||
return $total + ($cartItem->options->weight ? ($cartItem->options->weight*$cartItem->qty) : 0);
|
||||
}, 0);
|
||||
|
||||
return $total;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue