Tax Inclusive Added For Velocity Theme

This commit is contained in:
devansh bawari 2021-05-05 16:53:53 +05:30
parent d69e5a41b0
commit 18a3e811d7
7 changed files with 212 additions and 119 deletions

View File

@ -8,13 +8,14 @@ use Illuminate\Support\Facades\Storage;
use Webkul\Product\Facades\ProductImage;
use Webkul\Product\Models\ProductAttributeValue;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Tax\Repositories\TaxCategoryRepository;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Product\Datatypes\CartItemValidationResult;
use Webkul\Product\Repositories\ProductImageRepository;
use Webkul\Product\Repositories\ProductVideoRepository;
use Webkul\Customer\Repositories\CustomerGroupRepository;
use Webkul\Inventory\Repositories\InventorySourceRepository;
use Webkul\Product\Repositories\ProductInventoryRepository;
use Webkul\Inventory\Repositories\InventorySourceRepository;
use Webkul\Product\Repositories\ProductAttributeValueRepository;
use Webkul\Product\Repositories\ProductCustomerGroupPriceRepository;
@ -63,76 +64,79 @@ abstract class AbstractType
protected $productVideoRepository;
/**
* Product model instance
* Product instance
*
* @var \Webkul\Product\Contracts\Product
*/
protected $product;
/**
* Is a composite product type
* Is a composite product type.
*
* @var bool
*/
protected $isComposite = false;
/**
* Is a stokable product type
* Is a stockable product type.
*
* @var bool
*/
protected $isStockable = true;
/**
* Show quantity box
* Show quantity box.
*
* @var bool
*/
protected $showQuantityBox = false;
/**
* Allow multiple qty.
*
* @var bool
*/
protected $allowMultipleQty = true;
/**
* Is product have sufficient quantity
* Is product have sufficient quantity.
*
* @var bool
*/
protected $haveSufficientQuantity = true;
/**
* Product can be moved from wishlist to cart or not
* Product can be moved from wishlist to cart or not.
*
* @var bool
*/
protected $canBeMovedFromWishlistToCart = true;
/**
* Products of this type can be copied in the admin backend?
* Products of this type can be copied in the admin backend.
*
* @var bool
*/
protected $canBeCopied = true;
/**
* Has child products aka variants
* Has child products aka variants.
*
* @var bool
*/
protected $hasVariants = false;
/**
* Product children price can be calculated or not
* Product children price can be calculated or not.
*
* @var bool
*/
protected $isChildrenCalculated = false;
/**
* product options
* product options.
*
* @var array
*/
protected $productOptions = [];
@ -153,12 +157,12 @@ abstract class AbstractType
/**
* Create a new product type instance.
*
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Product\Repositories\ProductAttributeValueRepository $attributeValueRepository
* @param \Webkul\Product\Repositories\ProductInventoryRepository $productInventoryRepository
* @param \Webkul\Product\Repositories\ProductImageRepository $productImageRepository
* @param \Webkul\Product\Repositories\ProductVideoRepository $productVideoRepository
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Product\Repositories\ProductAttributeValueRepository $attributeValueRepository
* @param \Webkul\Product\Repositories\ProductInventoryRepository $productInventoryRepository
* @param \Webkul\Product\Repositories\ProductImageRepository $productImageRepository
* @param \Webkul\Product\Repositories\ProductVideoRepository $productVideoRepository
*
* @return void
*/
@ -185,6 +189,8 @@ abstract class AbstractType
/**
* Is the administrator able to copy products of this type in the admin backend?
*
* @return bool
*/
public function canBeCopied(): bool
{
@ -192,8 +198,9 @@ abstract class AbstractType
}
/**
* @param array $data
* Create product.
*
* @param array $data
* @return \Webkul\Product\Contracts\Product
*/
public function create(array $data)
@ -202,10 +209,11 @@ abstract class AbstractType
}
/**
* @param array $data
* @param int $id
* @param string $attribute
* Update product.
*
* @param array $data
* @param int $id
* @param string $attribute
* @return \Webkul\Product\Contracts\Product
*/
public function update(array $data, $id, $attribute = "id")
@ -259,9 +267,11 @@ abstract class AbstractType
'locale' => $attribute->value_per_locale ? $data['locale'] : null,
]);
} else {
$this->attributeValueRepository->update([
ProductAttributeValue::$attributeTypeFields[$attribute->type] => $data[$attribute->code],
], $attributeValue->id
$this->attributeValueRepository->update(
[
ProductAttributeValue::$attributeTypeFields[$attribute->type] => $data[$attribute->code],
],
$attributeValue->id
);
if ($attribute->type == 'image' || $attribute->type == 'file') {
@ -273,7 +283,7 @@ abstract class AbstractType
$route = request()->route() ? request()->route()->getName() : "";
if ($route !== 'admin.catalog.products.massupdate') {
if (! isset($data['categories'])) {
if (!isset($data['categories'])) {
$data['categories'] = [];
}
@ -291,18 +301,19 @@ abstract class AbstractType
$this->productVideoRepository->uploadVideos($data, $product);
app(ProductCustomerGroupPriceRepository::class)->saveCustomerGroupPrices($data,
$product);
app(ProductCustomerGroupPriceRepository::class)->saveCustomerGroupPrices(
$data,
$product
);
}
return $product;
}
/**
* Specify type instance product
*
* @param \Webkul\Product\Contracts\Product $product
* Specify type instance product.
*
* @param \Webkul\Product\Contracts\Product $product
* @return \Webkul\Product\Type\AbstractType
*/
public function setProduct($product)
@ -313,7 +324,7 @@ abstract class AbstractType
}
/**
* Returns children ids
* Returns children ids.
*
* @return array
*/
@ -323,7 +334,7 @@ abstract class AbstractType
}
/**
* Check if catalog rule can be applied
* Check if catalog rule can be applied.
*
* @return bool
*/
@ -333,7 +344,7 @@ abstract class AbstractType
}
/**
* Return true if this product type is saleable
* Return true if this product type is saleable.
*
* @return bool
*/
@ -343,8 +354,10 @@ abstract class AbstractType
return false;
}
if (is_callable(config('products.isSaleable')) &&
call_user_func(config('products.isSaleable'), $this->product) === false) {
if (
is_callable(config('products.isSaleable')) &&
call_user_func(config('products.isSaleable'), $this->product) === false
) {
return false;
}
@ -352,7 +365,7 @@ abstract class AbstractType
}
/**
* Return true if this product can have inventory
* Return true if this product can have inventory.
*
* @return bool
*/
@ -362,7 +375,7 @@ abstract class AbstractType
}
/**
* Return true if this product can be composite
* Return true if this product can be composite.
*
* @return bool
*/
@ -372,7 +385,7 @@ abstract class AbstractType
}
/**
* Return true if this product can have variants
* Return true if this product can have variants.
*
* @return bool
*/
@ -382,7 +395,7 @@ abstract class AbstractType
}
/**
* Product children price can be calculated or not
* Product children price can be calculated or not.
*
* @return bool
*/
@ -392,8 +405,9 @@ abstract class AbstractType
}
/**
* @param int $qty
* Have sufficient quantity.
*
* @param int $qty
* @return bool
*/
public function haveSufficientQuantity(int $qty): bool
@ -402,7 +416,7 @@ abstract class AbstractType
}
/**
* Return true if this product can have inventory
* Return true if this product can have inventory.
*
* @return bool
*/
@ -412,7 +426,7 @@ abstract class AbstractType
}
/**
* Return true if more than one qty can be added to cart
* Return true if more than one qty can be added to cart.
*
* @return bool
*/
@ -422,8 +436,9 @@ abstract class AbstractType
}
/**
* @param \Webkul\Checkout\Contracts\CartItem $cartItem
* Is item have quantity.
*
* @param \Webkul\Checkout\Contracts\CartItem $cartItem
* @return bool
*/
public function isItemHaveQuantity($cartItem)
@ -432,6 +447,8 @@ abstract class AbstractType
}
/**
* Total quantity.
*
* @return int
*/
public function totalQuantity()
@ -459,10 +476,9 @@ abstract class AbstractType
}
/**
* Return true if item can be moved to cart from wishlist
*
* @param \Webkul\Checkout\Contracts\CartItem $item
* Return true if item can be moved to cart from wishlist.
*
* @param \Webkul\Checkout\Contracts\CartItem $item
* @return bool
*/
public function canBeMovedFromWishlistToCart($item)
@ -471,30 +487,33 @@ abstract class AbstractType
}
/**
* Retrieve product attributes
*
* @param \Webkul\Attribute\Contracts\Group $group
* @param bool $skipSuperAttribute
* Retrieve product attributes.
*
* @param \Webkul\Attribute\Contracts\Group $group
* @param bool $skipSuperAttribute
* @return \Illuminate\Support\Collection
*/
public function getEditableAttributes($group = null, $skipSuperAttribute = true)
{
if ($skipSuperAttribute) {
$this->skipAttributes = array_merge($this->product->super_attributes->pluck('code')->toArray(),
$this->skipAttributes);
$this->skipAttributes = array_merge(
$this->product->super_attributes->pluck('code')->toArray(),
$this->skipAttributes
);
}
if (!$group) {
return $this->product->attribute_family->custom_attributes()->whereNotIn('attributes.code',
$this->skipAttributes)->get();
return $this->product->attribute_family->custom_attributes()->whereNotIn(
'attributes.code',
$this->skipAttributes
)->get();
}
return $group->custom_attributes()->whereNotIn('code', $this->skipAttributes)->get();
}
/**
* Returns additional views
* Returns additional views.
*
* @return array
*/
@ -504,7 +523,7 @@ abstract class AbstractType
}
/**
* Returns validation rules
* Returns validation rules.
*
* @return array
*/
@ -514,10 +533,9 @@ abstract class AbstractType
}
/**
* Get product minimal price
*
* @param int $qty
* Get product minimal price.
*
* @param int $qty
* @return float
*/
public function getMinimalPrice($qty = null)
@ -530,7 +548,7 @@ abstract class AbstractType
}
/**
* Get product maximam price
* Get product maximum price.
*
* @return float
*/
@ -540,10 +558,9 @@ abstract class AbstractType
}
/**
* Get product minimal price
*
* @param int $qty
* Get product minimal price.
*
* @param int $qty
* @return float
*/
public function getFinalPrice($qty = null)
@ -552,10 +569,9 @@ abstract class AbstractType
}
/**
* Returns the product's minimal price
*
* @param int $qty
* Returns the product's minimal price.
*
* @param int $qty
* @return float
*/
public function getSpecialPrice($qty = null)
@ -564,8 +580,9 @@ abstract class AbstractType
}
/**
* @param int $qty
* Have special price.
*
* @param int $qty
* @return bool
*/
public function haveSpecialPrice($qty = null)
@ -576,8 +593,8 @@ abstract class AbstractType
$specialPrice = $this->product->special_price;
if ((is_null($specialPrice) || ! (float) $specialPrice)
&& ! $rulePrice
if ((is_null($specialPrice) || !(float) $specialPrice)
&& !$rulePrice
&& $customerGroupPrice == $this->product->price
) {
return false;
@ -585,7 +602,7 @@ abstract class AbstractType
$haveSpecialPrice = false;
if (! (float) $specialPrice) {
if (!(float) $specialPrice) {
if ($rulePrice && $rulePrice->price < $this->product->price) {
$this->product->special_price = $rulePrice->price;
@ -597,8 +614,10 @@ abstract class AbstractType
$haveSpecialPrice = true;
} else {
if (core()->isChannelDateInInterval($this->product->special_price_from,
$this->product->special_price_to)) {
if (core()->isChannelDateInInterval(
$this->product->special_price_from,
$this->product->special_price_to
)) {
$haveSpecialPrice = true;
} elseif ($rulePrice) {
$this->product->special_price = $rulePrice->price;
@ -621,7 +640,7 @@ abstract class AbstractType
}
/**
* Get product group price
* Get product group price.
*
* @return float
*/
@ -668,7 +687,8 @@ abstract class AbstractType
continue;
}
if ($price->qty == $lastQty
if (
$price->qty == $lastQty
&& $lastCustomerGroupId != null
&& $price->customer_group_id == null
) {
@ -698,7 +718,7 @@ abstract class AbstractType
}
/**
* Returns product prices
* Returns product prices.
*
* @return array
*/
@ -717,7 +737,7 @@ abstract class AbstractType
}
/**
* Get product minimal price
* Get product minimal price.
*
* @return string
*/
@ -725,19 +745,83 @@ abstract class AbstractType
{
if ($this->haveSpecialPrice()) {
$html = '<div class="sticker sale">' . trans('shop::app.products.sale') . '</div>'
. '<span class="regular-price">' . core()->currency($this->product->price) . '</span>'
. '<span class="special-price">' . core()->currency($this->getSpecialPrice()) . '</span>';
. '<span class="regular-price">' . core()->currency($this->getTaxInclusiveRate($this->product->price)) . '</span>'
. '<span class="special-price">' . core()->currency($this->getTaxInclusiveRate($this->getSpecialPrice())) . '</span>';
} else {
$html = '<span>' . core()->currency($this->product->price) . '</span>';
$html = '<span>' . core()->currency($this->getTaxInclusiveRate($this->product->price)) . '</span>';
}
return $html;
}
/**
* Get inclusive tax rates.
*
* @param float $totalPrice
* @return array
*/
public function getTaxInclusiveRate($totalPrice)
{
/* this is added for future purpose like if shipping tax also added then case is needed */
$address = null;
$taxCategory = app(TaxCategoryRepository::class)->find($this->product->tax_category_id);
if ($taxCategory) {
if ($address === null && auth()->guard('customer')->check()) {
$address = auth()->guard('customer')->user()->addresses()->where('default_address', 1)->first();
}
if ($address === null) {
$address = new class()
{
public $country;
public $state;
public $postcode;
function __construct()
{
$this->country = strtoupper(config('app.default_country'));
}
};
}
$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);
}
}
}
}
return $totalPrice;
}
/**
* Add product. Returns error message if can't prepare product.
*
* @param array $data
* @param array $data
*
* @return array
*/
@ -747,7 +831,7 @@ abstract class AbstractType
$data = $this->getQtyRequest($data);
if (! $this->haveSufficientQuantity($data['quantity'])) {
if (!$this->haveSufficientQuantity($data['quantity'])) {
return trans('shop::app.checkout.cart.quantity.inventory_warning');
}
@ -775,10 +859,9 @@ abstract class AbstractType
}
/**
* Get request quantity
*
* @param array $data
* Get request quantity.
*
* @param array $data
* @return array
*/
public function getQtyRequest($data)
@ -791,10 +874,10 @@ abstract class AbstractType
}
/**
* Compare options.
*
* @param array $options1
* @param array $options2
*
* @param array $options1
* @param array $options2
* @return bool
*/
public function compareOptions($options1, $options2)
@ -819,10 +902,9 @@ abstract class AbstractType
}
/**
* Returns additional information for items
*
* @param array $data
* Returns additional information for items.
*
* @param array $data
* @return array
*/
public function getAdditionalOptions($data)
@ -831,10 +913,9 @@ abstract class AbstractType
}
/**
* Get actual ordered item
*
* @param \Webkul\Checkout\Contracts\CartItem $item
* Get actual ordered item.
*
* @param \Webkul\Checkout\Contracts\CartItem $item
* @return \Webkul\Checkout\Contracts\CartItem|\Webkul\Sales\Contracts\OrderItem|\Webkul\Sales\Contracts\InvoiceItem|\Webkul\Sales\Contracts\ShipmentItem|\Webkul\Customer\Contracts\Wishlist
*/
public function getOrderedItem($item)
@ -843,10 +924,9 @@ abstract class AbstractType
}
/**
* Get product base image
*
* @param \Webkul\Customer\Contracts\CartItem|\Webkul\Checkout\Contracts\CartItem $item
* Get product base image.
*
* @param \Webkul\Customer\Contracts\CartItem|\Webkul\Checkout\Contracts\CartItem $item
* @return array
*/
public function getBaseImage($item)
@ -855,10 +935,9 @@ abstract class AbstractType
}
/**
* Validate cart item product price and other things
*
* @param \Webkul\Checkout\Models\CartItem $item
* Validate cart item product price and other things.
*
* @param \Webkul\Checkout\Models\CartItem $item
* @return \Webkul\Product\Datatypes\CartItemValidationResult
*/
public function validateCartItem(CartItem $item): CartItemValidationResult
@ -888,17 +967,20 @@ abstract class AbstractType
return $result;
}
//get product options
/**
* Get product options.
*
* @return array
*/
public function getProductOptions()
{
return $this->productOptions;
}
/**
* Returns true, if cart item is inactive
*
* @param \Webkul\Checkout\Contracts\CartItem $item
* Returns true, if cart item is inactive.
*
* @param \Webkul\Checkout\Contracts\CartItem $item
* @return bool
*/
public function isCartItemInactive(\Webkul\Checkout\Contracts\CartItem $item): bool
@ -931,7 +1013,8 @@ abstract class AbstractType
*
* @return array
*/
public function getCustomerGroupPricingOffers() {
public function getCustomerGroupPricingOffers()
{
$offerLines = [];
$haveOffers = true;
$customerGroupId = null;
@ -946,10 +1029,11 @@ abstract class AbstractType
}
}
$customerGroupPrices = $this->product->customer_group_prices()->where(function ($query) use ($customerGroupId) {
$query->where('customer_group_id', $customerGroupId)
->orWhereNull('customer_group_id');
}
$customerGroupPrices = $this->product->customer_group_prices()->where(
function ($query) use ($customerGroupId) {
$query->where('customer_group_id', $customerGroupId)
->orWhereNull('customer_group_id');
}
)->groupBy('qty')->get()->sortBy('qty')->values()->all();
if ($this->haveSpecialPrice()) {
@ -980,17 +1064,20 @@ abstract class AbstractType
/**
* Get offers lines.
*
* @param array $customerGroupPrice
* @param array $customerGroupPrice
*
* @return array
*/
public function getOfferLines($customerGroupPrice) {
public function getOfferLines($customerGroupPrice)
{
$price = $this->getCustomerGroupPrice($this->product, $customerGroupPrice->qty);
$discount = number_format((($this->product->price - $price) * 100) / ($this->product->price), 2);
$offerLines = trans('shop::app.products.offers', ['qty' => $customerGroupPrice->qty,
'price' => core()->currency($price), 'discount' => $discount]);
$offerLines = trans('shop::app.products.offers', [
'qty' => $customerGroupPrice->qty,
'price' => core()->currency($price), 'discount' => $discount
]);
return $offerLines;
}
@ -1010,4 +1097,4 @@ abstract class AbstractType
return $loadedSaleableChecks[$product->id] = $callback($product);
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
{
"/js/velocity.js": "/js/velocity.js?id=f4dcc51b765ad0cf788d",
"/js/velocity.js": "/js/velocity.js?id=62226e927739251367f9",
"/css/velocity-admin.css": "/css/velocity-admin.css?id=4322502d80a0e4a0affd",
"/css/velocity.css": "/css/velocity.css?id=f4b5274a9b073352fac1"
"/css/velocity.css": "/css/velocity.css?id=888af38c032d3290b74a"
}

View File

@ -24,6 +24,7 @@ class CartController extends Controller
$cartItems = $items->toArray();
$cartDetails = [];
$cartDetails['base_grand_total'] = core()->currency($cart->base_grand_total);
$cartDetails['base_sub_total'] = core()->currency($cart->base_sub_total);
/* needed raw data for comparison */
@ -35,6 +36,7 @@ class CartController extends Controller
$cartItems[$index]['images'] = $images;
$cartItems[$index]['url_key'] = $item->product->url_key;
$cartItems[$index]['base_total'] = core()->currency($item->base_total);
$cartItems[$index]['base_total_with_tax'] = core()->currency($item->base_total + $item->tax_amount);
}
$response = [

View File

@ -31,7 +31,7 @@
<input type="text" disabled :value="item.quantity" class="ml5" />
</div>
<span class="card-total-price fw6">
{{ item.base_total }}
{{ item.base_total_with_tax }}
</span>
</div>
</div>
@ -44,7 +44,7 @@
{{ subtotalText }}
</h5>
<h5 class="col-6 text-right fw6 no-padding">{{ cartInformation.base_sub_total }}</h5>
<h5 class="col-6 text-right fw6 no-padding">{{ cartInformation.base_grand_total }}</h5>
</div>
<div class="modal-footer">

View File

@ -125,6 +125,10 @@
<div class="col-12 price">
@include ('shop::products.price', ['product' => $product])
<span>
Inclusive of all taxes.
</span>
</div>
@if (count($product->getTypeInstance()->getCustomerGroupPricingOffers()) > 0)