Various PSR12 code cleanup

This commit is contained in:
James Cole
2022-12-29 19:42:26 +01:00
parent dbf3e76ecc
commit 6cfdc58cb1
415 changed files with 7462 additions and 6874 deletions

View File

@@ -1,4 +1,5 @@
<?php
/**
* AccountRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -52,21 +53,11 @@ class AccountRepository implements AccountRepositoryInterface
{
private User $user;
/**
* @param array $types
*
* @return int
*/
public function count(array $types): int
{
return $this->user->accounts()->accountTypeIn($types)->count();
}
/**
* Moved here from account CRUD.
*
* @param Account $account
* @param Account|null $moveTo
* @param Account $account
* @param Account|null $moveTo
*
* @return bool
*
@@ -84,7 +75,7 @@ class AccountRepository implements AccountRepositoryInterface
/**
* Find account with same name OR same IBAN or both, but not the same type or ID.
*
* @param Collection $accounts
* @param Collection $accounts
*
* @return Collection
*/
@@ -115,16 +106,6 @@ class AccountRepository implements AccountRepositoryInterface
return $result;
}
/**
* @param int $accountId
*
* @return Account|null
*/
public function find(int $accountId): ?Account
{
return $this->user->accounts()->find($accountId);
}
/**
* @inheritDoc
*/
@@ -151,8 +132,8 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param string $iban
* @param array $types
* @param string $iban
* @param array $types
*
* @return Account|null
*/
@@ -169,8 +150,8 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param string $name
* @param array $types
* @param string $name
* @param array $types
*
* @return Account|null
*/
@@ -200,7 +181,7 @@ class AccountRepository implements AccountRepositoryInterface
/**
* Return account type or null if not found.
*
* @param string $type
* @param string $type
*
* @return AccountType|null
*/
@@ -210,7 +191,7 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param array $accountIds
* @param array $accountIds
*
* @return Collection
*/
@@ -229,16 +210,19 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param array $types
* @param array $types
*
* @return Collection
*/
public function getActiveAccountsByType(array $types): Collection
{
$query = $this->user->accounts()->with(
['accountmeta' => function (HasMany $query) {
$query->where('name', 'account_role');
}, 'attachments']
[
'accountmeta' => function (HasMany $query) {
$query->where('name', 'account_role');
},
'attachments',
]
);
if (0 !== count($types)) {
$query->accountTypeIn($types);
@@ -288,15 +272,23 @@ class AccountRepository implements AccountRepositoryInterface
return $factory->findOrCreate('Cash account', $type->type);
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @inheritDoc
*/
public function getCreditTransactionGroup(Account $account): ?TransactionGroup
{
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::LIABILITY_CREDIT])
->first(['transaction_journals.*']);
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::LIABILITY_CREDIT])
->first(['transaction_journals.*']);
if (null === $journal) {
return null;
}
@@ -305,16 +297,18 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param array $types
* @param array $types
*
* @return Collection
*/
public function getInactiveAccountsByType(array $types): Collection
{
$query = $this->user->accounts()->with(
['accountmeta' => function (HasMany $query) {
$query->where('name', 'account_role');
}]
[
'accountmeta' => function (HasMany $query) {
$query->where('name', 'account_role');
},
]
);
if (0 !== count($types)) {
$query->accountTypeIn($types);
@@ -338,7 +332,7 @@ class AccountRepository implements AccountRepositoryInterface
/**
* Get note text or null.
*
* @param Account $account
* @param Account $account
*
* @return null|string
*/
@@ -356,7 +350,7 @@ class AccountRepository implements AccountRepositoryInterface
/**
* Returns the amount of the opening balance for this account.
*
* @param Account $account
* @param Account $account
*
* @return string|null
*/
@@ -374,13 +368,13 @@ class AccountRepository implements AccountRepositoryInterface
return null;
}
return (string) $transaction->amount;
return (string)$transaction->amount;
}
/**
* Return date of opening balance as string or null.
*
* @param Account $account
* @param Account $account
*
* @return null|string
*/
@@ -398,7 +392,7 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param Account $account
* @param Account $account
*
* @return TransactionGroup|null
*/
@@ -410,20 +404,20 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param Account $account
* @param Account $account
*
* @return TransactionJournal|null
*/
public function getOpeningBalance(Account $account): ?TransactionJournal
{
return TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
->where('transactions.account_id', $account->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
}
/**
* @param Account $account
* @param Account $account
*
* @return Collection
*/
@@ -433,7 +427,7 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param Account $account
* @param Account $account
*
* @return Account|null
*
@@ -476,7 +470,7 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param Account $account
* @param Account $account
*
* @return TransactionCurrency|null
*/
@@ -489,7 +483,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);
}
@@ -500,8 +494,8 @@ class AccountRepository implements AccountRepositoryInterface
/**
* Return meta value for account. Null if not found.
*
* @param Account $account
* @param string $field
* @param Account $account
* @param string $field
*
* @return null|string
*/
@@ -516,12 +510,32 @@ class AccountRepository implements AccountRepositoryInterface
return null;
}
if (1 === $result->count()) {
return (string) $result->first()->data;
return (string)$result->first()->data;
}
return null;
}
/**
* @param array $types
*
* @return int
*/
public function count(array $types): int
{
return $this->user->accounts()->accountTypeIn($types)->count();
}
/**
* @param int $accountId
*
* @return Account|null
*/
public function find(int $accountId): ?Account
{
return $this->user->accounts()->find($accountId);
}
/**
* @inheritDoc
*/
@@ -530,8 +544,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);
@@ -539,7 +553,7 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param Account $account
* @param Account $account
*
* @return bool
*/
@@ -562,22 +576,22 @@ 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');
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');
Log::debug(sprintf('Return max order of "%s" set (specials!): %d', $type, $order));
return $order;
}
/**
* @param array $types
* @param array|null $sort
* @param array $types
* @param array|null $sort
*
* @return Collection
*/
@@ -610,7 +624,7 @@ class AccountRepository implements AccountRepositoryInterface
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
* @param Account $account
*
* @return Carbon|null
*/
@@ -624,7 +638,7 @@ class AccountRepository implements AccountRepositoryInterface
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
* @param Account $account
*
* @return TransactionJournal|null
*/
@@ -638,7 +652,7 @@ class AccountRepository implements AccountRepositoryInterface
->orderBy('transaction_journals.id', 'ASC')
->first(['transaction_journals.id']);
if (null !== $first) {
return TransactionJournal::find((int) $first->id);
return TransactionJournal::find((int)$first->id);
}
return null;
@@ -665,7 +679,7 @@ class AccountRepository implements AccountRepositoryInterface
$account->order = 0;
continue;
}
if ($index !== (int) $account->order) {
if ($index !== (int)$account->order) {
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();
@@ -676,9 +690,9 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param string $query
* @param array $types
* @param int $limit
* @param string $query
* @param array $types
* @param int $limit
*
* @return Collection
*/
@@ -745,15 +759,7 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
* @param array $data
*
* @return Account
* @throws FireflyException
@@ -769,8 +775,8 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @param Account $account
* @param array $data
* @param Account $account
* @param array $data
*
* @return Account
* @throws FireflyException

View File

@@ -1,4 +1,5 @@
<?php
/**
* AccountRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -40,7 +41,7 @@ interface AccountRepositoryInterface
/**
* Moved here from account CRUD.
*
* @param array $types
* @param array $types
*
* @return int
*/
@@ -50,8 +51,8 @@ interface AccountRepositoryInterface
/**
* Moved here from account CRUD.
*
* @param Account $account
* @param Account|null $moveTo
* @param Account $account
* @param Account|null $moveTo
*
* @return bool
*/
@@ -60,45 +61,45 @@ interface AccountRepositoryInterface
/**
* Find account with same name OR same IBAN or both, but not the same type or ID.
*
* @param Collection $accounts
* @param Collection $accounts
*
* @return Collection
*/
public function expandWithDoubles(Collection $accounts): Collection;
/**
* @param int $accountId
* @param int $accountId
*
* @return Account|null
*/
public function find(int $accountId): ?Account;
/**
* @param string $number
* @param array $types
* @param string $number
* @param array $types
*
* @return Account|null
*/
public function findByAccountNumber(string $number, array $types): ?Account;
/**
* @param string $iban
* @param array $types
* @param string $iban
* @param array $types
*
* @return Account|null
*/
public function findByIbanNull(string $iban, array $types): ?Account;
/**
* @param string $name
* @param array $types
* @param string $name
* @param array $types
*
* @return Account|null
*/
public function findByName(string $name, array $types): ?Account;
/**
* @param Account $account
* @param Account $account
*
* @return TransactionCurrency|null
*/
@@ -107,36 +108,36 @@ interface AccountRepositoryInterface
/**
* Return account type or null if not found.
*
* @param string $type
* @param string $type
*
* @return AccountType|null
*/
public function getAccountTypeByType(string $type): ?AccountType;
/**
* @param array $accountIds
* @param array $accountIds
*
* @return Collection
*/
public function getAccountsById(array $accountIds): Collection;
/**
* @param array $types
* @param array|null $sort
* @param array $types
* @param array|null $sort
*
* @return Collection
*/
public function getAccountsByType(array $types, ?array $sort = []): Collection;
/**
* @param array $types
* @param array $types
*
* @return Collection
*/
public function getActiveAccountsByType(array $types): Collection;
/**
* @param Account $account
* @param Account $account
*
* @return Collection
*/
@@ -148,14 +149,14 @@ interface AccountRepositoryInterface
public function getCashAccount(): Account;
/**
* @param Account $account
* @param Account $account
*
* @return TransactionGroup|null
*/
public function getCreditTransactionGroup(Account $account): ?TransactionGroup;
/**
* @param array $types
* @param array $types
*
* @return Collection
*/
@@ -164,7 +165,7 @@ interface AccountRepositoryInterface
/**
* Get account location, if any.
*
* @param Account $account
* @param Account $account
*
* @return Location|null
*/
@@ -173,8 +174,8 @@ interface AccountRepositoryInterface
/**
* Return meta value for account. Null if not found.
*
* @param Account $account
* @param string $field
* @param Account $account
* @param string $field
*
* @return null|string
*/
@@ -183,14 +184,14 @@ interface AccountRepositoryInterface
/**
* Get note text or null.
*
* @param Account $account
* @param Account $account
*
* @return null|string
*/
public function getNoteText(Account $account): ?string;
/**
* @param Account $account
* @param Account $account
*
* @return TransactionJournal|null
*
@@ -200,7 +201,7 @@ interface AccountRepositoryInterface
/**
* Returns the amount of the opening balance for this account.
*
* @param Account $account
* @param Account $account
*
* @return string|null
*/
@@ -209,21 +210,21 @@ interface AccountRepositoryInterface
/**
* Return date of opening balance as string or null.
*
* @param Account $account
* @param Account $account
*
* @return null|string
*/
public function getOpeningBalanceDate(Account $account): ?string;
/**
* @param Account $account
* @param Account $account
*
* @return TransactionGroup|null
*/
public function getOpeningBalanceGroup(Account $account): ?TransactionGroup;
/**
* @param Account $account
* @param Account $account
*
* @return Collection
*/
@@ -232,28 +233,28 @@ interface AccountRepositoryInterface
/**
* Find or create the opposing reconciliation account.
*
* @param Account $account
* @param Account $account
*
* @return Account|null
*/
public function getReconciliation(Account $account): ?Account;
/**
* @param Account $account
* @param Account $account
*
* @return Collection
*/
public function getUsedCurrencies(Account $account): Collection;
/**
* @param Account $account
* @param Account $account
*
* @return bool
*/
public function isLiability(Account $account): bool;
/**
* @param string $type
* @param string $type
*
* @return int
*/
@@ -262,7 +263,7 @@ interface AccountRepositoryInterface
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
* @param Account $account
*
* @return TransactionJournal|null
*/
@@ -271,7 +272,7 @@ interface AccountRepositoryInterface
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
* @param Account $account
*
* @return Carbon|null
*/
@@ -283,38 +284,38 @@ interface AccountRepositoryInterface
public function resetAccountOrder(): void;
/**
* @param string $query
* @param array $types
* @param int $limit
* @param string $query
* @param array $types
* @param int $limit
*
* @return Collection
*/
public function searchAccount(string $query, array $types, int $limit): Collection;
/**
* @param string $query
* @param array $types
* @param int $limit
* @param string $query
* @param array $types
* @param int $limit
*
* @return Collection
*/
public function searchAccountNr(string $query, array $types, int $limit): Collection;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* @param array $data
* @param array $data
*
* @return Account
*/
public function store(array $data): Account;
/**
* @param Account $account
* @param array $data
* @param Account $account
* @param array $data
*
* @return Account
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* AccountTasker.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -23,12 +24,14 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use JsonException;
use Log;
/**
@@ -40,13 +43,13 @@ class AccountTasker implements AccountTaskerInterface
private $user;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @throws \FireflyIII\Exceptions\FireflyException
* @throws \JsonException
* @throws FireflyException
* @throws JsonException
*/
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
{
@@ -70,14 +73,15 @@ class AccountTasker implements AccountTaskerInterface
$id = $account->id;
$currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
$return['sums'][$currency->id] = $return['sums'][$currency->id] ?? [
'start' => '0',
'end' => '0',
'difference' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_name' => $currency->name,
'currency_decimal_places' => $currency->decimal_places,];
'start' => '0',
'end' => '0',
'difference' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_name' => $currency->name,
'currency_decimal_places' => $currency->decimal_places,
];
$entry = [
'name' => $account->name,
'id' => $account->id,
@@ -114,9 +118,9 @@ class AccountTasker implements AccountTaskerInterface
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
@@ -140,7 +144,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']);
@@ -149,11 +153,11 @@ class AccountTasker implements AccountTaskerInterface
}
/**
* @param array $array
* @param array $array
*
* @return array
* @throws \FireflyIII\Exceptions\FireflyException
* @throws \JsonException
* @throws FireflyException
* @throws JsonException
*/
private function groupExpenseByDestination(array $array): array
{
@@ -168,22 +172,22 @@ 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] = $currencies[$currencyId] ?? $currencyRepos->find($currencyId);
$report['accounts'][$key] = $report['accounts'][$key] ?? [
'id' => $sourceId,
'name' => $journal['destination_account_name'],
'sum' => '0',
'average' => '0',
'count' => 0,
'currency_id' => $currencies[$currencyId]->id,
'currency_name' => $currencies[$currencyId]->name,
'currency_symbol' => $currencies[$currencyId]->symbol,
'currency_code' => $currencies[$currencyId]->code,
'currency_decimal_places' => $currencies[$currencyId]->decimal_places,
];
'id' => $sourceId,
'name' => $journal['destination_account_name'],
'sum' => '0',
'average' => '0',
'count' => 0,
'currency_id' => $currencies[$currencyId]->id,
'currency_name' => $currencies[$currencyId]->name,
'currency_symbol' => $currencies[$currencyId]->symbol,
'currency_code' => $currencies[$currencyId]->code,
'currency_decimal_places' => $currencies[$currencyId]->decimal_places,
];
$report['accounts'][$key]['sum'] = bcadd($report['accounts'][$key]['sum'], $journal['amount']);
Log::debug(sprintf('Sum for %s is now %s', $journal['destination_account_name'], $report['accounts'][$key]['sum']));
@@ -194,17 +198,17 @@ 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] = $report['sums'][$currencyId] ?? [
'sum' => '0',
'currency_id' => $report['accounts'][$key]['currency_id'],
'currency_name' => $report['accounts'][$key]['currency_name'],
'currency_symbol' => $report['accounts'][$key]['currency_symbol'],
'currency_code' => $report['accounts'][$key]['currency_code'],
'currency_decimal_places' => $report['accounts'][$key]['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $report['accounts'][$key]['currency_id'],
'currency_name' => $report['accounts'][$key]['currency_name'],
'currency_symbol' => $report['accounts'][$key]['currency_symbol'],
'currency_code' => $report['accounts'][$key]['currency_code'],
'currency_decimal_places' => $report['accounts'][$key]['currency_decimal_places'],
];
$report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $report['accounts'][$key]['sum']);
}
@@ -212,9 +216,9 @@ class AccountTasker implements AccountTaskerInterface
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
@@ -235,7 +239,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']);
@@ -244,11 +248,11 @@ class AccountTasker implements AccountTaskerInterface
}
/**
* @param array $array
* @param array $array
*
* @return array
* @throws \FireflyIII\Exceptions\FireflyException
* @throws \JsonException
* @throws FireflyException
* @throws JsonException
*/
private function groupIncomeBySource(array $array): array
{
@@ -263,8 +267,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] = $currencies[$currencyId] ?? $currencyRepos->find($currencyId);
@@ -288,17 +292,17 @@ 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] = $report['sums'][$currencyId] ?? [
'sum' => '0',
'currency_id' => $report['accounts'][$key]['currency_id'],
'currency_name' => $report['accounts'][$key]['currency_name'],
'currency_symbol' => $report['accounts'][$key]['currency_symbol'],
'currency_code' => $report['accounts'][$key]['currency_code'],
'currency_decimal_places' => $report['accounts'][$key]['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $report['accounts'][$key]['currency_id'],
'currency_name' => $report['accounts'][$key]['currency_name'],
'currency_symbol' => $report['accounts'][$key]['currency_symbol'],
'currency_code' => $report['accounts'][$key]['currency_code'],
'currency_decimal_places' => $report['accounts'][$key]['currency_decimal_places'],
];
$report['sums'][$currencyId]['sum'] = bcadd($report['sums'][$currencyId]['sum'], $report['accounts'][$key]['sum']);
}
@@ -306,7 +310,7 @@ class AccountTasker implements AccountTaskerInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{

View File

@@ -1,4 +1,5 @@
<?php
/**
* AccountTaskerInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -32,34 +33,34 @@ use Illuminate\Support\Collection;
interface AccountTaskerInterface
{
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
}

View File

@@ -43,9 +43,9 @@ class OperationsRepository implements OperationsRepositoryInterface
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
@@ -59,10 +59,10 @@ class OperationsRepository implements OperationsRepositoryInterface
/**
* Collect transactions with some parameters
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param string $type
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param string $type
*
* @return array
*/
@@ -78,8 +78,16 @@ class OperationsRepository implements OperationsRepositoryInterface
}
/**
* @param array $journals
* @param string $direction
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $journals
* @param string $direction
*
* @return array
*/
@@ -87,20 +95,20 @@ 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] = $array[$currencyId] ?? [
'currency_id' => $journal['currency_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'transaction_journals' => [],
];
'currency_id' => $journal['currency_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'transaction_journals' => [],
];
$array[$currencyId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->$direction((string) $journal['amount']),
'amount' => app('steam')->$direction((string)$journal['amount']),
'date' => $journal['date'],
'transaction_journal_id' => $journalId,
'budget_name' => $journal['budget_name'],
@@ -125,9 +133,9 @@ class OperationsRepository implements OperationsRepositoryInterface
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -138,14 +146,6 @@ class OperationsRepository implements OperationsRepositoryInterface
return $this->sortByCurrency($journals, 'positive');
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @inheritDoc
*/
@@ -162,12 +162,12 @@ class OperationsRepository implements OperationsRepositoryInterface
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $opposing
* @param TransactionCurrency|null $currency
* @param string $type
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $opposing
* @param TransactionCurrency|null $currency
* @param string $type
*
* @return array
*/
@@ -246,8 +246,8 @@ class OperationsRepository implements OperationsRepositoryInterface
}
/**
* @param array $journals
* @param string $direction
* @param array $journals
* @param string $direction
*
* @return array
*/
@@ -256,28 +256,28 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->$direction($journal['amount']));
// also do foreign amount:
$foreignId = (int) $journal['foreign_currency_id'];
$foreignId = (int)$journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] = $array[$foreignId] ?? [
'sum' => '0',
'currency_id' => $foreignId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $foreignId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->$direction($journal['foreign_amount']));
}
}
@@ -301,9 +301,9 @@ class OperationsRepository implements OperationsRepositoryInterface
}
/**
* @param array $journals
* @param string $direction
* @param string $method
* @param array $journals
* @param string $direction
* @param string $method
*
* @return array
*/
@@ -316,31 +316,31 @@ class OperationsRepository implements OperationsRepositoryInterface
foreach ($journals as $journal) {
$key = sprintf('%s-%s', $journal[$idKey], $journal['currency_id']);
$array[$key] = $array[$key] ?? [
'id' => $journal[$idKey],
'name' => $journal[$nameKey],
'sum' => '0',
'currency_id' => $journal['currency_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'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']));
// also do foreign amount:
if (0 !== (int)$journal['foreign_currency_id']) {
$key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']);
$array[$key] = $array[$key] ?? [
'id' => $journal[$idKey],
'name' => $journal[$nameKey],
'sum' => '0',
'currency_id' => $journal['currency_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'currency_id' => $journal['foreign_currency_id'],
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'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['amount']));
// also do foreign amount:
if (0 !== (int) $journal['foreign_currency_id']) {
$key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']);
$array[$key] = $array[$key] ?? [
'id' => $journal[$idKey],
'name' => $journal[$nameKey],
'sum' => '0',
'currency_id' => $journal['foreign_currency_id'],
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'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']));
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->$method((string)$journal['foreign_amount']));
}
}
@@ -418,7 +418,7 @@ class OperationsRepository implements OperationsRepositoryInterface
}
/**
* @param array $journals
* @param array $journals
*
* @return array
*/
@@ -431,9 +431,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;
}
@@ -441,8 +441,8 @@ class OperationsRepository implements OperationsRepositoryInterface
}
/**
* @param array $return
* @param array $journal
* @param array $return
* @param array $journal
*
* @return array
*/
@@ -457,31 +457,31 @@ class OperationsRepository implements OperationsRepositoryInterface
// source first
$return[$sourceKey] = $return[$sourceKey] ?? [
'id' => (string) $sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['currency_code'],
];
'id' => (string)$sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['currency_code'],
];
// dest next:
$return[$destKey] = $return[$destKey] ?? [
'id' => (string) $destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['currency_code'],
];
'id' => (string)$destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['currency_code'],
];
// source account? money goes out!
$return[$sourceKey]['out'] = bcadd($return[$sourceKey]['out'], app('steam')->negative($amount));
@@ -501,31 +501,31 @@ class OperationsRepository implements OperationsRepositoryInterface
// same as above:
// source first
$return[$sourceKey] = $return[$sourceKey] ?? [
'id' => (string) $sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
'id' => (string)$sourceId,
'name' => $journal['source_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
// dest next:
$return[$destKey] = $return[$destKey] ?? [
'id' => (string) $destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string) $currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
'id' => (string)$destinationId,
'name' => $journal['destination_account_name'],
'difference' => '0',
'difference_float' => 0,
'in' => '0',
'in_float' => 0,
'out' => '0',
'out_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['foreign_currency_code'],
];
// source account? money goes out! (same as above)
$return[$sourceKey]['out'] = bcadd($return[$sourceKey]['out'], app('steam')->negative($amount));
$return[$sourceKey]['difference'] = bcadd($return[$sourceKey]['out'], $return[$sourceKey]['in']);

View File

@@ -38,9 +38,9 @@ interface OperationsRepositoryInterface
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
@@ -51,27 +51,27 @@ interface OperationsRepositoryInterface
* which have the specified accounts. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* Sum of withdrawal journals in period for a set of accounts, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $expense
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $expense
* @param TransactionCurrency|null $currency
*
* @return array
*/
@@ -86,11 +86,11 @@ interface OperationsRepositoryInterface
/**
* Sum of withdrawal journals in period for a set of accounts, grouped per destination / currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $expense
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $expense
* @param TransactionCurrency|null $currency
*
* @return array
*/
@@ -105,11 +105,11 @@ interface OperationsRepositoryInterface
/**
* Sum of withdrawal journals in period for a set of accounts, grouped per source / currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $expense
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $expense
* @param TransactionCurrency|null $currency
*
* @return array
*/
@@ -124,11 +124,11 @@ interface OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of accounts, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $revenue
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $revenue
* @param TransactionCurrency|null $currency
*
* @return array
*/
@@ -143,16 +143,16 @@ interface OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of accounts, grouped per destination + currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $revenue
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $revenue
* @param TransactionCurrency|null $currency
*
* @return array
*/
public function sumIncomeByDestination(
Carbon $start,
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $revenue = null,
@@ -162,16 +162,16 @@ interface OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of accounts, grouped per source + currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $revenue
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $revenue
* @param TransactionCurrency|null $currency
*
* @return array
*/
public function sumIncomeBySource(
Carbon $start,
Carbon $start,
Carbon $end,
?Collection $accounts = null,
?Collection $revenue = null,
@@ -181,10 +181,10 @@ interface OperationsRepositoryInterface
/**
* Sum of transfers in period for a set of accounts, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param TransactionCurrency|null $currency
*
* @return array
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* AttachmentRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -44,7 +45,7 @@ class AttachmentRepository implements AttachmentRepositoryInterface
private $user;
/**
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return bool
* @throws Exception
@@ -66,7 +67,32 @@ class AttachmentRepository implements AttachmentRepositoryInterface
}
/**
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return string
*/
public function getContent(Attachment $attachment): string
{
// create a disk.
$disk = Storage::disk('upload');
$file = $attachment->fileName();
$unencryptedContent = '';
if ($disk->exists($file)) {
$encryptedContent = (string)$disk->get($file);
try {
$unencryptedContent = Crypt::decrypt($encryptedContent); // verified
} catch (DecryptException $e) {
$unencryptedContent = $encryptedContent;
}
}
return $unencryptedContent;
}
/**
* @param Attachment $attachment
*
* @return bool
*/
@@ -86,35 +112,10 @@ class AttachmentRepository implements AttachmentRepositoryInterface
return $this->user->attachments()->get();
}
/**
* @param Attachment $attachment
*
* @return string
*/
public function getContent(Attachment $attachment): string
{
// create a disk.
$disk = Storage::disk('upload');
$file = $attachment->fileName();
$unencryptedContent = '';
if ($disk->exists($file)) {
$encryptedContent = (string) $disk->get($file);
try {
$unencryptedContent = Crypt::decrypt($encryptedContent); // verified
} catch (DecryptException $e) {
$unencryptedContent = $encryptedContent;
}
}
return $unencryptedContent;
}
/**
* Get attachment note text or empty string.
*
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return string|null
*/
@@ -122,22 +123,14 @@ class AttachmentRepository implements AttachmentRepositoryInterface
{
$note = $attachment->notes()->first();
if (null !== $note) {
return (string) $note->text;
return (string)$note->text;
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
* @param array $data
*
* @return Attachment
* @throws FireflyException
@@ -156,8 +149,16 @@ class AttachmentRepository implements AttachmentRepositoryInterface
}
/**
* @param Attachment $attachment
* @param array $data
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param Attachment $attachment
* @param array $data
*
* @return Attachment
* @throws Exception
@@ -168,28 +169,28 @@ 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;
}
/**
* @param Attachment $attachment
* @param string $note
* @param Attachment $attachment
* @param string $note
*
* @return bool
* @throws Exception

View File

@@ -1,4 +1,5 @@
<?php
/**
* AttachmentRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -33,14 +34,14 @@ use Illuminate\Support\Collection;
interface AttachmentRepositoryInterface
{
/**
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return bool
*/
public function destroy(Attachment $attachment): bool;
/**
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return bool
*/
@@ -52,7 +53,7 @@ interface AttachmentRepositoryInterface
public function get(): Collection;
/**
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return string
*/
@@ -61,19 +62,19 @@ interface AttachmentRepositoryInterface
/**
* Get attachment note text or empty string.
*
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return string|null
*/
public function getNoteText(Attachment $attachment): ?string;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* @param array $data
* @param array $data
*
* @return Attachment
* @throws FireflyException
@@ -81,8 +82,8 @@ interface AttachmentRepositoryInterface
public function store(array $data): Attachment;
/**
* @param Attachment $attachment
* @param array $attachmentData
* @param Attachment $attachment
* @param array $attachmentData
*
* @return Attachment
*/

View File

@@ -33,6 +33,14 @@ use Illuminate\Support\Collection;
*/
class ALERepository implements ALERepositoryInterface
{
/**
* @inheritDoc
*/
public function getForObject(Model $model): Collection
{
return AuditLogEntry::where('auditable_id', $model->id)->where('auditable_type', get_class($model))->get();
}
/**
* @inheritDoc
*/
@@ -48,12 +56,4 @@ class ALERepository implements ALERepositoryInterface
$auditLogEntry->save();
return $auditLogEntry;
}
/**
* @inheritDoc
*/
public function getForObject(Model $model): Collection
{
return AuditLogEntry::where('auditable_id', $model->id)->where('auditable_type', get_class($model))->get();
}
}

View File

@@ -34,14 +34,14 @@ use Illuminate\Support\Collection;
interface ALERepositoryInterface
{
/**
* @param array $data
* @return AuditLogEntry
*/
public function store(array $data): AuditLogEntry;
/**
* @param Model $model
* @param Model $model
* @return Collection
*/
public function getForObject(Model $model): Collection;
/**
* @param array $data
* @return AuditLogEntry
*/
public function store(array $data): AuditLogEntry;
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* BillRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -106,198 +107,6 @@ class BillRepository implements BillRepositoryInterface
return $bills;
}
/**
* Correct order of piggies in case of issues.
*/
public function correctOrder(): void
{
$set = $this->user->bills()->orderBy('order', 'ASC')->get();
$current = 1;
foreach ($set as $bill) {
if ((int) $bill->order !== $current) {
$bill->order = $current;
$bill->save();
}
$current++;
}
}
/**
* @param Bill $bill
*
* @return bool
*
*/
public function destroy(Bill $bill): bool
{
/** @var BillDestroyService $service */
$service = app(BillDestroyService::class);
$service->destroy($bill);
return true;
}
/**
* @inheritDoc
*/
public function destroyAll(): void
{
$this->user->bills()->delete();
}
/**
* Find bill by parameters.
*
* @param int|null $billId
* @param string|null $billName
*
* @return Bill|null
*/
public function findBill(?int $billId, ?string $billName): ?Bill
{
if (null !== $billId) {
$searchResult = $this->find((int) $billId);
if (null !== $searchResult) {
Log::debug(sprintf('Found bill based on #%d, will return it.', $billId));
return $searchResult;
}
}
if (null !== $billName) {
$searchResult = $this->findByName((string) $billName);
if (null !== $searchResult) {
Log::debug(sprintf('Found bill based on "%s", will return it.', $billName));
return $searchResult;
}
}
Log::debug('Found nothing');
return null;
}
/**
* Find a bill by ID.
*
* @param int $billId
*
* @return Bill|null
*/
public function find(int $billId): ?Bill
{
return $this->user->bills()->find($billId);
}
/**
* Find a bill by name.
*
* @param string $name
*
* @return Bill|null
*/
public function findByName(string $name): ?Bill
{
return $this->user->bills()->where('name', $name)->first(['bills.*']);
}
/**
* Get all attachments.
*
* @param Bill $bill
*
* @return Collection
*/
public function getAttachments(Bill $bill): Collection
{
$set = $bill->attachments()->get();
/** @var Storage $disk */
$disk = Storage::disk('upload');
return $set->each(
static function (Attachment $attachment) use ($disk) {
$notes = $attachment->notes()->first();
$attachment->file_exists = $disk->exists($attachment->fileName());
$attachment->notes = $notes ? $notes->text : '';
return $attachment;
}
);
}
/**
* @return Collection
*/
public function getBills(): Collection
{
return $this->user->bills()
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('name', 'ASC')->get();
}
/**
* @param Collection $accounts
*
* @return Collection
*/
public function getBillsForAccounts(Collection $accounts): Collection
{
$fields = ['bills.id', 'bills.created_at', 'bills.updated_at', 'bills.deleted_at', 'bills.user_id', 'bills.name', 'bills.amount_min',
'bills.amount_max', 'bills.date', 'bills.transaction_currency_id', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active',];
$ids = $accounts->pluck('id')->toArray();
return $this->user->bills()
->leftJoin(
'transaction_journals',
static function (JoinClause $join) {
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
}
)
->leftJoin(
'transactions',
static function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereIn('transactions.account_id', $ids)
->whereNull('transaction_journals.deleted_at')
->orderBy('bills.active', 'DESC')
->orderBy('bills.name', 'ASC')
->groupBy($fields)
->get($fields);
}
/**
* Get the total amount of money paid for the users active bills in the date range given.
* This amount will be negative (they're expenses). This method is equal to
* getBillsUnpaidInRange. So the debug comments are gone.
*
* @param Carbon $start
* @param Carbon $end
* @return string
* @deprecated
*/
public function getBillsPaidInRange(Carbon $start, Carbon $end): string
{
$bills = $this->getActiveBills();
$sum = '0';
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray();
$amount = (string) Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$sum = bcadd($sum, $amount);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $amount, $sum));
}
}
return $sum;
}
/**
* @return Collection
*/
@@ -309,74 +118,12 @@ class BillRepository implements BillRepositoryInterface
->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),]);
}
/**
* Get the total amount of money paid for the users active bills in the date range given,
* grouped per currency.
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
*/
public function getBillsPaidInRangePerCurrency(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
$currencyId = (int) $bill->transaction_currency_id;
if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray();
$amount = (string) Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$return[$currencyId] = $return[$currencyId] ?? '0';
$return[$currencyId] = bcadd($amount, $return[$currencyId]);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (currency %d)', $amount, $return[$currencyId], $currencyId));
}
}
return $return;
}
/**
* Get the total amount of money due for the users active bills in the date range given. This amount will be positive.
*
* @param Carbon $start
* @param Carbon $end
* @return string
* @deprecated
*/
public function getBillsUnpaidInRange(Carbon $start, Carbon $end): string
{
$bills = $this->getActiveBills();
$sum = '0';
/** @var Bill $bill */
foreach ($bills as $bill) {
//Log::debug(sprintf('Now at bill #%d (%s)', $bill->id, $bill->name));
$dates = $this->getPayDatesInRange($bill, $start, $end);
$count = $bill->transactionJournals()->after($start)->before($end)->count();
$total = $dates->count() - $count;
//Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total));
if ($total > 0) {
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$multi = bcmul($average, (string) $total);
$sum = bcadd($sum, $multi);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $multi, $sum));
}
}
return $sum;
}
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
@@ -410,8 +157,8 @@ class BillRepository implements BillRepositoryInterface
* Given a bill and a date, this method will tell you at which moment this bill expects its next
* transaction. Whether or not it is there already, is not relevant.
*
* @param Bill $bill
* @param Carbon $date
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
*/
@@ -435,11 +182,302 @@ class BillRepository implements BillRepositoryInterface
return $start;
}
/**
* @param array $data
*
* @return Bill
* @throws FireflyException
*/
public function store(array $data): Bill
{
/** @var BillFactory $factory */
$factory = app(BillFactory::class);
$factory->setUser($this->user);
return $factory->create($data);
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Correct order of piggies in case of issues.
*/
public function correctOrder(): void
{
$set = $this->user->bills()->orderBy('order', 'ASC')->get();
$current = 1;
foreach ($set as $bill) {
if ((int)$bill->order !== $current) {
$bill->order = $current;
$bill->save();
}
$current++;
}
}
/**
* @param Bill $bill
*
* @return bool
*
*/
public function destroy(Bill $bill): bool
{
/** @var BillDestroyService $service */
$service = app(BillDestroyService::class);
$service->destroy($bill);
return true;
}
/**
* @inheritDoc
*/
public function destroyAll(): void
{
$this->user->bills()->delete();
}
/**
* Find bill by parameters.
*
* @param int|null $billId
* @param string|null $billName
*
* @return Bill|null
*/
public function findBill(?int $billId, ?string $billName): ?Bill
{
if (null !== $billId) {
$searchResult = $this->find((int)$billId);
if (null !== $searchResult) {
Log::debug(sprintf('Found bill based on #%d, will return it.', $billId));
return $searchResult;
}
}
if (null !== $billName) {
$searchResult = $this->findByName((string)$billName);
if (null !== $searchResult) {
Log::debug(sprintf('Found bill based on "%s", will return it.', $billName));
return $searchResult;
}
}
Log::debug('Found nothing');
return null;
}
/**
* Find a bill by ID.
*
* @param int $billId
*
* @return Bill|null
*/
public function find(int $billId): ?Bill
{
return $this->user->bills()->find($billId);
}
/**
* Find a bill by name.
*
* @param string $name
*
* @return Bill|null
*/
public function findByName(string $name): ?Bill
{
return $this->user->bills()->where('name', $name)->first(['bills.*']);
}
/**
* Get all attachments.
*
* @param Bill $bill
*
* @return Collection
*/
public function getAttachments(Bill $bill): Collection
{
$set = $bill->attachments()->get();
/** @var Storage $disk */
$disk = Storage::disk('upload');
return $set->each(
static function (Attachment $attachment) use ($disk) {
$notes = $attachment->notes()->first();
$attachment->file_exists = $disk->exists($attachment->fileName());
$attachment->notes = $notes ? $notes->text : '';
return $attachment;
}
);
}
/**
* @return Collection
*/
public function getBills(): Collection
{
return $this->user->bills()
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('name', 'ASC')->get();
}
/**
* @param Collection $accounts
*
* @return Collection
*/
public function getBillsForAccounts(Collection $accounts): Collection
{
$fields = [
'bills.id',
'bills.created_at',
'bills.updated_at',
'bills.deleted_at',
'bills.user_id',
'bills.name',
'bills.amount_min',
'bills.amount_max',
'bills.date',
'bills.transaction_currency_id',
'bills.repeat_freq',
'bills.skip',
'bills.automatch',
'bills.active',
];
$ids = $accounts->pluck('id')->toArray();
return $this->user->bills()
->leftJoin(
'transaction_journals',
static function (JoinClause $join) {
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
}
)
->leftJoin(
'transactions',
static function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereIn('transactions.account_id', $ids)
->whereNull('transaction_journals.deleted_at')
->orderBy('bills.active', 'DESC')
->orderBy('bills.name', 'ASC')
->groupBy($fields)
->get($fields);
}
/**
* Get the total amount of money paid for the users active bills in the date range given.
* This amount will be negative (they're expenses). This method is equal to
* getBillsUnpaidInRange. So the debug comments are gone.
*
* @param Carbon $start
* @param Carbon $end
* @return string
* @deprecated
*/
public function getBillsPaidInRange(Carbon $start, Carbon $end): string
{
$bills = $this->getActiveBills();
$sum = '0';
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray();
$amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$sum = bcadd($sum, $amount);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $amount, $sum));
}
}
return $sum;
}
/**
* Get the total amount of money paid for the users active bills in the date range given,
* grouped per currency.
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
*/
public function getBillsPaidInRangePerCurrency(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
$currencyId = (int)$bill->transaction_currency_id;
if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray();
$amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$return[$currencyId] = $return[$currencyId] ?? '0';
$return[$currencyId] = bcadd($amount, $return[$currencyId]);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (currency %d)', $amount, $return[$currencyId], $currencyId));
}
}
return $return;
}
/**
* Get the total amount of money due for the users active bills in the date range given. This amount will be positive.
*
* @param Carbon $start
* @param Carbon $end
* @return string
* @deprecated
*/
public function getBillsUnpaidInRange(Carbon $start, Carbon $end): string
{
$bills = $this->getActiveBills();
$sum = '0';
/** @var Bill $bill */
foreach ($bills as $bill) {
//Log::debug(sprintf('Now at bill #%d (%s)', $bill->id, $bill->name));
$dates = $this->getPayDatesInRange($bill, $start, $end);
$count = $bill->transactionJournals()->after($start)->before($end)->count();
$total = $dates->count() - $count;
//Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total));
if ($total > 0) {
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$multi = bcmul($average, (string)$total);
$sum = bcadd($sum, $multi);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $multi, $sum));
}
}
return $sum;
}
/**
* Get the total amount of money due for the users active bills in the date range given.
*
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @return array
* @deprecated
*/
@@ -453,13 +491,13 @@ class BillRepository implements BillRepositoryInterface
$dates = $this->getPayDatesInRange($bill, $start, $end);
$count = $bill->transactionJournals()->after($start)->before($end)->count();
$total = $dates->count() - $count;
$currencyId = (int) $bill->transaction_currency_id;
$currencyId = (int)$bill->transaction_currency_id;
//Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total));
if ($total > 0) {
$average = bcdiv(bcadd((string) $bill->amount_max, (string) $bill->amount_min), '2');
$multi = bcmul($average, (string) $total);
$average = bcdiv(bcadd((string)$bill->amount_max, (string)$bill->amount_min), '2');
$multi = bcmul($average, (string)$total);
$return[$currencyId] = $return[$currencyId] ?? '0';
$return[$currencyId] = bcadd($return[$currencyId], $multi);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (for currency %d)', $multi, $return[$currencyId], $currencyId));
@@ -469,70 +507,10 @@ class BillRepository implements BillRepositoryInterface
return $return;
}
/**
* @inheritDoc
*/
public function sumUnpaidInRange(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
$dates = $this->getPayDatesInRange($bill, $start, $end);
$count = $bill->transactionJournals()->after($start)->before($end)->count();
$total = $dates->count() - $count;
if ($total > 0) {
$currency = $bill->transactionCurrency;
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$return[$currency->id] = $return[$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 $return;
}
/**
* @inheritDoc
*/
public function sumPaidInRange(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
$currency = $bill->transactionCurrency;
if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray();
$amount = (string) Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$return[$currency->id] = $return[$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'], $amount);
}
}
return $return;
}
/**
* Get all bills with these ID's.
*
* @param array $billIds
* @param array $billIds
*
* @return Collection
*/
@@ -544,7 +522,7 @@ class BillRepository implements BillRepositoryInterface
/**
* Get text or return empty string.
*
* @param Bill $bill
* @param Bill $bill
*
* @return string
*/
@@ -553,14 +531,14 @@ class BillRepository implements BillRepositoryInterface
/** @var Note $note */
$note = $bill->notes()->first();
if (null !== $note) {
return (string) $note->text;
return (string)$note->text;
}
return '';
}
/**
* @param Bill $bill
* @param Bill $bill
*
* @return array
*/
@@ -578,35 +556,35 @@ 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] = $result[$currencyId] ?? [
'sum' => '0',
'count' => 0,
'avg' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
'sum' => '0',
'count' => 0,
'avg' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
$result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], $transaction->amount);
$result[$currencyId]['count']++;
}
// after loop, re-loop for avg.
/**
* @var int $currencyId
* @var int $currencyId
* @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;
}
/**
* @param int $size
* @param int $size
*
* @return LengthAwarePaginator
*/
@@ -620,9 +598,9 @@ class BillRepository implements BillRepositoryInterface
/**
* The "paid dates" list is a list of dates of transaction journals that are linked to this bill.
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
@@ -633,7 +611,8 @@ class BillRepository implements BillRepositoryInterface
return $bill->transactionJournals()
->before($end)->after($start)->get(
[
'transaction_journals.id', 'transaction_journals.date',
'transaction_journals.id',
'transaction_journals.date',
'transaction_journals.transaction_group_id',
]
);
@@ -642,7 +621,7 @@ class BillRepository implements BillRepositoryInterface
/**
* Return all rules for one bill
*
* @param Bill $bill
* @param Bill $bill
*
* @return Collection
*/
@@ -661,7 +640,7 @@ class BillRepository implements BillRepositoryInterface
*
* 5 => [['id' => 1, 'title' => 'Some rule'],['id' => 2, 'title' => 'Some other rule']]
*
* @param Collection $collection
* @param Collection $collection
*
* @return array
*/
@@ -686,8 +665,8 @@ class BillRepository implements BillRepositoryInterface
}
/**
* @param Bill $bill
* @param Carbon $date
* @param Bill $bill
* @param Carbon $date
*
* @return array
*/
@@ -701,8 +680,8 @@ class BillRepository implements BillRepositoryInterface
$result = [];
$journals = $bill->transactionJournals()
->where('date', '>=', $date->year . '-01-01 00:00:00')
->where('date', '<=', $date->year . '-12-31 23:59:59')
->where('date', '>=', $date->year.'-01-01 00:00:00')
->where('date', '<=', $date->year.'-12-31 23:59:59')
->get();
/** @var TransactionJournal $journal */
@@ -712,28 +691,28 @@ 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] = $result[$currencyId] ?? [
'sum' => '0',
'count' => 0,
'avg' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
'sum' => '0',
'count' => 0,
'avg' => '0',
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
$result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], $transaction->amount);
$result[$currencyId]['count']++;
}
// after loop, re-loop for avg.
/**
* @var int $currencyId
* @var int $currencyId
* @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;
@@ -742,14 +721,14 @@ class BillRepository implements BillRepositoryInterface
/**
* Link a set of journals to a bill.
*
* @param Bill $bill
* @param array $transactions
* @param Bill $bill
* @param array $transactions
*/
public function linkCollectionToBill(Bill $bill, array $transactions): void
{
/** @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();
Log::debug(sprintf('Linked journal #%d to bill #%d', $journal->id, $bill->id));
@@ -759,8 +738,8 @@ class BillRepository implements BillRepositoryInterface
/**
* Given the date in $date, this method will return a moment in the future where the bill is expected to be paid.
*
* @param Bill $bill
* @param Carbon $date
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
* @throws JsonException
@@ -776,12 +755,12 @@ class BillRepository implements BillRepositoryInterface
}
// find the most recent date for this bill NOT in the future. Cache this date:
$start = clone $bill->date;
Log::debug('nextExpectedMatch: Start is ' . $start->format('Y-m-d'));
Log::debug('nextExpectedMatch: Start is '.$start->format('Y-m-d'));
while ($start < $date) {
Log::debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d'), $date->format('Y-m-d')));
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
Log::debug('Start is now ' . $start->format('Y-m-d'));
Log::debug('Start is now '.$start->format('Y-m-d'));
}
$end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
@@ -795,8 +774,8 @@ class BillRepository implements BillRepositoryInterface
$start = clone $end;
$end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
}
Log::debug('nextExpectedMatch: Final start is ' . $start->format('Y-m-d'));
Log::debug('nextExpectedMatch: Matching end is ' . $end->format('Y-m-d'));
Log::debug('nextExpectedMatch: Final start is '.$start->format('Y-m-d'));
Log::debug('nextExpectedMatch: Matching end is '.$end->format('Y-m-d'));
$cache->store($start);
@@ -814,8 +793,8 @@ class BillRepository implements BillRepositoryInterface
}
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
@@ -849,30 +828,67 @@ class BillRepository implements BillRepositoryInterface
}
/**
* @param User $user
* @inheritDoc
*/
public function setUser(User $user): void
public function sumPaidInRange(Carbon $start, Carbon $end): array
{
$this->user = $user;
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
$currency = $bill->transactionCurrency;
if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray();
$amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$return[$currency->id] = $return[$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'], $amount);
}
}
return $return;
}
/**
* @param array $data
*
* @return Bill
* @throws FireflyException
* @inheritDoc
*/
public function store(array $data): Bill
public function sumUnpaidInRange(Carbon $start, Carbon $end): array
{
/** @var BillFactory $factory */
$factory = app(BillFactory::class);
$factory->setUser($this->user);
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
$dates = $this->getPayDatesInRange($bill, $start, $end);
$count = $bill->transactionJournals()->after($start)->before($end)->count();
$total = $dates->count() - $count;
return $factory->create($data);
if ($total > 0) {
$currency = $bill->transactionCurrency;
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$return[$currency->id] = $return[$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 $return;
}
/**
* @param Bill $bill
* @param Bill $bill
*/
public function unlinkAll(Bill $bill): void
{
@@ -880,8 +896,8 @@ class BillRepository implements BillRepositoryInterface
}
/**
* @param Bill $bill
* @param array $data
* @param Bill $bill
* @param array $data
*
* @return Bill
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* BillRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -35,34 +36,16 @@ use Illuminate\Support\Collection;
interface BillRepositoryInterface
{
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function billEndsWith(string $query, int $limit): Collection;
/**
* Collect multi-currency of sum of bills yet to pay.
*
* @param Carbon $start
* @param Carbon $end
* @return array
*/
public function sumUnpaidInRange(Carbon $start, Carbon $end): array;
/**
* Collect multi-currency of sum of bills already paid.
*
* @param Carbon $start
* @param Carbon $end
* @return array
*/
public function sumPaidInRange(Carbon $start, Carbon $end): array;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
@@ -71,10 +54,10 @@ interface BillRepositoryInterface
/**
* Get the total amount of money due for the users active bills in the date range given.
*
* @param Carbon $start
* @param Carbon $end
* @deprecated
* @param Carbon $start
* @param Carbon $end
* @return Collection
* @deprecated
*/
public function collectBillsUnpaidInRange(Carbon $start, Carbon $end): Collection;
@@ -84,7 +67,7 @@ interface BillRepositoryInterface
public function correctOrder(): void;
/**
* @param Bill $bill
* @param Bill $bill
*
* @return bool
*/
@@ -98,7 +81,7 @@ interface BillRepositoryInterface
/**
* Find a bill by ID.
*
* @param int $billId
* @param int $billId
*
* @return Bill|null
*/
@@ -107,8 +90,8 @@ interface BillRepositoryInterface
/**
* Find bill by parameters.
*
* @param int|null $billId
* @param string|null $billName
* @param int|null $billId
* @param string|null $billName
*
* @return Bill|null
*/
@@ -117,7 +100,7 @@ interface BillRepositoryInterface
/**
* Find a bill by name.
*
* @param string $name
* @param string $name
*
* @return Bill|null
*/
@@ -131,7 +114,7 @@ interface BillRepositoryInterface
/**
* Get all attachments.
*
* @param Bill $bill
* @param Bill $bill
*
* @return Collection
*/
@@ -145,7 +128,7 @@ interface BillRepositoryInterface
/**
* Gets the bills which have some kind of relevance to the accounts mentioned.
*
* @param Collection $accounts
* @param Collection $accounts
*
* @return Collection
*/
@@ -153,8 +136,8 @@ interface BillRepositoryInterface
/**
* Get the total amount of money paid for the users active bills in the date range given.
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @deprecated
@@ -164,8 +147,8 @@ interface BillRepositoryInterface
/**
* Get the total amount of money paid for the users active bills in the date range given,
* grouped per currency.
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -174,8 +157,8 @@ interface BillRepositoryInterface
/**
* Get the total amount of money due for the users active bills in the date range given.
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @deprecated
@@ -184,8 +167,8 @@ interface BillRepositoryInterface
/**
* Get the total amount of money due for the users active bills in the date range given.
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -195,7 +178,7 @@ interface BillRepositoryInterface
/**
* Get all bills with these ID's.
*
* @param array $billIds
* @param array $billIds
*
* @return Collection
*/
@@ -204,30 +187,30 @@ interface BillRepositoryInterface
/**
* Get text or return empty string.
*
* @param Bill $bill
* @param Bill $bill
*
* @return string
*/
public function getNoteText(Bill $bill): string;
/**
* @param Bill $bill
* @param Bill $bill
*
* @return array
*/
public function getOverallAverage(Bill $bill): array;
/**
* @param int $size
* @param int $size
*
* @return LengthAwarePaginator
*/
public function getPaginator(int $size): LengthAwarePaginator;
/**
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
@@ -236,9 +219,9 @@ interface BillRepositoryInterface
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
@@ -247,7 +230,7 @@ interface BillRepositoryInterface
/**
* Return all rules for one bill
*
* @param Bill $bill
* @param Bill $bill
*
* @return Collection
*/
@@ -259,15 +242,15 @@ interface BillRepositoryInterface
*
* 5 => [['id' => 1, 'title' => 'Some rule'],['id' => 2, 'title' => 'Some other rule']]
*
* @param Collection $collection
* @param Collection $collection
*
* @return array
*/
public function getRulesForBills(Collection $collection): array;
/**
* @param Bill $bill
* @param Carbon $date
* @param Bill $bill
* @param Carbon $date
*
* @return array
*/
@@ -276,8 +259,8 @@ interface BillRepositoryInterface
/**
* Link a set of journals to a bill.
*
* @param Bill $bill
* @param array $transactions
* @param Bill $bill
* @param array $transactions
*/
public function linkCollectionToBill(Bill $bill, array $transactions): void;
@@ -285,39 +268,39 @@ interface BillRepositoryInterface
* Given a bill and a date, this method will tell you at which moment this bill expects its next
* transaction. Whether or not it is there already, is not relevant.
*
* @param Bill $bill
* @param Carbon $date
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
*/
public function nextDateMatch(Bill $bill, Carbon $date): Carbon;
/**
* @param Bill $bill
* @param Carbon $date
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
*/
public function nextExpectedMatch(Bill $bill, Carbon $date): Carbon;
/**
* @param Bill $bill
* @param Bill $bill
*
* @return Bill
*/
public function removeObjectGroup(Bill $bill): Bill;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchBill(string $query, int $limit): Collection;
/**
* @param Bill $bill
* @param string $objectGroupTitle
* @param Bill $bill
* @param string $objectGroupTitle
*
* @return Bill
*/
@@ -326,18 +309,18 @@ interface BillRepositoryInterface
/**
* Set specific piggy bank to specific order.
*
* @param Bill $bill
* @param int $order
* @param Bill $bill
* @param int $order
*/
public function setOrder(Bill $bill, int $order): void;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* @param array $data
* @param array $data
*
* @return Bill
* @throws FireflyException
@@ -345,13 +328,31 @@ interface BillRepositoryInterface
public function store(array $data): Bill;
/**
* @param Bill $bill
* Collect multi-currency of sum of bills already paid.
*
* @param Carbon $start
* @param Carbon $end
* @return array
*/
public function sumPaidInRange(Carbon $start, Carbon $end): array;
/**
* Collect multi-currency of sum of bills yet to pay.
*
* @param Carbon $start
* @param Carbon $end
* @return array
*/
public function sumUnpaidInRange(Carbon $start, Carbon $end): array;
/**
* @param Bill $bill
*/
public function unlinkAll(Bill $bill): void;
/**
* @param Bill $bill
* @param array $data
* @param Bill $bill
* @param array $data
*
* @return Bill
*/

View File

@@ -48,7 +48,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
/**
* @param AvailableBudget $availableBudget
* @param AvailableBudget $availableBudget
*/
public function destroyAvailableBudget(AvailableBudget $availableBudget): void
{
@@ -59,12 +59,20 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
}
/**
* @inheritDoc
*/
public function findById(int $id): ?AvailableBudget
{
return $this->user->availableBudgets->find($id);
}
/**
* Find existing AB.
*
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return AvailableBudget|null
*/
@@ -78,18 +86,51 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
/**
* @inheritDoc
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function findById(int $id): ?AvailableBudget
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string
{
return $this->user->availableBudgets->find($id);
$amount = '0';
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
if (null !== $availableBudget) {
$amount = (string)$availableBudget->amount;
}
return $amount;
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array
{
$return = [];
$availableBudgets = $this->user->availableBudgets()
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->get();
/** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) {
$return[$availableBudget->transaction_currency_id] = $availableBudget->amount;
}
return $return;
}
/**
* Return a list of all available budgets (in all currencies) (for the selected period).
*
* @param Carbon|null $start
* @param Carbon|null $end
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
@@ -108,51 +149,10 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
return $query->get(['available_budgets.*']);
}
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string
{
$amount = '0';
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
if (null !== $availableBudget) {
$amount = (string) $availableBudget->amount;
}
return $amount;
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array
{
$return = [];
$availableBudgets = $this->user->availableBudgets()
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->get();
/** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) {
$return[$availableBudget->transaction_currency_id] = $availableBudget->amount;
}
return $return;
}
/**
* Returns all available budget objects.
*
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return Collection
*/
@@ -164,8 +164,8 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
/**
* Returns all available budget objects.
*
* @param Carbon|null $start
* @param Carbon|null $end
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*
@@ -197,10 +197,10 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return AvailableBudget
* @deprecated
@@ -225,7 +225,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{
@@ -233,7 +233,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return AvailableBudget|null
*/
@@ -261,8 +261,8 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
/**
* @param AvailableBudget $availableBudget
* @param array $data
* @param AvailableBudget $availableBudget
* @param array $data
*
* @return AvailableBudget
*/
@@ -277,8 +277,8 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
}
/**
* @param AvailableBudget $availableBudget
* @param array $data
* @param AvailableBudget $availableBudget
* @param array $data
*
* @return AvailableBudget
*/

View File

@@ -40,23 +40,23 @@ interface AvailableBudgetRepositoryInterface
public function destroyAll(): void;
/**
* @param AvailableBudget $availableBudget
* @param AvailableBudget $availableBudget
*/
public function destroyAvailableBudget(AvailableBudget $availableBudget): void;
/**
* Find existing AB.
*
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return AvailableBudget|null
*/
public function find(TransactionCurrency $currency, Carbon $start, Carbon $end): ?AvailableBudget;
/**
* @param int $id
* @param int $id
*
* @return AvailableBudget|null
*/
@@ -65,17 +65,17 @@ interface AvailableBudgetRepositoryInterface
/**
* Return a list of all available budgets (in all currencies) (for the selected period).
*
* @param Carbon|null $start
* @param Carbon|null $end
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
public function get(?Carbon $start = null, ?Carbon $end = null): Collection;
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @deprecated
@@ -83,8 +83,8 @@ interface AvailableBudgetRepositoryInterface
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string;
/**
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -93,7 +93,7 @@ interface AvailableBudgetRepositoryInterface
/**
* Returns all available budget objects.
*
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return Collection
*/
@@ -102,8 +102,8 @@ interface AvailableBudgetRepositoryInterface
/**
* Returns all available budget objects.
*
* @param Carbon|null $start
* @param Carbon|null $end
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*
@@ -113,19 +113,19 @@ interface AvailableBudgetRepositoryInterface
/**
* Get by transaction currency and date. Should always result in one entry or NULL.
*
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
*
* @return null|AvailableBudget
*/
public function getByCurrencyDate(Carbon $start, Carbon $end, TransactionCurrency $currency): ?AvailableBudget;
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return AvailableBudget
* @deprecated
@@ -133,28 +133,28 @@ interface AvailableBudgetRepositoryInterface
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* @param array $data
* @param array $data
*
* @return AvailableBudget|null
*/
public function store(array $data): ?AvailableBudget;
/**
* @param AvailableBudget $availableBudget
* @param array $data
* @param AvailableBudget $availableBudget
* @param array $data
*
* @return AvailableBudget
*/
public function update(AvailableBudget $availableBudget, array $data): AvailableBudget;
/**
* @param AvailableBudget $availableBudget
* @param array $data
* @param AvailableBudget $availableBudget
* @param array $data
*
* @return AvailableBudget
*/

View File

@@ -48,10 +48,10 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
* Tells you which amount has been budgeted (for the given budgets)
* in the selected query. Returns a positive amount as a string.
*
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Collection|null $budgets
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Collection|null $budgets
*
* @return string
*/
@@ -60,37 +60,37 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
$query = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
// same complex where query as below.
->where(
static function (Builder $q5) use ($start, $end) {
$q5->where(
static function (Builder $q1) use ($start, $end) {
$q1->where(
static function (Builder $q2) use ($start, $end) {
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d'));
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d'));
->where(
static function (Builder $q5) use ($start, $end) {
$q5->where(
static function (Builder $q1) use ($start, $end) {
$q1->where(
static function (Builder $q2) use ($start, $end) {
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d'));
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d'));
}
)
->orWhere(
static function (Builder $q3) use ($start, $end) {
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
}
);
}
)
->orWhere(
static function (Builder $q4) use ($start, $end) {
// or start is before start AND end is after end.
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d'));
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d'));
}
);
}
)
->orWhere(
static function (Builder $q3) use ($start, $end) {
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d'));
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d'));
}
);
}
)
->orWhere(
static function (Builder $q4) use ($start, $end) {
// or start is before start AND end is after end.
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d'));
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d'));
}
);
}
)
->where('budget_limits.transaction_currency_id', $currency->id)
->whereNull('budgets.deleted_at')
->where('budgets.active', true)
->where('budgets.user_id', $this->user->id);
->where('budget_limits.transaction_currency_id', $currency->id)
->whereNull('budgets.deleted_at')
->where('budgets.active', true)
->where('budgets.user_id', $this->user->id);
if (null !== $budgets && $budgets->count() > 0) {
$query->whereIn('budget_limits.budget_id', $budgets->pluck('id')->toArray());
}
@@ -120,7 +120,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
/**
* Destroy a budget limit.
*
* @param BudgetLimit $budgetLimit
* @param BudgetLimit $budgetLimit
*/
public function destroyBudgetLimit(BudgetLimit $budgetLimit): void
{
@@ -132,25 +132,9 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
/**
* @param Budget $budget
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return BudgetLimit|null
*/
public function find(Budget $budget, TransactionCurrency $currency, Carbon $start, Carbon $end): ?BudgetLimit
{
return $budget->budgetlimits()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
}
/**
* @param TransactionCurrency $currency
* @param Carbon|null $start
* @param Carbon|null $end
* @param TransactionCurrency $currency
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
@@ -164,8 +148,8 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
/**
* @param Carbon|null $start
* @param Carbon|null $end
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
@@ -232,9 +216,9 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
/**
* @param Budget $budget
* @param Carbon|null $start
* @param Carbon|null $end
* @param Budget $budget
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
@@ -292,7 +276,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{
@@ -300,7 +284,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return BudgetLimit
* @throws FireflyException
@@ -319,7 +303,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
$currency->save();
// find the budget:
$budget = $this->user->budgets()->find((int) $data['budget_id']);
$budget = $this->user->budgets()->find((int)$data['budget_id']);
if (null === $budget) {
throw new FireflyException('200004: Budget does not exist.');
}
@@ -349,8 +333,24 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
/**
* @param BudgetLimit $budgetLimit
* @param array $data
* @param Budget $budget
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return BudgetLimit|null
*/
public function find(Budget $budget, TransactionCurrency $currency, Carbon $start, Carbon $end): ?BudgetLimit
{
return $budget->budgetlimits()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
}
/**
* @param BudgetLimit $budgetLimit
* @param array $data
*
* @return BudgetLimit
* @throws FireflyException
@@ -386,10 +386,10 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param string $amount
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return BudgetLimit|null
*

View File

@@ -39,10 +39,10 @@ interface BudgetLimitRepositoryInterface
* Tells you which amount has been budgeted (for the given budgets)
* in the selected query. Returns a positive amount as a string.
*
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Collection|null $budgets
* @param Carbon $start
* @param Carbon $end
* @param TransactionCurrency $currency
* @param Collection|null $budgets
*
* @return string
*/
@@ -56,15 +56,15 @@ interface BudgetLimitRepositoryInterface
/**
* Destroy a budget limit.
*
* @param BudgetLimit $budgetLimit
* @param BudgetLimit $budgetLimit
*/
public function destroyBudgetLimit(BudgetLimit $budgetLimit): void;
/**
* @param Budget $budget
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param Budget $budget
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return BudgetLimit|null
*/
@@ -73,56 +73,56 @@ interface BudgetLimitRepositoryInterface
/**
* TODO this method is not multi currency aware.
*
* @param Carbon|null $start
* @param Carbon|null $end
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
public function getAllBudgetLimits(Carbon $start = null, Carbon $end = null): Collection;
/**
* @param TransactionCurrency $currency
* @param Carbon|null $start
* @param Carbon|null $end
* @param TransactionCurrency $currency
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
public function getAllBudgetLimitsByCurrency(TransactionCurrency $currency, Carbon $start = null, Carbon $end = null): Collection;
/**
* @param Budget $budget
* @param Carbon|null $start
* @param Carbon|null $end
* @param Budget $budget
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
public function getBudgetLimits(Budget $budget, Carbon $start = null, Carbon $end = null): Collection;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* @param array $data
* @param array $data
*
* @return BudgetLimit
*/
public function store(array $data): BudgetLimit;
/**
* @param BudgetLimit $budgetLimit
* @param array $data
* @param BudgetLimit $budgetLimit
* @param array $data
*
* @return BudgetLimit
*/
public function update(BudgetLimit $budgetLimit, array $data): BudgetLimit;
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param string $amount
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return BudgetLimit|null
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* BudgetRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -105,42 +106,52 @@ class BudgetRepository implements BudgetRepositoryInterface
Log::debug(sprintf('Budget limit #%d', $limit->id));
$currency = $limit->transactionCurrency;
$return[$currency->id] = $return[$currency->id] ?? [
'id' => (string) $currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
'decimal_places' => $currency->decimal_places,
'sum' => '0',
];
'id' => (string)$currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
'decimal_places' => $currency->decimal_places,
'sum' => '0',
];
// same period
if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end)) {
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string) $limit->amount);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string)$limit->amount);
Log::debug(sprintf('Add full amount [1]: %s', $limit->amount));
continue;
}
// limit is inside of date range
if ($start->lte($limit->start_date) && $end->gte($limit->end_date)) {
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string) $limit->amount);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string)$limit->amount);
Log::debug(sprintf('Add full amount [2]: %s', $limit->amount));
continue;
}
$total = $limit->start_date->diffInDays($limit->end_date) + 1; // include the day itself.
$days = $this->daysInOverlap($limit, $start, $end);
$amount = bcmul(bcdiv((string) $limit->amount, (string) $total), (string) $days);
$amount = bcmul(bcdiv((string)$limit->amount, (string)$total), (string)$days);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $amount);
Log::debug(sprintf(
'Amount per day: %s (%s over %d days). Total amount for %d days: %s',
bcdiv((string) $limit->amount, (string) $total),
$limit->amount,
$total,
$days,
$amount
));
Log::debug(
sprintf(
'Amount per day: %s (%s over %d days). Total amount for %d days: %s',
bcdiv((string)$limit->amount, (string)$total),
$limit->amount,
$total,
$days,
$amount
)
);
}
}
return $return;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @return Collection
*/
@@ -155,9 +166,9 @@ class BudgetRepository implements BudgetRepositoryInterface
/**
* How many days of this budget limit are between start and end?
*
* @param BudgetLimit $limit
* @param Carbon $start
* @param Carbon $end
* @param BudgetLimit $limit
* @param Carbon $start
* @param Carbon $end
* @return int
*/
private function daysInOverlap(BudgetLimit $limit, Carbon $start, Carbon $end): int
@@ -203,7 +214,7 @@ class BudgetRepository implements BudgetRepositoryInterface
}
$budgets = $this->getActiveBudgets();
/**
* @var int $index
* @var int $index
* @var Budget $budget
*/
foreach ($budgets as $index => $budget) {
@@ -217,7 +228,193 @@ class BudgetRepository implements BudgetRepositoryInterface
}
/**
* @param Budget $budget
* @param Budget $budget
* @param array $data
*
* @return Budget
*/
public function update(Budget $budget, array $data): Budget
{
Log::debug('Now in update()');
$oldName = $budget->name;
if (array_key_exists('name', $data)) {
$budget->name = $data['name'];
$this->updateRuleActions($oldName, $budget->name);
$this->updateRuleTriggers($oldName, $budget->name);
}
if (array_key_exists('active', $data)) {
$budget->active = $data['active'];
}
if (array_key_exists('notes', $data)) {
$this->setNoteText($budget, (string)$data['notes']);
}
$budget->save();
// update or create auto-budget:
$autoBudget = $this->getAutoBudget($budget);
// first things first: delete when no longer required:
$autoBudgetType = array_key_exists('auto_budget_type', $data) ? $data['auto_budget_type'] : null;
if (0 === $autoBudgetType && null !== $autoBudget) {
// delete!
$autoBudget->delete();
return $budget;
}
if (0 === $autoBudgetType && null === $autoBudget) {
return $budget;
}
if (null === $autoBudgetType && null === $autoBudget) {
return $budget;
}
$this->updateAutoBudget($budget, $data);
return $budget;
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleActions(string $oldName, string $newName): void
{
$types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleTriggers(string $oldName, string $newName): void
{
$types = ['budget_is',];
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*']);
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
/** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) {
$trigger->trigger_value = $newName;
$trigger->save();
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
}
}
/**
* @param Budget $budget
* @param string $text
* @return void
*/
private function setNoteText(Budget $budget, string $text): void
{
$dbNote = $budget->notes()->first();
if ('' !== $text) {
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($budget);
}
$dbNote->text = trim($text);
$dbNote->save();
return;
}
if (null !== $dbNote) {
try {
$dbNote->delete();
} catch (Exception $e) {
// @ignoreException
}
}
}
/**
* @inheritDoc
*/
public function getAutoBudget(Budget $budget): ?AutoBudget
{
return $budget->autoBudgets()->first();
}
/**
* @param Budget $budget
* @param array $data
* @throws FireflyException
* @throws JsonException
*/
private function updateAutoBudget(Budget $budget, array $data): void
{
// update or create auto-budget:
$autoBudget = $this->getAutoBudget($budget);
// grab default currency:
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
if (null === $autoBudget) {
// at this point it's a blind assumption auto_budget_type is 1 or 2.
$autoBudget = new AutoBudget();
$autoBudget->auto_budget_type = $data['auto_budget_type'];
$autoBudget->budget_id = $budget->id;
$autoBudget->transaction_currency_id = $currency->id;
}
// set or update the currency.
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
$repos = app(CurrencyRepositoryInterface::class);
$currencyId = (int)($data['currency_id'] ?? 0);
$currencyCode = (string)($data['currency_code'] ?? '');
$currency = $repos->find($currencyId);
if (null === $currency) {
$currency = $repos->findByCodeNull($currencyCode);
}
if (null !== $currency) {
$autoBudget->transaction_currency_id = $currency->id;
}
}
// change values if submitted or presented:
if (array_key_exists('auto_budget_type', $data)) {
$autoBudget->auto_budget_type = $data['auto_budget_type'];
}
if (array_key_exists('auto_budget_amount', $data)) {
$autoBudget->amount = $data['auto_budget_amount'];
}
if (array_key_exists('auto_budget_period', $data)) {
$autoBudget->period = $data['auto_budget_period'];
}
$autoBudget->save();
}
/**
* Find a budget or return NULL
*
* @param int|null $budgetId |null
*
* @return Budget|null
*/
public function find(int $budgetId = null): ?Budget
{
return $this->user->budgets()->find($budgetId);
}
/**
* @param Budget $budget
*
* @return bool
*/
@@ -238,10 +435,10 @@ class BudgetRepository implements BudgetRepositoryInterface
$budgets = $this->getBudgets();
/** @var Budget $budget */
foreach ($budgets as $budget) {
DB::table('budget_transaction')->where('budget_id', (int) $budget->id)->delete();
DB::table('budget_transaction_journal')->where('budget_id', (int) $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();
DB::table('budget_transaction')->where('budget_id', (int)$budget->id)->delete();
DB::table('budget_transaction_journal')->where('budget_id', (int)$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();
}
}
@@ -267,8 +464,8 @@ class BudgetRepository implements BudgetRepositoryInterface
}
/**
* @param int|null $budgetId
* @param string|null $budgetName
* @param int|null $budgetId
* @param string|null $budgetName
*
* @return Budget|null
*/
@@ -276,10 +473,10 @@ class BudgetRepository implements BudgetRepositoryInterface
{
Log::debug('Now in findBudget()');
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) {
Log::debug(sprintf('Searching for budget with name %s...', $budgetName));
$result = $this->findByName((string) $budgetName);
$result = $this->findByName((string)$budgetName);
}
if (null !== $result) {
Log::debug(sprintf('Found budget #%d: %s', $result->id, $result->name));
@@ -289,22 +486,10 @@ class BudgetRepository implements BudgetRepositoryInterface
return $result;
}
/**
* Find a budget or return NULL
*
* @param int|null $budgetId |null
*
* @return Budget|null
*/
public function find(int $budgetId = null): ?Budget
{
return $this->user->budgets()->find($budgetId);
}
/**
* Find budget by name.
*
* @param string|null $name
* @param string|null $name
*
* @return Budget|null
*/
@@ -322,7 +507,7 @@ class BudgetRepository implements BudgetRepositoryInterface
* This method returns the oldest journal or transaction date known to this budget.
* Will cache result.
*
* @param Budget $budget
* @param Budget $budget
*
* @return Carbon|null
*/
@@ -360,7 +545,7 @@ class BudgetRepository implements BudgetRepositoryInterface
/**
* Get all budgets with these ID's.
*
* @param array $budgetIds
* @param array $budgetIds
*
* @return Collection
*/
@@ -393,8 +578,8 @@ class BudgetRepository implements BudgetRepositoryInterface
}
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
@@ -411,8 +596,8 @@ class BudgetRepository implements BudgetRepositoryInterface
}
/**
* @param Budget $budget
* @param int $order
* @param Budget $budget
* @param int $order
*/
public function setBudgetOrder(Budget $budget, int $order): void
{
@@ -420,14 +605,6 @@ class BudgetRepository implements BudgetRepositoryInterface
$budget->save();
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @inheritDoc
*/
@@ -462,28 +639,28 @@ class BudgetRepository implements BudgetRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'id' => (string) $currencyId,
'name' => $journal['currency_name'],
'symbol' => $journal['currency_symbol'],
'code' => $journal['currency_code'],
'decimal_places' => $journal['currency_decimal_places'],
'sum' => '0',
];
'id' => (string)$currencyId,
'name' => $journal['currency_name'],
'symbol' => $journal['currency_symbol'],
'code' => $journal['currency_code'],
'decimal_places' => $journal['currency_decimal_places'],
'sum' => '0',
];
$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] = $array[$foreignId] ?? [
'id' => (string) $foreignId,
'name' => $journal['foreign_currency_name'],
'symbol' => $journal['foreign_currency_symbol'],
'code' => $journal['foreign_currency_code'],
'decimal_places' => $journal['foreign_currency_decimal_places'],
'sum' => '0',
];
'id' => (string)$foreignId,
'name' => $journal['foreign_currency_name'],
'symbol' => $journal['foreign_currency_symbol'],
'code' => $journal['foreign_currency_code'],
'decimal_places' => $journal['foreign_currency_decimal_places'],
'sum' => '0',
];
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->negative($journal['foreign_amount']));
}
}
@@ -492,7 +669,7 @@ class BudgetRepository implements BudgetRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return Budget
* @throws FireflyException
@@ -518,7 +695,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)) {
@@ -542,10 +719,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')->getDefaultCurrencyByUser($this->user);
@@ -581,180 +758,6 @@ class BudgetRepository implements BudgetRepositoryInterface
public function getMaxOrder(): int
{
return (int) $this->user->budgets()->max('order');
}
/**
* @param Budget $budget
* @param string $text
* @return void
*/
private function setNoteText(Budget $budget, string $text): void
{
$dbNote = $budget->notes()->first();
if ('' !== $text) {
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($budget);
}
$dbNote->text = trim($text);
$dbNote->save();
return;
}
if (null !== $dbNote) {
try {
$dbNote->delete();
} catch (Exception $e) {
// @ignoreException
}
}
}
/**
* @param Budget $budget
* @param array $data
*
* @return Budget
*/
public function update(Budget $budget, array $data): Budget
{
Log::debug('Now in update()');
$oldName = $budget->name;
if (array_key_exists('name', $data)) {
$budget->name = $data['name'];
$this->updateRuleActions($oldName, $budget->name);
$this->updateRuleTriggers($oldName, $budget->name);
}
if (array_key_exists('active', $data)) {
$budget->active = $data['active'];
}
if (array_key_exists('notes', $data)) {
$this->setNoteText($budget, (string) $data['notes']);
}
$budget->save();
// update or create auto-budget:
$autoBudget = $this->getAutoBudget($budget);
// first things first: delete when no longer required:
$autoBudgetType = array_key_exists('auto_budget_type', $data) ? $data['auto_budget_type'] : null;
if (0 === $autoBudgetType && null !== $autoBudget) {
// delete!
$autoBudget->delete();
return $budget;
}
if (0 === $autoBudgetType && null === $autoBudget) {
return $budget;
}
if (null === $autoBudgetType && null === $autoBudget) {
return $budget;
}
$this->updateAutoBudget($budget, $data);
return $budget;
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleActions(string $oldName, string $newName): void
{
$types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleTriggers(string $oldName, string $newName): void
{
$types = ['budget_is',];
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*']);
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
/** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) {
$trigger->trigger_value = $newName;
$trigger->save();
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
}
}
/**
* @inheritDoc
*/
public function getAutoBudget(Budget $budget): ?AutoBudget
{
return $budget->autoBudgets()->first();
}
/**
* @param Budget $budget
* @param array $data
* @throws FireflyException
* @throws JsonException
*/
private function updateAutoBudget(Budget $budget, array $data): void
{
// update or create auto-budget:
$autoBudget = $this->getAutoBudget($budget);
// grab default currency:
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
if (null === $autoBudget) {
// at this point it's a blind assumption auto_budget_type is 1 or 2.
$autoBudget = new AutoBudget();
$autoBudget->auto_budget_type = $data['auto_budget_type'];
$autoBudget->budget_id = $budget->id;
$autoBudget->transaction_currency_id = $currency->id;
}
// set or update the currency.
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
$repos = app(CurrencyRepositoryInterface::class);
$currencyId = (int) ($data['currency_id'] ?? 0);
$currencyCode = (string) ($data['currency_code'] ?? '');
$currency = $repos->find($currencyId);
if (null === $currency) {
$currency = $repos->findByCodeNull($currencyCode);
}
if (null !== $currency) {
$autoBudget->transaction_currency_id = $currency->id;
}
}
// change values if submitted or presented:
if (array_key_exists('auto_budget_type', $data)) {
$autoBudget->auto_budget_type = $data['auto_budget_type'];
}
if (array_key_exists('auto_budget_amount', $data)) {
$autoBudget->amount = $data['auto_budget_amount'];
}
if (array_key_exists('auto_budget_period', $data)) {
$autoBudget->period = $data['auto_budget_period'];
}
$autoBudget->save();
return (int)$this->user->budgets()->max('order');
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* BudgetRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -35,16 +36,16 @@ use Illuminate\Support\Collection;
interface BudgetRepositoryInterface
{
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function budgetEndsWith(string $query, int $limit): Collection;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
@@ -53,8 +54,8 @@ interface BudgetRepositoryInterface
/**
* Returns the amount that is budgeted in a period.
*
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @return array
*/
public function budgetedInPeriod(Carbon $start, Carbon $end): array;
@@ -65,7 +66,7 @@ interface BudgetRepositoryInterface
public function cleanupBudgets(): bool;
/**
* @param Budget $budget
* @param Budget $budget
*
* @return bool
*/
@@ -77,21 +78,21 @@ interface BudgetRepositoryInterface
public function destroyAll(): void;
/**
* @param Budget $budget
* @param Budget $budget
*/
public function destroyAutoBudget(Budget $budget): void;
/**
*
* @param int|null $budgetId
* @param int|null $budgetId
*
* @return Budget|null
*/
public function find(int $budgetId = null): ?Budget;
/**
* @param int|null $budgetId
* @param string|null $budgetName
* @param int|null $budgetId
* @param string|null $budgetName
*
* @return Budget|null
*/
@@ -100,7 +101,7 @@ interface BudgetRepositoryInterface
/**
* Find budget by name.
*
* @param string|null $name
* @param string|null $name
*
* @return Budget|null
*/
@@ -110,7 +111,7 @@ interface BudgetRepositoryInterface
* This method returns the oldest journal or transaction date known to this budget.
* Will cache result.
*
* @param Budget $budget
* @param Budget $budget
*
* @return Carbon|null
*/
@@ -122,14 +123,14 @@ interface BudgetRepositoryInterface
public function getActiveBudgets(): Collection;
/**
* @param Budget $budget
* @param Budget $budget
*
* @return Collection
*/
public function getAttachments(Budget $budget): Collection;
/**
* @param Budget $budget
* @param Budget $budget
*
* @return AutoBudget|null
*/
@@ -143,7 +144,7 @@ interface BudgetRepositoryInterface
/**
* Get all budgets with these ID's.
*
* @param array $budgetIds
* @param array $budgetIds
*
* @return Collection
*/
@@ -160,42 +161,42 @@ interface BudgetRepositoryInterface
public function getMaxOrder(): int;
/**
* @param Budget $budget
* @param Budget $budget
* @return string|null
*/
public function getNoteText(Budget $budget): ?string;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchBudget(string $query, int $limit): Collection;
/**
* @param Budget $budget
* @param int $order
* @param Budget $budget
* @param int $order
*/
public function setBudgetOrder(Budget $budget, int $order): void;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* Used in the v2 API to calculate the amount of money spent in all active budgets.
*
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function spentInPeriod(Carbon $start, Carbon $end): array;
/**
* @param array $data
* @param array $data
*
* @return Budget
* @throws FireflyException
@@ -203,8 +204,8 @@ interface BudgetRepositoryInterface
public function store(array $data): Budget;
/**
* @param Budget $budget
* @param array $data
* @param Budget $budget
* @param array $data
*
* @return Budget
*/

View File

@@ -40,9 +40,9 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
private $user;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -61,19 +61,19 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
/** @var array $journal */
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$data[$currencyId] = $data[$currencyId] ?? [
'id' => 0,
'name' => sprintf('%s (%s)', trans('firefly.no_budget'), $journal['currency_name']),
'sum' => '0',
'currency_id' => $currencyId,
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'entries' => [],
];
'id' => 0,
'name' => sprintf('%s (%s)', trans('firefly.no_budget'), $journal['currency_name']),
'sum' => '0',
'currency_id' => $currencyId,
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'entries' => [],
];
$date = $journal['date']->format($carbonFormat);
if (!array_key_exists($date, $data[$currencyId]['entries'])) {
@@ -86,17 +86,9 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -133,7 +125,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'],
@@ -145,14 +137,22 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
return $return;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* TODO this method does not include multi currency. It just counts.
* TODO this probably also applies to the other "sumExpenses" methods.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param TransactionCurrency|null $currency
*
* @return array
*/
@@ -174,15 +174,15 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
}

View File

@@ -34,9 +34,9 @@ use Illuminate\Support\Collection;
interface NoBudgetRepositoryInterface
{
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -44,14 +44,14 @@ interface NoBudgetRepositoryInterface
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -59,10 +59,10 @@ interface NoBudgetRepositoryInterface
public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param TransactionCurrency|null $currency
*
* @return array
*/

View File

@@ -46,7 +46,7 @@ class OperationsRepository implements OperationsRepositoryInterface
* A method that returns the amount of money budgeted per day for this budget,
* on average.
*
* @param Budget $budget
* @param Budget $budget
*
* @return string
*/
@@ -58,15 +58,15 @@ class OperationsRepository implements OperationsRepositoryInterface
foreach ($budget->budgetlimits as $limit) {
$diff = $limit->start_date->diffInDays($limit->end_date);
$diff = 0 === $diff ? 1 : $diff;
$amount = (string) $limit->amount;
$perDay = bcdiv($amount, (string) $diff);
$amount = (string)$limit->amount;
$perDay = bcdiv($amount, (string)$diff);
$total = bcadd($total, $perDay);
$count++;
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);
}
Log::debug(sprintf('%s / %d = %s = average.', $total, $count, $avg));
@@ -77,10 +77,10 @@ class OperationsRepository implements OperationsRepositoryInterface
* This method is being used to generate the budget overview in the year/multi-year report. Its used
* in both the year/multi-year budget overview AND in the accompanying chart.
*
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -100,22 +100,22 @@ 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] = $data[$key] ?? [
'id' => $budgetId,
'name' => sprintf('%s (%s)', $budgetName, $journal['currency_name']),
'sum' => '0',
'currency_id' => $currencyId,
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'entries' => [],
];
'id' => $budgetId,
'name' => sprintf('%s (%s)', $budgetName, $journal['currency_name']),
'sum' => '0',
'currency_id' => $currencyId,
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'entries' => [],
];
$date = $journal['date']->format($carbonFormat);
$data[$key]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date] ?? '0', $journal['amount']);
}
@@ -128,10 +128,10 @@ class OperationsRepository implements OperationsRepositoryInterface
* which have the specified budget set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
*
* @return array
*/
@@ -154,9 +154,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) {
@@ -165,24 +165,24 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the currency:
$array[$currencyId] = $array[$currencyId] ?? [
'budgets' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'budgets' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// info about the categories:
$array[$currencyId]['budgets'][$budgetId] = $array[$currencyId]['budgets'][$budgetId] ?? [
'id' => $budgetId,
'name' => $budgetName,
'transaction_journals' => [],
];
'id' => $budgetId,
'name' => $budgetName,
'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]['budgets'][$budgetId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->negative($journal['amount']),
'destination_account_id' => $journal['destination_account_id'],
@@ -199,6 +199,14 @@ class OperationsRepository implements OperationsRepositoryInterface
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @return Collection
*/
@@ -210,20 +218,11 @@ class OperationsRepository implements OperationsRepositoryInterface
return $repos->getActiveBudgets();
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -268,7 +267,7 @@ class OperationsRepository implements OperationsRepositoryInterface
/** @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'],
@@ -281,13 +280,13 @@ class OperationsRepository implements OperationsRepositoryInterface
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
* @param TransactionCurrency|null $currency
* @deprecated
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
* @param TransactionCurrency|null $currency
* @return array
* @deprecated
*/
public function sumExpenses(
Carbon $start,
@@ -355,28 +354,28 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$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] = $array[$foreignId] ?? [
'sum' => '0',
'currency_id' => $foreignId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $foreignId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->negative($journal['foreign_amount']));
}
}
@@ -388,9 +387,9 @@ class OperationsRepository implements OperationsRepositoryInterface
* For now, simply refer to whichever repository holds this function.
* TODO perhaps better in the future.
*
* @param Budget $budget
* @param Carbon|null $start
* @param Carbon|null $end
* @param Budget $budget
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/

