139 lines
3.2 KiB
PHP
139 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Purchase\Models;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Cart\Models\Cart;
|
|
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;
|
|
|
|
#[Fillable([
|
|
'cart_id',
|
|
'tenant_codigo',
|
|
'user_id',
|
|
'status',
|
|
'payment_method',
|
|
'total',
|
|
'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';
|
|
|
|
protected $table = 'compras';
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'cart_id' => 'integer',
|
|
'user_id' => 'integer',
|
|
'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 \Illuminate\Database\Eloquent\Relations\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') && $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()->with('items.variant.product')->first();
|
|
|
|
if (! $cart) {
|
|
return 0.0;
|
|
}
|
|
|
|
return $cart->getTotalAmount();
|
|
}
|
|
|
|
public function markAsPendingPayment(): void
|
|
{
|
|
$this->update([
|
|
'status' => self::STATUS_PENDING_PAYMENT,
|
|
]);
|
|
}
|
|
|
|
public function markAsPaid(): void
|
|
{
|
|
$this->update([
|
|
'status' => self::STATUS_PAID,
|
|
]);
|
|
}
|
|
}
|