feat: add total field to purchases and update related logic for payment processing

This commit is contained in:
2026-07-07 10:16:28 -03:00
parent 362e888e46
commit 7b2a741884
9 changed files with 377 additions and 56 deletions

View File

@@ -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',
]);
}
}