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);
$quantity = $selected->sum('quantity');
$amount = $selected->sum('subtotal');
$totalService = new TotalService($carts);
$data = [
'carts' => $carts,
'quantity' => $quantity,
'amount' => $amount,
'amount_format' => currency_format($amount),
'totals' => TotalService::getTotals(),
'totals' => $totalService->getTotals(),
];
return $data;
}

View File

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

View File

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