View File

@@ -38,17 +38,17 @@ interface OperationsRepositoryInterface
* A method that returns the amount of money budgeted per day for this budget,
* on average.
*
* @param Budget $budget
* @param Budget $budget
*
* @return string
*/
public function budgetedPerDay(Budget $budget): string;
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -60,17 +60,17 @@ interface OperationsRepositoryInterface
* which have the specified budget set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
*
* @return array
*/
public function listExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
@@ -78,10 +78,10 @@ interface OperationsRepositoryInterface
/**
* Return multi-currency spent information.
*
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @deprecated
@@ -89,14 +89,14 @@ interface OperationsRepositoryInterface
public function spentInPeriodMc(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @deprecated
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
* @param TransactionCurrency|null $currency
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
* @param TransactionCurrency|null $currency
*
* @return array
* @deprecated
*/
public function sumExpenses(
Carbon $start,

View File

@@ -1,4 +1,5 @@
<?php
/**
* CategoryRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -73,7 +74,7 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param Category $category
*
* @return bool
*
@@ -115,8 +116,8 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param int|null $categoryId
* @param string|null $categoryName
* @param int|null $categoryId
* @param string|null $categoryName
*
* @return Category|null
* @throws FireflyException
@@ -125,11 +126,11 @@ class CategoryRepository implements CategoryRepositoryInterface
{
Log::debug('Now in findCategory()');
Log::debug(sprintf('Searching for category with ID #%d...', $categoryId));
$result = $this->find((int) $categoryId);
$result = $this->find((int)$categoryId);
if (null === $result) {
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]);
}
@@ -145,7 +146,7 @@ class CategoryRepository implements CategoryRepositoryInterface
/**
* Find a category or return NULL
*
* @param int $categoryId
* @param int $categoryId
*
* @return Category|null
*/
@@ -157,7 +158,7 @@ class CategoryRepository implements CategoryRepositoryInterface
/**
* Find a category.
*
* @param string $name
* @param string $name
*
* @return Category|null
*/
@@ -167,7 +168,7 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return Category
* @throws FireflyException
@@ -195,7 +196,15 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param Category $category
*/
public function removeNotes(Category $category): void
{
@@ -217,7 +226,7 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param Category $category
*
* @return Carbon|null
*
@@ -245,7 +254,7 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param Category $category
*
* @return Carbon|null
*/
@@ -262,7 +271,7 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param Category $category
*
* @return Carbon|null
*/
@@ -305,7 +314,7 @@ class CategoryRepository implements CategoryRepositoryInterface
/**
* Get all categories with ID's.
*
* @param array $categoryIds
* @param array $categoryIds
*
* @return Collection
*/
@@ -328,8 +337,8 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param Collection $accounts
* @param Category $category
* @param Collection $accounts
*
* @return Carbon|null
* @throws Exception
@@ -357,8 +366,8 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param Collection $accounts
* @param Category $category
* @param Collection $accounts
*
* @return Carbon|null
*/
@@ -381,8 +390,8 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param Category $category
* @param Collection $accounts
* @param Category $category
* @param Collection $accounts
*
* @return Carbon|null
* @throws Exception
@@ -407,8 +416,8 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
@@ -423,16 +432,8 @@ class CategoryRepository implements CategoryRepositoryInterface
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param Category $category
* @param array $data
* @param Category $category
* @param array $data
*
* @return Category
* @throws Exception

View File

@@ -1,4 +1,5 @@
<?php
/**
* CategoryRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -34,23 +35,23 @@ use Illuminate\Support\Collection;
interface CategoryRepositoryInterface
{
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function categoryEndsWith(string $query, int $limit): Collection;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function categoryStartsWith(string $query, int $limit): Collection;
/**
* @param Category $category
* @param Category $category
*
* @return bool
*/
@@ -64,7 +65,7 @@ interface CategoryRepositoryInterface
/**
* Find a category or return NULL
*
* @param int $categoryId
* @param int $categoryId
*
* @return Category|null
*/
@@ -73,29 +74,29 @@ interface CategoryRepositoryInterface
/**
* Find a category.
*
* @param string $name
* @param string $name
*
* @return Category|null
*/
public function findByName(string $name): ?Category;
/**
* @param int|null $categoryId
* @param string|null $categoryName
* @param int|null $categoryId
* @param string|null $categoryName
*
* @return Category|null
*/
public function findCategory(?int $categoryId, ?string $categoryName): ?Category;
/**
* @param Category $category
* @param Category $category
*
* @return Carbon|null
*/
public function firstUseDate(Category $category): ?Carbon;
/**
* @param Category $category
* @param Category $category
*
* @return Collection
*/
@@ -104,7 +105,7 @@ interface CategoryRepositoryInterface
/**
* Get all categories with ID's.
*
* @param array $categoryIds
* @param array $categoryIds
*
* @return Collection
*/
@@ -118,7 +119,7 @@ interface CategoryRepositoryInterface
public function getCategories(): Collection;
/**
* @param Category $category
* @param Category $category
*
* @return string|null
*/
@@ -127,8 +128,8 @@ interface CategoryRepositoryInterface
/**
* Return most recent transaction(journal) date or null when never used before.
*
* @param Category $category
* @param Collection $accounts
* @param Category $category
* @param Collection $accounts
*
* @return Carbon|null
*/
@@ -137,25 +138,25 @@ interface CategoryRepositoryInterface
/**
* Remove notes.
*
* @param Category $category
* @param Category $category
*/
public function removeNotes(Category $category): void;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchCategory(string $query, int $limit): Collection;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* @param array $data
* @param array $data
*
* @return Category
* @throws FireflyException
@@ -163,16 +164,16 @@ interface CategoryRepositoryInterface
public function store(array $data): Category;
/**
* @param Category $category
* @param array $data
* @param Category $category
* @param array $data
*
* @return Category
*/
public function update(Category $category, array $data): Category;
/**
* @param Category $category
* @param string $notes
* @param Category $category
* @param string $notes
*/
public function updateNotes(Category $category, string $notes): void;
}

View File

@@ -42,9 +42,9 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
* which have no category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -60,25 +60,25 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// info about the non-existent category:
$array[$currencyId]['categories'][0] = $array[$currencyId]['categories'][0] ?? [
'id' => 0,
'name' => (string) trans('firefly.noCategory'),
'transaction_journals' => [],
];
'id' => 0,
'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']),
@@ -89,14 +89,22 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* This method returns a list of all the deposit transaction journals (as arrays) set in that period
* which have no category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -112,25 +120,25 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// info about the non-existent category:
$array[$currencyId]['categories'][0] = $array[$currencyId]['categories'][0] ?? [
'id' => 0,
'name' => (string) trans('firefly.noCategory'),
'transaction_journals' => [],
];
'id' => 0,
'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']),
@@ -141,20 +149,12 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Sum of withdrawal journals in period without a category, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -171,15 +171,15 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount'] ?? '0'));
}
@@ -189,9 +189,9 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
/**
* Sum of income journals in period without a category, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -208,15 +208,15 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
}
@@ -239,15 +239,15 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
}

View File

@@ -38,9 +38,9 @@ interface NoCategoryRepositoryInterface
* which have no category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -51,25 +51,25 @@ interface NoCategoryRepositoryInterface
* which have no category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* Sum of withdrawal journals in period without a category, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -78,9 +78,9 @@ interface NoCategoryRepositoryInterface
/**
* Sum of income journals in period without a category, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
@@ -89,9 +89,9 @@ interface NoCategoryRepositoryInterface
/**
* Sum of transfers in period without a category, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/

View File

@@ -44,10 +44,10 @@ class OperationsRepository implements OperationsRepositoryInterface
*
* First currency, then categories.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -70,9 +70,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) {
@@ -81,24 +81,24 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the currency:
$array[$currencyId] = $array[$currencyId] ?? [
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// info about the categories:
$array[$currencyId]['categories'][$categoryId] = $array[$currencyId]['categories'][$categoryId] ?? [
'id' => $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
'id' => $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'],
@@ -115,6 +115,14 @@ class OperationsRepository implements OperationsRepositoryInterface
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Returns a list of all the categories belonging to a user.
*
@@ -130,10 +138,10 @@ class OperationsRepository implements OperationsRepositoryInterface
* which have the specified category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -156,35 +164,35 @@ 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] = $array[$currencyId] ?? [
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// info about the categories:
$array[$currencyId]['categories'][$categoryId] = $array[$currencyId]['categories'][$categoryId] ?? [
'id' => $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
'id' => $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'],
@@ -220,9 +228,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) {
@@ -231,24 +239,24 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the currency:
$array[$currencyId] = $array[$currencyId] ?? [
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// info about the categories:
$array[$currencyId]['categories'][$categoryId] = $array[$currencyId]['categories'][$categoryId] ?? [
'id' => $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
'id' => $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'],
@@ -285,9 +293,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) {
@@ -296,24 +304,24 @@ class OperationsRepository implements OperationsRepositoryInterface
// info about the currency:
$array[$currencyId] = $array[$currencyId] ?? [
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'categories' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// info about the categories:
$array[$currencyId]['categories'][$categoryId] = $array[$currencyId]['categories'][$categoryId] ?? [
'id' => $categoryId,
'name' => $categoryName,
'transaction_journals' => [],
];
'id' => $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'],
@@ -330,21 +338,13 @@ class OperationsRepository implements OperationsRepositoryInterface
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Sum of withdrawal journals in period for a set of categories, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -367,15 +367,15 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => (int) $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => (int)$journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($journal['amount']));
}
@@ -385,10 +385,10 @@ class OperationsRepository implements OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of categories, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -410,15 +410,15 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
}
@@ -428,10 +428,10 @@ class OperationsRepository implements OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of categories, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -453,15 +453,15 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
}

View File

@@ -38,10 +38,10 @@ interface OperationsRepositoryInterface
* which have the specified category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -52,10 +52,10 @@ interface OperationsRepositoryInterface
* which have the specified category set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -67,10 +67,10 @@ interface OperationsRepositoryInterface
* It excludes any transfers between the listed accounts.
* It's grouped per currency, with as few details in the array as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -82,27 +82,27 @@ interface OperationsRepositoryInterface
* It excludes any transfers between the listed accounts.
* It's grouped per currency, with as few details in the array as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection|null $categories
*
* @return array
*/
public function listTransferredOut(Carbon $start, Carbon $end, Collection $accounts, ?Collection $categories = null): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* Sum of withdrawal journals in period for a set of categories, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -111,10 +111,10 @@ interface OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of categories, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
@@ -123,10 +123,10 @@ interface OperationsRepositoryInterface
/**
* Sum of transfers in period for a set of categories, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* CurrencyRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -50,7 +51,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
private User $user;
/**
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return bool
* @throws FireflyException
@@ -63,7 +64,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return string|null
* @throws FireflyException
@@ -86,7 +87,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
// is being used in accounts:
$meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((string) $currency->id))->count();
$meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((string)$currency->id))->count();
if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
@@ -113,8 +114,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface
// is being used in accounts (as integer)
$meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode((int) $currency->id))->count();
->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode((int)$currency->id))->count();
if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
@@ -151,7 +152,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return int
*/
@@ -172,7 +173,15 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param TransactionCurrency $currency
* @return Collection
*/
public function get(): Collection
{
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
}
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
@@ -192,7 +201,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Disables a currency
*
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void
{
@@ -219,11 +228,21 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
}
/**
* @param TransactionCurrency $currency
* Enables a currency
*/
public function enable(TransactionCurrency $currency): void
{
$currency->enabled = true;
$currency->save();
}
/**
* Find by currency code, return NULL if unfound.
* Used in Import Currency!
*
* @param string $currencyCode
* @param string $currencyCode
*
* @return TransactionCurrency|null
* @deprecated
@@ -236,7 +255,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by currency name.
*
* @param string $currencyName
* @param string $currencyName
*
* @return TransactionCurrency|null
*/
@@ -249,7 +268,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
* Find by currency name or return null.
* Used in Import Currency!
*
* @param string $currencyName
* @param string $currencyName
*
* @return TransactionCurrency|null
* @deprecated
@@ -262,7 +281,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by currency symbol.
*
* @param string $currencySymbol
* @param string $currencySymbol
*
* @return TransactionCurrency|null
*/
@@ -275,7 +294,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
* Find by currency symbol or return NULL
* Used in Import Currency!
*
* @param string $currencySymbol
* @param string $currencySymbol
*
* @return TransactionCurrency|null
* @deprecated
@@ -288,8 +307,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
* @throws FireflyException
@@ -320,18 +339,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
Log::debug('Now in findCurrencyNull()');
$result = $this->find((int) $currencyId);
$result = $this->find((int)$currencyId);
if (null === $result) {
Log::debug(sprintf('Searching for currency with code %s...', $currencyCode));
$result = $this->findByCode((string) $currencyCode);
$result = $this->findByCode((string)$currencyCode);
}
if (null !== $result && false === $result->enabled) {
Log::debug(sprintf('Also enabled currency %s', $result->code));
@@ -344,7 +363,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
@@ -356,7 +375,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
@@ -366,25 +385,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param TransactionCurrency $currency
* Enables a currency
*/
public function enable(TransactionCurrency $currency): void
{
$currency->enabled = true;
$currency->save();
}
/**
* @return Collection
*/
public function get(): Collection
{
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
}
/**
* @param array $ids
* @param array $ids
*
* @return Collection
*/
@@ -394,7 +395,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param Preference $preference
* @param Preference $preference
*
* @return TransactionCurrency
*/
@@ -411,9 +412,9 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* Get currency exchange rate.
*
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
*
* @return CurrencyExchangeRate|null
*/
@@ -440,12 +441,36 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return null;
}
/**
* @inheritDoc
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool
{
return $currency->code === config('firefly.default_currency', 'EUR');
}
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', true);
if ('' !== $search) {
$query->where('name', 'LIKE', sprintf('%%%s%%', $search));
}
return $query->take($limit)->get();
}
/**
* TODO must be a factory
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
* @param float $rate
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
* @param float $rate
* @return CurrencyExchangeRate
*/
public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate
@@ -462,31 +487,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @inheritDoc
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool
{
return $currency->code === config('firefly.default_currency', 'EUR');
}
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', true);
if ('' !== $search) {
$query->where('name', 'LIKE', sprintf('%%%s%%', $search));
}
return $query->take($limit)->get();
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{
@@ -494,7 +495,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
@@ -513,8 +514,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
/**
* @param TransactionCurrency $currency
* @param array $data
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* CurrencyRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -36,14 +37,14 @@ use Illuminate\Support\Collection;
interface CurrencyRepositoryInterface
{
/**
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return int
*/
public function countJournals(TransactionCurrency $currency): int;
/**
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return bool
*/
@@ -52,14 +53,14 @@ interface CurrencyRepositoryInterface
/**
* Currency is in use where exactly.
*
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return string|null
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string;
/**
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return bool
*/
@@ -68,14 +69,14 @@ interface CurrencyRepositoryInterface
/**
* Disables a currency
*
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void;
/**
* Enables a currency
*
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*/
public function enable(TransactionCurrency $currency): void;
@@ -87,7 +88,7 @@ interface CurrencyRepositoryInterface
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
@@ -96,7 +97,7 @@ interface CurrencyRepositoryInterface
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
@@ -105,7 +106,7 @@ interface CurrencyRepositoryInterface
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
@@ -114,7 +115,7 @@ interface CurrencyRepositoryInterface
/**
* Find by currency name.
*
* @param string $currencyName
* @param string $currencyName
*
* @return TransactionCurrency|null
*/
@@ -123,7 +124,7 @@ interface CurrencyRepositoryInterface
/**
* Find by currency name.
*
* @param string $currencyName
* @param string $currencyName
*
* @return TransactionCurrency|null
*/
@@ -132,7 +133,7 @@ interface CurrencyRepositoryInterface
/**
* Find by currency symbol.
*
* @param string $currencySymbol
* @param string $currencySymbol
*
* @return TransactionCurrency|null
*/
@@ -141,7 +142,7 @@ interface CurrencyRepositoryInterface
/**
* Find by currency symbol.
*
* @param string $currencySymbol
* @param string $currencySymbol
*
* @return TransactionCurrency|null
*/
@@ -150,8 +151,8 @@ interface CurrencyRepositoryInterface
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
*/
@@ -160,8 +161,8 @@ interface CurrencyRepositoryInterface
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
@@ -178,14 +179,14 @@ interface CurrencyRepositoryInterface
public function getAll(): Collection;
/**
* @param array $ids
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids): Collection;
/**
* @param Preference $preference
* @param Preference $preference
*
* @return TransactionCurrency
*/
@@ -194,46 +195,46 @@ interface CurrencyRepositoryInterface
/**
* Get currency exchange rate.
*
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
*
* @return CurrencyExchangeRate|null
*/
public function getExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): ?CurrencyExchangeRate;
/**
* TODO must be a factory
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
* @param float $rate
* @return CurrencyExchangeRate
*/
public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate;
/**
* @param TransactionCurrency $currency
* @param TransactionCurrency $currency
*
* @return bool
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool;
/**
* @param string $search
* @param int $limit
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection;
/**
* @param User $user
* TODO must be a factory
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
* @param float $rate
* @return CurrencyExchangeRate
*/
public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate;
/**
* @param User $user
*/
public function setUser(User $user);
/**
* @param array $data
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
@@ -241,8 +242,8 @@ interface CurrencyRepositoryInterface
public function store(array $data): TransactionCurrency;
/**
* @param TransactionCurrency $currency
* @param array $data
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/

View File

@@ -41,7 +41,7 @@ class JournalAPIRepository implements JournalAPIRepositoryInterface
/**
* Returns transaction by ID. Used to validate attachments.
*
* @param int $transactionId
* @param int $transactionId
*
* @return Transaction|null
*/
@@ -56,7 +56,7 @@ class JournalAPIRepository implements JournalAPIRepositoryInterface
/**
* Return all attachments for journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Collection
*/
@@ -91,7 +91,7 @@ class JournalAPIRepository implements JournalAPIRepositoryInterface
/**
* Get all piggy bank events for a journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Collection
*/
@@ -108,7 +108,7 @@ class JournalAPIRepository implements JournalAPIRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{

View File

@@ -36,7 +36,7 @@ interface JournalAPIRepositoryInterface
/**
* Returns transaction by ID. Used to validate attachments.
*
* @param int $transactionId
* @param int $transactionId
*
* @return Transaction|null
*/
@@ -45,7 +45,7 @@ interface JournalAPIRepositoryInterface
/**
* Return all attachments for journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Collection
*/
@@ -54,7 +54,7 @@ interface JournalAPIRepositoryInterface
/**
* Return all journal links for journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Collection
*/
@@ -63,14 +63,14 @@ interface JournalAPIRepositoryInterface
/**
* Get all piggy bank events for a journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getPiggyBankEvents(TransactionJournal $journal): Collection;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
}

View File

@@ -30,7 +30,6 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
use FireflyIII\User;
use Illuminate\Support\Collection;
use JsonException;
use stdClass;
/**
@@ -44,22 +43,22 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
/**
* Get all transaction journals with a specific type, regardless of user.
*
* @param array $types
* @param array $types
*
* @return Collection
*/
public function getAllJournals(array $types): Collection
{
return TransactionJournal::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->whereIn('transaction_types.type', $types)
->with(['user', 'transactionType', 'transactionCurrency', 'transactions', 'transactions.account'])
->get(['transaction_journals.*']);
->whereIn('transaction_types.type', $types)
->with(['user', 'transactionType', 'transactionCurrency', 'transactions', 'transactions.account'])
->get(['transaction_journals.*']);
}
/**
* Return the ID of the budget linked to the journal (if any) or the transactions (if any).
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return int
*/
@@ -80,7 +79,7 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
/**
* Return the ID of the category linked to the journal (if any) or to the transactions (if any).
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return int
*/
@@ -111,8 +110,8 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
/**
* Return Carbon value of a meta field (or NULL).
*
* @param TransactionJournal $journal
* @param string $field
* @param TransactionJournal $journal
* @param string $field
*
* @return null|Carbon
*/
@@ -154,8 +153,8 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
/**
* Return value of a meta field (or NULL) as a string.
*
* @param TransactionJournal $journal
* @param string $field
* @param TransactionJournal $journal
* @param string $field
*
* @return null|string
*/
@@ -185,7 +184,7 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
}
// return when something else:
$return = (string) $value;
$return = (string)$value;
try {
$cache->store($return);
} catch (Exception $e) {
@@ -198,7 +197,7 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
/**
* Return text of a note attached to journal, or NULL
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return string|null
*/
@@ -221,25 +220,25 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
public function getSplitJournals(): Collection
{
$query = TransactionJournal::leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->groupBy('transaction_journals.id');
->groupBy('transaction_journals.id');
$result = $query->get(['transaction_journals.id as id', DB::raw('count(transactions.id) as transaction_count')]);
$journalIds = [];
/** @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);
return TransactionJournal::with(['transactions'])
->whereIn('id', $journalIds)->get();
->whereIn('id', $journalIds)->get();
}
/**
* Return all tags as strings in an array.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return array
*/
@@ -249,7 +248,7 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{

View File

@@ -36,7 +36,7 @@ interface JournalCLIRepositoryInterface
/**
* Get all transaction journals with a specific type, regardless of user.
*
* @param array $types
* @param array $types
*
* @return Collection
*/
@@ -45,7 +45,7 @@ interface JournalCLIRepositoryInterface
/**
* Return the ID of the budget linked to the journal (if any) or the transactions (if any).
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return int
*/
@@ -54,7 +54,7 @@ interface JournalCLIRepositoryInterface
/**
* Return the ID of the category linked to the journal (if any) or to the transactions (if any).
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return int
*/
@@ -70,8 +70,8 @@ interface JournalCLIRepositoryInterface
/**
* Return Carbon value of a meta field (or NULL).
*
* @param TransactionJournal $journal
* @param string $field
* @param TransactionJournal $journal
* @param string $field
*
* @return null|Carbon
*/
@@ -80,8 +80,8 @@ interface JournalCLIRepositoryInterface
/**
* Return value of a meta field (or NULL).
*
* @param TransactionJournal $journal
* @param string $field
* @param TransactionJournal $journal
* @param string $field
*
* @return null|string
*/
@@ -90,7 +90,7 @@ interface JournalCLIRepositoryInterface
/**
* Return text of a note attached to journal, or NULL
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return string|null
*/
@@ -107,14 +107,14 @@ interface JournalCLIRepositoryInterface
/**
* Return all tags as strings in an array.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return array
*/
public function getTags(TransactionJournal $journal): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* JournalRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -37,7 +38,6 @@ use FireflyIII\Services\Internal\Update\JournalUpdateService;
use FireflyIII\Support\CacheProperties;
use FireflyIII\User;
use Illuminate\Support\Collection;
use JsonException;
/**
* Class JournalRepository.
@@ -48,7 +48,7 @@ class JournalRepository implements JournalRepositoryInterface
private $user;
/**
* @param TransactionGroup $transactionGroup
* @param TransactionGroup $transactionGroup
*
*/
public function destroyGroup(TransactionGroup $transactionGroup): void
@@ -59,7 +59,7 @@ class JournalRepository implements JournalRepositoryInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
*/
public function destroyJournal(TransactionJournal $journal): void
@@ -69,18 +69,6 @@ class JournalRepository implements JournalRepositoryInterface
$service->destroy($journal);
}
/**
* Find a specific journal.
*
* @param int $journalId
*
* @return TransactionJournal|null
*/
public function find(int $journalId): ?TransactionJournal
{
return $this->user->transactionJournals()->find($journalId);
}
/**
* @inheritDoc
*/
@@ -127,7 +115,7 @@ class JournalRepository implements JournalRepositoryInterface
/**
* Return total amount of journal. Is always positive.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return string
*/
@@ -142,7 +130,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;
@@ -164,7 +152,7 @@ class JournalRepository implements JournalRepositoryInterface
}
/**
* @param TransactionJournalLink $link
* @param TransactionJournalLink $link
*
* @return string
*/
@@ -182,8 +170,8 @@ class JournalRepository implements JournalRepositoryInterface
/**
* Return Carbon value of a meta field (or NULL).
*
* @param int $journalId
* @param string $field
* @param int $journalId
* @param string $field
*
* @return null|Carbon
*/
@@ -223,7 +211,7 @@ class JournalRepository implements JournalRepositoryInterface
}
/**
* @param int $journalId
* @param int $journalId
*/
public function reconcileById(int $journalId): void
{
@@ -232,11 +220,23 @@ class JournalRepository implements JournalRepositoryInterface
$journal?->transactions()->update(['reconciled' => true]);
}
/**
* Find a specific journal.
*
* @param int $journalId
*
* @return TransactionJournal|null
*/
public function find(int $journalId): ?TransactionJournal
{
return $this->user->transactionJournals()->find($journalId);
}
/**
* Search in journal descriptions.
*
* @param string $search
* @param int $limit
* @param string $search
* @param int $limit
*
* @return Collection
*/
@@ -252,7 +252,7 @@ class JournalRepository implements JournalRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{
@@ -262,8 +262,8 @@ class JournalRepository implements JournalRepositoryInterface
/**
* Update budget for a journal.
*
* @param TransactionJournal $journal
* @param int $budgetId
* @param TransactionJournal $journal
* @param int $budgetId
*
* @return TransactionJournal
*/
@@ -287,8 +287,8 @@ class JournalRepository implements JournalRepositoryInterface
/**
* Update category for a journal.
*
* @param TransactionJournal $journal
* @param string $category
* @param TransactionJournal $journal
* @param string $category
*
* @return TransactionJournal
*/
@@ -311,8 +311,8 @@ class JournalRepository implements JournalRepositoryInterface
/**
* Update tag(s) for a journal.
*
* @param TransactionJournal $journal
* @param array $tags
* @param TransactionJournal $journal
* @param array $tags
*
* @return TransactionJournal
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* JournalRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -39,28 +40,28 @@ interface JournalRepositoryInterface
/**
* Deletes a transaction group.
*
* @param TransactionGroup $transactionGroup
* @param TransactionGroup $transactionGroup
*/
public function destroyGroup(TransactionGroup $transactionGroup): void;
/**
* Deletes a journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*/
public function destroyJournal(TransactionJournal $journal): void;
/**
* Find a specific journal.
*
* @param int $journalId
* @param int $journalId
*
* @return TransactionJournal|null
*/
public function find(int $journalId): ?TransactionJournal;
/**
* @param array $types
* @param array $types
*
* @return Collection
*/
@@ -76,7 +77,7 @@ interface JournalRepositoryInterface
/**
* Returns the destination account of the journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
@@ -86,7 +87,7 @@ interface JournalRepositoryInterface
/**
* Return total amount of journal. Is always positive.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return string
*/
@@ -98,7 +99,7 @@ interface JournalRepositoryInterface
public function getLast(): ?TransactionJournal;
/**
* @param TransactionJournalLink $link
* @param TransactionJournalLink $link
*
* @return string
*/
@@ -107,8 +108,8 @@ interface JournalRepositoryInterface
/**
* Return Carbon value of a meta field (or NULL).
*
* @param int $journalId
* @param string $field
* @param int $journalId
* @param string $field
*
* @return null|Carbon
*/
@@ -117,7 +118,7 @@ interface JournalRepositoryInterface
/**
* Returns the source account of the journal.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Account
* @throws FireflyException
@@ -127,30 +128,30 @@ interface JournalRepositoryInterface
/**
* TODO Maybe to account repository? Do this wen reconcile is API only.
*
* @param int $journalId
* @param int $journalId
*/
public function reconcileById(int $journalId): void;
/**
* Search in journal descriptions.
*
* @param string $search
* @param int $limit
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchJournalDescriptions(string $search, int $limit): Collection;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* Update budget for a journal.
*
* @param TransactionJournal $journal
* @param int $budgetId
* @param TransactionJournal $journal
* @param int $budgetId
*
* @return TransactionJournal
*/
@@ -159,8 +160,8 @@ interface JournalRepositoryInterface
/**
* Update category for a journal.
*
* @param TransactionJournal $journal
* @param string $category
* @param TransactionJournal $journal
* @param string $category
*
* @return TransactionJournal
*/
@@ -169,8 +170,8 @@ interface JournalRepositoryInterface
/**
* Update tag(s) for a journal.
*
* @param TransactionJournal $journal
* @param array $tags
* @param TransactionJournal $journal
* @param array $tags
*
* @return TransactionJournal
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* LinkTypeRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -41,7 +42,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
private User $user;
/**
* @param LinkType $linkType
* @param LinkType $linkType
*
* @return int
*/
@@ -51,8 +52,8 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
}
/**
* @param LinkType $linkType
* @param LinkType|null $moveTo
* @param LinkType $linkType
* @param LinkType|null $moveTo
*
* @return bool
*/
@@ -67,7 +68,29 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
}
/**
* @param TransactionJournalLink $link
* @param LinkType $linkType
* @param array $data
*
* @return LinkType
*/
public function update(LinkType $linkType, array $data): LinkType
{
if (array_key_exists('name', $data) && '' !== (string)$data['name']) {
$linkType->name = $data['name'];
}
if (array_key_exists('inward', $data) && '' !== (string)$data['inward']) {
$linkType->inward = $data['inward'];
}
if (array_key_exists('outward', $data) && '' !== (string)$data['outward']) {
$linkType->outward = $data['outward'];
}
$linkType->save();
return $linkType;
}
/**
* @param TransactionJournalLink $link
*
* @return bool
* @throws Exception
@@ -83,8 +106,8 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
/**
* Check if link exists between journals.
*
* @param TransactionJournal $one
* @param TransactionJournal $two
* @param TransactionJournal $one
* @param TransactionJournal $two
*
* @return bool
*/
@@ -97,18 +120,10 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
return $count + $opposingCount > 0;
}
/**
* @return Collection
*/
public function get(): Collection
{
return LinkType::orderBy('name', 'ASC')->get();
}
/**
* Return array of all journal ID's for this type of link.
*
* @param LinkType $linkType
* @param LinkType $linkType
*
* @return array
*/
@@ -121,22 +136,30 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
return array_unique(array_merge($sources, $destinations));
}
/**
* @return Collection
*/
public function get(): Collection
{
return LinkType::orderBy('name', 'ASC')->get();
}
/**
* Returns all the journal links (of a specific type).
*
* @param LinkType|null $linkType
* @param LinkType|null $linkType
*
* @return Collection
*/
public function getJournalLinks(LinkType $linkType = null): Collection
{
$query = TransactionJournalLink::with(['source', 'destination'])
->leftJoin('transaction_journals as source_journals', 'journal_links.source_id', '=', 'source_journals.id')
->leftJoin('transaction_journals as dest_journals', 'journal_links.destination_id', '=', 'dest_journals.id')
->where('source_journals.user_id', $this->user->id)
->where('dest_journals.user_id', $this->user->id)
->whereNull('source_journals.deleted_at')
->whereNull('dest_journals.deleted_at');
->leftJoin('transaction_journals as source_journals', 'journal_links.source_id', '=', 'source_journals.id')
->leftJoin('transaction_journals as dest_journals', 'journal_links.destination_id', '=', 'dest_journals.id')
->where('source_journals.user_id', $this->user->id)
->where('dest_journals.user_id', $this->user->id)
->whereNull('source_journals.deleted_at')
->whereNull('dest_journals.deleted_at');
if (null !== $linkType) {
$query->where('journal_links.link_type_id', $linkType->id);
@@ -158,7 +181,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
/**
* Return list of existing connections.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Collection
*/
@@ -176,7 +199,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{
@@ -184,7 +207,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return LinkType
*/
@@ -203,16 +226,16 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
/**
* Store link between two journals.
*
* @param array $information
* @param TransactionJournal $inward
* @param TransactionJournal $outward
* @param array $information
* @param TransactionJournal $inward
* @param TransactionJournal $outward
*
* @return TransactionJournalLink|null
* @throws Exception
*/
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']);
@@ -244,13 +267,13 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
$link->save();
// make note in noteable:
$this->setNoteText($link, (string) $information['notes']);
$this->setNoteText($link, (string)$information['notes']);
return $link;
}
/**
* @param int $linkTypeId
* @param int $linkTypeId
*
* @return LinkType|null
*/
@@ -260,7 +283,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
}
/**
* @param string|null $name
* @param string|null $name
*
* @return LinkType|null
*/
@@ -276,22 +299,22 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
/**
* See if such a link already exists (and get it).
*
* @param LinkType $linkType
* @param TransactionJournal $inward
* @param TransactionJournal $outward
* @param LinkType $linkType
* @param TransactionJournal $inward
* @param TransactionJournal $outward
*
* @return TransactionJournalLink|null
*/
public function findSpecificLink(LinkType $linkType, TransactionJournal $inward, TransactionJournal $outward): ?TransactionJournalLink
{
return TransactionJournalLink::where('link_type_id', $linkType->id)
->where('source_id', $inward->id)
->where('destination_id', $outward->id)->first();
->where('source_id', $inward->id)
->where('destination_id', $outward->id)->first();
}
/**
* @param TransactionJournalLink $link
* @param string $text
* @param TransactionJournalLink $link
* @param string $text
*
* @throws Exception
*/
@@ -332,7 +355,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
}
/**
* @param TransactionJournalLink $link
* @param TransactionJournalLink $link
*
* @return bool
*/
@@ -346,33 +369,11 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
return true;
}
/**
* @param LinkType $linkType
* @param array $data
*
* @return LinkType
*/
public function update(LinkType $linkType, array $data): LinkType
{
if (array_key_exists('name', $data) && '' !== (string) $data['name']) {
$linkType->name = $data['name'];
}
if (array_key_exists('inward', $data) && '' !== (string) $data['inward']) {
$linkType->inward = $data['inward'];
}
if (array_key_exists('outward', $data) && '' !== (string) $data['outward']) {
$linkType->outward = $data['outward'];
}
$linkType->save();
return $linkType;
}
/**
* Update an existing transaction journal link.
*
* @param TransactionJournalLink $journalLink
* @param array $data
* @param TransactionJournalLink $journalLink
* @param array $data
*
* @return TransactionJournalLink
* @throws Exception

View File

@@ -1,4 +1,5 @@
<?php
/**
* LinkTypeRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -34,29 +35,29 @@ use Illuminate\Support\Collection;
interface LinkTypeRepositoryInterface
{
/**
* @param LinkType $linkType
* @param LinkType $linkType
*
* @return int
*/
public function countJournals(LinkType $linkType): int;
/**
* @param LinkType $linkType
* @param LinkType|null $moveTo
* @param LinkType $linkType
* @param LinkType|null $moveTo
*
* @return bool
*/
public function destroy(LinkType $linkType, LinkType $moveTo = null): bool;
/**
* @param TransactionJournalLink $link
* @param TransactionJournalLink $link
*
* @return bool
*/
public function destroyLink(TransactionJournalLink $link): bool;
/**
* @param int $linkTypeId
* @param int $linkTypeId
*
* @return LinkType|null
*/
@@ -65,7 +66,7 @@ interface LinkTypeRepositoryInterface
/**
* Find link type by name.
*
* @param string|null $name
* @param string|null $name
*
* @return LinkType|null
*/
@@ -74,8 +75,8 @@ interface LinkTypeRepositoryInterface
/**
* Check if link exists between journals.
*
* @param TransactionJournal $one
* @param TransactionJournal $two
* @param TransactionJournal $one
* @param TransactionJournal $two
*
* @return bool
*/
@@ -84,9 +85,9 @@ interface LinkTypeRepositoryInterface
/**
* See if such a link already exists (and get it).
*
* @param LinkType $linkType
* @param TransactionJournal $inward
* @param TransactionJournal $outward
* @param LinkType $linkType
* @param TransactionJournal $inward
* @param TransactionJournal $outward
*
* @return TransactionJournalLink|null
*/
@@ -100,14 +101,14 @@ interface LinkTypeRepositoryInterface
/**
* Return array of all journal ID's for this type of link.
*
* @param LinkType $linkType
* @param LinkType $linkType
*
* @return array
*/
public function getJournalIds(LinkType $linkType): array;
/**
* @param LinkType|null $linkType
* @param LinkType|null $linkType
*
* @return Collection
*/
@@ -116,7 +117,7 @@ interface LinkTypeRepositoryInterface
/**
* Return list of existing connections.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return Collection
*/
@@ -125,12 +126,12 @@ interface LinkTypeRepositoryInterface
/**
* Set the user for this instance.
*
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* @param array $data
* @param array $data
*
* @return LinkType
*/
@@ -139,31 +140,31 @@ interface LinkTypeRepositoryInterface
/**
* Store link between two journals.
*
* @param array $information
* @param TransactionJournal $inward
* @param TransactionJournal $outward
* @param array $information
* @param TransactionJournal $inward
* @param TransactionJournal $outward
*
* @return TransactionJournalLink|null
*/
public function storeLink(array $information, TransactionJournal $inward, TransactionJournal $outward): ?TransactionJournalLink;
/**
* @param TransactionJournalLink $link
* @param TransactionJournalLink $link
*
* @return bool
*/
public function switchLink(TransactionJournalLink $link): bool;
/**
* @param int $linkId
* @param int $linkId
*
* @return bool
*/
public function switchLinkById(int $linkId): bool;
/**
* @param LinkType $linkType
* @param array $data
* @param LinkType $linkType
* @param array $data
*
* @return LinkType
*/
@@ -172,8 +173,8 @@ interface LinkTypeRepositoryInterface
/**
* Update an existing transaction journal link.
*
* @param TransactionJournalLink $journalLink
* @param array $data
* @param TransactionJournalLink $journalLink
* @param array $data
*
* @return TransactionJournalLink
*/

