From 1596c985b86490acc25d21002983753c668b7c31 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Mon, 29 Aug 2022 18:23:01 +0800 Subject: [PATCH] fixed tax --- beike/Models/Cart.php | 16 ++++++++++++++++ beike/Repositories/CartRepo.php | 1 + beike/Shop/Services/CheckoutService.php | 13 ++++++++----- beike/Shop/Services/TotalService.php | 18 +++++++++++++----- .../Services/TotalServices/SubtotalService.php | 2 +- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/beike/Models/Cart.php b/beike/Models/Cart.php index b5cc7499..6dde9274 100644 --- a/beike/Models/Cart.php +++ b/beike/Models/Cart.php @@ -2,6 +2,7 @@ namespace Beike\Models; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Factories\HasFactory; class Cart extends Base @@ -11,4 +12,19 @@ class Cart extends Base protected $fillable = [ 'customer_id', 'shipping_address_id', 'shipping_method_code', 'payment_address_id', 'payment_method_code' ]; + + public function customer(): BelongsTo + { + return $this->belongsTo(Customer::class, 'customer_id', 'id'); + } + + public function shippingAddress(): BelongsTo + { + return $this->belongsTo(Address::class, 'shipping_address_id', 'id'); + } + + public function paymentAddress(): BelongsTo + { + return $this->belongsTo(Address::class, 'payment_address_id', 'id'); + } } diff --git a/beike/Repositories/CartRepo.php b/beike/Repositories/CartRepo.php index 716c4741..8be341cc 100644 --- a/beike/Repositories/CartRepo.php +++ b/beike/Repositories/CartRepo.php @@ -55,6 +55,7 @@ class CartRepo } $cart->save(); } + $cart->loadMissing(['shippingAddress', 'paymentAddress']); return $cart; } diff --git a/beike/Shop/Services/CheckoutService.php b/beike/Shop/Services/CheckoutService.php index 63841709..3edcc5e9 100644 --- a/beike/Shop/Services/CheckoutService.php +++ b/beike/Shop/Services/CheckoutService.php @@ -163,20 +163,23 @@ class CheckoutService public function checkoutData(): array { $customer = $this->customer; + $currentCart = $this->cart; + $addresses = AddressRepo::listByCustomer($customer); $shipments = ShippingMethodItem::collection(PluginRepo::getShippingMethods())->jsonSerialize(); $payments = PaymentMethodItem::collection(PluginRepo::getPaymentMethods())->jsonSerialize(); $cartList = CartService::list($customer, true); $carts = CartService::reloadData($cartList); - $totalService = (new TotalService($cartList))->setShippingMethod($this->cart->shipping_method_code); + + $totalService = (new TotalService($currentCart, $cartList)); $data = [ 'current' => [ - 'shipping_address_id' => $this->cart->shipping_address_id, - 'shipping_method_code' => $this->cart->shipping_method_code, - 'payment_address_id' => $this->cart->payment_address_id, - 'payment_method_code' => $this->cart->payment_method_code, + 'shipping_address_id' => $currentCart->shipping_address_id, + 'shipping_method_code' => $currentCart->shipping_method_code, + 'payment_address_id' => $currentCart->payment_address_id, + 'payment_method_code' => $currentCart->payment_method_code, ], 'country_id' => (int)system_setting('base.country_id'), 'customer_id' => $customer->id ?? null, diff --git a/beike/Shop/Services/TotalService.php b/beike/Shop/Services/TotalService.php index 9292a28f..f29a5a2e 100644 --- a/beike/Shop/Services/TotalService.php +++ b/beike/Shop/Services/TotalService.php @@ -12,6 +12,7 @@ namespace Beike\Shop\Services; use Beike\Libraries\Tax; +use Beike\Models\Cart; use Illuminate\Support\Str; class TotalService @@ -23,15 +24,17 @@ class TotalService 'order_total' ]; - public array $carts; + public Cart $currentCart; + public array $cartProducts; public array $taxes = []; public array $totals; public float $amount = 0; public string $shippingMethod = ''; - public function __construct($carts) + public function __construct($currentCart, $cartProducts) { - $this->carts = $carts; + $this->currentCart = $currentCart; + $this->cartProducts = $cartProducts; $this->getTaxes(); } @@ -53,8 +56,13 @@ class TotalService */ public function getTaxes(): array { - $taxLib = Tax::getInstance(); - foreach ($this->carts as $product) { + $addressInfo = [ + 'shipping_address' => $this->currentCart->shippingAddress, + 'payment_address' => $this->currentCart->paymentAddress, + ]; + $taxLib = Tax::getInstance($addressInfo); + + foreach ($this->cartProducts as $product) { if (empty($product['tax_class_id'])) { continue; } diff --git a/beike/Shop/Services/TotalServices/SubtotalService.php b/beike/Shop/Services/TotalServices/SubtotalService.php index 2e788440..1faee86e 100644 --- a/beike/Shop/Services/TotalServices/SubtotalService.php +++ b/beike/Shop/Services/TotalServices/SubtotalService.php @@ -18,7 +18,7 @@ class SubtotalService { public static function getTotal(TotalService $totalService) { - $carts = $totalService->carts; + $carts = $totalService->cartProducts; $amount = collect($carts)->sum('subtotal'); $totalData = [ 'code' => 'sub_total',