From e01b9912816ce538aff13f6d56b5bfd30c0d738a Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Thu, 6 May 2021 15:45:56 +0530 Subject: [PATCH] Finishing And Refactoring Dog Done --- packages/Webkul/Checkout/src/Cart.php | 66 ++++---------- .../Webkul/Product/src/Type/AbstractType.php | 61 ++----------- packages/Webkul/Product/src/Type/Bundle.php | 16 ++-- .../Webkul/Product/src/Type/Configurable.php | 6 +- .../views/checkout/cart/mini-cart.blade.php | 4 +- .../Resources/views/products/view.blade.php | 2 +- packages/Webkul/Tax/src/Helpers/Tax.php | 86 ++++++++++++++++++- .../shop/checkout/cart/mini-cart.blade.php | 2 +- .../views/shop/products/view.blade.php | 2 +- 9 files changed, 122 insertions(+), 123 deletions(-) diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index de8122eac..26864a480 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -665,65 +665,29 @@ class Cart } if ($address === null) { - $address = new class() { - public $country; - public $state; - public $postcode; - - function __construct() - { - $this->country = core()->getConfigData('taxes.catalogue.default-location-calculation.country') != '' ? core()->getConfigData('taxes.catalogue.default-location-calculation.country') : strtoupper(config('app.default_country')); - $this->state = core()->getConfigData('taxes.catalogue.default-location-calculation.state'); - $this->postcode = core()->getConfigData('taxes.catalogue.default-location-calculation.post_code'); - } - }; + $address = Tax::getDefaultAddress(); } - $taxRates = $taxCategory->tax_rates()->where([ - 'country' => $address->country, - ])->orderBy('tax_rate', 'desc')->get(); - $item = $this->setItemTaxToZero($item); - if ($taxRates->count()) { - foreach ($taxRates as $rate) { - $haveTaxRate = false; + Tax::isTaxApplicableInCurrentAddress($taxCategory, $address, function ($rate) use ($cart, $item) { + /* assigning tax percent */ + $item->tax_percent = $rate->tax_rate; - if ($rate->state != '' && $rate->state != $address->state) { - continue; - } + /* getting shipping rate for tax calculation */ + $shippingPrice = $shippingBasePrice = 0; - if (! $rate->is_zip) { - if (empty($rate->zip_code) || in_array($rate->zip_code, ['*', $address->postcode])) { - $haveTaxRate = true; - } - } else { - if ($address->postcode >= $rate->zip_from && $address->postcode <= $rate->zip_to) { - $haveTaxRate = true; - } - } - - if ($haveTaxRate) { - $item->tax_percent = $rate->tax_rate; - - /* getting shipping rate for tax calculation */ - $shippingPrice = $shippingBasePrice = 0; - - if ($shipping = $cart->selected_shipping_rate) { - if ($shipping->is_calculate_tax) { - $shippingPrice = $shipping->price - $shipping->discount_amount; - $shippingBasePrice = $shipping->base_price - $shipping->base_discount_amount; - } - } - - /* now assigning shipping prices for tax calculation */ - $item->tax_amount = round((($item->total + $shippingPrice) * $rate->tax_rate) / 100, 4); - $item->base_tax_amount = round((($item->base_total + $shippingBasePrice) * $rate->tax_rate) / 100, 4); - - break; + if ($shipping = $cart->selected_shipping_rate) { + if ($shipping->is_calculate_tax) { + $shippingPrice = $shipping->price - $shipping->discount_amount; + $shippingBasePrice = $shipping->base_price - $shipping->base_discount_amount; } } - } + + /* now assigning shipping prices for tax calculation */ + $item->tax_amount = round((($item->total + $shippingPrice) * $rate->tax_rate) / 100, 4); + $item->base_tax_amount = round((($item->base_total + $shippingBasePrice) * $rate->tax_rate) / 100, 4); + }); $item->save(); } diff --git a/packages/Webkul/Product/src/Type/AbstractType.php b/packages/Webkul/Product/src/Type/AbstractType.php index 037a27d7a..680dff0b5 100644 --- a/packages/Webkul/Product/src/Type/AbstractType.php +++ b/packages/Webkul/Product/src/Type/AbstractType.php @@ -2,6 +2,7 @@ namespace Webkul\Product\Type; +use Webkul\Tax\Helpers\Tax; use Webkul\Checkout\Facades\Cart; use Webkul\Checkout\Models\CartItem; use Illuminate\Support\Facades\Storage; @@ -745,25 +746,15 @@ abstract class AbstractType { if ($this->haveSpecialPrice()) { $html = '
' . trans('shop::app.products.sale') . '
' - . '' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . '' - . '' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getSpecialPrice()) : $this->getSpecialPrice()) . ''; + . '' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . '' + . '' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getSpecialPrice()) : $this->getSpecialPrice()) . ''; } else { - $html = '' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . ''; + $html = '' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . ''; } return $html; } - /** - * Is tax inclusive enabled in backend. - * - * @return bool - */ - public function isTaxInclusive(): bool - { - return (bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive'); - } - /** * Get inclusive tax rates. * @@ -783,48 +774,12 @@ abstract class AbstractType } if ($address === null) { - $address = new class() - { - public $country; - public $state; - public $postcode; - - function __construct() - { - $this->country = core()->getConfigData('taxes.catalogue.default-location-calculation.country') != '' ? core()->getConfigData('taxes.catalogue.default-location-calculation.country') : strtoupper(config('app.default_country')); - $this->state = core()->getConfigData('taxes.catalogue.default-location-calculation.state'); - $this->postcode = core()->getConfigData('taxes.catalogue.default-location-calculation.post_code'); - } - }; + $address = Tax::getDefaultAddress(); } - $taxRates = $taxCategory->tax_rates()->where([ - 'country' => $address->country, - ])->orderBy('tax_rate', 'desc')->get(); - - if ($taxRates->count()) { - foreach ($taxRates as $rate) { - $haveTaxRate = false; - - if ($rate->state != '' && $rate->state != $address->state) { - continue; - } - - if (!$rate->is_zip) { - if (empty($rate->zip_code) || in_array($rate->zip_code, ['*', $address->postcode])) { - $haveTaxRate = true; - } - } else { - if ($address->postcode >= $rate->zip_from && $address->postcode <= $rate->zip_to) { - $haveTaxRate = true; - } - } - - if ($haveTaxRate) { - $totalPrice = round($totalPrice, 4) + round(($totalPrice * $rate->tax_rate) / 100, 4); - } - } - } + Tax::isTaxApplicableInCurrentAddress($taxCategory, $address, function ($rate) use (&$totalPrice) { + $totalPrice = round($totalPrice, 4) + round(($totalPrice * $rate->tax_rate) / 100, 4); + }); } return $totalPrice; diff --git a/packages/Webkul/Product/src/Type/Bundle.php b/packages/Webkul/Product/src/Type/Bundle.php index 1c2852f5e..ee4ba114d 100644 --- a/packages/Webkul/Product/src/Type/Bundle.php +++ b/packages/Webkul/Product/src/Type/Bundle.php @@ -363,23 +363,23 @@ class Bundle extends AbstractType return [ 'from' => [ 'regular_price' => [ - 'price' => core()->convertPrice($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMinimalPrice()) : $this->getRegularMinimalPrice()), - 'formated_price' => core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMinimalPrice()) : $this->getRegularMinimalPrice()), + 'price' => core()->convertPrice(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMinimalPrice()) : $this->getRegularMinimalPrice()), + 'formated_price' => core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMinimalPrice()) : $this->getRegularMinimalPrice()), ], 'final_price' => [ - 'price' => core()->convertPrice($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()), - 'formated_price' => core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()), + 'price' => core()->convertPrice(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()), + 'formated_price' => core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()), ] ], 'to' => [ 'regular_price' => [ - 'price' => core()->convertPrice($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMaximamPrice()) : $this->getRegularMaximamPrice()), - 'formated_price' => core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMaximamPrice()) : $this->getRegularMaximamPrice()), + 'price' => core()->convertPrice(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMaximamPrice()) : $this->getRegularMaximamPrice()), + 'formated_price' => core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getRegularMaximamPrice()) : $this->getRegularMaximamPrice()), ], 'final_price' => [ - 'price' => core()->convertPrice($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMaximamPrice()) : $this->getMaximamPrice()), - 'formated_price' => core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMaximamPrice()) : $this->getMaximamPrice()), + 'price' => core()->convertPrice(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMaximamPrice()) : $this->getMaximamPrice()), + 'formated_price' => core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMaximamPrice()) : $this->getMaximamPrice()), ] ] ]; diff --git a/packages/Webkul/Product/src/Type/Configurable.php b/packages/Webkul/Product/src/Type/Configurable.php index 6f0e10778..f321149ca 100644 --- a/packages/Webkul/Product/src/Type/Configurable.php +++ b/packages/Webkul/Product/src/Type/Configurable.php @@ -452,12 +452,12 @@ class Configurable extends AbstractType if ($this->haveOffer()) { return '
' . trans('shop::app.products.sale') . '
' . '' . trans('shop::app.products.price-label') . '' - . '' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . '' - . '' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getOfferPrice()) : $this->getOfferPrice()) . ''; + . '' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . '' + . '' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getOfferPrice()) : $this->getOfferPrice()) . ''; } else { return '' . trans('shop::app.products.price-label') . '' . ' ' - . '' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . ''; + . '' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . ''; } } diff --git a/packages/Webkul/Shop/src/Resources/views/checkout/cart/mini-cart.blade.php b/packages/Webkul/Shop/src/Resources/views/checkout/cart/mini-cart.blade.php index 01c847b25..d80ed8c9e 100755 --- a/packages/Webkul/Shop/src/Resources/views/checkout/cart/mini-cart.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/checkout/cart/mini-cart.blade.php @@ -25,7 +25,7 @@ {!! view_render_event('bagisto.shop.checkout.cart-mini.subtotal.before', ['cart' => $cart]) !!} - @if ((bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive')) + @if (Webkul\Tax\Helpers\Tax::isTaxInclusive()) {{ core()->currency($cart->base_grand_total) }} @else {{ core()->currency($cart->base_sub_total) }} @@ -72,7 +72,7 @@ {!! view_render_event('bagisto.shop.checkout.cart-mini.item.price.before', ['item' => $item]) !!}
- @if ((bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive')) + @if (Webkul\Tax\Helpers\Tax::isTaxInclusive()) {{ core()->currency($item->base_total + $item->tax_amount) }} @else {{ core()->currency($item->base_total) }} diff --git a/packages/Webkul/Shop/src/Resources/views/products/view.blade.php b/packages/Webkul/Shop/src/Resources/views/products/view.blade.php index ce6dab3e3..5ded2858d 100755 --- a/packages/Webkul/Shop/src/Resources/views/products/view.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/products/view.blade.php @@ -63,7 +63,7 @@ @include ('shop::products.price', ['product' => $product]) - @if ((bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive')) + @if (Webkul\Tax\Helpers\Tax::isTaxInclusive())
{{ __('shop::app.products.tax-inclusive') }}
diff --git a/packages/Webkul/Tax/src/Helpers/Tax.php b/packages/Webkul/Tax/src/Helpers/Tax.php index 123ffc527..979df5cfa 100644 --- a/packages/Webkul/Tax/src/Helpers/Tax.php +++ b/packages/Webkul/Tax/src/Helpers/Tax.php @@ -2,6 +2,11 @@ namespace Webkul\Tax\Helpers; +/** + * Tax class. + * + * To Do (@devansh-webkul): Convert this to facade. + */ class Tax { /** @@ -18,6 +23,16 @@ class Tax */ private const TAX_AMOUNT_PRECISION = 2; + /** + * Is tax inclusive enabled in backend. + * + * @return bool + */ + public static function isTaxInclusive(): bool + { + return (bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive'); + } + /** * Returns an array with tax rates and tax amount. * @@ -32,14 +47,14 @@ class Tax foreach ($that->items as $item) { $taxRate = (string) round((float) $item->tax_percent, self::TAX_RATE_PRECISION); - if (! array_key_exists($taxRate, $taxes)) { + if (!array_key_exists($taxRate, $taxes)) { $taxes[$taxRate] = 0; } $taxes[$taxRate] += $asBase ? $item->base_tax_amount : $item->tax_amount; } - // finally round tax amounts now (to reduce rounding differences) + /* finally round tax amounts now (to reduce rounding differences) */ foreach ($taxes as $taxRate => $taxAmount) { $taxes[$taxRate] = round($taxAmount, self::TAX_AMOUNT_PRECISION); } @@ -67,4 +82,69 @@ class Tax return $result; } -} \ No newline at end of file + + /** + * Get default address from core config. + * + * @return object + */ + public static function getDefaultAddress() + { + return new class() + { + public $country; + public $state; + public $postcode; + + function __construct() + { + $this->country = core()->getConfigData('taxes.catalogue.default-location-calculation.country') != '' + ? core()->getConfigData('taxes.catalogue.default-location-calculation.country') + : strtoupper(config('app.default_country')); + $this->state = core()->getConfigData('taxes.catalogue.default-location-calculation.state'); + $this->postcode = core()->getConfigData('taxes.catalogue.default-location-calculation.post_code'); + } + }; + } + + /** + * This method is check tax for the current address. If applicable then + * custom operation can be done. + * + * @param object $address + * @param object $taxCategory + * @param \Closure $operation + * @return void + */ + public static function isTaxApplicableInCurrentAddress($taxCategory, $address, $operation) + { + $taxRates = $taxCategory->tax_rates()->where([ + 'country' => $address->country, + ])->orderBy('tax_rate', 'desc')->get(); + + if ($taxRates->count()) { + foreach ($taxRates as $rate) { + $haveTaxRate = false; + + if ($rate->state != '' && $rate->state != $address->state) { + continue; + } + + if (!$rate->is_zip) { + if (empty($rate->zip_code) || in_array($rate->zip_code, ['*', $address->postcode])) { + $haveTaxRate = true; + } + } else { + if ($address->postcode >= $rate->zip_from && $address->postcode <= $rate->zip_to) { + $haveTaxRate = true; + } + } + + if ($haveTaxRate) { + $operation($rate); + break; + } + } + } + } +} diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/checkout/cart/mini-cart.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/checkout/cart/mini-cart.blade.php index ab641bd27..223c4c80c 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/checkout/cart/mini-cart.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/checkout/cart/mini-cart.blade.php @@ -1,6 +1,6 @@
@include ('shop::products.price', ['product' => $product]) - @if ((bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive')) + @if (Webkul\Tax\Helpers\Tax::isTaxInclusive()) {{ __('velocity::app.products.tax-inclusive') }}