fix quantity validation

This commit is contained in:
hackerESQ
2024-09-06 23:15:52 -05:00
parent 96a408b32d
commit a5c95e95ae
2 changed files with 20 additions and 6 deletions
+19 -5
View File
@@ -15,11 +15,13 @@ class QuantityValidationRule implements ValidationRule
public function __construct(
protected Portfolio $portfolio,
protected string $symbol,
protected string $transactionType
protected string $transactionType,
protected string $date
) {
$this->portfolio = $portfolio;
$this->symbol = $symbol;
$this->symbol = $symbol;
$this->transactionType = $transactionType;
$this->date = $date;
}
/**
@@ -33,9 +35,21 @@ class QuantityValidationRule implements ValidationRule
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if ($this->transactionType == 'SELL') {
$holding = $this->portfolio->holdings()->symbol($this->symbol)->first();
$maxQuantity = $holding ? $holding->quantity : 0;
$purchase_qty = $this->portfolio->transactions()
->symbol($this->symbol)
->buy()
->beforeDate($this->date)
->sum('quantity');
$sales_qty = $this->portfolio->transactions()
->symbol($this->symbol)
->sell()
->beforeDate($this->date)
->sum('quantity');
$maxQuantity = $purchase_qty - $sales_qty;
if ($value > $maxQuantity) {
$fail(__('The quantity must not be greater than the available quantity.'));
}