tax helper introduced, views changed to display multiple tax rates

This commit is contained in:
Steffen Mahler 2020-01-31 16:08:01 +01:00
parent 8b196c9c9c
commit 8abce3302a
10 changed files with 297 additions and 188 deletions

View File

@ -104,7 +104,7 @@ return [
|
*/
'default_country' => null,
'default_country' => 'DE',
/*
|--------------------------------------------------------------------------

View File

@ -266,7 +266,7 @@
@if (isset($item->additional['attributes']))
<div class="item-options">
@foreach ($item->additional['attributes'] as $attribute)
<b>{{ $attribute['attribute_name'] }} : </b>{{ $attribute['option_label'] }}</br>
@endforeach
@ -344,11 +344,14 @@
</tr>
@endif
<tr class="border">
<td>{{ __('admin::app.sales.orders.tax') }}</td>
@php ($taxRates = Webkul\Checkout\Helpers\Tax::getTaxRatesWithAmount($order, true))
@foreach ($taxRates as $taxRate => $baseTaxAmount)
<tr {{ $loop->last ? 'class=border' : ''}}>
<td id="taxrate-{{ $taxRate }}">{{ __('admin::app.sales.orders.tax') }} {{ $taxRate }} %</td>
<td>-</td>
<td>{{ core()->formatBasePrice($order->base_tax_amount) }}</td>
<td id="taxamount-{{ $taxRate }}">{{ core()->formatBasePrice($baseTaxAmount) }}</td>
</tr>
@endforeach
<tr class="bold">
<td>{{ __('admin::app.sales.orders.grand-total') }}</td>

View File

@ -5,6 +5,7 @@ namespace Webkul\Checkout;
use Webkul\Checkout\Repositories\CartRepository;
use Webkul\Checkout\Repositories\CartItemRepository;
use Webkul\Checkout\Repositories\CartAddressRepository;
use Webkul\Customer\Models\CustomerAddress;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Tax\Repositories\TaxCategoryRepository;
use Webkul\Checkout\Models\CartItem;
@ -14,6 +15,17 @@ use Webkul\Customer\Repositories\CustomerAddressRepository;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Arr;
class addressHelper
{
public $country;
public $postcode;
function __construct()
{
$this->country = config('app.default_country');
}
}
/**
* Facades handler for all the methods to be implemented in Cart.
*
@ -21,7 +33,8 @@ use Illuminate\Support\Arr;
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class Cart {
class Cart
{
/**
* CartRepository instance
@ -75,15 +88,16 @@ class Cart {
/**
* Create a new controller instance.
*
* @param Webkul\Checkout\Repositories\CartRepository $cart
* @param Webkul\Checkout\Repositories\CartItemRepository $cartItem
* @param Webkul\Checkout\Repositories\CartAddressRepository $cartAddress
* @param Webkul\Product\Repositories\ProductRepository $product
* @param Webkul\Product\Repositories\TaxCategoryRepository $taxCategory
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
* @param Webkul\Discount\Repositories\CartRuleRepository $cartRule
* @param Webkul\Helpers\Discount $discount
* @param Webkul\Checkout\Repositories\CartRepository $cart
* @param Webkul\Checkout\Repositories\CartItemRepository $cartItem
* @param Webkul\Checkout\Repositories\CartAddressRepository $cartAddress
* @param Webkul\Product\Repositories\ProductRepository $product
* @param Webkul\Product\Repositories\TaxCategoryRepository $taxCategory
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
* @param Webkul\Discount\Repositories\CartRuleRepository $cartRule
* @param Webkul\Helpers\Discount $discount
*
* @return void
*/
public function __construct(
@ -94,8 +108,7 @@ class Cart {
TaxCategoryRepository $taxCategoryRepository,
WishlistRepository $wishlistRepository,
CustomerAddressRepository $customerAddressRepository
)
{
) {
$this->cartRepository = $cartRepository;
$this->cartItemRepository = $cartItemRepository;
@ -128,6 +141,7 @@ class Cart {
*
* @param integer $productId
* @param array $data
*
* @return Cart
*/
public function addProduct($productId, $data)
@ -136,8 +150,9 @@ class Cart {
$cart = $this->getCart();
if (! $cart && ! $cart = $this->create($data))
if (!$cart && !$cart = $this->create($data)) {
return;
}
$product = $this->productRepository->findOneByField('id', $productId);
@ -146,7 +161,7 @@ class Cart {
if (is_string($cartProducts)) {
$this->collectTotals();
if (! count($cart->all_items) > 0) {
if (!count($cart->all_items) > 0) {
session()->forget('cart');
}
@ -157,21 +172,24 @@ class Cart {
foreach ($cartProducts as $cartProduct) {
$cartItem = $this->getItemByProduct($cartProduct);
if (isset($cartProduct['parent_id']))
if (isset($cartProduct['parent_id'])) {
$cartProduct['parent_id'] = $parentCartItem->id;
}
if (! $cartItem) {
if (!$cartItem) {
$cartItem = $this->cartItemRepository->create(array_merge($cartProduct, ['cart_id' => $cart->id]));
} else {
if (isset($cartProduct['parent_id']) && $cartItem->parent_id != $parentCartItem->id) {
$cartItem = $this->cartItemRepository->create(array_merge($cartProduct, ['cart_id' => $cart->id]));
$cartItem = $this->cartItemRepository->create(array_merge($cartProduct,
['cart_id' => $cart->id]));
} else {
$cartItem = $this->cartItemRepository->update($cartProduct, $cartItem->id);
}
}
if (! $parentCartItem)
if (!$parentCartItem) {
$parentCartItem = $cartItem;
}
}
}
@ -186,17 +204,18 @@ class Cart {
* Create new cart instance.
*
* @param array $data
*
* @return Cart|null
*/
public function create($data)
{
$cartData = [
'channel_id' => core()->getCurrentChannel()->id,
'global_currency_code' => core()->getBaseCurrencyCode(),
'base_currency_code' => core()->getBaseCurrencyCode(),
'channel_id' => core()->getCurrentChannel()->id,
'global_currency_code' => core()->getBaseCurrencyCode(),
'base_currency_code' => core()->getBaseCurrencyCode(),
'channel_currency_code' => core()->getChannelBaseCurrencyCode(),
'cart_currency_code' => core()->getCurrentCurrencyCode(),
'items_count' => 1
'cart_currency_code' => core()->getCurrentCurrencyCode(),
'items_count' => 1,
];
//Authentication details
@ -212,7 +231,7 @@ class Cart {
$cart = $this->cartRepository->create($cartData);
if (! $cart) {
if (!$cart) {
session()->flash('error', trans('shop::app.checkout.cart.create-error'));
return;
@ -235,8 +254,9 @@ class Cart {
foreach ($data['qty'] as $itemId => $quantity) {
$item = $this->cartItemRepository->findOneByField('id', $itemId);
if (! $item)
if (!$item) {
continue;
}
if ($quantity <= 0) {
$this->removeItem($itemId);
@ -246,18 +266,19 @@ class Cart {
$item->quantity = $quantity;
if (! $this->isItemHaveQuantity($item))
if (!$this->isItemHaveQuantity($item)) {
throw new \Exception(trans('shop::app.checkout.cart.quantity.inventory_warning'));
}
Event::dispatch('checkout.cart.update.before', $item);
$this->cartItemRepository->update([
'quantity' => $quantity,
'total' => core()->convertPrice($item->price * $quantity),
'base_total' => $item->price * $quantity,
'total_weight' => $item->weight * $quantity,
'base_total_weight' => $item->weight * $quantity
], $itemId);
'quantity' => $quantity,
'total' => core()->convertPrice($item->price * $quantity),
'base_total' => $item->price * $quantity,
'total_weight' => $item->weight * $quantity,
'base_total_weight' => $item->weight * $quantity,
], $itemId);
Event::dispatch('checkout.cart.update.after', $item);
}
@ -271,6 +292,7 @@ class Cart {
* Get cart item by product
*
* @param array $data
*
* @return CartItem|void
*/
public function getItemByProduct($data)
@ -280,8 +302,10 @@ class Cart {
foreach ($items as $item) {
if ($item->product->getTypeInstance()->compareOptions($item->additional, $data['additional'])) {
if (isset($data['additional']['parent_id'])) {
if ($item->parent->product->getTypeInstance()->compareOptions($item->parent->additional, request()->all()))
if ($item->parent->product->getTypeInstance()->compareOptions($item->parent->additional,
request()->all())) {
return $item;
}
} else {
return $item;
}
@ -293,14 +317,16 @@ class Cart {
* Remove the item from the cart
*
* @param integer $itemId
*
* @return boolean
*/
public function removeItem($itemId)
{
Event::dispatch('checkout.cart.delete.before', $itemId);
if (! $cart = $this->getCart())
if (!$cart = $this->getCart()) {
return false;
}
$this->cartItemRepository->delete($itemId);
@ -328,18 +354,21 @@ class Cart {
public function mergeCart()
{
if (session()->has('cart')) {
$cart = $this->cartRepository->findOneWhere(['customer_id' => $this->getCurrentCustomer()->user()->id, 'is_active' => 1]);
$cart = $this->cartRepository->findOneWhere([
'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_active' => 1,
]);
$guestCart = session()->get('cart');
//when the logged in customer is not having any of the cart instance previously and are active.
if (! $cart) {
if (!$cart) {
$this->cartRepository->update([
'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_guest' => 0,
'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_guest' => 0,
'customer_first_name' => $this->getCurrentCustomer()->user()->first_name,
'customer_last_name' => $this->getCurrentCustomer()->user()->last_name,
'customer_email' => $this->getCurrentCustomer()->user()->email
'customer_last_name' => $this->getCurrentCustomer()->user()->last_name,
'customer_email' => $this->getCurrentCustomer()->user()->email,
], $guestCart->id);
session()->forget('cart');
@ -352,23 +381,25 @@ class Cart {
$found = false;
foreach ($cart->items as $cartItem) {
if (! $cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $guestCartItem->additional))
if (!$cartItem->product->getTypeInstance()->compareOptions($cartItem->additional,
$guestCartItem->additional)) {
continue;
}
$cartItem->quantity = $newQuantity = $cartItem->quantity + $guestCartItem->quantity;
if (! $this->isItemHaveQuantity($cartItem)) {
if (!$this->isItemHaveQuantity($cartItem)) {
$this->cartItemRepository->delete($guestCartItem->id);
continue;
}
$this->cartItemRepository->update([
'quantity' => $newQuantity,
'total' => core()->convertPrice($cartItem->price * $newQuantity),
'base_total' => $cartItem->price * $newQuantity,
'total_weight' => $cartItem->weight * $newQuantity,
'base_total_weight' => $cartItem->weight * $newQuantity
'quantity' => $newQuantity,
'total' => core()->convertPrice($cartItem->price * $newQuantity),
'base_total' => $cartItem->price * $newQuantity,
'total_weight' => $cartItem->weight * $newQuantity,
'base_total_weight' => $cartItem->weight * $newQuantity,
], $cartItem->id);
$guestCart->items->forget($key);
@ -378,14 +409,14 @@ class Cart {
$found = true;
}
if (! $found) {
if (!$found) {
$this->cartItemRepository->update([
'cart_id' => $cart->id
'cart_id' => $cart->id,
], $guestCartItem->id);
foreach ($guestCartItem->children as $child) {
$this->cartItemRepository->update([
'cart_id' => $cart->id
'cart_id' => $cart->id,
], $child->id);
}
}
@ -405,11 +436,12 @@ class Cart {
* Save cart
*
* @param Cart $cart
*
* @return void
*/
public function putCart($cart)
{
if (! $this->getCurrentCustomer()->check()) {
if (!$this->getCurrentCustomer()->check()) {
session()->put('cart', $cart);
}
}
@ -426,7 +458,7 @@ class Cart {
if ($this->getCurrentCustomer()->check()) {
$cart = $this->cartRepository->findOneWhere([
'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_active' => 1
'is_active' => 1,
]);
} elseif (session()->has('cart')) {
$cart = $this->cartRepository->find(session()->get('cart')->id);
@ -465,18 +497,20 @@ class Cart {
* Save customer address
*
* @param array $data
*
* @return boolean
*/
public function saveCustomerAddress($data)
{
if (! $cart = $this->getCart())
if (!$cart = $this->getCart()) {
return false;
}
$billingAddress = $data['billing'];
$billingAddress['cart_id'] = $cart->id;
if (isset($data['billing']['address_id']) && $data['billing']['address_id']) {
$address = $this->customerAddressRepository->findOneWhere(['id'=> $data['billing']['address_id']])->toArray();
$address = $this->customerAddressRepository->findOneWhere(['id' => $data['billing']['address_id']])->toArray();
$billingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
$billingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
@ -490,7 +524,7 @@ class Cart {
}
if (isset($data['billing']['save_as_address']) && $data['billing']['save_as_address']) {
$billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
$billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
$this->customerAddressRepository->create($billingAddress);
}
@ -499,7 +533,7 @@ class Cart {
$shippingAddress['cart_id'] = $cart->id;
if (isset($data['shipping']['address_id']) && $data['shipping']['address_id']) {
$address = $this->customerAddressRepository->findOneWhere(['id'=> $data['shipping']['address_id']])->toArray();
$address = $this->customerAddressRepository->findOneWhere(['id' => $data['shipping']['address_id']])->toArray();
$shippingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
$shippingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
@ -513,7 +547,7 @@ class Cart {
}
if (isset($data['shipping']['save_as_address']) && $data['shipping']['save_as_address']) {
$shippingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
$shippingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
$this->customerAddressRepository->create($shippingAddress);
}
@ -531,9 +565,11 @@ class Cart {
}
} else {
if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
$this->cartAddressRepository->create(array_merge($billingAddress, ['address_type' => 'shipping']));
$this->cartAddressRepository->create(array_merge($billingAddress,
['address_type' => 'shipping']));
} else {
$this->cartAddressRepository->create(array_merge($shippingAddress, ['address_type' => 'shipping']));
$this->cartAddressRepository->create(array_merge($shippingAddress,
['address_type' => 'shipping']));
}
}
}
@ -568,12 +604,14 @@ class Cart {
* Save shipping method for cart
*
* @param string $shippingMethodCode
*
* @return boolean
*/
public function saveShippingMethod($shippingMethodCode)
{
if (! $cart = $this->getCart())
if (!$cart = $this->getCart()) {
return false;
}
$cart->shipping_method = $shippingMethodCode;
$cart->save();
@ -585,15 +623,18 @@ class Cart {
* Save payment method for cart
*
* @param string $payment
*
* @return CartPayment
*/
public function savePaymentMethod($payment)
{
if (! $cart = $this->getCart())
if (!$cart = $this->getCart()) {
return false;
}
if ($cartPayment = $cart->payment)
if ($cartPayment = $cart->payment) {
$cartPayment->delete();
}
$cartPayment = new CartPayment;
@ -613,11 +654,13 @@ class Cart {
{
$validated = $this->validateItems();
if (! $validated)
if (!$validated) {
return false;
}
if (! $cart = $this->getCart())
if (!$cart = $this->getCart()) {
return false;
}
Event::dispatch('checkout.cart.collect.totals.before', $cart);
@ -632,19 +675,19 @@ class Cart {
$cart->discount_amount += $item->discount_amount;
$cart->base_discount_amount += $item->base_discount_amount;
$cart->grand_total = (float) $cart->grand_total + $item->total + $item->tax_amount - $item->discount_amount;
$cart->base_grand_total = (float) $cart->base_grand_total + $item->base_total + $item->base_tax_amount - $item->base_discount_amount;
$cart->grand_total = (float)$cart->grand_total + $item->total + $item->tax_amount - $item->discount_amount;
$cart->base_grand_total = (float)$cart->base_grand_total + $item->base_total + $item->base_tax_amount - $item->base_discount_amount;
$cart->sub_total = (float) $cart->sub_total + $item->total;
$cart->base_sub_total = (float) $cart->base_sub_total + $item->base_total;
$cart->sub_total = (float)$cart->sub_total + $item->total;
$cart->base_sub_total = (float)$cart->base_sub_total + $item->base_total;
$cart->tax_total = (float) $cart->tax_total + $item->tax_amount;
$cart->base_tax_total = (float) $cart->base_tax_total + $item->base_tax_amount;
$cart->tax_total = (float)$cart->tax_total + $item->tax_amount;
$cart->base_tax_total = (float)$cart->base_tax_total + $item->base_tax_amount;
}
if ($shipping = $cart->selected_shipping_rate) {
$cart->grand_total = (float) $cart->grand_total + $shipping->price - $shipping->discount_amount;
$cart->base_grand_total = (float) $cart->base_grand_total + $shipping->base_price - $shipping->base_discount_amount;
$cart->grand_total = (float)$cart->grand_total + $shipping->price - $shipping->discount_amount;
$cart->base_grand_total = (float)$cart->base_grand_total + $shipping->base_price - $shipping->base_discount_amount;
$cart->discount_amount += $shipping->discount_amount;
$cart->base_discount_amount += $shipping->base_discount_amount;
@ -673,8 +716,9 @@ class Cart {
*/
public function validateItems()
{
if (! $cart = $this->getCart())
if (!$cart = $this->getCart()) {
return;
}
//rare case of accident-->used when there are no items.
if (count($cart->items) == 0) {
@ -685,12 +729,12 @@ class Cart {
foreach ($cart->items as $item) {
$item->product->getTypeInstance()->validateCartItem($item);
$price = ! is_null($item->custom_price) ? $item->custom_price : $item->base_price;
$price = !is_null($item->custom_price) ? $item->custom_price : $item->base_price;
$this->cartItemRepository->update([
'price' => core()->convertPrice($price),
'price' => core()->convertPrice($price),
'base_price' => $price,
'total' => core()->convertPrice($price * $item->quantity),
'total' => core()->convertPrice($price * $item->quantity),
'base_total' => $price * $item->quantity,
], $item->id);
}
@ -706,17 +750,19 @@ class Cart {
*/
public function calculateItemsTax()
{
if (! $cart = $this->getCart())
if (!$cart = $this->getCart()) {
return false;
}
if (! $cart->shipping_address && ! $cart->billing_address)
return;
// if (! $cart->shipping_address && ! $cart->billing_address)
// return;
foreach ($cart->items()->get() as $item) {
$taxCategory = $this->taxCategoryRepository->find($item->product->tax_category_id);
if (! $taxCategory)
if (!$taxCategory) {
continue;
}
if ($item->product->getTypeInstance()->isStockable()) {
$address = $cart->shipping_address;
@ -724,25 +770,30 @@ class Cart {
$address = $cart->billing_address;
}
if ($address === null) {
$address = new addressHelper();
}
$taxRates = $taxCategory->tax_rates()->where([
'country' => $address->country,
])->orderBy('tax_rate', 'desc')->get();
'country' => $address->country,
])->orderBy('tax_rate', 'desc')->get();
if ($taxRates->count()) {
foreach ($taxRates as $rate) {
$haveTaxRate = false;
if ($rate->state != '' && $rate->state != $address->state)
if ($rate->state != '' && $rate->state != $address->state) {
continue;
if (! $rate->is_zip) {
if ($rate->zip_code == '*' || $rate->zip_code == $address->postcode)
$haveTaxRate = true;
} else {
if ($address->postcode >= $rate->zip_from && $address->postcode <= $rate->zip_to)
$haveTaxRate = true;
}
if (!$rate->is_zip) {
if ($rate->zip_code == '*' || $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;
$item->tax_amount = ($item->total * $rate->tax_rate) / 100;
@ -769,11 +820,13 @@ class Cart {
*/
public function hasError()
{
if (! $this->getCart())
if (!$this->getCart()) {
return true;
}
if (! $this->isItemsHaveSufficientQuantity())
if (!$this->isItemsHaveSufficientQuantity()) {
return true;
}
return false;
}
@ -786,8 +839,9 @@ class Cart {
public function isItemsHaveSufficientQuantity()
{
foreach ($this->getCart()->items as $item) {
if (! $this->isItemHaveQuantity($item))
if (!$this->isItemHaveQuantity($item)) {
return false;
}
}
return true;
@ -797,6 +851,7 @@ class Cart {
* Checks if all cart items have sufficient quantity.
*
* @param CartItem $item
*
* @return boolean
*/
public function isItemHaveQuantity($item)
@ -830,42 +885,42 @@ class Cart {
$data = $this->toArray();
$finalData = [
'cart_id' => $this->getCart()->id,
'customer_id' => $data['customer_id'],
'is_guest' => $data['is_guest'],
'customer_email' => $data['customer_email'],
'customer_first_name' => $data['customer_first_name'],
'customer_last_name' => $data['customer_last_name'],
'customer' => $this->getCurrentCustomer()->check() ? $this->getCurrentCustomer()->user() : null,
'total_item_count' => $data['items_count'],
'total_qty_ordered' => $data['items_qty'],
'base_currency_code' => $data['base_currency_code'],
'cart_id' => $this->getCart()->id,
'customer_id' => $data['customer_id'],
'is_guest' => $data['is_guest'],
'customer_email' => $data['customer_email'],
'customer_first_name' => $data['customer_first_name'],
'customer_last_name' => $data['customer_last_name'],
'customer' => $this->getCurrentCustomer()->check() ? $this->getCurrentCustomer()->user() : null,
'total_item_count' => $data['items_count'],
'total_qty_ordered' => $data['items_qty'],
'base_currency_code' => $data['base_currency_code'],
'channel_currency_code' => $data['channel_currency_code'],
'order_currency_code' => $data['cart_currency_code'],
'grand_total' => $data['grand_total'],
'base_grand_total' => $data['base_grand_total'],
'sub_total' => $data['sub_total'],
'base_sub_total' => $data['base_sub_total'],
'tax_amount' => $data['tax_total'],
'base_tax_amount' => $data['base_tax_total'],
'coupon_code' => $data['coupon_code'],
'order_currency_code' => $data['cart_currency_code'],
'grand_total' => $data['grand_total'],
'base_grand_total' => $data['base_grand_total'],
'sub_total' => $data['sub_total'],
'base_sub_total' => $data['base_sub_total'],
'tax_amount' => $data['tax_total'],
'base_tax_amount' => $data['base_tax_total'],
'coupon_code' => $data['coupon_code'],
'applied_cart_rule_ids' => $data['applied_cart_rule_ids'],
'discount_amount' => $data['discount_amount'],
'base_discount_amount' => $data['base_discount_amount'],
'billing_address' => Arr::except($data['billing_address'], ['id', 'cart_id']),
'payment' => Arr::except($data['payment'], ['id', 'cart_id']),
'channel' => core()->getCurrentChannel(),
'discount_amount' => $data['discount_amount'],
'base_discount_amount' => $data['base_discount_amount'],
'billing_address' => Arr::except($data['billing_address'], ['id', 'cart_id']),
'payment' => Arr::except($data['payment'], ['id', 'cart_id']),
'channel' => core()->getCurrentChannel(),
];
if ($this->getCart()->haveStockableItems()) {
$finalData = array_merge($finalData, [
'shipping_method' => $data['selected_shipping_rate']['method'],
'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'],
'shipping_description' => $data['selected_shipping_rate']['method_description'],
'shipping_amount' => $data['selected_shipping_rate']['price'],
'base_shipping_amount' => $data['selected_shipping_rate']['base_price'],
'shipping_address' => Arr::except($data['shipping_address'], ['id', 'cart_id']),
'shipping_discount_amount' => $data['selected_shipping_rate']['discount_amount'],
'shipping_method' => $data['selected_shipping_rate']['method'],
'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'],
'shipping_description' => $data['selected_shipping_rate']['method_description'],
'shipping_amount' => $data['selected_shipping_rate']['price'],
'base_shipping_amount' => $data['selected_shipping_rate']['base_price'],
'shipping_address' => Arr::except($data['shipping_address'], ['id', 'cart_id']),
'shipping_discount_amount' => $data['selected_shipping_rate']['discount_amount'],
'base_shipping_discount_amount' => $data['selected_shipping_rate']['base_discount_amount'],
]);
}
@ -881,29 +936,30 @@ class Cart {
* Prepares data for order item
*
* @param array $data
*
* @return array
*/
public function prepareDataForOrderItem($data)
{
$finalData = [
'product' => $this->productRepository->find($data['product_id']),
'sku' => $data['sku'],
'type' => $data['type'],
'name' => $data['name'],
'weight' => $data['weight'],
'total_weight' => $data['total_weight'],
'qty_ordered' => $data['quantity'],
'price' => $data['price'],
'base_price' => $data['base_price'],
'total' => $data['total'],
'base_total' => $data['base_total'],
'tax_percent' => $data['tax_percent'],
'tax_amount' => $data['tax_amount'],
'base_tax_amount' => $data['base_tax_amount'],
'discount_percent' => $data['discount_percent'],
'discount_amount' => $data['discount_amount'],
'product' => $this->productRepository->find($data['product_id']),
'sku' => $data['sku'],
'type' => $data['type'],
'name' => $data['name'],
'weight' => $data['weight'],
'total_weight' => $data['total_weight'],
'qty_ordered' => $data['quantity'],
'price' => $data['price'],
'base_price' => $data['base_price'],
'total' => $data['total'],
'base_total' => $data['base_total'],
'tax_percent' => $data['tax_percent'],
'tax_amount' => $data['tax_amount'],
'base_tax_amount' => $data['base_tax_amount'],
'discount_percent' => $data['discount_percent'],
'discount_amount' => $data['discount_amount'],
'base_discount_amount' => $data['base_discount_amount'],
'additional' => $data['additional'],
'additional' => $data['additional'],
];
if (isset($data['children']) && $data['children']) {
@ -921,15 +977,18 @@ class Cart {
* Move a wishlist item to cart
*
* @param WishlistItem $wishlistItem
*
* @return boolean
*/
public function moveToCart($wishlistItem)
{
if (! $wishlistItem->product->getTypeInstance()->canBeMovedFromWishlistToCart($wishlistItem))
if (!$wishlistItem->product->getTypeInstance()->canBeMovedFromWishlistToCart($wishlistItem)) {
return false;
}
if (! $wishlistItem->additional)
if (!$wishlistItem->additional) {
$wishlistItem->additional = ['product_id' => $wishlistItem->product_id];
}
request()->merge($wishlistItem->additional);
@ -948,6 +1007,7 @@ class Cart {
* Function to move a already added product to wishlist will run only on customer authentication.
*
* @param integer $itemId
*
* @return boolean|void
*/
public function moveToWishlist($itemId)
@ -956,34 +1016,38 @@ class Cart {
$cartItem = $cart->items()->find($itemId);
if (! $cartItem)
if (!$cartItem) {
return false;
}
$wishlistItems = $this->wishlistRepository->findWhere([
'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id
]);
'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id,
]);
$found = false;
foreach ($wishlistItems as $wishlistItem) {
if ($cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $wishlistItem->item_options))
if ($cartItem->product->getTypeInstance()->compareOptions($cartItem->additional,
$wishlistItem->item_options)) {
$found = true;
}
}
if (! $found) {
if (!$found) {
$this->wishlistRepository->create([
'channel_id' => $cart->channel_id,
'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id,
'additional' => $cartItem->additional
]);
'channel_id' => $cart->channel_id,
'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id,
'additional' => $cartItem->additional,
]);
}
$result = $this->cartItemRepository->delete($itemId);
if (! $cart->items()->count())
if (!$cart->items()->count()) {
$this->cartRepository->delete($cart->id);
}
$this->collectTotals();
@ -994,6 +1058,7 @@ class Cart {
* Set coupon code to the cart
*
* @param string $code
*
* @return Cart
*/
public function setCouponCode($code)

View File

@ -0,0 +1,31 @@
<?php
namespace Webkul\Checkout\Helpers;
class Tax
{
private const TAX_PRECISION = 4;
/**
* Returns an array with tax rates and tax amounts
* @param object $that
* @param bool $asBase
*
* @return array
*/
public static function getTaxRatesWithAmount(object $that, bool $asBase): array
{
$taxes = [];
foreach ($that->items as $item) {
$taxRate = (string)round((float)$item->tax_percent, self::TAX_PRECISION);
if (array_key_exists($taxRate, $taxes)) {
$taxes[$taxRate] += $asBase ? $item->base_tax_amount : $item->tax_amount;
} else {
$taxes += [$taxRate => $asBase ? $item->base_tax_amount : $item->tax_amount];
}
}
return $taxes;
}
}

View File

@ -18,10 +18,12 @@
@endif
@if ($cart->base_tax_total)
@foreach (Webkul\Checkout\Helpers\Tax::getTaxRatesWithAmount($cart, false) as $taxRate => $taxAmount )
<div class="item-detail">
<label>{{ __('shop::app.checkout.total.tax') }}</label>
<label class="right">{{ core()->currency($cart->base_tax_total) }}</label>
<label id="taxrate-{{ $taxRate }}">{{ __('shop::app.checkout.total.tax') }} {{ $taxRate }} %</label>
<label class="right" id="taxamount-{{ $taxRate }}">{{ core()->currency($taxAmount) }}</label>
</div>
@endforeach
@endif
<div class="item-detail" id="discount-detail" @if ($cart->base_discount_amount && $cart->base_discount_amount > 0) style="display: block;" @else style="display: none;" @endif>

View File

@ -123,7 +123,7 @@
@if (isset($item->additional['attributes']))
<div class="item-options">
@foreach ($item->additional['attributes'] as $attribute)
<b>{{ $attribute['attribute_name'] }} : </b>{{ $attribute['option_label'] }}</br>
@endforeach
@ -161,12 +161,14 @@
</span>
</div>
@foreach (Webkul\Checkout\Helpers\Tax::getTaxRatesWithAmount($order, true) as $taxRate => $baseTaxAmount )
<div>
<span>{{ __('shop::app.mail.order.tax') }}</span>
<span style="float: right;">
{{ core()->formatBasePrice($order->base_tax_amount) }}
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="basetaxamount-{{ $taxRate }}" style="float: right;">
{{ core()->formatBasePrice($baseTaxAmount) }}
</span>
</div>
@endforeach
@if ($order->discount_amount > 0)
<div>

View File

@ -115,10 +115,10 @@
<tr>
<td data-value="{{ __('shop::app.customer.account.order.view.product-name') }}" style="text-align: left;padding: 8px">
{{ $item->name }}
@if (isset($item->additional['attributes']))
<div class="item-options">
@foreach ($item->additional['attributes'] as $attribute)
<b>{{ $attribute['attribute_name'] }} : </b>{{ $attribute['option_label'] }}</br>
@endforeach
@ -146,7 +146,7 @@
{{ core()->formatPrice($invoice->sub_total, $invoice->order_currency_code) }}
</span>
</div>
@if ($order->shipping_address)
<div>
<span>{{ __('shop::app.mail.order.shipping-handling') }}</span>
@ -156,12 +156,14 @@
</div>
@endif
<div>
<span>{{ __('shop::app.mail.order.tax') }}</span>
<span style="float: right;">
{{ core()->formatPrice($invoice->tax_amount, $invoice->order_currency_code) }}
@foreach ($order->getTaxRatesWithAmount(false) as $taxRate => $taxAmount)
<div>
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ $taxRate }}" style="float: right;">
{{ core()->formatPrice($taxAmount, $order->order_currency_code) }}
</span>
</div>
</div>
@endforeach
@if ($invoice->discount_amount > 0)
<div>

View File

@ -116,10 +116,10 @@
<td data-value="{{ __('shop::app.customer.account.order.view.product-name') }}" style="text-align: left;padding: 8px">
{{ $item->name }}
@if (isset($item->additional['attributes']))
<div class="item-options">
@foreach ($item->additional['attributes'] as $attribute)
<b>{{ $attribute['attribute_name'] }} : </b>{{ $attribute['option_label'] }}</br>
@endforeach
@ -156,12 +156,14 @@
</div>
@endif
@foreach (Webkul\Checkout\Helpers\Tax::getTaxRatesWithAmount($order, false) as $taxRate => $taxAmount )
<div>
<span>{{ __('shop::app.mail.order.tax') }}</span>
<span style="float: right;">
{{ core()->formatPrice($order->tax_amount, $order->order_currency_code) }}
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ $taxRate }}" style="float: right;">
{{ core()->formatPrice($taxAmount, $order->order_currency_code) }}
</span>
</div>
@endforeach
@if ($order->discount_amount > 0)
<div>

View File

@ -115,10 +115,10 @@
<tr>
<td data-value="{{ __('shop::app.customer.account.order.view.product-name') }}" style="text-align: left;padding: 8px">
{{ $item->name }}
@if (isset($item->additional['attributes']))
<div class="item-options">
@foreach ($item->additional['attributes'] as $attribute)
<b>{{ $attribute['attribute_name'] }} : </b>{{ $attribute['option_label'] }}</br>
@endforeach
@ -158,7 +158,7 @@
</span>
</div>
@endif
@php(//ToDo: taxes)
@if ($refund->tax_amount > 0)
<div>
<span>{{ __('shop::app.mail.order.tax') }}</span>

View File

@ -119,7 +119,7 @@
@if (isset($item->additional['attributes']))
<div class="item-options">
@foreach ($item->additional['attributes'] as $attribute)
<b>{{ $attribute['attribute_name'] }} : </b>{{ $attribute['option_label'] }}</br>
@endforeach
@ -157,12 +157,14 @@
</span>
</div>
<div>
<span>{{ __('shop::app.mail.order.cancel.tax') }}</span>
<span style="float: right;">
{{ core()->formatPrice($order->tax_amount, $order->order_currency_code) }}
@foreach (Webkul\Checkout\Helpers\Tax::getTaxRatesWithAmount($order, false) as $taxRate => $taxAmount )
<div>
<span id="taxrate-{{ $taxRate }}">{{ __('shop::app.mail.order.cancel.tax') }} {{ $taxRate }} %</span>
<span id="taxamount-{{ $taxRate }}" style="float: right;">
{{ core()->formatPrice($taxAmount, $order->order_currency_code) }}
</span>
</div>
</div>
@endforeach
@if ($order->discount_amount > 0)
<div>