fixed totals

This commit is contained in:
Edward Yang 2022-07-27 16:53:28 +08:00
parent 2d8ff03822
commit ef3e5dc70d
3 changed files with 23 additions and 8 deletions

View File

@ -145,13 +145,14 @@ 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(), 'totals' => $totalService->getTotals(),
]; ];
return $data; return $data;
} }

View File

@ -11,7 +11,6 @@
namespace Beike\Shop\Services; namespace Beike\Shop\Services;
use \Beike\Shop\Services\TotalServices;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class TotalService class TotalService
@ -23,16 +22,27 @@ class TotalService
'total' 'total'
]; ];
public array $carts;
public array $totals;
public function __construct($carts)
{
$this->carts = $carts;
}
/** /**
* @return array * @return array
*/ */
public static function getTotals(): array public function getTotals(): array
{ {
$totals = []; $totals = [];
foreach (self::TOTAL_CODES as $code) { foreach (self::TOTAL_CODES as $code) {
$serviceName = Str::studly($code); $serviceName = Str::studly($code) . 'Service';
$service = "TotalServices\{$serviceName}"; $service = "\Beike\\Shop\\Services\\TotalServices\\{$serviceName}";
$totals[] = $service::getTotal(); if (!class_exists($service) || !method_exists($service, 'getTotal')) {
continue;
}
$this->totals[] = $service::getTotal($this);
} }
return $totals; return $totals;

View File

@ -14,8 +14,12 @@ namespace Beike\Shop\Services\TotalServices;
class SubtotalService class SubtotalService
{ {
public static function getTotal() public static function getTotal($totalService)
{ {
$carts = $totalService->carts;
return [
'code' => 'sub_total',
'value' => collect($carts)->sum('total')
];
} }
} }