Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds validation for transaction date #14

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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