87 lines
2.0 KiB
PHP
87 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class ConditionalOrder extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
public const STATUS_PENDING = 'pending';
|
|
public const STATUS_EXECUTED = 'executed';
|
|
public const STATUS_FAILED = 'failed';
|
|
public const STATUS_EXPIRED = 'expired';
|
|
|
|
public const CONDITION_GTE = 'gte';
|
|
public const CONDITION_LTE = 'lte';
|
|
|
|
protected $fillable = [
|
|
'customer_id',
|
|
'product_id',
|
|
'direction',
|
|
'price_condition',
|
|
'trigger_price',
|
|
'amount',
|
|
'number',
|
|
'expires_at',
|
|
'description',
|
|
'is_active',
|
|
'status',
|
|
'fail_reason',
|
|
'order_id',
|
|
'fee_charged',
|
|
'triggered_price',
|
|
'executed_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
'direction' => 'integer',
|
|
'trigger_price' => 'integer',
|
|
'amount' => 'float',
|
|
'number' => 'float',
|
|
'fee_charged' => 'integer',
|
|
'triggered_price' => 'integer',
|
|
'expires_at' => 'datetime',
|
|
'executed_at' => 'datetime',
|
|
];
|
|
|
|
public function customer()
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function order()
|
|
{
|
|
return $this->belongsTo(Order::class);
|
|
}
|
|
|
|
public function isPending(): bool
|
|
{
|
|
return $this->status === self::STATUS_PENDING;
|
|
}
|
|
|
|
public function conditionMet(int $currentPrice): bool
|
|
{
|
|
if ($currentPrice <= 0) {
|
|
return false;
|
|
}
|
|
|
|
$target = (int) $this->trigger_price;
|
|
|
|
return match ($this->price_condition) {
|
|
self::CONDITION_GTE => $currentPrice >= $target,
|
|
self::CONDITION_LTE => $currentPrice <= $target,
|
|
default => false,
|
|
};
|
|
}
|
|
}
|