- Introduced a new configuration file for purchase settings, including checkout and payment expiration times. - Added a migration to include a `reservation_status` column in the `compra_items` table, defaulting to 'active' and updating existing records based on purchase status. - Created a migration to add an `expires_at` timestamp to the `compras` table, updating it for existing records based on their status. - Scoped unique indexes in the `carritos` table to only active carts, adding virtual columns for active user and guest tokens. - Implemented a console command to expire overdue purchases and release stock reservations, scheduled to run every minute. - Added tests for Google token exchange and login functionality, ensuring proper cart handling and user authentication. - Updated purchase-related tests to reflect changes in item handling and customer data validation.
154 lines
3.5 KiB
PHP
154 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Purchase\Models;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Cart\Models\Cart;
|
|
use App\Domains\Purchase\Events\PurchasePaid;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
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\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
#[Fillable([
|
|
'cart_id',
|
|
'tenant_codigo',
|
|
'user_id',
|
|
'status',
|
|
'payment_method',
|
|
'expires_at',
|
|
'total',
|
|
'dni',
|
|
'transfer_payer_dni',
|
|
'telefono',
|
|
'nombre_apellido',
|
|
'email',
|
|
])]
|
|
class Purchase extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public const STATUS_CREATED = 'created';
|
|
|
|
public const STATUS_PENDING_PAYMENT = 'pending_payment';
|
|
|
|
public const STATUS_PAID = 'paid';
|
|
|
|
public const STATUS_CANCELLED = 'cancelled';
|
|
|
|
public const STATUS_REJECTED = 'rejected';
|
|
|
|
public const STATUS_EXPIRED = 'expired';
|
|
|
|
protected $table = 'compras';
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'cart_id' => 'integer',
|
|
'user_id' => 'integer',
|
|
'expires_at' => 'datetime',
|
|
'total' => 'decimal:2',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Tenant, $this>
|
|
*/
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Cart, $this>
|
|
*/
|
|
public function cart(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Cart::class, 'cart_id');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<PurchaseItem, $this>
|
|
*/
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(PurchaseItem::class, 'compra_id');
|
|
}
|
|
|
|
/**
|
|
* @return HasOne<TelepagosQr, $this>
|
|
*/
|
|
public function telepagosQr()
|
|
{
|
|
return $this->hasOne(TelepagosQr::class, 'compra_id');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<TelepagosPayment, $this>
|
|
*/
|
|
public function telepagosPayments(): HasMany
|
|
{
|
|
return $this->hasMany(TelepagosPayment::class, 'compra_id');
|
|
}
|
|
|
|
public function getTotalAmount(): float
|
|
{
|
|
if ($this->total !== null) {
|
|
return (float) $this->total;
|
|
}
|
|
|
|
return $this->calculateCurrentTotalAmount();
|
|
}
|
|
|
|
public function calculateCurrentTotalAmount(): float
|
|
{
|
|
if ($this->relationLoaded('items')) {
|
|
return (float) $this->getRelation('items')->sum('total');
|
|
}
|
|
|
|
return (float) $this->items()->sum('total');
|
|
}
|
|
|
|
public function markAsPendingPayment(): void
|
|
{
|
|
$this->update([
|
|
'status' => self::STATUS_PENDING_PAYMENT,
|
|
]);
|
|
}
|
|
|
|
public function markAsPaid(): void
|
|
{
|
|
DB::transaction(function (): void {
|
|
$currentStatus = self::query()
|
|
->whereKey($this->getKey())
|
|
->lockForUpdate()
|
|
->value('status');
|
|
|
|
if ($currentStatus === self::STATUS_PAID) {
|
|
$this->status = self::STATUS_PAID;
|
|
|
|
return;
|
|
}
|
|
|
|
$this->update([
|
|
'status' => self::STATUS_PAID,
|
|
]);
|
|
|
|
PurchasePaid::dispatch($this);
|
|
});
|
|
}
|
|
}
|