feat: add total field to purchases and update related logic for payment processing
This commit is contained in:
@@ -55,9 +55,11 @@ class PurchaseController extends Controller
|
||||
{
|
||||
$compra = $this->resolveScopedPurchase($tenant, $request->user()->id, $compra);
|
||||
$method = $request->validated('method');
|
||||
$totalAmount = $compra->calculateCurrentTotalAmount();
|
||||
|
||||
$compra->update([
|
||||
'payment_method' => $method,
|
||||
'total' => $totalAmount,
|
||||
]);
|
||||
|
||||
if ($method === 'transfer') {
|
||||
@@ -89,9 +91,9 @@ class PurchaseController extends Controller
|
||||
$telepagosService->forTenant($tenant->codigo);
|
||||
|
||||
try {
|
||||
Log::info("Generating QR for purchase ID: {$compra->id}, amount: {$compra->getTotalAmount()}");
|
||||
Log::info("Generating QR for purchase ID: {$compra->id}, amount: {$totalAmount}");
|
||||
$qrResponse = $telepagosService->generateQr(
|
||||
$compra->getTotalAmount(),
|
||||
$totalAmount,
|
||||
'Compra',
|
||||
"Compra #{$compra->id}"
|
||||
);
|
||||
|
||||
@@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'status',
|
||||
'payment_status',
|
||||
'payment_method',
|
||||
'total',
|
||||
'dni',
|
||||
'telefono',
|
||||
'nombre_apellido',
|
||||
@@ -34,6 +35,7 @@ class Purchase extends Model
|
||||
return [
|
||||
'cart_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'total' => 'decimal:2',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -87,13 +89,26 @@ class Purchase extends Model
|
||||
|
||||
public function getTotalAmount(): float
|
||||
{
|
||||
if ($this->total !== null) {
|
||||
return (float) $this->total;
|
||||
}
|
||||
|
||||
return $this->calculateCurrentTotalAmount();
|
||||
}
|
||||
|
||||
public function calculateCurrentTotalAmount(): float
|
||||
{
|
||||
if ($this->relationLoaded('items') && $this->getRelation('items')->isNotEmpty()) {
|
||||
return (float) $this->getRelation('items')->sum('total');
|
||||
}
|
||||
|
||||
if ($this->items()->exists()) {
|
||||
return (float) $this->items()->sum('total');
|
||||
}
|
||||
|
||||
$cart = $this->relationLoaded('cart')
|
||||
? $this->getRelation('cart')
|
||||
: $this->cart()->first();
|
||||
: $this->cart()->with('items.variant.product')->first();
|
||||
|
||||
if (! $cart) {
|
||||
return 0.0;
|
||||
@@ -105,8 +120,8 @@ class Purchase extends Model
|
||||
public function finalize(): void
|
||||
{
|
||||
$this->update([
|
||||
'status' => 'pagado', // Or the equivalent final status in your system
|
||||
'payment_status' => 'paid',
|
||||
'status' => 'paid',
|
||||
'payment_status' => 'approved',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,15 +19,19 @@ class PurchaseResource extends JsonResource
|
||||
? $this->resource->getRelation('items')
|
||||
: collect();
|
||||
|
||||
$subtotal = $items->reduce(
|
||||
fn (float $carry, $item): float => $carry + ((float) $item->precio_unitario * $item->cantidad),
|
||||
0.0,
|
||||
);
|
||||
$subtotal = $items->isNotEmpty()
|
||||
? $items->reduce(
|
||||
fn (float $carry, $item): float => $carry + ((float) $item->precio_unitario * $item->cantidad),
|
||||
0.0,
|
||||
)
|
||||
: (float) ($this->total ?? 0);
|
||||
|
||||
$total = $items->reduce(
|
||||
fn (float $carry, $item): float => $carry + (float) $item->total,
|
||||
0.0,
|
||||
);
|
||||
$total = $items->isNotEmpty()
|
||||
? $items->reduce(
|
||||
fn (float $carry, $item): float => $carry + (float) $item->total,
|
||||
0.0,
|
||||
)
|
||||
: (float) ($this->total ?? 0);
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
|
||||
@@ -29,6 +29,10 @@ class CheckoutService
|
||||
]);
|
||||
}
|
||||
|
||||
$cartItems->load('variant.product');
|
||||
$cart->setRelation('items', $cartItems);
|
||||
$totalAmount = $cart->getTotalAmount();
|
||||
|
||||
/** @var Purchase $purchase */
|
||||
$purchase = Purchase::query()->updateOrCreate(
|
||||
[
|
||||
@@ -41,6 +45,7 @@ class CheckoutService
|
||||
...$purchaseData,
|
||||
'payment_status' => 'pending',
|
||||
'payment_method' => null,
|
||||
'total' => $totalAmount,
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user