prevent selling more than owned
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use App\Models\Portfolio;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class QuantityValidationRule implements ValidationRule
|
||||
{
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
protected Portfolio $portfolio,
|
||||
protected string $symbol,
|
||||
protected string $transactionType
|
||||
) {
|
||||
$this->portfolio = $portfolio;
|
||||
$this->symbol = $symbol;
|
||||
$this->transactionType = $transactionType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the attribute.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param \Closure $fail
|
||||
* @return void
|
||||
*/
|
||||
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;
|
||||
|
||||
if ($value > $maxQuantity) {
|
||||
$fail(__('The quantity must not be greater than the available quantity.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,7 @@
|
||||
"Symbol": "Symbol",
|
||||
"Quantity": "Quantity",
|
||||
"Quantity Owned": "Quantity Owned",
|
||||
"The quantity must not be greater than the available quantity.": "The quantity must not be greater than the available quantity.",
|
||||
"Average Cost Basis": "Average Cost Basis",
|
||||
"Market Value": "Market Value",
|
||||
"52 week": "52 week",
|
||||
|
||||
@@ -134,6 +134,7 @@
|
||||
"Symbol": "Símbolo",
|
||||
"Quantity": "Cantidad",
|
||||
"Quantity Owned": "Cantidad de propiedad",
|
||||
"The quantity must not be greater than the available quantity.": "La cantidad no debe ser mayor que la cantidad disponible.",
|
||||
"Average Cost Basis": "Costo Promedio",
|
||||
"Market Value": "Valor de Mercado",
|
||||
"52 week": "52 semanas",
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
use App\Models\Transaction;
|
||||
use App\Models\Portfolio;
|
||||
use App\Rules\SymbolValidationRule;
|
||||
use App\Rules\QuantityValidationRule;
|
||||
use Illuminate\Support\Collection;
|
||||
use Livewire\Attributes\{Computed};
|
||||
use Livewire\Volt\Component;
|
||||
use Mary\Traits\Toast;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
new class extends Component {
|
||||
use Toast;
|
||||
@@ -34,7 +36,12 @@ new class extends Component {
|
||||
'transaction_type' => 'required|string|in:BUY,SELL',
|
||||
'portfolio_id' => 'required|exists:portfolios,id',
|
||||
'date' => 'required|date_format:Y-m-d',
|
||||
'quantity' => 'required|min:0|numeric',
|
||||
'quantity' => [
|
||||
'required',
|
||||
'numeric',
|
||||
'min:0',
|
||||
new QuantityValidationRule($this->portfolio, $this->symbol, $this->transaction_type)
|
||||
],
|
||||
'cost_basis' => 'exclude_if:transaction_type,SELL|min:0|numeric',
|
||||
'sale_price' => 'exclude_if:transaction_type,BUY|min:0|numeric',
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user