From 3703ae9bcf2aa716f8624239f2497c914894d51d Mon Sep 17 00:00:00 2001 From: ncoronel Date: Mon, 6 Jul 2026 15:26:41 -0300 Subject: [PATCH] feat: add getTotalAmount method to Cart and refactor Purchase model to utilize it --- app/Domains/Cart/Models/Cart.php | 12 ++++++ .../Controllers/PurchaseController.php | 38 +++++++++---------- app/Domains/Purchase/Models/Purchase.php | 19 +++------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/app/Domains/Cart/Models/Cart.php b/app/Domains/Cart/Models/Cart.php index d4aac7b..6d15a49 100644 --- a/app/Domains/Cart/Models/Cart.php +++ b/app/Domains/Cart/Models/Cart.php @@ -59,6 +59,18 @@ class Cart extends Model 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 { if ($quantity <= 0) { diff --git a/app/Domains/Purchase/Controllers/PurchaseController.php b/app/Domains/Purchase/Controllers/PurchaseController.php index aa45d6d..10aa1be 100644 --- a/app/Domains/Purchase/Controllers/PurchaseController.php +++ b/app/Domains/Purchase/Controllers/PurchaseController.php @@ -11,6 +11,7 @@ use App\Http\Controllers\Controller; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use App\Domains\Purchase\Services\CheckoutService; +use Illuminate\Support\Facades\Log; use Illuminate\Validation\ValidationException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -74,28 +75,25 @@ class PurchaseController extends Controller } if ($method === 'qr') { - $telepagosQr = $compra->telepagosQr; + $telepagosService = new \App\Domains\Integration\Services\TelepagosIntegrationService(); + $telepagosService->forTenant($tenant->codigo); - if (!$telepagosQr) { - $telepagosService = new \App\Domains\Integration\Services\TelepagosIntegrationService(); - $telepagosService->forTenant($tenant->codigo); - - try { - $qrResponse = $telepagosService->generateQr( - $compra->getTotalAmount(), - 'Compra', - "Compra #{$compra->id}" - ); + try { + Log::info("Generating QR for purchase ID: {$compra->id}, amount: {$compra->getTotalAmount()}"); + $qrResponse = $telepagosService->generateQr( + $compra->getTotalAmount(), + 'Compra', + "Compra #{$compra->id}" + ); - $telepagosQr = $compra->telepagosQr()->create([ - 'qr_order_id' => (string) ($qrResponse['id'] ?? $qrResponse['order_id'] ?? $qrResponse['cashin_id'] ?? ''), - 'qr_code' => $qrResponse['qr'] ?? $qrResponse['qr_code'] ?? $qrResponse['qr_data'] ?? '', - ]); - } catch (\Exception $e) { - return response()->json([ - 'message' => 'Error generating QR: ' . $e->getMessage() - ], 500); - } + $telepagosQr = $compra->telepagosQr()->create([ + 'qr_order_id' => (string) ($qrResponse['id'] ?? $qrResponse['order_id'] ?? $qrResponse['cashin_id'] ?? ''), + 'qr_code' => $qrResponse['qr'] ?? $qrResponse['qr_code'] ?? $qrResponse['qr_data'] ?? '', + ]); + } catch (\Exception $e) { + return response()->json([ + 'message' => 'Error generating QR: ' . $e->getMessage() + ], 500); } return response()->json([ diff --git a/app/Domains/Purchase/Models/Purchase.php b/app/Domains/Purchase/Models/Purchase.php index 9760240..ce3c87b 100644 --- a/app/Domains/Purchase/Models/Purchase.php +++ b/app/Domains/Purchase/Models/Purchase.php @@ -10,7 +10,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Collection; #[Fillable([ 'cart_id', @@ -59,7 +58,7 @@ class Purchase extends Model */ 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'); } - $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; } - /** @var Collection $items */ - $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, - ); + return $cart->getTotalAmount(); } public function finalize(): void