Finishing And Refactoring Dog Done

This commit is contained in:
devansh bawari 2021-05-06 15:45:56 +05:30
parent daa829b0b8
commit e01b991281
9 changed files with 122 additions and 123 deletions

View File

@ -665,45 +665,13 @@ 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;
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) {
Tax::isTaxApplicableInCurrentAddress($taxCategory, $address, function ($rate) use ($cart, $item) {
/* assigning tax percent */
$item->tax_percent = $rate->tax_rate;
/* getting shipping rate for tax calculation */
@ -719,11 +687,7 @@ class Cart
/* 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;
}
}
}
});
$item->save();
}

View File

@ -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 = '<div class="sticker sale">' . trans('shop::app.products.sale') . '</div>'
. '<span class="regular-price">' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . '</span>'
. '<span class="special-price">' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getSpecialPrice()) : $this->getSpecialPrice()) . '</span>';
. '<span class="regular-price">' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . '</span>'
. '<span class="special-price">' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getSpecialPrice()) : $this->getSpecialPrice()) . '</span>';
} else {
$html = '<span>' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . '</span>';
$html = '<span>' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->product->price) : $this->product->price) . '</span>';
}
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) {
Tax::isTaxApplicableInCurrentAddress($taxCategory, $address, function ($rate) use (&$totalPrice) {
$totalPrice = round($totalPrice, 4) + round(($totalPrice * $rate->tax_rate) / 100, 4);
}
}
}
});
}
return $totalPrice;

View File

@ -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()),
]
]
];

View File

@ -452,12 +452,12 @@ class Configurable extends AbstractType
if ($this->haveOffer()) {
return '<div class="sticker sale">' . trans('shop::app.products.sale') . '</div>'
. '<span class="price-label">' . trans('shop::app.products.price-label') . '</span>'
. '<span class="regular-price">' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . '</span>'
. '<span class="final-price">' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getOfferPrice()) : $this->getOfferPrice()) . '</span>';
. '<span class="regular-price">' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . '</span>'
. '<span class="final-price">' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getOfferPrice()) : $this->getOfferPrice()) . '</span>';
} else {
return '<span class="price-label">' . trans('shop::app.products.price-label') . '</span>'
. ' '
. '<span class="final-price">' . core()->currency($this->isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . '</span>';
. '<span class="final-price">' . core()->currency(Tax::isTaxInclusive() ? $this->getTaxInclusiveRate($this->getMinimalPrice()) : $this->getMinimalPrice()) . '</span>';
}
}

View File

@ -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())
<b>{{ core()->currency($cart->base_grand_total) }}</b>
@else
<b>{{ core()->currency($cart->base_sub_total) }}</b>
@ -72,7 +72,7 @@
{!! view_render_event('bagisto.shop.checkout.cart-mini.item.price.before', ['item' => $item]) !!}
<div class="item-price">
@if ((bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive'))
@if (Webkul\Tax\Helpers\Tax::isTaxInclusive())
<b>{{ core()->currency($item->base_total + $item->tax_amount) }}</b>
@else
<b>{{ core()->currency($item->base_total) }}</b>

View File

@ -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())
<div>
{{ __('shop::app.products.tax-inclusive') }}
</div>

View File

@ -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.
*
@ -39,7 +54,7 @@ class Tax
$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;
}
/**
* 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;
}
}
}
}
}

View File

@ -1,6 +1,6 @@
<div class="mini-cart-container">
<mini-cart
is-tax-inclusive="{{ (bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive') }}"
is-tax-inclusive="{{ Webkul\Tax\Helpers\Tax::isTaxInclusive() }}"
view-cart="{{ route('shop.checkout.cart.index') }}"
cart-text="{{ __('shop::app.minicart.view-cart') }}"
checkout-text="{{ __('shop::app.minicart.checkout') }}"

View File

@ -126,7 +126,7 @@
<div class="col-12 price">
@include ('shop::products.price', ['product' => $product])
@if ((bool) core()->getConfigData('taxes.catalogue.pricing.tax_inclusive'))
@if (Webkul\Tax\Helpers\Tax::isTaxInclusive())
<span>
{{ __('velocity::app.products.tax-inclusive') }}
</span>