refactor(checkout): materialize purchase items on confirmation

This commit is contained in:
2026-08-19 12:06:29 -03:00
parent e6c4b40a37
commit f1649e0e4b
14 changed files with 487 additions and 145 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Domains\Purchase\Models;
use App\Domains\Auth\Models\User;
use App\Domains\Cart\Models\Cart;
use App\Domains\Catalog\Models\StockReservation;
use App\Domains\Logging\Models\Concerns\LogsValueChanges;
use App\Domains\Purchase\Events\PurchasePaid;
use App\Domains\Tenant\Models\Tenant;
@@ -116,6 +117,12 @@ class Purchase extends Model
return $this->hasMany(Ticket::class, 'source_purchase_id');
}
/** @return HasMany<StockReservation, $this> */
public function stockReservations(): HasMany
{
return $this->hasMany(StockReservation::class);
}
/**
* @return HasOne<TelepagosQr, $this>
*/
@@ -143,11 +150,16 @@ class Purchase extends Model
public function calculateCurrentTotalAmount(): float
{
if ($this->relationLoaded('items')) {
if ($this->relationLoaded('items') && $this->getRelation('items')->isNotEmpty()) {
return (float) $this->getRelation('items')->sum('total');
}
return (float) $this->items()->sum('total');
$itemsTotal = (float) $this->items()->sum('total');
if ($itemsTotal > 0 || $this->items()->exists()) {
return $itemsTotal;
}
return (float) ($this->cart?->getTotalAmount() ?? $this->total ?? 0);
}
protected function valueChangeTenantCode(): string