View File

@@ -32,7 +32,7 @@ use FireflyIII\Models\ObjectGroup;
trait CreatesObjectGroups
{
/**
* @param int $groupId
* @param int $groupId
*
* @return ObjectGroup|null
*/
@@ -42,7 +42,7 @@ trait CreatesObjectGroups
}
/**
* @param string $title
* @param string $title
*
* @return ObjectGroup|null
*/
@@ -67,11 +67,11 @@ trait CreatesObjectGroups
*/
protected function getObjectGroupMaxOrder(): int
{
return (int) $this->user->objectGroups()->max('order');
return (int)$this->user->objectGroups()->max('order');
}
/**
* @param string $title
* @param string $title
*
* @return bool
*/
@@ -81,7 +81,7 @@ trait CreatesObjectGroups
}
/**
* @param string $title
* @param string $title
*
* @return null|ObjectGroup
*/

View File

@@ -118,7 +118,7 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
$index = 1;
/** @var ObjectGroup $objectGroup */
foreach ($list as $objectGroup) {
if ($index !== (int) $objectGroup->order) {
if ($index !== (int)$objectGroup->order) {
Log::debug(
sprintf('objectGroup #%d ("%s"): order should %d be but is %d.', $objectGroup->id, $objectGroup->title, $index, $objectGroup->order)
);
@@ -130,8 +130,8 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
}
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
@@ -151,7 +151,7 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{
@@ -168,7 +168,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();
@@ -181,7 +181,7 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
*/
public function setOrder(ObjectGroup $objectGroup, int $newOrder): ObjectGroup
{
$oldOrder = (int) $objectGroup->order;
$oldOrder = (int)$objectGroup->order;
if ($newOrder > $oldOrder) {
$this->user->objectGroups()->where('object_groups.order', '<=', $newOrder)->where('object_groups.order', '>', $oldOrder)

View File

@@ -44,7 +44,7 @@ interface ObjectGroupRepositoryInterface
public function deleteEmpty(): void;
/**
* @param ObjectGroup $objectGroup
* @param ObjectGroup $objectGroup
*/
public function destroy(ObjectGroup $objectGroup): void;
@@ -54,14 +54,14 @@ interface ObjectGroupRepositoryInterface
public function get(): Collection;
/**
* @param ObjectGroup $objectGroup
* @param ObjectGroup $objectGroup
*
* @return Collection
*/
public function getBills(ObjectGroup $objectGroup): Collection;
/**
* @param ObjectGroup $objectGroup
* @param ObjectGroup $objectGroup
*
* @return Collection
*/
@@ -73,29 +73,29 @@ interface ObjectGroupRepositoryInterface
public function resetOrder(): void;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function search(string $query, int $limit): Collection;
/**
* @param ObjectGroup $objectGroup
* @param int $newOrder
* @param ObjectGroup $objectGroup
* @param int $newOrder
*
* @return ObjectGroup
*/
public function setOrder(ObjectGroup $objectGroup, int $newOrder): ObjectGroup;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* @param ObjectGroup $objectGroup
* @param array $data
* @param ObjectGroup $objectGroup
* @param array $data
*
* @return ObjectGroup
*/

View File

@@ -42,6 +42,46 @@ trait ModifiesPiggyBanks
{
use CreatesObjectGroups;
/**
* @param PiggyBankRepetition $repetition
* @param string $amount
*
* @return void
*/
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void
{
Log::debug(sprintf('addAmountToRepetition: %s', $amount));
if (-1 === bccomp($amount, '0')) {
Log::debug('Remove amount.');
$this->removeAmount($repetition->piggyBank, bcmul($amount, '-1'), $journal);
}
if (1 === bccomp($amount, '0')) {
Log::debug('Add amount.');
$this->addAmount($repetition->piggyBank, $amount, $journal);
}
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function removeAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
{
$repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return false;
}
$repetition->currentamount = bcsub($repetition->currentamount, $amount);
$repetition->save();
Log::debug('addAmount: Trigger change for negative amount.');
event(new ChangedPiggyBankAmount($piggyBank, bcmul($amount, '-1'), $journal, null));
return true;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
@@ -64,25 +104,6 @@ trait ModifiesPiggyBanks
return true;
}
/**
* @param PiggyBankRepetition $repetition
* @param string $amount
*
* @return void
*/
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void
{
Log::debug(sprintf('addAmountToRepetition: %s', $amount));
if (-1 === bccomp($amount, '0')) {
Log::debug('Remove amount.');
$this->removeAmount($repetition->piggyBank, bcmul($amount, '-1'), $journal);
}
if (1 === bccomp($amount, '0')) {
Log::debug('Add amount.');
$this->addAmount($repetition->piggyBank, $amount, $journal);
}
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
@@ -144,27 +165,6 @@ trait ModifiesPiggyBanks
return true;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function removeAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
{
$repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return false;
}
$repetition->currentamount = bcsub($repetition->currentamount, $amount);
$repetition->save();
Log::debug('addAmount: Trigger change for negative amount.');
event(new ChangedPiggyBankAmount($piggyBank, bcmul($amount, '-1'), $journal, null));
return true;
}
/**
* @inheritDoc
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* PiggyBankRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -23,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\PiggyBank;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank;
@@ -33,6 +35,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use JsonException;
use Log;
use Storage;
@@ -55,8 +58,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
}
/**
* @param int|null $piggyBankId
* @param string|null $piggyBankName
* @param int|null $piggyBankId
* @param string|null $piggyBankName
*
* @return PiggyBank|null
*/
@@ -65,7 +68,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
Log::debug('Searching for piggy information.');
if (null !== $piggyBankId) {
$searchResult = $this->find((int) $piggyBankId);
$searchResult = $this->find((int)$piggyBankId);
if (null !== $searchResult) {
Log::debug(sprintf('Found piggy based on #%d, will return it.', $piggyBankId));
@@ -73,7 +76,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
}
}
if (null !== $piggyBankName) {
$searchResult = $this->findByName((string) $piggyBankName);
$searchResult = $this->findByName((string)$piggyBankName);
if (null !== $searchResult) {
Log::debug(sprintf('Found piggy based on "%s", will return it.', $piggyBankName));
@@ -86,7 +89,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
}
/**
* @param int $piggyBankId
* @param int $piggyBankId
*
* @return PiggyBank|null
*/
@@ -98,7 +101,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/**
* Find by name or return NULL.
*
* @param string $name
* @param string $name
*
* @return PiggyBank|null
*/
@@ -131,7 +134,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/**
* Get current amount saved in piggy bank.
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return string
*/
@@ -142,11 +145,11 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return '0';
}
return (string) $rep->currentamount;
return (string)$rep->currentamount;
}
/**
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return PiggyBankRepetition|null
*/
@@ -156,7 +159,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
}
/**
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return Collection
*/
@@ -168,13 +171,13 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/**
* Used for connecting to a piggy bank.
*
* @param PiggyBank $piggyBank
* @param PiggyBankRepetition $repetition
* @param TransactionJournal $journal
* @param PiggyBank $piggyBank
* @param PiggyBankRepetition $repetition
* @param TransactionJournal $journal
*
* @return string
* @throws \FireflyIII\Exceptions\FireflyException
* @throws \JsonException
* @throws FireflyException
* @throws JsonException
*/
public function getExactAmount(PiggyBank $piggyBank, PiggyBankRepetition $repetition, TransactionJournal $journal): string
{
@@ -221,11 +224,11 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
// currency of the account + the piggy bank currency are almost the same.
// which amount from the transaction matches?
$amount = null;
if ((int) $source->transaction_currency_id === (int) $currency->id) {
if ((int)$source->transaction_currency_id === (int)$currency->id) {
Log::debug('Use normal amount');
$amount = app('steam')->$operator($source->amount);
}
if ((int) $source->foreign_currency_id === (int) $currency->id) {
if ((int)$source->foreign_currency_id === (int)$currency->id) {
Log::debug('Use foreign amount');
$amount = app('steam')->$operator($source->foreign_amount);
}
@@ -236,10 +239,10 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
}
Log::debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount));
$room = bcsub((string) $piggyBank->targetamount, (string) $repetition->currentamount);
$room = bcsub((string)$piggyBank->targetamount, (string)$repetition->currentamount);
$compare = bcmul($repetition->currentamount, '-1');
if (bccomp((string) $piggyBank->targetamount, '0') === 0) {
if (bccomp((string)$piggyBank->targetamount, '0') === 0) {
// amount is zero? then the "room" is positive amount of we wish to add or remove.
$room = app('steam')->positive($amount);
Log::debug(sprintf('Room is now %s', $room));
@@ -266,7 +269,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $compare;
}
return (string) $amount;
return (string)$amount;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
@@ -274,13 +285,13 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*/
public function getMaxOrder(): int
{
return (int) $this->user->piggyBanks()->max('piggy_banks.order');
return (int)$this->user->piggyBanks()->max('piggy_banks.order');
}
/**
* Return note for piggy bank.
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return string
*/
@@ -309,7 +320,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/** @var PiggyBank $piggy */
foreach ($set as $piggy) {
$currentAmount = $this->getRepetition($piggy)->currentamount ?? '0';
$piggy->name = $piggy->name . ' (' . app('amount')->formatAnything($currency, $currentAmount, false) . ')';
$piggy->name = $piggy->name.' ('.app('amount')->formatAnything($currency, $currentAmount, false).')';
}
return $set;
@@ -326,7 +337,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/**
* Returns the suggested amount the user should save per month, or "".
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return string
*
@@ -346,7 +357,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
// more than 1 month to go and still need money to save:
if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) {
$savePerMonth = bcdiv($remainingAmount, (string) $diffInMonths);
$savePerMonth = bcdiv($remainingAmount, (string)$diffInMonths);
}
// less than 1 month to go but still need money to save:
@@ -361,11 +372,11 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/**
* Get for piggy account what is left to put in piggies.
*
* @param PiggyBank $piggyBank
* @param Carbon $date
* @param PiggyBank $piggyBank
* @param Carbon $date
*
* @return string
* @throws \JsonException
* @throws JsonException
*/
public function leftOnAccount(PiggyBank $piggyBank, Carbon $date): string
{
@@ -399,12 +410,4 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $search->take($limit)->get();
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* PiggyBankRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -25,7 +26,6 @@ namespace FireflyIII\Repositories\PiggyBank;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
@@ -45,22 +45,22 @@ interface PiggyBankRepositoryInterface
public function addAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool;
/**
* @param PiggyBankRepetition $repetition
* @param string $amount
* @param PiggyBankRepetition $repetition
* @param string $amount
*/
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void;
/**
* @param PiggyBank $piggyBank
* @param string $amount
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool;
/**
* @param PiggyBank $piggyBank
* @param string $amount
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
@@ -69,7 +69,7 @@ interface PiggyBankRepositoryInterface
/**
* Destroy piggy bank.
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return bool
*/
@@ -81,7 +81,7 @@ interface PiggyBankRepositoryInterface
public function destroyAll(): void;
/**
* @param int $piggyBankId
* @param int $piggyBankId
*
* @return PiggyBank|null
*/
@@ -90,22 +90,22 @@ interface PiggyBankRepositoryInterface
/**
* Find by name or return NULL.
*
* @param string $name
* @param string $name
*
* @return PiggyBank|null
*/
public function findByName(string $name): ?PiggyBank;
/**
* @param int|null $piggyBankId
* @param string|null $piggyBankName
* @param int|null $piggyBankId
* @param string|null $piggyBankName
*
* @return PiggyBank|null
*/
public function findPiggyBank(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank;
/**
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return Collection
*/
@@ -114,7 +114,7 @@ interface PiggyBankRepositoryInterface
/**
* Get current amount saved in piggy bank.
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return string
*/
@@ -123,7 +123,7 @@ interface PiggyBankRepositoryInterface
/**
* Get all events.
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return Collection
*/
@@ -132,9 +132,9 @@ interface PiggyBankRepositoryInterface
/**
* Used for connecting to a piggy bank.
*
* @param PiggyBank $piggyBank
* @param PiggyBankRepetition $repetition
* @param TransactionJournal $journal
* @param PiggyBank $piggyBank
* @param PiggyBankRepetition $repetition
* @param TransactionJournal $journal
*
* @return string
*/
@@ -150,7 +150,7 @@ interface PiggyBankRepositoryInterface
/**
* Return note for piggy bank.
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return string
*/
@@ -171,7 +171,7 @@ interface PiggyBankRepositoryInterface
public function getPiggyBanksWithAmount(): Collection;
/**
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return PiggyBankRepetition|null
*/
@@ -180,7 +180,7 @@ interface PiggyBankRepositoryInterface
/**
* Returns the suggested amount the user should save per month, or "".
*
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return string
*/
@@ -189,8 +189,8 @@ interface PiggyBankRepositoryInterface
/**
* Get for piggy account what is left to put in piggies.
*
* @param PiggyBank $piggyBank
* @param Carbon $date
* @param PiggyBank $piggyBank
* @param Carbon $date
*
* @return string
*/
@@ -205,7 +205,7 @@ interface PiggyBankRepositoryInterface
public function removeAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool;
/**
* @param PiggyBank $piggyBank
* @param PiggyBank $piggyBank
*
* @return PiggyBank
*/
@@ -219,24 +219,24 @@ interface PiggyBankRepositoryInterface
/**
* Search for piggy banks.
*
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchPiggyBank(string $query, int $limit): Collection;
/**
* @param PiggyBank $piggyBank
* @param string $amount
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return PiggyBank
*/
public function setCurrentAmount(PiggyBank $piggyBank, string $amount): PiggyBank;
/**
* @param PiggyBank $piggyBank
* @param string $objectGroupTitle
* @param PiggyBank $piggyBank
* @param string $objectGroupTitle
*
* @return PiggyBank
*/
@@ -245,22 +245,22 @@ interface PiggyBankRepositoryInterface
/**
* Set specific piggy bank to specific order.
*
* @param PiggyBank $piggyBank
* @param int $newOrder
* @param PiggyBank $piggyBank
* @param int $newOrder
*
* @return bool
*/
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* Store new piggy bank.
*
* @param array $data
* @param array $data
*
* @return PiggyBank
* @throws FireflyException
@@ -270,8 +270,8 @@ interface PiggyBankRepositoryInterface
/**
* Update existing piggy bank.
*
* @param PiggyBank $piggyBank
* @param array $data
* @param PiggyBank $piggyBank
* @param array $data
*
* @return PiggyBank
*/

View File

@@ -61,6 +61,36 @@ class RecurringRepository implements RecurringRepositoryInterface
private User $user;
/**
* @inheritDoc
*/
public function createdPreviously(Recurrence $recurrence, Carbon $date): bool
{
// if not, loop set and try to read the recurrence_date. If it matches start or end, return it as well.
$set =
TransactionJournalMeta::where(function (Builder $q1) use ($recurrence) {
$q1->where('name', 'recurrence_id');
$q1->where('data', json_encode((string)$recurrence->id));
})->get(['journal_meta.transaction_journal_id']);
// there are X journals made for this recurrence. Any of them meant for today?
foreach ($set as $journalMeta) {
$count = TransactionJournalMeta::where(function (Builder $q2) use ($date) {
$string = (string)$date;
Log::debug(sprintf('Search for date: %s', json_encode($string)));
$q2->where('name', 'recurrence_date');
$q2->where('data', json_encode($string));
})
->where('transaction_journal_id', $journalMeta->transaction_journal_id)
->count();
if ($count > 0) {
Log::debug(sprintf('Looks like journal #%d was already created', $journalMeta->transaction_journal_id));
return true;
}
}
return false;
}
/**
* Destroy a recurring transaction.
*
@@ -81,6 +111,20 @@ class RecurringRepository implements RecurringRepositoryInterface
$this->user->recurrences()->delete();
}
/**
* Get ALL recurring transactions.
*
* @return Collection
*/
public function getAll(): Collection
{
// grab ALL recurring transactions:
return Recurrence::with(['TransactionCurrency', 'TransactionType', 'RecurrenceRepetitions', 'RecurrenceTransactions'])
->orderBy('active', 'DESC')
->orderBy('title', 'ASC')
->get();
}
/**
* Returns all of the user's recurring transactions.
*
@@ -96,20 +140,6 @@ class RecurringRepository implements RecurringRepositoryInterface
->get();
}
/**
* Get ALL recurring transactions.
*
* @return Collection
*/
public function getAll(): Collection
{
// grab ALL recurring transactions:
return Recurrence::with(['TransactionCurrency', 'TransactionType', 'RecurrenceRepetitions', 'RecurrenceTransactions'])
->orderBy('active', 'DESC')
->orderBy('title', 'ASC')
->get();
}
/**
* @inheritDoc
*/
@@ -314,6 +344,16 @@ class RecurringRepository implements RecurringRepositoryInterface
return $collector->getPaginatedGroups();
}
/**
* Set user for in repository.
*
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param Recurrence $recurrence
*
@@ -521,16 +561,6 @@ class RecurringRepository implements RecurringRepositoryInterface
return $search->take($limit)->get(['id', 'title', 'description']);
}
/**
* Set user for in repository.
*
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
*
@@ -629,34 +659,4 @@ class RecurringRepository implements RecurringRepositoryInterface
return $service->update($recurrence, $data);
}
/**
* @inheritDoc
*/
public function createdPreviously(Recurrence $recurrence, Carbon $date): bool
{
// if not, loop set and try to read the recurrence_date. If it matches start or end, return it as well.
$set =
TransactionJournalMeta::where(function (Builder $q1) use ($recurrence) {
$q1->where('name', 'recurrence_id');
$q1->where('data', json_encode((string)$recurrence->id));
})->get(['journal_meta.transaction_journal_id']);
// there are X journals made for this recurrence. Any of them meant for today?
foreach ($set as $journalMeta) {
$count = TransactionJournalMeta::where(function (Builder $q2) use ($date) {
$string = (string)$date;
Log::debug(sprintf('Search for date: %s', json_encode($string)));
$q2->where('name', 'recurrence_date');
$q2->where('data', json_encode($string));
})
->where('transaction_journal_id', $journalMeta->transaction_journal_id)
->count();
if ($count > 0) {
Log::debug(sprintf('Looks like journal #%d was already created', $journalMeta->transaction_journal_id));
return true;
}
}
return false;
}
}

View File

@@ -38,13 +38,6 @@ use Illuminate\Support\Collection;
*/
interface RecurringRepositoryInterface
{
/**
* Destroy a recurring transaction.
*
* @param Recurrence $recurrence
*/
public function destroy(Recurrence $recurrence): void;
/**
* @param Recurrence $recurrence
* @param Carbon $date
@@ -52,6 +45,13 @@ interface RecurringRepositoryInterface
*/
public function createdPreviously(Recurrence $recurrence, Carbon $date): bool;
/**
* Destroy a recurring transaction.
*
* @param Recurrence $recurrence
*/
public function destroy(Recurrence $recurrence): void;
/**
* Destroy all recurring transactions.
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* RuleRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -44,15 +45,7 @@ class RuleRepository implements RuleRepositoryInterface
private $user;
/**
* @return int
*/
public function count(): int
{
return $this->user->rules()->count();
}
/**
* @param Rule $rule
* @param Rule $rule
*
* @return bool
* @throws Exception
@@ -76,7 +69,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
@@ -98,16 +91,6 @@ class RuleRepository implements RuleRepositoryInterface
return $newRule;
}
/**
* @param int $ruleId
*
* @return Rule|null
*/
public function find(int $ruleId): ?Rule
{
return $this->user->rules()->find($ruleId);
}
/**
* Get all the users rules.
*
@@ -129,17 +112,17 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup $ruleGroup
*
* @return int
*/
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup): int
{
return (int) $ruleGroup->rules()->max('order');
return (int)$ruleGroup->rules()->max('order');
}
/**
* @param Rule $rule
* @param Rule $rule
*
* @return string
*
@@ -149,14 +132,22 @@ class RuleRepository implements RuleRepositoryInterface
{
$count = $rule->ruleTriggers()->count();
if (0 === $count) {
throw new FireflyException('Rules should have more than zero triggers, rule #' . $rule->id . ' has none!');
throw new FireflyException('Rules should have more than zero triggers, rule #'.$rule->id.' has none!');
}
return $rule->ruleTriggers()->where('trigger_type', 'user_action')->first()->trigger_value;
}
/**
* @param Rule $rule
* @return int
*/
public function count(): int
{
return $this->user->rules()->count();
}
/**
* @param Rule $rule
*
* @return Collection
*/
@@ -166,7 +157,7 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param Rule $rule
* @param Rule $rule
*
* @return Collection
*/
@@ -268,15 +259,7 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
* @param array $data
*
* @return Rule
* @throws FireflyException
@@ -330,8 +313,18 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param string $moment
* @param Rule $rule
* @param int $ruleId
*
* @return Rule|null
*/
public function find(int $ruleId): ?Rule
{
return $this->user->rules()->find($ruleId);
}
/**
* @param string $moment
* @param Rule $rule
*/
private function setRuleTrigger(string $moment, Rule $rule): void
{
@@ -354,7 +347,7 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup $ruleGroup
*
* @return bool
*/
@@ -367,13 +360,21 @@ class RuleRepository implements RuleRepositoryInterface
return true;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @inheritDoc
*/
public function setOrder(Rule $rule, int $newOrder): void
{
$oldOrder = (int) $rule->order;
$groupId = (int) $rule->rule_group_id;
$oldOrder = (int)$rule->order;
$groupId = (int)$rule->rule_group_id;
$maxOrder = $this->maxOrder($rule->ruleGroup);
$newOrder = $newOrder > $maxOrder ? $maxOrder + 1 : $newOrder;
Log::debug(sprintf('New order will be %d', $newOrder));
@@ -408,12 +409,12 @@ class RuleRepository implements RuleRepositoryInterface
*/
public function maxOrder(RuleGroup $ruleGroup): int
{
return (int) $ruleGroup->rules()->max('order');
return (int)$ruleGroup->rules()->max('order');
}
/**
* @param Rule $rule
* @param array $data
* @param Rule $rule
* @param array $data
*
* @return void
*/
@@ -442,8 +443,8 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param Rule $rule
* @param array $values
* @param Rule $rule
* @param array $values
*
* @return RuleTrigger
*/
@@ -462,8 +463,8 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param Rule $rule
* @param array $data
* @param Rule $rule
* @param array $data
*
* @return void
*/
@@ -487,8 +488,8 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param Rule $rule
* @param array $values
* @param Rule $rule
* @param array $values
*
* @return RuleAction
*/
@@ -507,8 +508,8 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param Rule $rule
* @param array $data
* @param Rule $rule
* @param array $data
*
* @return Rule
*/
@@ -534,7 +535,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']);
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* RuleRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -40,21 +41,21 @@ interface RuleRepositoryInterface
public function count(): int;
/**
* @param Rule $rule
* @param Rule $rule
*
* @return bool
*/
public function destroy(Rule $rule): bool;
/**
* @param Rule $rule
* @param Rule $rule
*
* @return Rule
*/
public function duplicate(Rule $rule): Rule;
/**
* @param int $ruleId
* @param int $ruleId
*
* @return Rule|null
*/
@@ -73,28 +74,28 @@ interface RuleRepositoryInterface
public function getFirstRuleGroup(): RuleGroup;
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup $ruleGroup
*
* @return int
*/
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup): int;
/**
* @param Rule $rule
* @param Rule $rule
*
* @return string
*/
public function getPrimaryTrigger(Rule $rule): string;
/**
* @param Rule $rule
* @param Rule $rule
*
* @return Collection
*/
public function getRuleActions(Rule $rule): Collection;
/**
* @param Rule $rule
* @param Rule $rule
*
* @return Collection
*/
@@ -103,7 +104,7 @@ interface RuleRepositoryInterface
/**
* Return search query for rule.
*
* @param Rule $rule
* @param Rule $rule
*
* @return string
*/
@@ -124,73 +125,73 @@ interface RuleRepositoryInterface
public function getUpdateRules(): Collection;
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup $ruleGroup
*
* @return int
*/
public function maxOrder(RuleGroup $ruleGroup): int;
/**
* @param Rule $rule
* @param RuleGroup $ruleGroup
* @param int $order
* @param Rule $rule
* @param RuleGroup $ruleGroup
* @param int $order
*
* @return Rule
*/
public function moveRule(Rule $rule, RuleGroup $ruleGroup, int $order): Rule;
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup $ruleGroup
*
* @return bool
*/
public function resetRuleOrder(RuleGroup $ruleGroup): bool;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchRule(string $query, int $limit): Collection;
/**
* @param Rule $rule
* @param int $newOrder
* @param Rule $rule
* @param int $newOrder
*/
public function setOrder(Rule $rule, int $newOrder): void;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* @param array $data
* @param array $data
*
* @return Rule
*/
public function store(array $data): Rule;
/**
* @param Rule $rule
* @param array $values
* @param Rule $rule
* @param array $values
*
* @return RuleAction
*/
public function storeAction(Rule $rule, array $values): RuleAction;
/**
* @param Rule $rule
* @param array $values
* @param Rule $rule
* @param array $values
*
* @return RuleTrigger
*/
public function storeTrigger(Rule $rule, array $values): RuleTrigger;
/**
* @param Rule $rule
* @param array $data
* @param Rule $rule
* @param array $data
*
* @return Rule
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* RuleGroupRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -61,6 +62,14 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
}
/**
* @return Collection
*/
public function get(): Collection
{
return $this->user->ruleGroups()->orderBy('order', 'ASC')->get();
}
/**
* @return int
*/
@@ -70,8 +79,8 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup|null $moveTo
* @param RuleGroup $ruleGroup
* @param RuleGroup|null $moveTo
*
* @return bool
* @throws Exception
@@ -128,7 +137,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup $ruleGroup
*
* @return bool
*/
@@ -142,7 +151,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$count = 1;
/** @var Rule $entry */
foreach ($set as $entry) {
if ((int) $entry->order !== $count) {
if ((int)$entry->order !== $count) {
Log::debug(sprintf('Rule #%d was on spot %d but must be on spot %d', $entry->id, $entry->order, $count));
$entry->order = $count;
$entry->save();
@@ -157,7 +166,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param Rule $rule
* @param Rule $rule
*/
private function resetRuleActionOrder(Rule $rule): void
{
@@ -169,7 +178,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$index = 1;
/** @var RuleAction $action */
foreach ($actions as $action) {
if ((int) $action->order !== $index) {
if ((int)$action->order !== $index) {
$action->order = $index;
$action->save();
Log::debug(sprintf('Rule action #%d was on spot %d but must be on spot %d', $action->id, $action->order, $index));
@@ -179,7 +188,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param Rule $rule
* @param Rule $rule
*/
private function resetRuleTriggerOrder(Rule $rule): void
{
@@ -191,7 +200,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$index = 1;
/** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) {
$order = (int) $trigger->order;
$order = (int)$trigger->order;
if ($order !== $index) {
$trigger->order = $index;
$trigger->save();
@@ -215,15 +224,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @return Collection
*/
public function get(): Collection
{
return $this->user->ruleGroups()->orderBy('order', 'ASC')->get();
}
/**
* @param int $ruleGroupId
* @param int $ruleGroupId
*
* @return RuleGroup|null
*/
@@ -233,7 +234,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param string $title
* @param string $title
*
* @return RuleGroup|null
*/
@@ -251,7 +252,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
@@ -263,7 +264,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
@@ -278,7 +279,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
@@ -293,7 +294,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param string|null $filter
* @param string|null $filter
*
* @return Collection
*/
@@ -351,11 +352,11 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
{
$entry = $this->user->ruleGroups()->max('order');
return (int) $entry;
return (int)$entry;
}
/**
* @param string|null $filter
* @param string|null $filter
*
* @return Collection
*/
@@ -408,7 +409,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
@@ -423,7 +424,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');
}
/**
@@ -442,7 +443,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void
{
@@ -450,7 +451,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return RuleGroup
*/
@@ -479,7 +480,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
*/
public function setOrder(RuleGroup $ruleGroup, int $newOrder): void
{
$oldOrder = (int) $ruleGroup->order;
$oldOrder = (int)$ruleGroup->order;
if ($newOrder > $oldOrder) {
$this->user->ruleGroups()->where('rule_groups.order', '<=', $newOrder)->where('rule_groups.order', '>', $oldOrder)
@@ -501,8 +502,8 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @param RuleGroup $ruleGroup
* @param array $data
* @param RuleGroup $ruleGroup
* @param array $data
*
* @return RuleGroup
*/
@@ -521,7 +522,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

@@ -1,4 +1,5 @@
<?php
/**
* RuleGroupRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -42,8 +43,8 @@ interface RuleGroupRepositoryInterface
public function count(): int;
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup|null $moveTo
* @param RuleGroup $ruleGroup
* @param RuleGroup|null $moveTo
*
* @return bool
*/
@@ -55,14 +56,14 @@ interface RuleGroupRepositoryInterface
public function destroyAll(): void;
/**
* @param int $ruleGroupId
* @param int $ruleGroupId
*
* @return RuleGroup|null
*/
public function find(int $ruleGroupId): ?RuleGroup;
/**
* @param string $title
* @param string $title
*
* @return RuleGroup|null
*/
@@ -81,21 +82,21 @@ interface RuleGroupRepositoryInterface
public function getActiveGroups(): Collection;
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
public function getActiveRules(RuleGroup $group): Collection;
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
public function getActiveStoreRules(RuleGroup $group): Collection;
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
@@ -104,7 +105,7 @@ interface RuleGroupRepositoryInterface
/**
* Also inactive groups.
*
* @param string|null $filter
* @param string|null $filter
*
* @return Collection
*/
@@ -116,14 +117,14 @@ interface RuleGroupRepositoryInterface
public function getHighestOrderRuleGroup(): int;
/**
* @param string|null $filter
* @param string|null $filter
*
* @return Collection
*/
public function getRuleGroupsWithRules(?string $filter): Collection;
/**
* @param RuleGroup $group
* @param RuleGroup $group
*
* @return Collection
*/
@@ -142,41 +143,41 @@ interface RuleGroupRepositoryInterface
public function resetOrder(): bool;
/**
* @param RuleGroup $ruleGroup
* @param RuleGroup $ruleGroup
*
* @return bool
*/
public function resetRuleOrder(RuleGroup $ruleGroup): bool;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchRuleGroup(string $query, int $limit): Collection;
/**
* @param RuleGroup $ruleGroup
* @param int $newOrder
* @param RuleGroup $ruleGroup
* @param int $newOrder
*/
public function setOrder(RuleGroup $ruleGroup, int $newOrder): void;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* @param array $data
* @param array $data
*
* @return RuleGroup
*/
public function store(array $data): RuleGroup;
/**
* @param RuleGroup $ruleGroup
* @param array $data
* @param RuleGroup $ruleGroup
* @param array $data
*
* @return RuleGroup
*/

View File

@@ -29,6 +29,8 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
*
@@ -44,10 +46,10 @@ class OperationsRepository implements OperationsRepositoryInterface
* which have the specified tag(s) set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
*/
@@ -70,31 +72,31 @@ class OperationsRepository implements OperationsRepositoryInterface
$array = [];
$listedJournals = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'tags' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'tags' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// 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($journalId, $listedJournals, true)) {
continue;
}
$listedJournals[] = $journalId;
$array[$currencyId]['tags'][$tagId] = $array[$currencyId]['tags'][$tagId] ?? [
'id' => $tagId,
'name' => $tagName,
'transaction_journals' => [],
];
'id' => $tagId,
'name' => $tagName,
'transaction_journals' => [],
];
$array[$currencyId]['tags'][$tagId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->negative($journal['amount']),
@@ -114,10 +116,18 @@ class OperationsRepository implements OperationsRepositoryInterface
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @return Collection
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getTags(): Collection
{
@@ -131,10 +141,10 @@ class OperationsRepository implements OperationsRepositoryInterface
* which have the specified tag(s) set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
*/
@@ -158,21 +168,21 @@ class OperationsRepository implements OperationsRepositoryInterface
$listedJournals = [];
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'tags' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
'tags' => [],
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
// 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($journalId, $listedJournals, true)) {
continue;
@@ -180,11 +190,11 @@ class OperationsRepository implements OperationsRepositoryInterface
$listedJournals[] = $journalId;
$array[$currencyId]['tags'][$tagId] = $array[$currencyId]['tags'][$tagId] ?? [
'id' => $tagId,
'name' => $tagName,
'transaction_journals' => [],
];
$journalId = (int) $journal['transaction_journal_id'];
'id' => $tagId,
'name' => $tagName,
'transaction_journals' => [],
];
$journalId = (int)$journal['transaction_journal_id'];
$array[$currencyId]['tags'][$tagId]['transaction_journals'][$journalId] = [
'amount' => app('steam')->positive($journal['amount']),
'date' => $journal['date'],
@@ -202,21 +212,13 @@ class OperationsRepository implements OperationsRepositoryInterface
return $array;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Sum of withdrawal journals in period for a set of tags, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
* @throws FireflyException
@@ -229,10 +231,10 @@ class OperationsRepository implements OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of tags, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
* @throws FireflyException

View File

@@ -39,10 +39,10 @@ interface OperationsRepositoryInterface
* which have the specified tag(s) set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
*/
@@ -53,27 +53,27 @@ interface OperationsRepositoryInterface
* which have the specified tag(s) set to them. It's grouped per currency, with as few details in the array
* as possible. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
*/
public function listIncome(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $tags = null): array;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* Sum of withdrawal journals in period for a set of tags, grouped per currency. Amounts are always negative.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
*/
@@ -82,10 +82,10 @@ interface OperationsRepositoryInterface
/**
* Sum of income journals in period for a set of tags, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $tags
*
* @return array
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* TagRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -54,7 +55,7 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param Tag $tag
*
* @return bool
* @throws Exception
@@ -90,9 +91,9 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -108,7 +109,15 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param int $tagId
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param int $tagId
*
* @return Tag|null
*/
@@ -118,7 +127,7 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param string $tag
* @param string $tag
*
* @return Tag|null
*/
@@ -128,7 +137,7 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param Tag $tag
*
* @return Carbon|null
*/
@@ -163,7 +172,7 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param int|null $year
* @param int|null $year
*
* @return array
*/
@@ -180,7 +189,7 @@ class TagRepository implements TagRepositoryInterface
if (null !== $year) {
Log::debug(sprintf('Get tags with year %s.', $year));
$tagQuery->where('tags.date', '>=', $year . '-01-01 00:00:00')->where('tags.date', '<=', $year . '-12-31 23:59:59');
$tagQuery->where('tags.date', '>=', $year.'-01-01 00:00:00')->where('tags.date', '<=', $year.'-12-31 23:59:59');
}
$collection = $tagQuery->get();
$return = [];
@@ -200,9 +209,9 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -218,7 +227,7 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param Tag $tag
*
* @return Carbon|null
*/
@@ -253,7 +262,7 @@ class TagRepository implements TagRepositoryInterface
/**
* Find one or more tags based on the query.
*
* @param string $query
* @param string $query
*
* @return Collection
*/
@@ -267,8 +276,8 @@ class TagRepository implements TagRepositoryInterface
/**
* Search the users tags.
*
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
@@ -285,15 +294,7 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
* @param array $data
*
* @return Tag
*/
@@ -307,9 +308,9 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return array
*
@@ -330,21 +331,21 @@ class TagRepository implements TagRepositoryInterface
/** @var array $journal */
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$sums[$currencyId] = $sums[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
TransactionType::WITHDRAWAL => '0',
TransactionType::DEPOSIT => '0',
TransactionType::TRANSFER => '0',
TransactionType::RECONCILIATION => '0',
TransactionType::OPENING_BALANCE => '0',
];
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
TransactionType::WITHDRAWAL => '0',
TransactionType::DEPOSIT => '0',
TransactionType::TRANSFER => '0',
TransactionType::RECONCILIATION => '0',
TransactionType::OPENING_BALANCE => '0',
];
// 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');
@@ -354,18 +355,18 @@ class TagRepository implements TagRepositoryInterface
$foreignCurrencyId = $journal['foreign_currency_id'];
if (null !== $foreignCurrencyId && 0 !== $foreignCurrencyId) {
$sums[$foreignCurrencyId] = $sums[$foreignCurrencyId] ?? [
'currency_id' => $foreignCurrencyId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
TransactionType::WITHDRAWAL => '0',
TransactionType::DEPOSIT => '0',
TransactionType::TRANSFER => '0',
TransactionType::RECONCILIATION => '0',
TransactionType::OPENING_BALANCE => '0',
];
'currency_id' => $foreignCurrencyId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
TransactionType::WITHDRAWAL => '0',
TransactionType::DEPOSIT => '0',
TransactionType::TRANSFER => '0',
TransactionType::RECONCILIATION => '0',
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']);
$type = $journal['transaction_type_type'];
if (TransactionType::WITHDRAWAL === $type) {
$amount = bcmul($amount, '-1');
@@ -378,9 +379,9 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -395,8 +396,8 @@ class TagRepository implements TagRepositoryInterface
}
/**
* @param Tag $tag
* @param array $data
* @param Tag $tag
* @param array $data
*
* @return Tag
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* TagRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -41,7 +42,7 @@ interface TagRepositoryInterface
/**
* This method destroys a tag.
*
* @param Tag $tag
* @param Tag $tag
*
* @return bool
*/
@@ -53,30 +54,30 @@ interface TagRepositoryInterface
public function destroyAll(): void;
/**
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function expenseInPeriod(Tag $tag, Carbon $start, Carbon $end): array;
/**
* @param int $tagId
* @param int $tagId
*
* @return Tag|null
*/
public function find(int $tagId): ?Tag;
/**
* @param string $tag
* @param string $tag
*
* @return Tag|null
*/
public function findByTag(string $tag): ?Tag;
/**
* @param Tag $tag
* @param Tag $tag
*
* @return Carbon|null
*/
@@ -90,7 +91,7 @@ interface TagRepositoryInterface
public function get(): Collection;
/**
* @param Tag $tag
* @param Tag $tag
*
* @return Collection
*/
@@ -99,30 +100,30 @@ interface TagRepositoryInterface
/**
* Return location, or NULL.
*
* @param Tag $tag
* @param Tag $tag
*
* @return Location|null
*/
public function getLocation(Tag $tag): ?Location;
/**
* @param int|null $year
* @param int|null $year
*
* @return array
*/
public function getTagsInYear(?int $year): array;
/**
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function incomeInPeriod(Tag $tag, Carbon $start, Carbon $end): array;
/**
* @param Tag $tag
* @param Tag $tag
*
* @return Carbon|null
*/
@@ -145,7 +146,7 @@ interface TagRepositoryInterface
/**
* Find one or more tags based on the query.
*
* @param string $query
* @param string $query
*
* @return Collection
*/
@@ -154,22 +155,22 @@ interface TagRepositoryInterface
/**
* Search the users tags.
*
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchTags(string $query, int $limit): Collection;
/**
* @param User $user
* @param User $user
*/
public function setUser(User $user);
/**
* This method stores a tag.
*
* @param array $data
* @param array $data
*
* @return Tag
*/
@@ -178,18 +179,18 @@ interface TagRepositoryInterface
/**
* Calculates various amounts in tag.
*
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
* @param Tag $tag
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return array
*/
public function sumsOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): array;
/**
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
* @param Tag $tag
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -198,8 +199,8 @@ interface TagRepositoryInterface
/**
* Update a tag.
*
* @param Tag $tag
* @param array $data
* @param Tag $tag
* @param array $data
*
* @return Tag
*/

View File

@@ -47,6 +47,7 @@ use FireflyIII\Support\NullArrayObject;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use JsonException;
use Log;
/**
@@ -68,7 +69,19 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
/**
* @param TransactionGroup $group
* Find a transaction group by its ID.
*
* @param int $groupId
*
* @return TransactionGroup|null
*/
public function find(int $groupId): ?TransactionGroup
{
return $this->user->transactionGroups()->find($groupId);
}
/**
* @param TransactionGroup $group
*/
public function destroy(TransactionGroup $group): void
{
@@ -92,7 +105,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return array
*/
@@ -119,7 +132,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
/**
* @param Transaction $transaction
* @param Transaction $transaction
*
* @return array
*/
@@ -141,22 +154,10 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
return $array;
}
/**
* Find a transaction group by its ID.
*
* @param int $groupId
*
* @return TransactionGroup|null
*/
public function find(int $groupId): ?TransactionGroup
{
return $this->user->transactionGroups()->find($groupId);
}
/**
* Return all attachments for all journals in the group.
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return array
*/
@@ -173,7 +174,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
$result = [];
/** @var Attachment $attachment */
foreach ($set as $attachment) {
$journalId = (int) $attachment->attachable_id;
$journalId = (int)$attachment->attachable_id;
$result[$journalId] = $result[$journalId] ?? [];
$current = $attachment->toArray();
$current['file_exists'] = true;
@@ -185,10 +186,38 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
return $result;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* Get the note text for a journal (by ID).
*
* @param int $journalId
*
* @return string|null
*/
public function getNoteText(int $journalId): ?string
{
/** @var Note|null $note */
$note = Note::where('noteable_id', $journalId)
->where('noteable_type', TransactionJournal::class)
->first();
if (null === $note) {
return null;
}
return $note->text;
}
/**
* Return all journal links for all journals in the group.
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return array
*/
@@ -202,9 +231,9 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
$q->orWhereIn('destination_id', $journals);
}
)
->with(['source', 'destination', 'source.transactions'])
->leftJoin('link_types', 'link_types.id', '=', 'journal_links.link_type_id')
->get(['journal_links.*', 'link_types.inward', 'link_types.outward', 'link_types.editable']);
->with(['source', 'destination', 'source.transactions'])
->leftJoin('link_types', 'link_types.id', '=', 'journal_links.link_type_id')
->get(['journal_links.*', 'link_types.inward', 'link_types.outward', 'link_types.editable']);
/** @var TransactionJournalLink $entry */
foreach ($set as $entry) {
$journalId = in_array($entry->source_id, $journals, true) ? $entry->source_id : $entry->destination_id;
@@ -242,7 +271,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return string
*/
@@ -265,7 +294,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return string
*/
@@ -307,8 +336,8 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
/**
* Return object with all found meta field things as Carbon objects.
*
* @param int $journalId
* @param array $fields
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
* @throws Exception
@@ -316,10 +345,10 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
public function getMetaDateFields(int $journalId, array $fields): NullArrayObject
{
$query = DB::table('journal_meta')
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->whereNull('deleted_at')
->get(['name', 'data']);
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->whereNull('deleted_at')
->get(['name', 'data']);
$return = [];
foreach ($query as $row) {
@@ -332,18 +361,18 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
/**
* Return object with all found meta field things.
*
* @param int $journalId
* @param array $fields
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
*/
public function getMetaFields(int $journalId, array $fields): NullArrayObject
{
$query = DB::table('journal_meta')
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->whereNull('deleted_at')
->get(['name', 'data']);
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->whereNull('deleted_at')
->get(['name', 'data']);
$return = [];
foreach ($query as $row) {
@@ -353,34 +382,14 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
return new NullArrayObject($return);
}
/**
* Get the note text for a journal (by ID).
*
* @param int $journalId
*
* @return string|null
*/
public function getNoteText(int $journalId): ?string
{
/** @var Note|null $note */
$note = Note::where('noteable_id', $journalId)
->where('noteable_type', TransactionJournal::class)
->first();
if (null === $note) {
return null;
}
return $note->text;
}
/**
* Return all piggy bank events for all journals in the group.
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return array
* @throws FireflyException
* @throws \JsonException
* @throws JsonException
*/
public function getPiggyEvents(TransactionGroup $group): array
{
@@ -388,8 +397,8 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
$journals = $group->transactionJournals->pluck('id')->toArray();
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
$data = PiggyBankEvent::whereIn('transaction_journal_id', $journals)
->with('piggyBank', 'piggyBank.account')
->get(['piggy_bank_events.*']);
->with('piggyBank', 'piggyBank.account')
->get(['piggy_bank_events.*']);
/** @var PiggyBankEvent $row */
foreach ($data as $row) {
if (null === $row->piggyBank) {
@@ -397,8 +406,8 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
// get currency preference.
$currencyPreference = AccountMeta::where('account_id', $row->piggyBank->account_id)
->where('name', 'currency_id')
->first();
->where('name', 'currency_id')
->first();
if (null !== $currencyPreference) {
$currency = TransactionCurrency::where('id', $currencyPreference->data)->first();
}
@@ -406,7 +415,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
$currencyCode = app('preferences')->getForUser($this->user, 'currencyPreference', 'EUR')->data;
$currency = TransactionCurrency::where('code', $currencyCode)->first();
}
$journalId = (int) $row->transaction_journal_id;
$journalId = (int)$row->transaction_journal_id;
$return[$journalId] = $return[$journalId] ?? [];
$return[$journalId][] = [
@@ -433,31 +442,23 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
/**
* Get the tags for a journal (by ID).
*
* @param int $journalId
* @param int $journalId
*
* @return array
*/
public function getTags(int $journalId): array
{
$result = DB::table('tag_transaction_journal')
->leftJoin('tags', 'tag_transaction_journal.tag_id', '=', 'tags.id')
->where('tag_transaction_journal.transaction_journal_id', $journalId)
->orderBy('tags.tag', 'ASC')
->get(['tags.tag']);
->leftJoin('tags', 'tag_transaction_journal.tag_id', '=', 'tags.id')
->where('tag_transaction_journal.transaction_journal_id', $journalId)
->orderBy('tags.tag', 'ASC')
->get(['tags.tag']);
return $result->pluck('tag')->toArray();
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
* @param array $data
*
* @return TransactionGroup
* @throws DuplicateTransactionException
@@ -482,8 +483,8 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
/**
* @param TransactionGroup $transactionGroup
* @param array $data
* @param TransactionGroup $transactionGroup
* @param array $data
*
* @return TransactionGroup
*

View File

@@ -37,21 +37,21 @@ use Illuminate\Support\Collection;
interface TransactionGroupRepositoryInterface
{
/**
* @param int $journalId
* @param int $journalId
*
* @return int
*/
public function countAttachments(int $journalId): int;
/**
* @param TransactionGroup $group
* @param TransactionGroup $group
*/
public function destroy(TransactionGroup $group): void;
/**
* Return a group and expand all meta data etc.
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return array
*/
@@ -60,7 +60,7 @@ interface TransactionGroupRepositoryInterface
/**
* Find a transaction group by its ID.
*
* @param int $groupId
* @param int $groupId
*
* @return TransactionGroup|null
*/
@@ -69,7 +69,7 @@ interface TransactionGroupRepositoryInterface
/**
* Return all attachments for all journals in the group.
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return array
*/
@@ -78,7 +78,7 @@ interface TransactionGroupRepositoryInterface
/**
* Return all journal links for all journals in the group.
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return array
*/
@@ -87,7 +87,7 @@ interface TransactionGroupRepositoryInterface
/**
* Get the location of a journal or NULL.
*
* @param int $journalId
* @param int $journalId
*
* @return Location|null
*/
@@ -96,8 +96,8 @@ interface TransactionGroupRepositoryInterface
/**
* Return object with all found meta field things as Carbon objects.
*
* @param int $journalId
* @param array $fields
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
*/
@@ -106,8 +106,8 @@ interface TransactionGroupRepositoryInterface
/**
* Return object with all found meta field things.
*
* @param int $journalId
* @param array $fields
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
*/
@@ -116,7 +116,7 @@ interface TransactionGroupRepositoryInterface
/**
* Get the note text for a journal (by ID).
*
* @param int $journalId
* @param int $journalId
*
* @return string|null
*/
@@ -125,7 +125,7 @@ interface TransactionGroupRepositoryInterface
/**
* Return all piggy bank events for all journals in the group.
*
* @param TransactionGroup $group
* @param TransactionGroup $group
*
* @return array
*/
@@ -134,7 +134,7 @@ interface TransactionGroupRepositoryInterface
/**
* Get the tags for a journal (by ID) as Tag objects.
*
* @param int $journalId
* @param int $journalId
*
* @return Collection
*/
@@ -143,7 +143,7 @@ interface TransactionGroupRepositoryInterface
/**
* Get the tags for a journal (by ID).
*
* @param int $journalId
* @param int $journalId
*
* @return array
*/
@@ -152,14 +152,14 @@ interface TransactionGroupRepositoryInterface
/**
* Set the user.
*
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* Create a new transaction group.
*
* @param array $data
* @param array $data
*
* @return TransactionGroup
* @throws DuplicateTransactionException
@@ -170,8 +170,8 @@ interface TransactionGroupRepositoryInterface
/**
* Update an existing transaction group.
*
* @param TransactionGroup $transactionGroup
* @param array $data
* @param TransactionGroup $transactionGroup
* @param array $data
*
* @return TransactionGroup
*/

View File

@@ -33,8 +33,8 @@ use Log;
class TransactionTypeRepository implements TransactionTypeRepositoryInterface
{
/**
* @param TransactionType|null $type
* @param string|null $typeString
* @param TransactionType|null $type
* @param string|null $typeString
*
* @return TransactionType
*/
@@ -57,7 +57,7 @@ class TransactionTypeRepository implements TransactionTypeRepositoryInterface
}
/**
* @param string $type
* @param string $type
*
* @return TransactionType|null
*/
@@ -69,8 +69,8 @@ class TransactionTypeRepository implements TransactionTypeRepositoryInterface
}
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/

View File

@@ -32,23 +32,23 @@ use Illuminate\Support\Collection;
interface TransactionTypeRepositoryInterface
{
/**
* @param string $type
* @param string $type
*
* @return TransactionType|null
*/
public function findByType(string $type): ?TransactionType;
/**
* @param TransactionType|null $type
* @param string|null $typeString
* @param TransactionType|null $type
* @param string|null $typeString
*
* @return TransactionType
*/
public function findTransactionType(?TransactionType $type, ?string $typeString): TransactionType;
/**
* @param string $query
* @param int $limit
* @param string $query
* @param int $limit
*
* @return Collection
*/

View File

@@ -1,4 +1,5 @@
<?php
/**
* UserRepository.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -45,8 +46,8 @@ class UserRepository implements UserRepositoryInterface
* This updates the users email address and records some things so it can be confirmed or undone later.
* The user is blocked until the change is confirmed.
*
* @param User $user
* @param string $newEmail
* @param User $user
* @param string $newEmail
*
* @return bool
* @throws Exception
@@ -59,7 +60,7 @@ class UserRepository implements UserRepositoryInterface
// save old email as pref
app('preferences')->setForUser($user, 'previous_email_latest', $oldEmail);
app('preferences')->setForUser($user, 'previous_email_' . date('Y-m-d-H-i-s'), $oldEmail);
app('preferences')->setForUser($user, 'previous_email_'.date('Y-m-d-H-i-s'), $oldEmail);
// set undo and confirm token:
app('preferences')->setForUser($user, 'email_change_undo_token', bin2hex(random_bytes(16)));
@@ -75,8 +76,8 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param User $user
* @param string $password
* @param User $user
* @param string $password
*
* @return bool
*/
@@ -89,9 +90,9 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param User $user
* @param bool $isBlocked
* @param string $code
* @param User $user
* @param bool $isBlocked
* @param string $code
*
* @return bool
*/
@@ -106,9 +107,9 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param string $name
* @param string $displayName
* @param string $description
* @param string $name
* @param string $displayName
* @param string $description
*
* @return Role
*/
@@ -118,7 +119,7 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param User $user
* @param User $user
*
* @return bool
* @throws Exception
@@ -167,7 +168,7 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param int $userId
* @param int $userId
*
* @return User|null
*/
@@ -177,7 +178,7 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param string $email
* @param string $email
*
* @return User|null
*/
@@ -197,7 +198,15 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param User $user
* @inheritDoc
*/
public function getInvitedUsers(): Collection
{
return InvitedUser::with('user')->get();
}
/**
* @param User $user
*
* @return string|null
*/
@@ -215,7 +224,7 @@ class UserRepository implements UserRepositoryInterface
/**
* Return basic user information.
*
* @param User $user
* @param User $user
*
* @return array
*/
@@ -226,7 +235,7 @@ class UserRepository implements UserRepositoryInterface
// two factor:
$return['has_2fa'] = $user->mfa_secret !== null;
$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();
@@ -250,8 +259,8 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param User $user
* @param string $role
* @param User $user
* @param string $role
*
* @return bool
*/
@@ -285,11 +294,23 @@ class UserRepository implements UserRepositoryInterface
return $invitee;
}
/**
* @inheritDoc
*/
public function redeemCode(string $code): void
{
$obj = InvitedUser::where('invite_code', $code)->where('redeemed', 0)->first();
if ($obj) {
$obj->redeemed = true;
$obj->save();
}
}
/**
* Set MFA code.
*
* @param User $user
* @param string|null $code
* @param User $user
* @param string|null $code
*/
public function setMFACode(User $user, ?string $code): void
{
@@ -298,7 +319,7 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param array $data
* @param array $data
*
* @return User
*/
@@ -321,8 +342,8 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param User $user
* @param string $role
* @param User $user
* @param string $role
*
* @return bool
*/
@@ -346,7 +367,7 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param User $user
* @param User $user
*/
public function unblockUser(User $user): void
{
@@ -358,8 +379,8 @@ class UserRepository implements UserRepositoryInterface
/**
* Update user info.
*
* @param User $user
* @param array $data
* @param User $user
* @param array $data
*
* @return User
* @throws FireflyException
@@ -387,8 +408,8 @@ class UserRepository implements UserRepositoryInterface
* This updates the users email address. Same as changeEmail just without most logging. This makes sure that the undo/confirm routine can't catch this one.
* The user is NOT blocked.
*
* @param User $user
* @param string $newEmail
* @param User $user
* @param string $newEmail
*
* @return bool
* @throws FireflyException
@@ -403,7 +424,7 @@ class UserRepository implements UserRepositoryInterface
// save old email as pref
app('preferences')->setForUser($user, 'admin_previous_email_latest', $oldEmail);
app('preferences')->setForUser($user, 'admin_previous_email_' . date('Y-m-d-H-i-s'), $oldEmail);
app('preferences')->setForUser($user, 'admin_previous_email_'.date('Y-m-d-H-i-s'), $oldEmail);
$user->email = $newEmail;
$user->save();
@@ -414,8 +435,8 @@ class UserRepository implements UserRepositoryInterface
/**
* Remove any role the user has.
*
* @param User $user
* @param string $role
* @param User $user
* @param string $role
*/
public function removeRole(User $user, string $role): void
{
@@ -427,7 +448,7 @@ class UserRepository implements UserRepositoryInterface
}
/**
* @param string $role
* @param string $role
*
* @return Role|null
*/
@@ -436,14 +457,6 @@ class UserRepository implements UserRepositoryInterface
return Role::where('name', $role)->first();
}
/**
* @inheritDoc
*/
public function getInvitedUsers(): Collection
{
return InvitedUser::with('user')->get();
}
/**
* @inheritDoc
*/
@@ -453,16 +466,4 @@ class UserRepository implements UserRepositoryInterface
$invitee = InvitedUser::where('invite_code', $code)->where('expires', '>', $now->format('Y-m-d H:i:s'))->where('redeemed', 0)->first();
return null !== $invitee;
}
/**
* @inheritDoc
*/
public function redeemCode(string $code): void
{
$obj = InvitedUser::where('invite_code', $code)->where('redeemed', 0)->first();
if ($obj) {
$obj->redeemed = true;
$obj->save();
}
}
}

View File

@@ -1,4 +1,5 @@
<?php
/**
* UserRepositoryInterface.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -42,8 +43,8 @@ interface UserRepositoryInterface
/**
* Gives a user a role.
*
* @param User $user
* @param string $role
* @param User $user
* @param string $role
*
* @return bool
*/
@@ -53,8 +54,8 @@ interface UserRepositoryInterface
* This updates the users email address and records some things so it can be confirmed or undone later.
* The user is blocked until the change is confirmed.
*
* @param User $user
* @param string $newEmail
* @param User $user
* @param string $newEmail
*
* @return bool
* @see updateEmail
@@ -63,17 +64,17 @@ interface UserRepositoryInterface
public function changeEmail(User $user, string $newEmail): bool;
/**
* @param User $user
* @param string $password
* @param User $user
* @param string $password
*
* @return mixed
*/
public function changePassword(User $user, string $password);
/**
* @param User $user
* @param bool $isBlocked
* @param string $code
* @param User $user
* @param bool $isBlocked
* @param string $code
*
* @return bool
*/
@@ -87,9 +88,9 @@ interface UserRepositoryInterface
public function count(): int;
/**
* @param string $name
* @param string $displayName
* @param string $description
* @param string $name
* @param string $displayName
* @param string $description
*
* @return Role
*/
@@ -101,21 +102,21 @@ interface UserRepositoryInterface
public function deleteEmptyGroups(): void;
/**
* @param User $user
* @param User $user
*
* @return bool
*/
public function destroy(User $user): bool;
/**
* @param int $userId
* @param int $userId
*
* @return User|null
*/
public function find(int $userId): ?User;
/**
* @param string $email
* @param string $email
*
* @return User|null
*/
@@ -129,14 +130,19 @@ interface UserRepositoryInterface
public function first(): ?User;
/**
* @param string $role
* @return Collection
*/
public function getInvitedUsers(): Collection;
/**
* @param string $role
*
* @return Role|null
*/
public function getRole(string $role): ?Role;
/**
* @param User $user
* @param User $user
*
* @return string|null
*/
@@ -145,40 +151,29 @@ interface UserRepositoryInterface
/**
* Return basic user information.
*
* @param User $user
* @param User $user
*
* @return array
*/
public function getUserData(User $user): array;
/**
* @param User $user
* @param string $role
* @param User $user
* @param string $role
*
* @return bool
*/
public function hasRole(User $user, string $role): bool;
/**
* @param User $user
* @param string $email
* @param User $user
* @param string $email
* @return InvitedUser
*/
public function inviteUser(User $user, string $email): InvitedUser;
/**
* @return Collection
*/
public function getInvitedUsers(): Collection;
/**
* @param string $code
* @return bool
*/
public function validateInviteCode(string $code): bool;
/**
* @param string $code
* @param string $code
* @return void
*/
public function redeemCode(string $code): void;
@@ -186,36 +181,36 @@ interface UserRepositoryInterface
/**
* Remove any role the user has.
*
* @param User $user
* @param string $role
* @param User $user
* @param string $role
*/
public function removeRole(User $user, string $role): void;
/**
* Set MFA code.
*
* @param User $user
* @param string|null $code
* @param User $user
* @param string|null $code
*/
public function setMFACode(User $user, ?string $code): void;
/**
* @param array $data
* @param array $data
*
* @return User
*/
public function store(array $data): User;
/**
* @param User $user
* @param User $user
*/
public function unblockUser(User $user): void;
/**
* Update user info.
*
* @param User $user
* @param array $data
* @param User $user
* @param array $data
*
* @return User
*/
@@ -225,12 +220,18 @@ interface UserRepositoryInterface
* This updates the users email address. Same as changeEmail just without most logging. This makes sure that the undo/confirm routine can't catch this one.
* The user is NOT blocked.
*
* @param User $user
* @param string $newEmail
* @param User $user
* @param string $newEmail
*
* @return bool
* @see changeEmail
*
*/
public function updateEmail(User $user, string $newEmail): bool;
/**
* @param string $code
* @return bool
*/
public function validateInviteCode(string $code): bool;
}

View File

@@ -42,36 +42,36 @@ interface WebhookRepositoryInterface
public function all(): Collection;
/**
* @param Webhook $webhook
* @param Webhook $webhook
*/
public function destroy(Webhook $webhook): void;
/**
* @param WebhookAttempt $attempt
* @param WebhookAttempt $attempt
*/
public function destroyAttempt(WebhookAttempt $attempt): void;
/**
* @param WebhookMessage $message
* @param WebhookMessage $message
*/
public function destroyMessage(WebhookMessage $message): void;
/**
* @param WebhookMessage $webhookMessage
* @param WebhookMessage $webhookMessage
*
* @return Collection
*/
public function getAttempts(WebhookMessage $webhookMessage): Collection;
/**
* @param Webhook $webhook
* @param Webhook $webhook
*
* @return Collection
*/
public function getMessages(Webhook $webhook): Collection;
/**
* @param Webhook $webhook
* @param Webhook $webhook
*
* @return Collection
*/
@@ -80,20 +80,20 @@ interface WebhookRepositoryInterface
/**
* Set user.
*
* @param User $user
* @param User $user
*/
public function setUser(User $user): void;
/**
* @param array $data
* @param array $data
*
* @return Webhook
*/
public function store(array $data): Webhook;
/**
* @param Webhook $webhook
* @param array $data
* @param Webhook $webhook
* @param array $data
*
* @return Webhook
*/