add totals

This commit is contained in:
Edward Yang 2022-07-27 17:54:15 +08:00
parent 21fb3e07ed
commit acfad8a621
7 changed files with 83 additions and 13 deletions

View File

@ -145,14 +145,12 @@ class CartService
$selected = collect($carts)->where('selected', 1); $selected = collect($carts)->where('selected', 1);
$quantity = $selected->sum('quantity'); $quantity = $selected->sum('quantity');
$amount = $selected->sum('subtotal'); $amount = $selected->sum('subtotal');
$totalService = new TotalService($carts);
$data = [ $data = [
'carts' => $carts, 'carts' => $carts,
'quantity' => $quantity, 'quantity' => $quantity,
'amount' => $amount, 'amount' => $amount,
'amount_format' => currency_format($amount), 'amount_format' => currency_format($amount),
'totals' => $totalService->getTotals(),
]; ];
return $data; return $data;
} }

View File

@ -122,6 +122,7 @@ class CheckoutService
$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);
$data = [ $data = [
'current' => [ 'current' => [
@ -136,8 +137,10 @@ class CheckoutService
'addresses' => AddressResource::collection($addresses), 'addresses' => AddressResource::collection($addresses),
'shipping_methods' => $shipments, 'shipping_methods' => $shipments,
'payment_methods' => $payments, 'payment_methods' => $payments,
'carts' => $carts 'carts' => $carts,
'totals' => $totalService->getTotals(),
]; ];
return $data; return $data;
} }
} }

View File

@ -19,32 +19,48 @@ class TotalService
'subtotal', 'subtotal',
'tax', 'tax',
'shipping', 'shipping',
'total' 'order_total'
]; ];
public array $carts; public array $carts;
public array $totals; public array $totals;
public float $amount = 0;
public string $shippingMethod = '';
public function __construct($carts) public function __construct($carts)
{ {
$this->carts = $carts; $this->carts = $carts;
} }
/**
* 设置配送方式
*/
public function setShippingMethod($methodCode): TotalService
{
$this->shippingMethod = $methodCode;
return $this;
}
/** /**
* @return array * @return array
*/ */
public function getTotals(): array public function getTotals(): array
{ {
$totals = [];
foreach (self::TOTAL_CODES as $code) { foreach (self::TOTAL_CODES as $code) {
$serviceName = Str::studly($code) . 'Service'; $serviceName = Str::studly($code) . 'Service';
$service = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}"; $service = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}";
if (!class_exists($service) || !method_exists($service, 'getTotal')) { if (!class_exists($service) || !method_exists($service, 'getTotal')) {
continue; continue;
} }
$this->totals[] = $service::getTotal($this); $totalData = $service::getTotal($this);
if ($totalData) {
$this->amount += $totalData['amount'];
$this->totals[] = $totalData;
}
} }
return $totals; return $this->totals;
} }
} }

View File

@ -0,0 +1,28 @@
<?php
/**
* TotalService.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-07-27 17:49:15
* @modified 2022-07-27 17:49:15
*/
namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\TotalService;
class OrderTotalService
{
public static function getTotal(TotalService $totalService)
{
$amount = $totalService->amount;
return [
'code' => 'order_total',
'title' => '应付总金额',
'amount' => $amount,
'amount_format' => currency_format($amount)
];
}
}

View File

@ -12,10 +12,22 @@
namespace Beike\Shop\Services\TotalServices; namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\TotalService;
class ShippingService class ShippingService
{ {
public static function getTotal() public static function getTotal(TotalService $totalService)
{ {
$shippingMethod = $totalService->shippingMethod;
if (empty($shippingMethod)) {
return null;
}
$amount = 5;
return [
'code' => 'shipping',
'title' => '运费',
'amount' => $amount,
'amount_format' => currency_format($amount)
];
} }
} }

View File

@ -12,14 +12,19 @@
namespace Beike\Shop\Services\TotalServices; namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\TotalService;
class SubtotalService class SubtotalService
{ {
public static function getTotal($totalService) public static function getTotal(TotalService $totalService)
{ {
$carts = $totalService->carts; $carts = $totalService->carts;
$amount = collect($carts)->sum('subtotal');
return [ return [
'code' => 'sub_total', 'code' => 'sub_total',
'value' => collect($carts)->sum('total') 'title' => '商品总额',
'amount' => $amount,
'amount_format' => currency_format($amount)
]; ];
} }
} }

View File

@ -12,10 +12,18 @@
namespace Beike\Shop\Services\TotalServices; namespace Beike\Shop\Services\TotalServices;
use Beike\Shop\Services\TotalService;
class TaxService class TaxService
{ {
public static function getTotal() public static function getTotal(TotalService $totalService)
{ {
$amount = $totalService->amount * 0.02;
return [
'code' => 'shipping',
'title' => '运费',
'amount' => $amount,
'amount_format' => currency_format($amount)
];
} }
} }