Shipping Tax Card

This commit is contained in:
Kevin Adametz 2020-07-01 16:13:38 +02:00
parent da08e9ff37
commit eb55b01b0d
13 changed files with 86 additions and 81 deletions

View file

@ -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;