Skip to content

Commit

Permalink
Merge pull request #14 from investbrainapp/dev
Browse files Browse the repository at this point in the history
fix: adds validation for transaction date
  • Loading branch information
hackeresq authored Nov 8, 2024
2 parents 0f55d84 + 0c29393 commit 307f74b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
14 changes: 8 additions & 6 deletions app/Models/Dividend.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ public function scopeSymbol($query, $symbol)
/**
* Grab new dividend data
*
* @param string $symbol
* @return void
*/
public static function refreshDividendData(string $symbol)
public static function refreshDividendData(string $symbol): void
{
$dividends_meta = self::where(['symbol' => $symbol])
->selectRaw('COUNT(symbol) as total_dividends')
Expand All @@ -68,7 +66,13 @@ public static function refreshDividendData(string $symbol)
// nope, refresh forward looking only
if ( $dividends_meta->total_dividends ) {

$start_date = $dividends_meta->last_date->addHours(48);
$start_date = $dividends_meta->last_date->addHours(24);
}

// skip refresh if there's already recent data
if ($start_date >= $end_date) {

return;
}

// get some data
Expand Down Expand Up @@ -99,8 +103,6 @@ public static function refreshDividendData(string $symbol)
$market_data->last_dividend_amount = $dividend_data->sortByDesc('date')->first()['dividend_amount'];
$market_data->save();
}

return $dividend_data;
}

public static function syncHoldings(string $symbol): void
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function scopeSell($query)

public function scopeBeforeDate($query, $date)
{
return $query->whereDate('date', '<', $date);
return $query->whereDate('date', '<=', $date);
}

public function scopeMyTransactions()
Expand Down
2 changes: 1 addition & 1 deletion app/Rules/QuantityValidationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function validate(string $attribute, mixed $value, \Closure $fail): void

$maxQuantity = $purchase_qty - $sales_qty;

if ($value > $maxQuantity) {
if (round($value, 3) > round($maxQuantity, 3)) {
$fail(__('The quantity must not be greater than the available quantity.'));
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/manage-transaction-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function rules()
'symbol' => ['required', 'string', new SymbolValidationRule],
'transaction_type' => 'required|string|in:BUY,SELL',
'portfolio_id' => 'required|exists:portfolios,id',
'date' => 'required|date_format:Y-m-d',
'date' => ['required', 'date_format:Y-m-d', 'before_or_equal:' . now()->format('Y-m-d')],
'quantity' => [
'required',
'numeric',
Expand Down

0 comments on commit 307f74b

Please sign in to comment.