fixed tax

This commit is contained in:
Edward Yang 2022-08-29 18:23:01 +08:00
parent 46b9f9b967
commit 1596c985b8
5 changed files with 39 additions and 11 deletions

View File

@ -2,6 +2,7 @@
namespace Beike\Models; namespace Beike\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
class Cart extends Base class Cart extends Base
@ -11,4 +12,19 @@ class Cart extends Base
protected $fillable = [ protected $fillable = [
'customer_id', 'shipping_address_id', 'shipping_method_code', 'payment_address_id', 'payment_method_code' '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');
}
} }

View File

@ -55,6 +55,7 @@ class CartRepo
} }
$cart->save(); $cart->save();
} }
$cart->loadMissing(['shippingAddress', 'paymentAddress']);
return $cart; return $cart;
} }

View File

@ -163,20 +163,23 @@ class CheckoutService
public function checkoutData(): array public function checkoutData(): array
{ {
$customer = $this->customer; $customer = $this->customer;
$currentCart = $this->cart;
$addresses = AddressRepo::listByCustomer($customer); $addresses = AddressRepo::listByCustomer($customer);
$shipments = ShippingMethodItem::collection(PluginRepo::getShippingMethods())->jsonSerialize(); $shipments = ShippingMethodItem::collection(PluginRepo::getShippingMethods())->jsonSerialize();
$payments = PaymentMethodItem::collection(PluginRepo::getPaymentMethods())->jsonSerialize(); $payments = PaymentMethodItem::collection(PluginRepo::getPaymentMethods())->jsonSerialize();
$cartList = CartService::list($customer, true); $cartList = CartService::list($customer, true);
$carts = CartService::reloadData($cartList); $carts = CartService::reloadData($cartList);
$totalService = (new TotalService($cartList))->setShippingMethod($this->cart->shipping_method_code);
$totalService = (new TotalService($currentCart, $cartList));
$data = [ $data = [
'current' => [ 'current' => [
'shipping_address_id' => $this->cart->shipping_address_id, 'shipping_address_id' => $currentCart->shipping_address_id,
'shipping_method_code' => $this->cart->shipping_method_code, 'shipping_method_code' => $currentCart->shipping_method_code,
'payment_address_id' => $this->cart->payment_address_id, 'payment_address_id' => $currentCart->payment_address_id,
'payment_method_code' => $this->cart->payment_method_code, 'payment_method_code' => $currentCart->payment_method_code,
], ],
'country_id' => (int)system_setting('base.country_id'), 'country_id' => (int)system_setting('base.country_id'),
'customer_id' => $customer->id ?? null, 'customer_id' => $customer->id ?? null,

View File

@ -12,6 +12,7 @@
namespace Beike\Shop\Services; namespace Beike\Shop\Services;
use Beike\Libraries\Tax; use Beike\Libraries\Tax;
use Beike\Models\Cart;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class TotalService class TotalService
@ -23,15 +24,17 @@ class TotalService
'order_total' 'order_total'
]; ];
public array $carts; public Cart $currentCart;
public array $cartProducts;
public array $taxes = []; public array $taxes = [];
public array $totals; public array $totals;
public float $amount = 0; public float $amount = 0;
public string $shippingMethod = ''; public string $shippingMethod = '';
public function __construct($carts) public function __construct($currentCart, $cartProducts)
{ {
$this->carts = $carts; $this->currentCart = $currentCart;
$this->cartProducts = $cartProducts;
$this->getTaxes(); $this->getTaxes();
} }
@ -53,8 +56,13 @@ class TotalService
*/ */
public function getTaxes(): array public function getTaxes(): array
{ {
$taxLib = Tax::getInstance(); $addressInfo = [
foreach ($this->carts as $product) { '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'])) { if (empty($product['tax_class_id'])) {
continue; continue;
} }

View File

@ -18,7 +18,7 @@ class SubtotalService
{ {
public static function getTotal(TotalService $totalService) public static function getTotal(TotalService $totalService)
{ {
$carts = $totalService->carts; $carts = $totalService->cartProducts;
$amount = collect($carts)->sum('subtotal'); $amount = collect($carts)->sum('subtotal');
$totalData = [ $totalData = [
'code' => 'sub_total', 'code' => 'sub_total',