Code cleanup

This commit is contained in:
James Cole
2024-12-22 08:43:12 +01:00
parent 5751f7e5a3
commit 565bd87959
574 changed files with 4600 additions and 4604 deletions

View File

@@ -391,7 +391,7 @@ class AccountRepository implements AccountRepositoryInterface
if (!in_array($type, $list, true)) {
return null;
}
$currencyId = (int)$this->getMetaValue($account, 'currency_id');
$currencyId = (int) $this->getMetaValue($account, 'currency_id');
if ($currencyId > 0) {
return TransactionCurrency::find($currencyId);
}
@@ -413,7 +413,7 @@ class AccountRepository implements AccountRepositoryInterface
return null;
}
if (1 === $result->count()) {
return (string)$result->first()->data;
return (string) $result->first()->data;
}
return null;
@@ -434,8 +434,8 @@ class AccountRepository implements AccountRepositoryInterface
$info = $account->transactions()->get(['transaction_currency_id', 'foreign_currency_id'])->toArray();
$currencyIds = [];
foreach ($info as $entry) {
$currencyIds[] = (int)$entry['transaction_currency_id'];
$currencyIds[] = (int)$entry['foreign_currency_id'];
$currencyIds[] = (int) $entry['transaction_currency_id'];
$currencyIds[] = (int) $entry['foreign_currency_id'];
}
$currencyIds = array_unique($currencyIds);
@@ -458,14 +458,14 @@ class AccountRepository implements AccountRepositoryInterface
AccountType::MORTGAGE => [AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
];
if (array_key_exists(ucfirst($type), $sets)) {
$order = (int)$this->getAccountsByType($sets[ucfirst($type)])->max('order');
$order = (int) $this->getAccountsByType($sets[ucfirst($type)])->max('order');
app('log')->debug(sprintf('Return max order of "%s" set: %d', $type, $order));
return $order;
}
$specials = [AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION];
$order = (int)$this->getAccountsByType($specials)->max('order');
$order = (int) $this->getAccountsByType($specials)->max('order');
app('log')->debug(sprintf('Return max order of "%s" set (specials!): %d', $type, $order));
return $order;
@@ -546,7 +546,7 @@ class AccountRepository implements AccountRepositoryInterface
continue;
}
if ($index !== (int)$account->order) {
if ($index !== (int) $account->order) {
app('log')->debug(sprintf('Account #%d ("%s"): order should %d be but is %d.', $account->id, $account->name, $index, $account->order));
$account->order = $index;
$account->save();
@@ -562,6 +562,17 @@ class AccountRepository implements AccountRepositoryInterface
;
}
/**
* @throws FireflyException
*/
public function update(Account $account, array $data): Account
{
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
return $service->update($account, $data);
}
public function searchAccount(string $query, array $types, int $limit): Collection
{
$dbQuery = $this->user->accounts()
@@ -634,15 +645,4 @@ class AccountRepository implements AccountRepositoryInterface
return $factory->create($data);
}
/**
* @throws FireflyException
*/
public function update(Account $account, array $data): Account
{
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
return $service->update($account, $data);
}
}

View File

@@ -130,7 +130,7 @@ class AccountTasker implements AccountTaskerInterface
// Obtain a list of columns
$sum = [];
foreach ($report['accounts'] as $accountId => $row) {
$sum[$accountId] = (float)$row['sum']; // intentional float
$sum[$accountId] = (float) $row['sum']; // intentional float
}
array_multisort($sum, SORT_ASC, $report['accounts']);
@@ -155,8 +155,8 @@ class AccountTasker implements AccountTaskerInterface
/** @var array $journal */
foreach ($array as $journal) {
$sourceId = (int)$journal['destination_account_id'];
$currencyId = (int)$journal['currency_id'];
$sourceId = (int) $journal['destination_account_id'];
$currencyId = (int) $journal['currency_id'];
$key = sprintf('%s-%s', $sourceId, $currencyId);
$currencies[$currencyId] ??= $currencyRepos->find($currencyId);
$report['accounts'][$key] ??= [
@@ -181,7 +181,7 @@ class AccountTasker implements AccountTaskerInterface
// do averages and sums.
foreach (array_keys($report['accounts']) as $key) {
if ($report['accounts'][$key]['count'] > 1) {
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string)$report['accounts'][$key]['count']);
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string) $report['accounts'][$key]['count']);
}
$currencyId = $report['accounts'][$key]['currency_id'];
$report['sums'][$currencyId] ??= [
@@ -218,7 +218,7 @@ class AccountTasker implements AccountTaskerInterface
// Obtain a list of columns
$sum = [];
foreach ($report['accounts'] as $accountId => $row) {
$sum[$accountId] = (float)$row['sum']; // intentional float
$sum[$accountId] = (float) $row['sum']; // intentional float
}
array_multisort($sum, SORT_DESC, $report['accounts']);
@@ -243,8 +243,8 @@ class AccountTasker implements AccountTaskerInterface
/** @var array $journal */
foreach ($array as $journal) {
$sourceId = (int)$journal['source_account_id'];
$currencyId = (int)$journal['currency_id'];
$sourceId = (int) $journal['source_account_id'];
$currencyId = (int) $journal['currency_id'];
$key = sprintf('%s-%s', $sourceId, $currencyId);
if (!array_key_exists($key, $report['accounts'])) {
$currencies[$currencyId] ??= $currencyRepos->find($currencyId);
@@ -268,7 +268,7 @@ class AccountTasker implements AccountTaskerInterface
// do averages and sums.
foreach (array_keys($report['accounts']) as $key) {
if ($report['accounts'][$key]['count'] > 1) {
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string)$report['accounts'][$key]['count']);
$report['accounts'][$key]['average'] = bcdiv($report['accounts'][$key]['sum'], (string) $report['accounts'][$key]['count']);
}
$currencyId = $report['accounts'][$key]['currency_id'];
$report['sums'][$currencyId] ??= [

View File

@@ -76,8 +76,8 @@ class OperationsRepository implements OperationsRepositoryInterface
{
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$journalId = (int)$journal['transaction_journal_id'];
$currencyId = (int) $journal['currency_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId] ??= [
'currency_id' => $journal['currency_id'],
'currency_name' => $journal['currency_name'],
@@ -88,7 +88,7 @@ class OperationsRepository implements OperationsRepositoryInterface
];
$array[$currencyId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->{$direction}((string)$journal['amount']), // @phpstan-ignore-line
'amount' => app('steam')->{$direction}((string) $journal['amount']), // @phpstan-ignore-line
'date' => $journal['date'],
'transaction_journal_id' => $journalId,
'budget_name' => $journal['budget_name'],
@@ -219,7 +219,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => $currencyId,
@@ -231,7 +231,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->{$direction}($journal['amount'])); // @phpstan-ignore-line
// also do foreign amount:
$foreignId = (int)$journal['foreign_currency_id'];
$foreignId = (int) $journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] ??= [
'sum' => '0',
@@ -281,10 +281,10 @@ class OperationsRepository implements OperationsRepositoryInterface
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string)$journal['amount'])); // @phpstan-ignore-line
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) $journal['amount'])); // @phpstan-ignore-line
// also do foreign amount:
if (0 !== (int)$journal['foreign_currency_id']) {
if (0 !== (int) $journal['foreign_currency_id']) {
$key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']);
$array[$key] ??= [
'id' => $journal[$idKey],
@@ -296,7 +296,7 @@ class OperationsRepository implements OperationsRepositoryInterface
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string)$journal['foreign_amount'])); // @phpstan-ignore-line
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) $journal['foreign_amount'])); // @phpstan-ignore-line
}
}
@@ -380,9 +380,9 @@ class OperationsRepository implements OperationsRepositoryInterface
}
$final = [];
foreach ($return as $array) {
$array['difference_float'] = (float)$array['difference'];
$array['in_float'] = (float)$array['in'];
$array['out_float'] = (float)$array['out'];
$array['difference_float'] = (float) $array['difference'];
$array['in_float'] = (float) $array['in'];
$array['out_float'] = (float) $array['out'];
$final[] = $array;
}
@@ -400,7 +400,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// source first
$return[$sourceKey] ??= [
'id' => (string)$sourceId,
'id' => (string) $sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
@@ -408,13 +408,13 @@ class OperationsRepository implements OperationsRepositoryInterface
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['currency_code'],
];
// dest next:
$return[$destKey] ??= [
'id' => (string)$destinationId,
'id' => (string) $destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
@@ -422,7 +422,7 @@ class OperationsRepository implements OperationsRepositoryInterface
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['currency_code'],
];
@@ -444,7 +444,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// same as above:
// source first
$return[$sourceKey] ??= [
'id' => (string)$sourceId,
'id' => (string) $sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
@@ -452,13 +452,13 @@ class OperationsRepository implements OperationsRepositoryInterface
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
// dest next:
$return[$destKey] ??= [
'id' => (string)$destinationId,
'id' => (string) $destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
@@ -466,7 +466,7 @@ class OperationsRepository implements OperationsRepositoryInterface
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
// source account? money goes out! (same as above)

View File

@@ -71,7 +71,7 @@ class AttachmentRepository implements AttachmentRepositoryInterface
$unencryptedContent = '';
if ($disk->exists($file)) {
$encryptedContent = (string)$disk->get($file);
$encryptedContent = (string) $disk->get($file);
try {
$unencryptedContent = \Crypt::decrypt($encryptedContent); // verified
@@ -104,7 +104,7 @@ class AttachmentRepository implements AttachmentRepositoryInterface
{
$note = $attachment->notes()->first();
if (null !== $note) {
return (string)$note->text;
return (string) $note->text;
}
return null;
@@ -139,20 +139,20 @@ class AttachmentRepository implements AttachmentRepositoryInterface
$attachment->title = $data['title'];
}
if (array_key_exists('filename', $data) && '' !== (string)$data['filename'] && $data['filename'] !== $attachment->filename) {
if (array_key_exists('filename', $data) && '' !== (string) $data['filename'] && $data['filename'] !== $attachment->filename) {
$attachment->filename = $data['filename'];
}
// update model (move attachment)
// should be validated already:
if (array_key_exists('attachable_type', $data) && array_key_exists('attachable_id', $data)) {
$attachment->attachable_id = (int)$data['attachable_id'];
$attachment->attachable_id = (int) $data['attachable_id'];
$attachment->attachable_type = sprintf('FireflyIII\Models\%s', $data['attachable_type']);
}
$attachment->save();
$attachment->refresh();
if (array_key_exists('notes', $data)) {
$this->updateNote($attachment, (string)$data['notes']);
$this->updateNote($attachment, (string) $data['notes']);
}
return $attachment;

View File

@@ -240,7 +240,7 @@ class BillRepository implements BillRepositoryInterface
/** @var null|Note $note */
$note = $bill->notes()->first();
return (string)$note?->text;
return (string) $note?->text;
}
public function getOverallAverage(Bill $bill): array
@@ -257,7 +257,7 @@ class BillRepository implements BillRepositoryInterface
foreach ($journals as $journal) {
/** @var Transaction $transaction */
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
$currencyId = (int)$journal->transaction_currency_id;
$currencyId = (int) $journal->transaction_currency_id;
$currency = $journal->transactionCurrency;
$result[$currencyId] ??= [
'sum' => '0',
@@ -278,7 +278,7 @@ class BillRepository implements BillRepositoryInterface
* @var array $arr
*/
foreach ($result as $currencyId => $arr) {
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string)$arr['count']);
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']);
}
return $result;
@@ -382,7 +382,7 @@ class BillRepository implements BillRepositoryInterface
if (null === $transaction) {
continue;
}
$currencyId = (int)$journal->transaction_currency_id;
$currencyId = (int) $journal->transaction_currency_id;
$currency = $journal->transactionCurrency;
$result[$currencyId] ??= [
'sum' => '0',
@@ -403,7 +403,7 @@ class BillRepository implements BillRepositoryInterface
* @var array $arr
*/
foreach ($result as $currencyId => $arr) {
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string)$arr['count']);
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']);
}
return $result;
@@ -416,7 +416,7 @@ class BillRepository implements BillRepositoryInterface
{
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$journal = $bill->user->transactionJournals()->find((int)$transaction['transaction_journal_id']);
$journal = $bill->user->transactionJournals()->find((int) $transaction['transaction_journal_id']);
$journal->bill_id = $bill->id;
$journal->save();
app('log')->debug(sprintf('Linked journal #%d to bill #%d', $journal->id, $bill->id));
@@ -520,7 +520,7 @@ class BillRepository implements BillRepositoryInterface
$currency = $bill->transactionCurrency;
$return[$currency->id] ??= [
'id' => (string)$currency->id,
'id' => (string) $currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
@@ -534,9 +534,9 @@ class BillRepository implements BillRepositoryInterface
$sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first();
if (null !== $sourceTransaction) {
$amount = $sourceTransaction->amount;
if ((int)$sourceTransaction->foreign_currency_id === $currency->id) {
if ((int) $sourceTransaction->foreign_currency_id === $currency->id) {
// use foreign amount instead!
$amount = (string)$sourceTransaction->foreign_amount;
$amount = (string) $sourceTransaction->foreign_amount;
}
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $amount);
}
@@ -574,14 +574,14 @@ class BillRepository implements BillRepositoryInterface
$currency = $bill->transactionCurrency;
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$return[$currency->id] ??= [
'id' => (string)$currency->id,
'id' => (string) $currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
'decimal_places' => $currency->decimal_places,
'sum' => '0',
];
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], bcmul($average, (string)$total));
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], bcmul($average, (string) $total));
}
}

