feat: add getTotalAmount method to Cart and refactor Purchase model to utilize it

This commit is contained in:
2026-07-06 15:26:41 -03:00
parent c19e00987e
commit 3703ae9bcf
3 changed files with 36 additions and 33 deletions

View File

@@ -59,6 +59,18 @@ class Cart extends Model
return $this->hasMany(CartItem::class, 'cart_id'); return $this->hasMany(CartItem::class, 'cart_id');
} }
public function getTotalAmount(): float
{
$items = $this->relationLoaded('items')
? $this->getRelation('items')
: $this->items()->with('variant.product')->get();
return (float) $items->reduce(
fn (float $carry, $item): float => $carry + ((float) ($item->variant?->product?->precio ?? 0) * $item->cantidad),
0.0,
);
}
public function addItem(int $productVariantId, int $quantity): CartItem public function addItem(int $productVariantId, int $quantity): CartItem
{ {
if ($quantity <= 0) { if ($quantity <= 0) {

View File

@@ -11,6 +11,7 @@ use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Domains\Purchase\Services\CheckoutService; use App\Domains\Purchase\Services\CheckoutService;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -74,13 +75,11 @@ class PurchaseController extends Controller
} }
if ($method === 'qr') { if ($method === 'qr') {
$telepagosQr = $compra->telepagosQr;
if (!$telepagosQr) {
$telepagosService = new \App\Domains\Integration\Services\TelepagosIntegrationService(); $telepagosService = new \App\Domains\Integration\Services\TelepagosIntegrationService();
$telepagosService->forTenant($tenant->codigo); $telepagosService->forTenant($tenant->codigo);
try { try {
Log::info("Generating QR for purchase ID: {$compra->id}, amount: {$compra->getTotalAmount()}");
$qrResponse = $telepagosService->generateQr( $qrResponse = $telepagosService->generateQr(
$compra->getTotalAmount(), $compra->getTotalAmount(),
'Compra', 'Compra',
@@ -96,7 +95,6 @@ class PurchaseController extends Controller
'message' => 'Error generating QR: ' . $e->getMessage() 'message' => 'Error generating QR: ' . $e->getMessage()
], 500); ], 500);
} }
}
return response()->json([ return response()->json([
'payment_method' => 'qr', 'payment_method' => 'qr',

View File

@@ -10,7 +10,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
#[Fillable([ #[Fillable([
'cart_id', 'cart_id',
@@ -59,7 +58,7 @@ class Purchase extends Model
*/ */
public function cart(): BelongsTo public function cart(): BelongsTo
{ {
return $this->belongsTo(Cart::class, 'cart_id')->withTrashed(); return $this->belongsTo(Cart::class, 'cart_id');
} }
/** /**
@@ -92,21 +91,15 @@ class Purchase extends Model
return (float) $this->items()->sum('total'); return (float) $this->items()->sum('total');
} }
$cart = $this->relationLoaded('cart') ? $this->getRelation('cart') : $this->cart; $cart = $this->relationLoaded('cart')
? $this->getRelation('cart')
: $this->cart()->first();
if ($cart === null) { if (! $cart) {
return 0.0; return 0.0;
} }
/** @var Collection<int, mixed> $items */ return $cart->getTotalAmount();
$items = $cart->relationLoaded('items')
? $cart->getRelation('items')
: $cart->items()->with('variant.product')->get();
return $items->reduce(
static fn (float $carry, $item): float => $carry + ((float) ($item->variant?->product?->precio ?? 0) * (int) $item->cantidad),
0.0,
);
} }
public function finalize(): void public function finalize(): void