fix quantity validation
This commit is contained in:
@@ -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.'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user