View File

@@ -256,6 +256,12 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
;
}
#[\Override]
public function getNoteText(BudgetLimit $budgetLimit): string
{
return (string) $budgetLimit->notes()->first()?->text;
}
public function setUser(null|Authenticatable|User $user): void
{
if ($user instanceof User) {
@@ -324,6 +330,23 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
;
}
#[\Override]
public function setNoteText(BudgetLimit $budgetLimit, string $text): void
{
$dbNote = $budgetLimit->notes()->first();
if ('' !== $text) {
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($budgetLimit);
}
$dbNote->text = trim($text);
$dbNote->save();
return;
}
$dbNote?->delete();
}
/**
* @throws FireflyException
*/
@@ -362,7 +385,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
// update notes if they exist.
if (array_key_exists('notes', $data)) {
$this->setNoteText($budgetLimit, (string)$data['notes']);
$this->setNoteText($budgetLimit, (string) $data['notes']);
}
return $budgetLimit;
@@ -427,27 +450,4 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
return $limit;
}
#[\Override]
public function getNoteText(BudgetLimit $budgetLimit): string
{
return (string) $budgetLimit->notes()->first()?->text;
}
#[\Override]
public function setNoteText(BudgetLimit $budgetLimit, string $text): void
{
$dbNote = $budgetLimit->notes()->first();
if ('' !== $text) {
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($budgetLimit);
}
$dbNote->text = trim($text);
$dbNote->save();
return;
}
$dbNote?->delete();
}
}

View File

@@ -48,10 +48,6 @@ interface BudgetLimitRepositoryInterface
*/
public function destroyAll(): void;
public function getNoteText(BudgetLimit $budgetLimit): string;
public function setNoteText(BudgetLimit $budgetLimit, string $text): void;
/**
* Destroy a budget limit.
*/
@@ -68,6 +64,10 @@ interface BudgetLimitRepositoryInterface
public function getBudgetLimits(Budget $budget, ?Carbon $start = null, ?Carbon $end = null): Collection;
public function getNoteText(BudgetLimit $budgetLimit): string;
public function setNoteText(BudgetLimit $budgetLimit, string $text): void;
public function setUser(null|Authenticatable|User $user): void;
public function store(array $data): BudgetLimit;

View File

@@ -103,12 +103,12 @@ class BudgetRepository implements BudgetRepositoryInterface
$rate = $converter->getCurrencyRate($currency, $defaultCurrency, $end);
$currencyCode = $currency->code;
$return[$currencyCode] ??= [
'currency_id' => (string)$currency->id,
'currency_id' => (string) $currency->id,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'currency_decimal_places' => $currency->decimal_places,
'native_currency_id' => (string)$defaultCurrency->id,
'native_currency_id' => (string) $defaultCurrency->id,
'native_currency_name' => $defaultCurrency->name,
'native_currency_symbol' => $defaultCurrency->symbol,
'native_currency_code' => $defaultCurrency->code,
@@ -134,13 +134,13 @@ class BudgetRepository implements BudgetRepositoryInterface
}
$total = $limit->start_date->diffInDays($limit->end_date, true) + 1; // include the day itself.
$days = $this->daysInOverlap($limit, $start, $end);
$amount = bcmul(bcdiv($limit->amount, (string)$total), (string)$days);
$amount = bcmul(bcdiv($limit->amount, (string) $total), (string) $days);
$return[$currencyCode]['sum'] = bcadd($return[$currencyCode]['sum'], $amount);
$return[$currencyCode]['native_sum'] = bcmul($rate, $return[$currencyCode]['sum']);
app('log')->debug(
sprintf(
'Amount per day: %s (%s over %d days). Total amount for %d days: %s',
bcdiv($limit->amount, (string)$total),
bcdiv($limit->amount, (string) $total),
$limit->amount,
$total,
$days,
@@ -183,21 +183,21 @@ class BudgetRepository implements BudgetRepositoryInterface
// |-----------|
// |----------------|
if ($start->gte($limit->start_date) && $end->lte($limit->end_date)) {
return (int)$start->diffInDays($end, true) + 1; // add one day
return (int) $start->diffInDays($end, true) + 1; // add one day
}
// limit starts earlier and limit ends first:
// |-----------|
// |-------|
if ($limit->start_date->lte($start) && $limit->end_date->lte($end)) {
// return days in the range $start-$limit_end
return (int)$start->diffInDays($limit->end_date, true) + 1; // add one day, the day itself
return (int) $start->diffInDays($limit->end_date, true) + 1; // add one day, the day itself
}
// limit starts later and limit ends earlier
// |-----------|
// |-------|
if ($limit->start_date->gte($start) && $limit->end_date->gte($end)) {
// return days in the range $limit_start - $end
return (int)$limit->start_date->diffInDays($end, true) + 1; // add one day, the day itself
return (int) $limit->start_date->diffInDays($end, true) + 1; // add one day, the day itself
}
return 0;
@@ -220,7 +220,7 @@ class BudgetRepository implements BudgetRepositoryInterface
app('log')->debug(sprintf('Budget limit #%d', $limit->id));
$currency = $limit->transactionCurrency;
$return[$currency->id] ??= [
'id' => (string)$currency->id,
'id' => (string) $currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
@@ -243,12 +243,12 @@ class BudgetRepository implements BudgetRepositoryInterface
}
$total = $limit->start_date->diffInDays($limit->end_date) + 1; // include the day itself.
$days = $this->daysInOverlap($limit, $start, $end);
$amount = bcmul(bcdiv($limit->amount, (string)$total), (string)$days);
$amount = bcmul(bcdiv($limit->amount, (string) $total), (string) $days);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $amount);
app('log')->debug(
sprintf(
'Amount per day: %s (%s over %d days). Total amount for %d days: %s',
bcdiv($limit->amount, (string)$total),
bcdiv($limit->amount, (string) $total),
$limit->amount,
$total,
$days,
@@ -297,7 +297,7 @@ class BudgetRepository implements BudgetRepositoryInterface
$budget->active = $data['active'];
}
if (array_key_exists('notes', $data)) {
$this->setNoteText($budget, (string)$data['notes']);
$this->setNoteText($budget, (string) $data['notes']);
}
$budget->save();
@@ -408,8 +408,8 @@ class BudgetRepository implements BudgetRepositoryInterface
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
/** @var CurrencyRepositoryInterface $repos */
$repos = app(CurrencyRepositoryInterface::class);
$currencyId = (int)($data['currency_id'] ?? 0);
$currencyCode = (string)($data['currency_code'] ?? '');
$currencyId = (int) ($data['currency_id'] ?? 0);
$currencyCode = (string) ($data['currency_code'] ?? '');
$currency = $repos->find($currencyId);
if (null === $currency) {
$currency = $repos->findByCode($currencyCode);
@@ -463,8 +463,8 @@ class BudgetRepository implements BudgetRepositoryInterface
foreach ($budgets as $budget) {
\DB::table('budget_transaction')->where('budget_id', $budget->id)->delete();
\DB::table('budget_transaction_journal')->where('budget_id', $budget->id)->delete();
RecurrenceTransactionMeta::where('name', 'budget_id')->where('value', (string)$budget->id)->delete();
RuleAction::where('action_type', 'set_budget')->where('action_value', (string)$budget->id)->delete();
RecurrenceTransactionMeta::where('name', 'budget_id')->where('value', (string) $budget->id)->delete();
RuleAction::where('action_type', 'set_budget')->where('action_value', (string) $budget->id)->delete();
$budget->delete();
}
Log::channel('audit')->info('Delete all budgets through destroyAll');
@@ -489,7 +489,7 @@ class BudgetRepository implements BudgetRepositoryInterface
{
app('log')->debug('Now in findBudget()');
app('log')->debug(sprintf('Searching for budget with ID #%d...', $budgetId));
$result = $this->find((int)$budgetId);
$result = $this->find((int) $budgetId);
if (null === $result && null !== $budgetName && '' !== $budgetName) {
app('log')->debug(sprintf('Searching for budget with name %s...', $budgetName));
$result = $this->findByName($budgetName);
@@ -625,9 +625,9 @@ class BudgetRepository implements BudgetRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'id' => (string)$currencyId,
'id' => (string) $currencyId,
'name' => $journal['currency_name'],
'symbol' => $journal['currency_symbol'],
'code' => $journal['currency_code'],
@@ -637,10 +637,10 @@ class BudgetRepository implements BudgetRepositoryInterface
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
// also do foreign amount:
$foreignId = (int)$journal['foreign_currency_id'];
$foreignId = (int) $journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] ??= [
'id' => (string)$foreignId,
'id' => (string) $foreignId,
'name' => $journal['foreign_currency_name'],
'symbol' => $journal['foreign_currency_symbol'],
'code' => $journal['foreign_currency_code'],
@@ -687,9 +687,9 @@ class BudgetRepository implements BudgetRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'id' => (string)$currencyId,
'id' => (string) $currencyId,
'name' => $journal['currency_name'],
'symbol' => $journal['currency_symbol'],
'code' => $journal['currency_code'],
@@ -699,10 +699,10 @@ class BudgetRepository implements BudgetRepositoryInterface
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
// also do foreign amount:
$foreignId = (int)$journal['foreign_currency_id'];
$foreignId = (int) $journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] ??= [
'id' => (string)$foreignId,
'id' => (string) $foreignId,
'name' => $journal['foreign_currency_name'],
'symbol' => $journal['foreign_currency_symbol'],
'code' => $journal['foreign_currency_code'],
@@ -744,7 +744,7 @@ class BudgetRepository implements BudgetRepositoryInterface
// set notes
if (array_key_exists('notes', $data)) {
$this->setNoteText($newBudget, (string)$data['notes']);
$this->setNoteText($newBudget, (string) $data['notes']);
}
if (!array_key_exists('auto_budget_type', $data) || !array_key_exists('auto_budget_amount', $data) || !array_key_exists('auto_budget_period', $data)) {
@@ -772,10 +772,10 @@ class BudgetRepository implements BudgetRepositoryInterface
$repos = app(CurrencyRepositoryInterface::class);
$currency = null;
if (array_key_exists('currency_id', $data)) {
$currency = $repos->find((int)$data['currency_id']);
$currency = $repos->find((int) $data['currency_id']);
}
if (array_key_exists('currency_code', $data)) {
$currency = $repos->findByCode((string)$data['currency_code']);
$currency = $repos->findByCode((string) $data['currency_code']);
}
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
@@ -811,6 +811,6 @@ class BudgetRepository implements BudgetRepositoryInterface
public function getMaxOrder(): int
{
return (int)$this->user->budgets()->max('order');
return (int) $this->user->budgets()->max('order');
}
}

View File

@@ -55,7 +55,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
/** @var array $journal */
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$data[$currencyId] ??= [
'id' => 0,
@@ -115,7 +115,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
/** @var TransactionCurrency $currency */
$currency = $currencies[$code];
$return[] = [
'currency_id' => (string)$currency['id'],
'currency_id' => (string) $currency['id'],
'currency_code' => $code,
'currency_name' => $currency['name'],
'currency_symbol' => $currency['symbol'],
@@ -156,7 +156,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => $currencyId,

View File

@@ -52,17 +52,17 @@ class OperationsRepository implements OperationsRepositoryInterface
$total = '0';
$count = 0;
foreach ($budget->budgetlimits as $limit) {
$diff = (int)$limit->start_date->diffInDays($limit->end_date, true);
$diff = (int) $limit->start_date->diffInDays($limit->end_date, true);
$diff = 0 === $diff ? 1 : $diff;
$amount = $limit->amount;
$perDay = bcdiv($amount, (string)$diff);
$perDay = bcdiv($amount, (string) $diff);
$total = bcadd($total, $perDay);
++$count;
app('log')->debug(sprintf('Found %d budget limits. Per day is %s, total is %s', $count, $perDay, $total));
}
$avg = $total;
if ($count > 0) {
$avg = bcdiv($total, (string)$count);
$avg = bcdiv($total, (string) $count);
}
app('log')->debug(sprintf('%s / %d = %s = average.', $total, $count, $avg));
@@ -91,9 +91,9 @@ class OperationsRepository implements OperationsRepositoryInterface
/** @var array $journal */
foreach ($journals as $journal) {
// prep data array for currency:
$budgetId = (int)$journal['budget_id'];
$budgetId = (int) $journal['budget_id'];
$budgetName = $journal['budget_name'];
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$key = sprintf('%d-%d', $budgetId, $currencyId);
$data[$key] ??= [
@@ -138,9 +138,9 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$budgetId = (int)$journal['budget_id'];
$budgetName = (string)$journal['budget_name'];
$currencyId = (int) $journal['currency_id'];
$budgetId = (int) $journal['budget_id'];
$budgetName = (string) $journal['budget_name'];
// catch "no category" entries.
if (0 === $budgetId) {
@@ -166,7 +166,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['budgets'][$budgetId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->negative($journal['amount']),
'destination_account_id' => $journal['destination_account_id'],
@@ -269,7 +269,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => $currencyId,
@@ -281,7 +281,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
// also do foreign amount:
$foreignId = (int)$journal['foreign_currency_id'];
$foreignId = (int) $journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] ??= [
'sum' => '0',

View File

@@ -107,11 +107,11 @@ class CategoryRepository implements CategoryRepositoryInterface
{
app('log')->debug('Now in findCategory()');
app('log')->debug(sprintf('Searching for category with ID #%d...', $categoryId));
$result = $this->find((int)$categoryId);
$result = $this->find((int) $categoryId);
if (null === $result) {
app('log')->debug(sprintf('Searching for category with name %s...', $categoryName));
$result = $this->findByName((string)$categoryName);
if (null === $result && '' !== (string)$categoryName) {
$result = $this->findByName((string) $categoryName);
if (null === $result && '' !== (string) $categoryName) {
// create it!
$result = $this->store(['name' => $categoryName]);
}

View File

@@ -55,7 +55,7 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'categories' => [],
'currency_id' => $currencyId,
@@ -67,13 +67,13 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
// info about the non-existent category:
$array[$currencyId]['categories'][0] ??= [
'id' => 0,
'name' => (string)trans('firefly.noCategory'),
'name' => (string) trans('firefly.noCategory'),
'transaction_journals' => [],
];
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][0]['transaction_journals'][$journalId]
= [
'amount' => app('steam')->negative($journal['amount']),
@@ -108,7 +108,7 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'categories' => [],
'currency_id' => $currencyId,
@@ -121,12 +121,12 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
// info about the non-existent category:
$array[$currencyId]['categories'][0] ??= [
'id' => 0,
'name' => (string)trans('firefly.noCategory'),
'name' => (string) trans('firefly.noCategory'),
'transaction_journals' => [],
];
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][0]['transaction_journals'][$journalId]
= [
'amount' => app('steam')->positive($journal['amount']),
@@ -153,7 +153,7 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => $currencyId,
@@ -184,7 +184,7 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => $currencyId,
@@ -212,7 +212,7 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => $currencyId,

View File

@@ -64,9 +64,9 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$categoryId = (int)$journal['category_id'];
$categoryName = (string)$journal['category_name'];
$currencyId = (int) $journal['currency_id'];
$categoryId = (int) $journal['category_id'];
$categoryName = (string) $journal['category_name'];
// catch "no category" entries.
if (0 === $categoryId) {
@@ -76,7 +76,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the currency:
$array[$currencyId] ??= [
'categories' => [],
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
@@ -85,24 +85,24 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the categories:
$array[$currencyId]['categories'][$categoryId] ??= [
'id' => (string)$categoryId,
'id' => (string) $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][$categoryId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->negative($journal['amount']),
'date' => $journal['date'],
'source_account_id' => (string)$journal['source_account_id'],
'source_account_id' => (string) $journal['source_account_id'],
'budget_name' => $journal['budget_name'],
'source_account_name' => $journal['source_account_name'],
'destination_account_id' => (string)$journal['destination_account_id'],
'destination_account_id' => (string) $journal['destination_account_id'],
'destination_account_name' => $journal['destination_account_name'],
'description' => $journal['description'],
'transaction_group_id' => (string)$journal['transaction_group_id'],
'transaction_group_id' => (string) $journal['transaction_group_id'],
];
}
@@ -148,19 +148,19 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$categoryId = (int)$journal['category_id'];
$categoryName = (string)$journal['category_name'];
$currencyId = (int) $journal['currency_id'];
$categoryId = (int) $journal['category_id'];
$categoryName = (string) $journal['category_name'];
// catch "no category" entries.
if (0 === $categoryId) {
$categoryName = (string)trans('firefly.no_category');
$categoryName = (string) trans('firefly.no_category');
}
// info about the currency:
$array[$currencyId] ??= [
'categories' => [],
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
@@ -169,23 +169,23 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the categories:
$array[$currencyId]['categories'][$categoryId] ??= [
'id' => (string)$categoryId,
'id' => (string) $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][$categoryId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->positive($journal['amount']),
'date' => $journal['date'],
'source_account_id' => (string)$journal['source_account_id'],
'destination_account_id' => (string)$journal['destination_account_id'],
'source_account_id' => (string) $journal['source_account_id'],
'destination_account_id' => (string) $journal['destination_account_id'],
'source_account_name' => $journal['source_account_name'],
'destination_account_name' => $journal['destination_account_name'],
'description' => $journal['description'],
'transaction_group_id' => (string)$journal['transaction_group_id'],
'transaction_group_id' => (string) $journal['transaction_group_id'],
];
}
@@ -210,9 +210,9 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$categoryId = (int)$journal['category_id'];
$categoryName = (string)$journal['category_name'];
$currencyId = (int) $journal['currency_id'];
$categoryId = (int) $journal['category_id'];
$categoryName = (string) $journal['category_name'];
// catch "no category" entries.
if (0 === $categoryId) {
@@ -222,7 +222,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the currency:
$array[$currencyId] ??= [
'categories' => [],
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
@@ -231,24 +231,24 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the categories:
$array[$currencyId]['categories'][$categoryId] ??= [
'id' => (string)$categoryId,
'id' => (string) $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][$categoryId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->positive($journal['amount']),
'date' => $journal['date'],
'source_account_id' => (string)$journal['source_account_id'],
'source_account_id' => (string) $journal['source_account_id'],
'category_name' => $journal['category_name'],
'source_account_name' => $journal['source_account_name'],
'destination_account_id' => (string)$journal['destination_account_id'],
'destination_account_id' => (string) $journal['destination_account_id'],
'destination_account_name' => $journal['destination_account_name'],
'description' => $journal['description'],
'transaction_group_id' => (string)$journal['transaction_group_id'],
'transaction_group_id' => (string) $journal['transaction_group_id'],
];
}
@@ -273,9 +273,9 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$categoryId = (int)$journal['category_id'];
$categoryName = (string)$journal['category_name'];
$currencyId = (int) $journal['currency_id'];
$categoryId = (int) $journal['category_id'];
$categoryName = (string) $journal['category_name'];
// catch "no category" entries.
if (0 === $categoryId) {
@@ -285,7 +285,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the currency:
$array[$currencyId] ??= [
'categories' => [],
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
@@ -294,24 +294,24 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the categories:
$array[$currencyId]['categories'][$categoryId] ??= [
'id' => (string)$categoryId,
'id' => (string) $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['categories'][$categoryId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->negative($journal['amount']),
'date' => $journal['date'],
'source_account_id' => (string)$journal['source_account_id'],
'source_account_id' => (string) $journal['source_account_id'],
'category_name' => $journal['category_name'],
'source_account_name' => $journal['source_account_name'],
'destination_account_id' => (string)$journal['destination_account_id'],
'destination_account_id' => (string) $journal['destination_account_id'],
'destination_account_name' => $journal['destination_account_name'],
'description' => $journal['description'],
'transaction_group_id' => (string)$journal['transaction_group_id'],
'transaction_group_id' => (string) $journal['transaction_group_id'],
];
}
@@ -341,14 +341,14 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => (int)$journal['currency_decimal_places'],
'currency_decimal_places' => (int) $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
}
@@ -378,10 +378,10 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
@@ -415,10 +415,10 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'sum' => '0',
'currency_id' => (string)$currencyId,
'currency_id' => (string) $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],

View File

@@ -37,6 +37,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface
{
private User $user;
#[\Override]
public function find(int $currencyId): ?TransactionCurrency
{
return TransactionCurrency::find($currencyId);
}
/**
* Find by currency code, return NULL if unfound.
*/
@@ -89,13 +95,13 @@ class CurrencyRepository implements CurrencyRepositoryInterface
{
return CurrencyExchangeRate::create(
[
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
'from_currency_id' => $fromCurrency->id,
'to_currency_id' => $toCurrency->id,
'date' => $date,
'date_tz' => $date->format('e'),
'rate' => $rate,
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
'from_currency_id' => $fromCurrency->id,
'to_currency_id' => $toCurrency->id,
'date' => $date,
'date_tz' => $date->format('e'),
'rate' => $rate,
]
);
}
@@ -106,10 +112,4 @@ class CurrencyRepository implements CurrencyRepositoryInterface
$this->user = $user;
}
}
#[\Override]
public function find(int $currencyId): ?TransactionCurrency
{
return TransactionCurrency::find($currencyId);
}
}

View File

@@ -35,6 +35,8 @@ use Illuminate\Support\Collection;
*/
interface CurrencyRepositoryInterface
{
public function find(int $currencyId): ?TransactionCurrency;
/**
* Find by currency code, return NULL if unfound.
*
@@ -42,8 +44,6 @@ interface CurrencyRepositoryInterface
*/
public function findByCode(string $currencyCode): ?TransactionCurrency;
public function find(int $currencyId): ?TransactionCurrency;
/**
* Returns the complete set of transactions but needs
* no user object.

View File

@@ -143,7 +143,7 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
}
// return when something else:
$return = (string)$value;
$return = (string) $value;
$cache->store($return);
return $return;
@@ -176,8 +176,8 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
/** @var \stdClass $row */
foreach ($result as $row) {
if ((int)$row->transaction_count > 2) {
$journalIds[] = (int)$row->id;
if ((int) $row->transaction_count > 2) {
$journalIds[] = (int) $row->id;
}
}
$journalIds = array_unique($journalIds);

View File

@@ -111,7 +111,7 @@ class JournalRepository implements JournalRepositoryInterface
// saves on queries:
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
$amount = (string)$amount;
$amount = (string) $amount;
$cache->store($amount);
return $amount;
@@ -134,7 +134,7 @@ class JournalRepository implements JournalRepositoryInterface
/** @var null|Note $note */
$note = $link->notes()->first();
return (string)$note?->text;
return (string) $note?->text;
}
/**

View File

@@ -56,13 +56,13 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
public function update(LinkType $linkType, array $data): LinkType
{
if (array_key_exists('name', $data) && '' !== (string)$data['name']) {
if (array_key_exists('name', $data) && '' !== (string) $data['name']) {
$linkType->name = $data['name'];
}
if (array_key_exists('inward', $data) && '' !== (string)$data['inward']) {
if (array_key_exists('inward', $data) && '' !== (string) $data['inward']) {
$linkType->inward = $data['inward'];
}
if (array_key_exists('outward', $data) && '' !== (string)$data['outward']) {
if (array_key_exists('outward', $data) && '' !== (string) $data['outward']) {
$linkType->outward = $data['outward'];
}
$linkType->save();
@@ -183,7 +183,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
*/
public function storeLink(array $information, TransactionJournal $inward, TransactionJournal $outward): ?TransactionJournalLink
{
$linkType = $this->find((int)($information['link_type_id'] ?? 0));
$linkType = $this->find((int) ($information['link_type_id'] ?? 0));
if (null === $linkType) {
$linkType = $this->findByName($information['link_type_name']);
@@ -215,7 +215,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
$link->save();
// make note in noteable:
$this->setNoteText($link, (string)$information['notes']);
$this->setNoteText($link, (string) $information['notes']);
return $link;
}

View File

@@ -56,7 +56,7 @@ trait CreatesObjectGroups
protected function getObjectGroupMaxOrder(): int
{
return (int)$this->user->objectGroups()->max('order');
return (int) $this->user->objectGroups()->max('order');
}
protected function hasObjectGroup(string $title): bool

View File

@@ -141,7 +141,7 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
}
if (array_key_exists('order', $data)) {
$this->setOrder($objectGroup, (int)$data['order']);
$this->setOrder($objectGroup, (int) $data['order']);
}
$objectGroup->save();

View File

@@ -80,24 +80,6 @@ trait ModifiesPiggyBanks
return true;
}
public function removeAmountFromAll(PiggyBank $piggyBank, string $amount): void
{
foreach ($piggyBank->accounts as $account) {
$current = $account->pivot->current_amount;
// if this account contains more than the amount, remove the amount and return.
if (1 === bccomp($current, $amount)) {
$this->removeAmount($piggyBank, $account, $amount);
return;
}
// if this account contains less than the amount, remove the current amount, update the amount and continue.
if (bccomp($current, $amount) < 1) {
$this->removeAmount($piggyBank, $account, $current);
$amount = bcsub($amount, $current);
}
}
}
public function addAmount(PiggyBank $piggyBank, Account $account, string $amount, ?TransactionJournal $journal = null): bool
{
$currentAmount = $this->getCurrentAmount($piggyBank, $account);
@@ -220,57 +202,6 @@ trait ModifiesPiggyBanks
return $factory->store($data);
}
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool
{
$oldOrder = $piggyBank->order;
// Log::debug(sprintf('Will move piggy bank #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
if ($newOrder > $oldOrder) {
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id)
->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->distinct()->decrement('piggy_banks.order')
;
$piggyBank->order = $newOrder;
Log::debug(sprintf('[1] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
$piggyBank->save();
return true;
}
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id)
->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->distinct()->increment('piggy_banks.order')
;
$piggyBank->order = $newOrder;
Log::debug(sprintf('[2] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
$piggyBank->save();
return true;
}
public function updateNote(PiggyBank $piggyBank, string $note): void
{
if ('' === $note) {
$dbNote = $piggyBank->notes()->first();
$dbNote?->delete();
return;
}
$dbNote = $piggyBank->notes()->first();
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($piggyBank);
}
$dbNote->text = trim($note);
$dbNote->save();
}
public function update(PiggyBank $piggyBank, array $data): PiggyBank
{
$piggyBank = $this->updateProperties($piggyBank, $data);
@@ -363,4 +294,73 @@ trait ModifiesPiggyBanks
return $piggyBank;
}
public function updateNote(PiggyBank $piggyBank, string $note): void
{
if ('' === $note) {
$dbNote = $piggyBank->notes()->first();
$dbNote?->delete();
return;
}
$dbNote = $piggyBank->notes()->first();
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($piggyBank);
}
$dbNote->text = trim($note);
$dbNote->save();
}
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool
{
$oldOrder = $piggyBank->order;
// Log::debug(sprintf('Will move piggy bank #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
if ($newOrder > $oldOrder) {
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id)
->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->distinct()->decrement('piggy_banks.order')
;
$piggyBank->order = $newOrder;
Log::debug(sprintf('[1] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
$piggyBank->save();
return true;
}
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id)
->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->distinct()->increment('piggy_banks.order')
;
$piggyBank->order = $newOrder;
Log::debug(sprintf('[2] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
$piggyBank->save();
return true;
}
public function removeAmountFromAll(PiggyBank $piggyBank, string $amount): void
{
foreach ($piggyBank->accounts as $account) {
$current = $account->pivot->current_amount;
// if this account contains more than the amount, remove the amount and return.
if (1 === bccomp($current, $amount)) {
$this->removeAmount($piggyBank, $account, $amount);
return;
}
// if this account contains less than the amount, remove the current amount, update the amount and continue.
if (bccomp($current, $amount) < 1) {
$this->removeAmount($piggyBank, $account, $current);
$amount = bcsub($amount, $current);
}
}
}
}

View File

@@ -119,35 +119,6 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
);
}
/**
* Get current amount saved in piggy bank.
*/
public function getCurrentAmount(PiggyBank $piggyBank, ?Account $account = null): string
{
$sum = '0';
foreach ($piggyBank->accounts as $current) {
if (null !== $account && $account->id !== $current->id) {
continue;
}
$amount = (string) $current->pivot->current_amount;
$amount = '' === $amount ? '0' : $amount;
$sum = bcadd($sum, $amount);
}
Log::debug(sprintf('Current amount in piggy bank #%d ("%s") is %s', $piggyBank->id, $piggyBank->name, $sum));
return $sum;
}
public function getRepetition(PiggyBank $piggyBank, bool $overrule = false): ?PiggyBankRepetition
{
if (false === $overrule) {
throw new FireflyException('[b] Piggy bank repetitions are EOL.');
}
Log::warning('Piggy bank repetitions are EOL.');
return $piggyBank->piggyBankRepetitions()->first();
}
public function getEvents(PiggyBank $piggyBank): Collection
{
return $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
@@ -302,6 +273,35 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
;
}
/**
* Get current amount saved in piggy bank.
*/
public function getCurrentAmount(PiggyBank $piggyBank, ?Account $account = null): string
{
$sum = '0';
foreach ($piggyBank->accounts as $current) {
if (null !== $account && $account->id !== $current->id) {
continue;
}
$amount = (string) $current->pivot->current_amount;
$amount = '' === $amount ? '0' : $amount;
$sum = bcadd($sum, $amount);
}
Log::debug(sprintf('Current amount in piggy bank #%d ("%s") is %s', $piggyBank->id, $piggyBank->name, $sum));
return $sum;
}
public function getRepetition(PiggyBank $piggyBank, bool $overrule = false): ?PiggyBankRepetition
{
if (false === $overrule) {
throw new FireflyException('[b] Piggy bank repetitions are EOL.');
}
Log::warning('Piggy bank repetitions are EOL.');
return $piggyBank->piggyBankRepetitions()->first();
}
/**
* Returns the suggested amount the user should save per month, or "".
*/
@@ -352,28 +352,6 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $balance;
}
public function searchPiggyBank(string $query, int $limit): Collection
{
$search = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id)
->with(
[
'objectGroups',
]
)
->orderBy('piggy_banks.order', 'ASC')->distinct()
;
if ('' !== $query) {
$search->whereLike('piggy_banks.name', sprintf('%%%s%%', $query));
}
$search->orderBy('piggy_banks.order', 'ASC')
->orderBy('piggy_banks.name', 'ASC')
;
return $search->take($limit)->get(['piggy_banks.*']);
}
#[\Override]
public function purgeAll(): void
{
@@ -398,4 +376,26 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
$factory->user = $this->user;
$factory->resetOrder();
}
public function searchPiggyBank(string $query, int $limit): Collection
{
$search = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', $this->user->id)
->with(
[
'objectGroups',
]
)
->orderBy('piggy_banks.order', 'ASC')->distinct()
;
if ('' !== $query) {
$search->whereLike('piggy_banks.name', sprintf('%%%s%%', $query));
}
$search->orderBy('piggy_banks.order', 'ASC')
->orderBy('piggy_banks.name', 'ASC')
;
return $search->take($limit)->get(['piggy_banks.*']);
}
}

View File

@@ -53,8 +53,6 @@ interface PiggyBankRepositoryInterface
public function destroyAll(): void;
public function purgeAll(): void;
public function find(int $piggyBankId): ?PiggyBank;
/**
@@ -70,22 +68,20 @@ interface PiggyBankRepositoryInterface
* Get current amount saved in piggy bank.
*/
public function getCurrentAmount(PiggyBank $piggyBank, ?Account $account = null): string;
/**
* Get current amount saved in piggy bank.
*/
/**
* Get all events.
*/
public function getEvents(PiggyBank $piggyBank): Collection;
/**
* Get current amount saved in piggy bank.
*/
/**
* Used for connecting to a piggy bank.
*/
public function getExactAmount(PiggyBank $piggyBank, PiggyBankRepetition $repetition, TransactionJournal $journal): string;
public function updateNote(PiggyBank $piggyBank, string $note): void;
/**
* Return note for piggy bank.
*/
@@ -113,6 +109,8 @@ interface PiggyBankRepositoryInterface
*/
public function leftOnAccount(PiggyBank $piggyBank, Account $account, Carbon $date): string;
public function purgeAll(): void;
public function removeAmount(PiggyBank $piggyBank, Account $account, string $amount, ?TransactionJournal $journal = null): bool;
public function removeAmountFromAll(PiggyBank $piggyBank, string $amount): void;
@@ -148,4 +146,6 @@ interface PiggyBankRepositoryInterface
* Update existing piggy bank.
*/
public function update(PiggyBank $piggyBank, array $data): PiggyBank;
public function updateNote(PiggyBank $piggyBank, string $note): void;
}

View File

@@ -61,7 +61,7 @@ class RuleRepository implements RuleRepositoryInterface
public function duplicate(Rule $rule): Rule
{
$newRule = $rule->replicate();
$newRule->title = (string)trans('firefly.rule_copy_of', ['title' => $rule->title]);
$newRule->title = (string) trans('firefly.rule_copy_of', ['title' => $rule->title]);
$newRule->save();
// replicate all triggers
@@ -101,7 +101,7 @@ class RuleRepository implements RuleRepositoryInterface
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup): int
{
return (int)$ruleGroup->rules()->max('order');
return (int) $ruleGroup->rules()->max('order');
}
/**
@@ -352,7 +352,7 @@ class RuleRepository implements RuleRepositoryInterface
public function maxOrder(RuleGroup $ruleGroup): int
{
return (int)$ruleGroup->rules()->max('order');
return (int) $ruleGroup->rules()->max('order');
}
private function storeTriggers(Rule $rule, array $data): void
@@ -477,7 +477,7 @@ class RuleRepository implements RuleRepositoryInterface
// update the order:
$this->resetRuleOrder($group);
if (array_key_exists('order', $data)) {
$this->moveRule($rule, $group, (int)$data['order']);
$this->moveRule($rule, $group, (int) $data['order']);
}
// update the triggers:

View File

@@ -303,7 +303,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
{
$entry = $this->user->ruleGroups()->max('order');
return (int)$entry;
return (int) $entry;
}
public function getRuleGroupsWithRules(?string $filter): Collection
@@ -364,7 +364,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
public function maxOrder(): int
{
return (int)$this->user->ruleGroups()->where('active', true)->max('order');
return (int) $this->user->ruleGroups()->where('active', true)->max('order');
}
public function searchRuleGroup(string $query, int $limit): Collection
@@ -448,7 +448,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
// order
if (array_key_exists('order', $data) && $ruleGroup->order !== $data['order']) {
$this->resetOrder();
$this->setOrder($ruleGroup, (int)$data['order']);
$this->setOrder($ruleGroup, (int) $data['order']);
}
$ruleGroup->save();

View File

@@ -66,7 +66,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
$listedJournals = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'tags' => [],
'currency_id' => $currencyId,
@@ -78,9 +78,9 @@ class OperationsRepository implements OperationsRepositoryInterface
// may have multiple tags:
foreach ($journal['tags'] as $tag) {
$tagId = (int)$tag['id'];
$tagName = (string)$tag['name'];
$journalId = (int)$journal['transaction_journal_id'];
$tagId = (int) $tag['id'];
$tagName = (string) $tag['name'];
$journalId = (int) $journal['transaction_journal_id'];
if (!in_array($tagId, $tagIds, true)) {
continue;
}
@@ -157,7 +157,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$listedJournals = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$array[$currencyId] ??= [
'tags' => [],
'currency_id' => $currencyId,
@@ -169,9 +169,9 @@ class OperationsRepository implements OperationsRepositoryInterface
// may have multiple tags:
foreach ($journal['tags'] as $tag) {
$tagId = (int)$tag['id'];
$tagName = (string)$tag['name'];
$journalId = (int)$journal['transaction_journal_id'];
$tagId = (int) $tag['id'];
$tagName = (string) $tag['name'];
$journalId = (int) $journal['transaction_journal_id'];
if (!in_array($tagId, $tagIds, true)) {
continue;
@@ -187,7 +187,7 @@ class OperationsRepository implements OperationsRepositoryInterface
'name' => $tagName,
'transaction_journals' => [],
];
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$array[$currencyId]['tags'][$tagId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->positive($journal['amount']),
'date' => $journal['date'],

View File

@@ -259,7 +259,7 @@ class TagRepository implements TagRepositoryInterface
if (false === $found) {
continue;
}
$currencyId = (int)$journal['currency_id'];
$currencyId = (int) $journal['currency_id'];
$sums[$currencyId] ??= [
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
@@ -273,7 +273,7 @@ class TagRepository implements TagRepositoryInterface
];
// add amount to correct type:
$amount = app('steam')->positive((string)$journal['amount']);
$amount = app('steam')->positive((string) $journal['amount']);
$type = $journal['transaction_type_type'];
if (TransactionType::WITHDRAWAL === $type) {
$amount = bcmul($amount, '-1');
@@ -294,7 +294,7 @@ class TagRepository implements TagRepositoryInterface
TransactionType::OPENING_BALANCE => '0',
];
// add foreign amount to correct type:
$amount = app('steam')->positive((string)$journal['foreign_amount']);
$amount = app('steam')->positive((string) $journal['foreign_amount']);
if (TransactionType::WITHDRAWAL === $type) {
$amount = bcmul($amount, '-1');
}

View File

@@ -219,7 +219,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
'link' => $entry->outward,
'group' => $entry->destination->transaction_group_id,
'description' => $entry->destination->description,
'editable' => 1 === (int)$entry->editable, // @phpstan-ignore-line
'editable' => 1 === (int) $entry->editable, // @phpstan-ignore-line
'amount' => $amount,
'foreign_amount' => $foreignAmount,
];
@@ -232,7 +232,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
'link' => $entry->inward,
'group' => $entry->source->transaction_group_id,
'description' => $entry->source->description,
'editable' => 1 === (int)$entry->editable, // @phpstan-ignore-line
'editable' => 1 === (int) $entry->editable, // @phpstan-ignore-line
'amount' => $amount,
'foreign_amount' => $foreignAmount,
];

View File

@@ -201,7 +201,7 @@ class UserRepository implements UserRepositoryInterface
// two factor:
$return['has_2fa'] = null !== $user->mfa_secret;
$return['is_admin'] = $this->hasRole($user, 'owner');
$return['blocked'] = 1 === (int)$user->blocked;
$return['blocked'] = 1 === (int) $user->blocked;
$return['blocked_code'] = $user->blocked_code;
$return['accounts'] = $user->accounts()->count();
$return['journals'] = $user->transactionJournals()->count();

View File

@@ -174,6 +174,18 @@ class UserGroupRepository implements UserGroupRepositoryInterface
return UserGroup::all();
}
#[\Override]
public function getById(int $id): ?UserGroup
{
return UserGroup::find($id);
}
#[\Override]
public function getMembershipsFromGroupId(int $groupId): Collection
{
return $this->user->groupMemberships()->where('user_group_id', $groupId)->get();
}
public function setUser(null|Authenticatable|User $user): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
@@ -207,7 +219,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface
$user = User::find($data['id']);
app('log')->debug('Found user by ID');
}
if (array_key_exists('email', $data) && '' !== (string)$data['email']) {
if (array_key_exists('email', $data) && '' !== (string) $data['email']) {
/** @var null|User $user */
$user = User::whereEmail($data['email'])->first();
app('log')->debug('Found user by email');
@@ -225,13 +237,13 @@ class UserGroupRepository implements UserGroupRepositoryInterface
if (1 === $membershipCount) {
$lastUserId = $userGroup->groupMemberships()->distinct()->first(['group_memberships.user_id'])->user_id;
// if this is also the user we're editing right now, and we remove all of their roles:
if ($lastUserId === (int)$user->id && 0 === count($data['roles'])) {
if ($lastUserId === (int) $user->id && 0 === count($data['roles'])) {
app('log')->debug('User is last in this group, refuse to act');
throw new FireflyException('You cannot remove the last member from this user group. Delete the user group instead.');
}
// if this is also the user we're editing right now, and do not grant them the owner role:
if ($lastUserId === (int)$user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) {
if ($lastUserId === (int) $user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) {
app('log')->debug('User needs to have owner role in this group, refuse to act');
throw new FireflyException('The last member in this user group must get or keep the "owner" role.');
@@ -295,16 +307,4 @@ class UserGroupRepository implements UserGroupRepositoryInterface
$this->user->user_group_id = $userGroup->id;
$this->user->save();
}
#[\Override]
public function getMembershipsFromGroupId(int $groupId): Collection
{
return $this->user->groupMemberships()->where('user_group_id', $groupId)->get();
}
#[\Override]
public function getById(int $id): ?UserGroup
{
return UserGroup::find($id);
}
}

View File

@@ -36,13 +36,13 @@ interface UserGroupRepositoryInterface
{
public function destroy(UserGroup $userGroup): void;
public function getMembershipsFromGroupId(int $groupId): Collection;
public function get(): Collection;
public function getAll(): Collection;
public function getById(int $id): ?UserGroup;
public function getAll(): Collection;
public function getMembershipsFromGroupId(int $groupId): Collection;
public function setUser(null|Authenticatable|User $user): void;

View File

@@ -117,6 +117,12 @@ class AccountRepository implements AccountRepositoryInterface
return $account;
}
#[\Override]
public function getAccountBalances(Account $account): Collection
{
return $account->accountBalances;
}
public function getAccountCurrency(Account $account): ?TransactionCurrency
{
$type = $account->accountType->type;
@@ -164,6 +170,15 @@ class AccountRepository implements AccountRepositoryInterface
return $account;
}
#[\Override]
public function getAccountTypes(Collection $accounts): Collection
{
return AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id')
->whereIn('accounts.id', $accounts->pluck('id')->toArray())
->get(['accounts.id', 'account_types.type'])
;
}
public function getAccountsById(array $accountIds): Collection
{
$query = $this->userGroup->accounts();
@@ -218,6 +233,57 @@ class AccountRepository implements AccountRepositoryInterface
return $query->get(['accounts.*']);
}
#[\Override]
public function getLastActivity(Collection $accounts): array
{
return Transaction::whereIn('account_id', $accounts->pluck('id')->toArray())
->leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
->groupBy('transactions.account_id')
->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) as date_max')])->toArray() // @phpstan-ignore-line
;
}
#[\Override]
public function getMetaValues(Collection $accounts, array $fields): Collection
{
$query = AccountMeta::whereIn('account_id', $accounts->pluck('id')->toArray());
if (count($fields) > 0) {
$query->whereIn('name', $fields);
}
return $query->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data']);
}
#[\Override]
public function getObjectGroups(Collection $accounts): array
{
$groupIds = [];
$return = [];
$set = DB::table('object_groupables')->where('object_groupable_type', Account::class)
->whereIn('object_groupable_id', $accounts->pluck('id')->toArray())->get()
;
/** @var \stdClass $row */
foreach ($set as $row) {
$groupIds[] = $row->object_group_id;
}
$groupIds = array_unique($groupIds);
$groups = ObjectGroup::whereIn('id', $groupIds)->get();
/** @var \stdClass $row */
foreach ($set as $row) {
if (!array_key_exists($row->object_groupable_id, $return)) {
/** @var null|ObjectGroup $group */
$group = $groups->firstWhere('id', '=', $row->object_group_id);
if (null !== $group) {
$return[$row->object_groupable_id] = ['title' => $group->title, 'order' => $group->order, 'id' => $group->id];
}
}
}
return $return;
}
public function resetAccountOrder(): void
{
$sets = [
@@ -298,6 +364,15 @@ class AccountRepository implements AccountRepositoryInterface
return $query->get(['accounts.*']);
}
#[\Override]
public function update(Account $account, array $data): Account
{
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
return $service->update($account, $data);
}
public function searchAccount(string $query, array $types, int $page, int $limit): Collection
{
// search by group, not by user
@@ -331,79 +406,4 @@ class AccountRepository implements AccountRepositoryInterface
return $dbQuery->get(['accounts.*']);
}
#[\Override]
public function update(Account $account, array $data): Account
{
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
return $service->update($account, $data);
}
#[\Override]
public function getMetaValues(Collection $accounts, array $fields): Collection
{
$query = AccountMeta::whereIn('account_id', $accounts->pluck('id')->toArray());
if (count($fields) > 0) {
$query->whereIn('name', $fields);
}
return $query->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data']);
}
#[\Override]
public function getAccountTypes(Collection $accounts): Collection
{
return AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id')
->whereIn('accounts.id', $accounts->pluck('id')->toArray())
->get(['accounts.id', 'account_types.type'])
;
}
#[\Override]
public function getLastActivity(Collection $accounts): array
{
return Transaction::whereIn('account_id', $accounts->pluck('id')->toArray())
->leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
->groupBy('transactions.account_id')
->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) as date_max')])->toArray() // @phpstan-ignore-line
;
}
#[\Override]
public function getObjectGroups(Collection $accounts): array
{
$groupIds = [];
$return = [];
$set = DB::table('object_groupables')->where('object_groupable_type', Account::class)
->whereIn('object_groupable_id', $accounts->pluck('id')->toArray())->get()
;
/** @var \stdClass $row */
foreach ($set as $row) {
$groupIds[] = $row->object_group_id;
}
$groupIds = array_unique($groupIds);
$groups = ObjectGroup::whereIn('id', $groupIds)->get();
/** @var \stdClass $row */
foreach ($set as $row) {
if (!array_key_exists($row->object_groupable_id, $return)) {
/** @var null|ObjectGroup $group */
$group = $groups->firstWhere('id', '=', $row->object_group_id);
if (null !== $group) {
$return[$row->object_groupable_id] = ['title' => $group->title, 'order' => $group->order, 'id' => $group->id];
}
}
}
return $return;
}
#[\Override]
public function getAccountBalances(Account $account): Collection
{
return $account->accountBalances;
}
}

View File

@@ -37,12 +37,6 @@ interface AccountRepositoryInterface
{
public function countAccounts(array $types): int;
public function getAccountTypes(Collection $accounts): Collection;
public function getLastActivity(Collection $accounts): array;
public function getMetaValues(Collection $accounts, array $fields): Collection;
public function find(int $accountId): ?Account;
public function findByAccountNumber(string $number, array $types): ?Account;
@@ -51,9 +45,11 @@ interface AccountRepositoryInterface
public function findByName(string $name, array $types): ?Account;
public function getAccountBalances(Account $account): Collection;
public function getAccountCurrency(Account $account): ?TransactionCurrency;
public function getAccountBalances(Account $account): Collection;
public function getAccountTypes(Collection $accounts): Collection;
public function getAccountsById(array $accountIds): Collection;
@@ -66,11 +62,15 @@ interface AccountRepositoryInterface
public function getActiveAccountsByType(array $types): Collection;
public function getLastActivity(Collection $accounts): array;
/**
* Return meta value for account. Null if not found.
*/
public function getMetaValue(Account $account, string $field): ?string;
public function getMetaValues(Collection $accounts, array $fields): Collection;
public function getObjectGroups(Collection $accounts): array;
public function getUserGroup(): UserGroup;

View File

@@ -81,12 +81,12 @@ class BillRepository implements BillRepositoryInterface
$currencyId = $bill->transaction_currency_id;
$return[$currencyId] ??= [
'currency_id' => (string)$currency->id,
'currency_id' => (string) $currency->id,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'currency_decimal_places' => $currency->decimal_places,
'native_currency_id' => (string)$default->id,
'native_currency_id' => (string) $default->id,
'native_currency_name' => $default->name,
'native_currency_symbol' => $default->symbol,
'native_currency_code' => $default->code,
@@ -101,9 +101,9 @@ class BillRepository implements BillRepositoryInterface
$sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first();
if (null !== $sourceTransaction) {
$amount = $sourceTransaction->amount;
if ((int)$sourceTransaction->foreign_currency_id === $currency->id) {
if ((int) $sourceTransaction->foreign_currency_id === $currency->id) {
// use foreign amount instead!
$amount = (string)$sourceTransaction->foreign_amount;
$amount = (string) $sourceTransaction->foreign_amount;
}
// convert to native currency
$nativeAmount = $amount;
@@ -111,9 +111,9 @@ class BillRepository implements BillRepositoryInterface
// get rate and convert.
$nativeAmount = $converter->convert($currency, $default, $transactionJournal->date, $amount);
}
if ((int)$sourceTransaction->foreign_currency_id === $default->id) {
if ((int) $sourceTransaction->foreign_currency_id === $default->id) {
// ignore conversion, use foreign amount
$nativeAmount = (string)$sourceTransaction->foreign_amount;
$nativeAmount = (string) $sourceTransaction->foreign_amount;
}
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], $amount);
$return[$currencyId]['native_sum'] = bcadd($return[$currencyId]['native_sum'], $nativeAmount);
@@ -154,12 +154,12 @@ class BillRepository implements BillRepositoryInterface
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$nativeAverage = $converter->convert($currency, $default, $start, $average);
$return[$currencyId] ??= [
'currency_id' => (string)$currency->id,
'currency_id' => (string) $currency->id,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'currency_decimal_places' => $currency->decimal_places,
'native_currency_id' => (string)$default->id,
'native_currency_id' => (string) $default->id,
'native_currency_name' => $default->name,
'native_currency_symbol' => $default->symbol,
'native_currency_code' => $default->code,
@@ -167,8 +167,8 @@ class BillRepository implements BillRepositoryInterface
'sum' => '0',
'native_sum' => '0',
];
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], bcmul($average, (string)$total));
$return[$currencyId]['native_sum'] = bcadd($return[$currencyId]['native_sum'], bcmul($nativeAverage, (string)$total));
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], bcmul($average, (string) $total));
$return[$currencyId]['native_sum'] = bcadd($return[$currencyId]['native_sum'], bcmul($nativeAverage, (string) $total));
}
}
$converter->summarize();

View File

@@ -60,9 +60,9 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$budgetId = (int)$journal['budget_id'];
$budgetName = (string)$journal['budget_name'];
$currencyId = (int) $journal['currency_id'];
$budgetId = (int) $journal['budget_id'];
$budgetName = (string) $journal['budget_name'];
// catch "no budget" entries.
if (0 === $budgetId) {
@@ -88,7 +88,7 @@ class OperationsRepository implements OperationsRepositoryInterface
// add journal to array:
// only a subset of the fields.
$journalId = (int)$journal['transaction_journal_id'];
$journalId = (int) $journal['transaction_journal_id'];
$final = [
'amount' => app('steam')->negative($journal['amount']),
'currency_id' => $journal['currency_id'],

View File

@@ -308,22 +308,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $currency->code === config('firefly.default_currency', 'EUR');
}
public function makeDefault(TransactionCurrency $currency): void
{
$current = app('amount')->getDefaultCurrencyByUserGroup($this->userGroup);
app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id));
$this->userGroup->currencies()->detach($currency->id);
foreach ($this->userGroup->currencies()->get() as $item) {
$this->userGroup->currencies()->updateExistingPivot($item->id, ['group_default' => false]);
}
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => true]]);
if ($current->id !== $currency->id) {
Log::debug('Trigger on a different default currency.');
// clear all native amounts through an event.
event(new UserGroupChangedDefaultCurrency($this->userGroup));
}
}
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', true);
@@ -388,4 +372,20 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $service->update($currency, $data);
}
public function makeDefault(TransactionCurrency $currency): void
{
$current = app('amount')->getDefaultCurrencyByUserGroup($this->userGroup);
app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id));
$this->userGroup->currencies()->detach($currency->id);
foreach ($this->userGroup->currencies()->get() as $item) {
$this->userGroup->currencies()->updateExistingPivot($item->id, ['group_default' => false]);
}
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => true]]);
if ($current->id !== $currency->id) {
Log::debug('Trigger on a different default currency.');
// clear all native amounts through an event.
event(new UserGroupChangedDefaultCurrency($this->userGroup));
}
}
}

View File

@@ -39,16 +39,19 @@ class ExchangeRateRepository implements ExchangeRateRepositoryInterface
// orderBy('date', 'DESC')->toRawSql();
return
$this->userGroup->currencyExchangeRates()
->where(function (Builder $q1) use ($from, $to) {
$q1->where(function (Builder $q) use ($from, $to) {
$q->where('from_currency_id', $from->id)
->where('to_currency_id', $to->id);
})->orWhere(function (Builder $q) use ($from, $to) {
$q->where('from_currency_id', $to->id)
->where('to_currency_id', $from->id);
});
})
->orderBy('date', 'DESC')->get(['currency_exchange_rates.*']);
->where(function (Builder $q1) use ($from, $to): void {
$q1->where(function (Builder $q) use ($from, $to): void {
$q->where('from_currency_id', $from->id)
->where('to_currency_id', $to->id)
;
})->orWhere(function (Builder $q) use ($from, $to): void {
$q->where('from_currency_id', $to->id)
->where('to_currency_id', $from->id)
;
});
})
->orderBy('date', 'DESC')->get(['currency_exchange_rates.*'])
;
}
}