mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-04 05:06:37 +00:00
Code optimizations.
This commit is contained in:
@@ -23,23 +23,24 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Repositories\Account;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\AccountFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Services\Internal\Destroy\AccountDestroyService;
|
||||
use FireflyIII\Services\Internal\Update\AccountUpdateService;
|
||||
use FireflyIII\Services\Internal\Update\JournalUpdateService;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class AccountRepository.
|
||||
*/
|
||||
class AccountRepository implements AccountRepositoryInterface
|
||||
{
|
||||
use FindAccountsTrait;
|
||||
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
@@ -72,6 +73,89 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $number
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function findByAccountNumber(string $number, array $types): ?Account
|
||||
{
|
||||
$query = $this->user->accounts()
|
||||
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
|
||||
->where('account_meta.name', 'accountNumber')
|
||||
->where('account_meta.data', json_encode($number));
|
||||
|
||||
if (\count($types) > 0) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
$query->whereIn('account_types.type', $types);
|
||||
}
|
||||
|
||||
/** @var Collection $accounts */
|
||||
$accounts = $query->get(['accounts.*']);
|
||||
if ($accounts->count() > 0) {
|
||||
return $accounts->first();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iban
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function findByIbanNull(string $iban, array $types): ?Account
|
||||
{
|
||||
$query = $this->user->accounts()->where('iban', '!=', '')->whereNotNull('iban');
|
||||
|
||||
if (\count($types) > 0) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
$query->whereIn('account_types.type', $types);
|
||||
}
|
||||
|
||||
$accounts = $query->get(['accounts.*']);
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
if ($account->iban === $iban) {
|
||||
return $account;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function findByName(string $name, array $types): ?Account
|
||||
{
|
||||
$query = $this->user->accounts();
|
||||
|
||||
if (\count($types) > 0) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
$query->whereIn('account_types.type', $types);
|
||||
}
|
||||
Log::debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
|
||||
|
||||
$accounts = $query->get(['accounts.*']);
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
if ($account->name === $name) {
|
||||
Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id));
|
||||
|
||||
return $account;
|
||||
}
|
||||
}
|
||||
Log::debug(sprintf('There is no account with name "%s" of types', $name), $types);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $accountId
|
||||
*
|
||||
@@ -82,6 +166,96 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return $this->user->accounts()->find($accountId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accountIds
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAccountsById(array $accountIds): Collection
|
||||
{
|
||||
/** @var Collection $result */
|
||||
$query = $this->user->accounts();
|
||||
|
||||
if (\count($accountIds) > 0) {
|
||||
$query->whereIn('accounts.id', $accountIds);
|
||||
}
|
||||
|
||||
$result = $query->get(['accounts.*']);
|
||||
$result = $result->sortBy(
|
||||
function (Account $account) {
|
||||
return strtolower($account->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAccountsByType(array $types): Collection
|
||||
{
|
||||
/** @var Collection $result */
|
||||
$query = $this->user->accounts();
|
||||
if (\count($types) > 0) {
|
||||
$query->accountTypeIn($types);
|
||||
}
|
||||
|
||||
$result = $query->get(['accounts.*']);
|
||||
$result = $result->sortBy(
|
||||
function (Account $account) {
|
||||
return strtolower($account->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveAccountsByType(array $types): Collection
|
||||
{
|
||||
/** @var Collection $result */
|
||||
$query = $this->user->accounts()->with(
|
||||
['accountmeta' => function (HasMany $query) {
|
||||
$query->where('name', 'accountRole');
|
||||
}]
|
||||
);
|
||||
if (\count($types) > 0) {
|
||||
$query->accountTypeIn($types);
|
||||
}
|
||||
$query->where('active', 1);
|
||||
$result = $query->get(['accounts.*']);
|
||||
$result = $result->sortBy(
|
||||
function (Account $account) {
|
||||
return strtolower($account->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getCashAccount(): Account
|
||||
{
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::where('type', AccountType::CASH)->first();
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate('Cash account', $type->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return meta value for account. Null if not found.
|
||||
*
|
||||
@@ -163,11 +337,41 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return $journal->date->format('Y-m-d');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Account|null
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getReconciliation(Account $account): ?Account
|
||||
{
|
||||
if (AccountType::ASSET !== $account->accountType->type) {
|
||||
throw new FireflyException(sprintf('%s is not an asset account.', $account->name));
|
||||
}
|
||||
$name = $account->name . ' reconciliation';
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
|
||||
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get();
|
||||
/** @var Account $current */
|
||||
foreach ($accounts as $current) {
|
||||
if ($current->name === $name) {
|
||||
return $current;
|
||||
}
|
||||
}
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
$account = $factory->findOrCreate($name, $type->type);
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the date of the very first transaction in this account.
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @deprecated
|
||||
* @return TransactionJournal
|
||||
*/
|
||||
public function oldestJournal(Account $account): TransactionJournal
|
||||
|
||||
@@ -164,7 +164,7 @@ interface AccountRepositoryInterface
|
||||
* Returns the date of the very first transaction in this account.
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @deprecated
|
||||
* @return TransactionJournal
|
||||
*/
|
||||
public function oldestJournal(Account $account): TransactionJournal;
|
||||
|
||||
@@ -1,248 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* FindAccountsTrait.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\Account;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\AccountFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* @property User $user
|
||||
*
|
||||
* Trait FindAccountsTrait
|
||||
*/
|
||||
trait FindAccountsTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $number
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function findByAccountNumber(string $number, array $types): ?Account
|
||||
{
|
||||
$query = $this->user->accounts()
|
||||
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
|
||||
->where('account_meta.name', 'accountNumber')
|
||||
->where('account_meta.data', json_encode($number));
|
||||
|
||||
if (\count($types) > 0) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
$query->whereIn('account_types.type', $types);
|
||||
}
|
||||
|
||||
/** @var Collection $accounts */
|
||||
$accounts = $query->get(['accounts.*']);
|
||||
if ($accounts->count() > 0) {
|
||||
return $accounts->first();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iban
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function findByIbanNull(string $iban, array $types): ?Account
|
||||
{
|
||||
$query = $this->user->accounts()->where('iban', '!=', '')->whereNotNull('iban');
|
||||
|
||||
if (\count($types) > 0) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
$query->whereIn('account_types.type', $types);
|
||||
}
|
||||
|
||||
$accounts = $query->get(['accounts.*']);
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
if ($account->iban === $iban) {
|
||||
return $account;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function findByName(string $name, array $types): ?Account
|
||||
{
|
||||
$query = $this->user->accounts();
|
||||
|
||||
if (\count($types) > 0) {
|
||||
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
|
||||
$query->whereIn('account_types.type', $types);
|
||||
}
|
||||
Log::debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
|
||||
|
||||
$accounts = $query->get(['accounts.*']);
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
if ($account->name === $name) {
|
||||
Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id));
|
||||
|
||||
return $account;
|
||||
}
|
||||
if ($account->name !== $name) {
|
||||
Log::debug(sprintf('"%s" does not equal "%s"', $account->name, $name));
|
||||
}
|
||||
}
|
||||
Log::debug(sprintf('There is no account with name "%s" of types', $name), $types);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accountIds
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAccountsById(array $accountIds): Collection
|
||||
{
|
||||
/** @var Collection $result */
|
||||
$query = $this->user->accounts();
|
||||
|
||||
if (\count($accountIds) > 0) {
|
||||
$query->whereIn('accounts.id', $accountIds);
|
||||
}
|
||||
|
||||
$result = $query->get(['accounts.*']);
|
||||
$result = $result->sortBy(
|
||||
function (Account $account) {
|
||||
return strtolower($account->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAccountsByType(array $types): Collection
|
||||
{
|
||||
/** @var Collection $result */
|
||||
$query = $this->user->accounts();
|
||||
if (\count($types) > 0) {
|
||||
$query->accountTypeIn($types);
|
||||
}
|
||||
|
||||
$result = $query->get(['accounts.*']);
|
||||
$result = $result->sortBy(
|
||||
function (Account $account) {
|
||||
return strtolower($account->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveAccountsByType(array $types): Collection
|
||||
{
|
||||
/** @var Collection $result */
|
||||
$query = $this->user->accounts()->with(
|
||||
['accountmeta' => function (HasMany $query) {
|
||||
$query->where('name', 'accountRole');
|
||||
}]
|
||||
);
|
||||
if (\count($types) > 0) {
|
||||
$query->accountTypeIn($types);
|
||||
}
|
||||
$query->where('active', 1);
|
||||
$result = $query->get(['accounts.*']);
|
||||
$result = $result->sortBy(
|
||||
function (Account $account) {
|
||||
return strtolower($account->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getCashAccount(): Account
|
||||
{
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::where('type', AccountType::CASH)->first();
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate('Cash account', $type->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Account|null
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getReconciliation(Account $account): ?Account
|
||||
{
|
||||
if (AccountType::ASSET !== $account->accountType->type) {
|
||||
throw new FireflyException(sprintf('%s is not an asset account.', $account->name));
|
||||
}
|
||||
$name = $account->name . ' reconciliation';
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
|
||||
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get();
|
||||
/** @var Account $current */
|
||||
foreach ($accounts as $current) {
|
||||
if ($current->name === $name) {
|
||||
return $current;
|
||||
}
|
||||
}
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
$account = $factory->findOrCreate($name, $type->type);
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -80,14 +80,14 @@ class AttachmentRepository implements AttachmentRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @param int $attachmentId
|
||||
* @deprecated
|
||||
* @return Attachment
|
||||
*/
|
||||
public function findWithoutUser(int $id): Attachment
|
||||
public function findWithoutUser(int $attachmentId): Attachment
|
||||
{
|
||||
|
||||
$attachment = Attachment::find($id);
|
||||
$attachment = Attachment::find($attachmentId);
|
||||
if (null === $attachment) {
|
||||
return new Attachment;
|
||||
}
|
||||
|
||||
@@ -49,11 +49,11 @@ interface AttachmentRepositoryInterface
|
||||
public function exists(Attachment $attachment): bool;
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @param int $attachmentId
|
||||
* @deprecated
|
||||
* @return Attachment
|
||||
*/
|
||||
public function findWithoutUser(int $id): Attachment;
|
||||
public function findWithoutUser(int $attachmentId): Attachment;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
|
||||
@@ -136,21 +136,8 @@ class BillRepository implements BillRepositoryInterface
|
||||
*/
|
||||
public function getBillsForAccounts(Collection $accounts): Collection
|
||||
{
|
||||
$fields = ['bills.id',
|
||||
'bills.created_at',
|
||||
'bills.updated_at',
|
||||
'bills.deleted_at',
|
||||
'bills.user_id',
|
||||
'bills.name',
|
||||
'bills.match',
|
||||
'bills.amount_min',
|
||||
'bills.amount_max',
|
||||
'bills.date',
|
||||
'bills.repeat_freq',
|
||||
'bills.skip',
|
||||
'bills.automatch',
|
||||
'bills.active',
|
||||
'bills.name_encrypted',
|
||||
$fields = ['bills.id', 'bills.created_at', 'bills.updated_at', 'bills.deleted_at', 'bills.user_id', 'bills.name', 'bills.match', 'bills.amount_min',
|
||||
'bills.amount_max', 'bills.date', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active', 'bills.name_encrypted',
|
||||
'bills.match_encrypted',];
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$set = $this->user->bills()
|
||||
@@ -336,28 +323,22 @@ class BillRepository implements BillRepositoryInterface
|
||||
public function getPayDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$set = new Collection;
|
||||
Log::debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
|
||||
|
||||
// Start at 2016-10-01, see when we expect the bill to hit:
|
||||
$currentStart = clone $start;
|
||||
Log::debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
|
||||
Log::debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
|
||||
|
||||
while ($currentStart <= $end) {
|
||||
Log::debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
|
||||
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
|
||||
Log::debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
|
||||
// If nextExpectedMatch is after end, we continue:
|
||||
if ($nextExpectedMatch > $end) {
|
||||
if ($nextExpectedMatch > $end) {// If nextExpectedMatch is after end, we continue
|
||||
Log::debug(
|
||||
sprintf('nextExpectedMatch %s is after %s, so we skip this bill now.', $nextExpectedMatch->format('Y-m-d'), $end->format('Y-m-d'))
|
||||
);
|
||||
break;
|
||||
}
|
||||
// add to set
|
||||
$set->push(clone $nextExpectedMatch);
|
||||
Log::debug(sprintf('Now %d dates in set.', $set->count()));
|
||||
|
||||
// add day if necessary.
|
||||
$nextExpectedMatch->addDay();
|
||||
|
||||
Log::debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
|
||||
|
||||
@@ -85,6 +85,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's 5.
|
||||
*/
|
||||
public function cleanupBudgets(): bool
|
||||
{
|
||||
@@ -123,6 +124,8 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array
|
||||
{
|
||||
@@ -138,7 +141,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
$return[$budgetId] = [
|
||||
'spent' => $this->spentInPeriod(new Collection([$budget]), $accounts, $start, $end),
|
||||
'budgeted' => '0',
|
||||
'currentRep' => false,
|
||||
];
|
||||
$budgetLimits = $this->getBudgetLimits($budget, $start, $end);
|
||||
$otherLimits = new Collection;
|
||||
@@ -244,12 +246,11 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
* Will cache result.
|
||||
*
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
public function firstUseDate(Budget $budget): Carbon
|
||||
public function firstUseDate(Budget $budget): ?Carbon
|
||||
{
|
||||
$oldest = Carbon::create()->startOfYear();
|
||||
$oldest = null;
|
||||
$journal = $budget->transactionJournals()->orderBy('date', 'ASC')->first();
|
||||
if (null !== $journal) {
|
||||
$oldest = $journal->date < $oldest ? $journal->date : $oldest;
|
||||
|
||||
@@ -105,7 +105,7 @@ interface BudgetRepositoryInterface
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
public function firstUseDate(Budget $budget): Carbon;
|
||||
public function firstUseDate(Budget $budget): ?Carbon;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
|
||||
@@ -81,6 +81,7 @@ class ExportJobRepository implements ExportJobRepositoryInterface
|
||||
|
||||
/**
|
||||
* @return ExportJob
|
||||
* @deprecated
|
||||
*/
|
||||
public function create(): ExportJob
|
||||
{
|
||||
@@ -120,6 +121,7 @@ class ExportJobRepository implements ExportJobRepositoryInterface
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @deprecated
|
||||
* @return ExportJob
|
||||
*/
|
||||
|
||||
@@ -45,6 +45,7 @@ interface ExportJobRepositoryInterface
|
||||
|
||||
/**
|
||||
* @return ExportJob
|
||||
* @deprecated
|
||||
*/
|
||||
public function create(): ExportJob;
|
||||
|
||||
@@ -57,6 +58,7 @@ interface ExportJobRepositoryInterface
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @deprecated
|
||||
* @return ExportJob
|
||||
*/
|
||||
|
||||
@@ -27,14 +27,11 @@ use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Illuminate\Support\Str;
|
||||
use Log;
|
||||
use SplFileObject;
|
||||
use Storage;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
@@ -118,6 +115,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
||||
* @param string $key
|
||||
*
|
||||
* @return ImportJob
|
||||
* @deprecated
|
||||
*/
|
||||
public function findByKey(string $key): ImportJob
|
||||
{
|
||||
|
||||
@@ -56,6 +56,7 @@ interface ImportJobRepositoryInterface
|
||||
* @param string $key
|
||||
*
|
||||
* @return ImportJob
|
||||
* @deprecated
|
||||
*/
|
||||
public function findByKey(string $key): ImportJob;
|
||||
|
||||
|
||||
@@ -193,122 +193,34 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
*/
|
||||
public function getOccurrencesInRange(RecurrenceRepetition $repetition, Carbon $start, Carbon $end): array
|
||||
{
|
||||
$return = [];
|
||||
$mutator = clone $start;
|
||||
$occurrences = [];
|
||||
$mutator = clone $start;
|
||||
$mutator->startOfDay();
|
||||
$skipMod = $repetition->repetition_skip + 1;
|
||||
$attempts = 0;
|
||||
$skipMod = $repetition->repetition_skip + 1;
|
||||
Log::debug(sprintf('Calculating occurrences for rep type "%s"', $repetition->repetition_type));
|
||||
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
||||
switch ($repetition->repetition_type) {
|
||||
default:
|
||||
throw new FireflyException(
|
||||
sprintf('Cannot calculate occurrences for recurring transaction repetition type "%s"', $repetition->repetition_type)
|
||||
);
|
||||
case 'daily':
|
||||
Log::debug('Rep is daily. Start of loop.');
|
||||
while ($mutator <= $end) {
|
||||
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
||||
if (0 === $attempts % $skipMod) {
|
||||
Log::debug(sprintf('Attempts modulo skipmod is zero, include %s', $mutator->format('Y-m-d')));
|
||||
$return[] = clone $mutator;
|
||||
}
|
||||
$mutator->addDay();
|
||||
$attempts++;
|
||||
}
|
||||
break;
|
||||
case 'weekly':
|
||||
Log::debug('Rep is weekly.');
|
||||
// monday = 1
|
||||
// sunday = 7
|
||||
$dayOfWeek = (int)$repetition->repetition_moment;
|
||||
Log::debug(sprintf('DoW in repetition is %d, in mutator is %d', $dayOfWeek, $mutator->dayOfWeekIso));
|
||||
if ($mutator->dayOfWeekIso > $dayOfWeek) {
|
||||
// day has already passed this week, add one week:
|
||||
$mutator->addWeek();
|
||||
Log::debug(sprintf('Jump to next week, so mutator is now: %s', $mutator->format('Y-m-d')));
|
||||
}
|
||||
// today is wednesday (3), expected is friday (5): add two days.
|
||||
// today is friday (5), expected is monday (1), subtract four days.
|
||||
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
||||
$dayDifference = $dayOfWeek - $mutator->dayOfWeekIso;
|
||||
$mutator->addDays($dayDifference);
|
||||
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
||||
while ($mutator <= $end) {
|
||||
if (0 === $attempts % $skipMod && $start->lte($mutator) && $end->gte($mutator)) {
|
||||
Log::debug('Date is in range of start+end, add to set.');
|
||||
$return[] = clone $mutator;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->addWeek();
|
||||
Log::debug(sprintf('Mutator is now (end of loop): %s', $mutator->format('Y-m-d')));
|
||||
}
|
||||
break;
|
||||
case 'monthly':
|
||||
$dayOfMonth = (int)$repetition->repetition_moment;
|
||||
Log::debug(sprintf('Day of month in repetition is %d', $dayOfMonth));
|
||||
Log::debug(sprintf('Start is %s.', $start->format('Y-m-d')));
|
||||
Log::debug(sprintf('End is %s.', $end->format('Y-m-d')));
|
||||
if ($mutator->day > $dayOfMonth) {
|
||||
Log::debug('Add a month.');
|
||||
// day has passed already, add a month.
|
||||
$mutator->addMonth();
|
||||
}
|
||||
Log::debug(sprintf('Start is now %s.', $mutator->format('Y-m-d')));
|
||||
Log::debug('Start loop.');
|
||||
while ($mutator < $end) {
|
||||
Log::debug(sprintf('Mutator is now %s.', $mutator->format('Y-m-d')));
|
||||
$domCorrected = min($dayOfMonth, $mutator->daysInMonth);
|
||||
Log::debug(sprintf('DoM corrected is %d', $domCorrected));
|
||||
$mutator->day = $domCorrected;
|
||||
Log::debug(sprintf('Mutator is now %s.', $mutator->format('Y-m-d')));
|
||||
Log::debug(sprintf('$attempts %% $skipMod === 0 is %s', var_export(0 === $attempts % $skipMod, true)));
|
||||
Log::debug(sprintf('$start->lte($mutator) is %s', var_export($start->lte($mutator), true)));
|
||||
Log::debug(sprintf('$end->gte($mutator) is %s', var_export($end->gte($mutator), true)));
|
||||
if (0 === $attempts % $skipMod && $start->lte($mutator) && $end->gte($mutator)) {
|
||||
Log::debug(sprintf('ADD %s to return!', $mutator->format('Y-m-d')));
|
||||
$return[] = clone $mutator;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->endOfMonth()->startOfDay()->addDay();
|
||||
}
|
||||
break;
|
||||
case 'ndom':
|
||||
$mutator->startOfMonth();
|
||||
// this feels a bit like a cop out but why reinvent the wheel?
|
||||
$counters = [1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth',];
|
||||
$daysOfWeek = [1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday',];
|
||||
$parts = explode(',', $repetition->repetition_moment);
|
||||
while ($mutator <= $end) {
|
||||
$string = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $mutator->format('F'), $mutator->format('Y'));
|
||||
$newCarbon = new Carbon($string);
|
||||
$return[] = clone $newCarbon;
|
||||
$mutator->endOfMonth()->addDay();
|
||||
}
|
||||
break;
|
||||
case 'yearly':
|
||||
$date = new Carbon($repetition->repetition_moment);
|
||||
$date->year = $mutator->year;
|
||||
if ($mutator > $date) {
|
||||
$date->addYear();
|
||||
|
||||
}
|
||||
|
||||
// is $date between $start and $end?
|
||||
$obj = clone $date;
|
||||
$count = 0;
|
||||
while ($obj <= $end && $obj >= $mutator && $count < 10) {
|
||||
|
||||
$return[] = clone $obj;
|
||||
$obj->addYears(1);
|
||||
$count++;
|
||||
}
|
||||
break;
|
||||
if ('daily' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getDailyInRange($mutator, $end, $skipMod);
|
||||
}
|
||||
if ('weekly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getWeeklyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('monthly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getMonthlyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('ndom' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getNdomInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('yearly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getYearlyInRange($mutator, $end, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
// filter out all the weekend days:
|
||||
$return = $this->filterWeekends($repetition, $return);
|
||||
|
||||
return $return;
|
||||
|
||||
// filter out all the weekend days:
|
||||
$occurrences = $this->filterWeekends($repetition, $occurrences);
|
||||
|
||||
return $occurrences;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -399,112 +311,31 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
* @param int $count
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getXOccurrences(RecurrenceRepetition $repetition, Carbon $date, int $count): array
|
||||
{
|
||||
$return = [];
|
||||
$mutator = clone $date;
|
||||
$skipMod = $repetition->repetition_skip + 1;
|
||||
$total = 0;
|
||||
$attempts = 0;
|
||||
switch ($repetition->repetition_type) {
|
||||
default:
|
||||
throw new FireflyException(
|
||||
sprintf('Cannot calculate occurrences for recurring transaction repetition type "%s"', $repetition->repetition_type)
|
||||
);
|
||||
case 'daily':
|
||||
while ($total < $count) {
|
||||
$mutator->addDay();
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $mutator;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
}
|
||||
break;
|
||||
case 'weekly':
|
||||
// monday = 1
|
||||
// sunday = 7
|
||||
$mutator->addDay(); // always assume today has passed.
|
||||
$dayOfWeek = (int)$repetition->repetition_moment;
|
||||
if ($mutator->dayOfWeekIso > $dayOfWeek) {
|
||||
// day has already passed this week, add one week:
|
||||
$mutator->addWeek();
|
||||
}
|
||||
// today is wednesday (3), expected is friday (5): add two days.
|
||||
// today is friday (5), expected is monday (1), subtract four days.
|
||||
$dayDifference = $dayOfWeek - $mutator->dayOfWeekIso;
|
||||
$mutator->addDays($dayDifference);
|
||||
|
||||
while ($total < $count) {
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $mutator;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->addWeek();
|
||||
}
|
||||
break;
|
||||
case 'monthly':
|
||||
$mutator->addDay(); // always assume today has passed.
|
||||
$dayOfMonth = (int)$repetition->repetition_moment;
|
||||
if ($mutator->day > $dayOfMonth) {
|
||||
// day has passed already, add a month.
|
||||
$mutator->addMonth();
|
||||
}
|
||||
|
||||
while ($total < $count) {
|
||||
$domCorrected = min($dayOfMonth, $mutator->daysInMonth);
|
||||
$mutator->day = $domCorrected;
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $mutator;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->endOfMonth()->addDay();
|
||||
}
|
||||
break;
|
||||
case 'ndom':
|
||||
$mutator->addDay(); // always assume today has passed.
|
||||
$mutator->startOfMonth();
|
||||
// this feels a bit like a cop out but why reinvent the wheel?
|
||||
$counters = [1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth',];
|
||||
$daysOfWeek = [1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday',];
|
||||
$parts = explode(',', $repetition->repetition_moment);
|
||||
|
||||
while ($total < $count) {
|
||||
$string = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $mutator->format('F'), $mutator->format('Y'));
|
||||
$newCarbon = new Carbon($string);
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $newCarbon;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->endOfMonth()->addDay();
|
||||
}
|
||||
break;
|
||||
case 'yearly':
|
||||
$date = new Carbon($repetition->repetition_moment);
|
||||
$date->year = $mutator->year;
|
||||
if ($mutator > $date) {
|
||||
$date->addYear();
|
||||
}
|
||||
$obj = clone $date;
|
||||
while ($total < $count) {
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $obj;
|
||||
$total++;
|
||||
}
|
||||
$obj->addYears(1);
|
||||
$attempts++;
|
||||
}
|
||||
break;
|
||||
$skipMod = $repetition->repetition_skip + 1;
|
||||
$occurrences = [];
|
||||
if ('daily' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXDailyOccurrences($date, $count, $skipMod);
|
||||
}
|
||||
if ('weekly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXWeeklyOccurrences($date, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('monthly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXMonthlyOccurrences($date, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('ndom' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXNDomOccurrences($date, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('yearly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXYearlyOccurrences($date, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
// filter out all the weekend days:
|
||||
$return = $this->filterWeekends($repetition, $return);
|
||||
|
||||
return $return;
|
||||
// filter out all the weekend days:
|
||||
$occurrences = $this->filterWeekends($repetition, $occurrences);
|
||||
|
||||
return $occurrences;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -545,8 +376,7 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
break;
|
||||
case 'yearly':
|
||||
//
|
||||
$today = new Carbon;
|
||||
$today->endOfYear();
|
||||
$today = Carbon::create()->endOfYear();
|
||||
$repDate = Carbon::createFromFormat('Y-m-d', $repetition->repetition_moment);
|
||||
$diffInYears = $today->diffInYears($repDate);
|
||||
$repDate->addYears($diffInYears); // technically not necessary.
|
||||
@@ -658,4 +488,378 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of daily occurrences for a recurring transaction until date $end is reached. Will skip every $skipMod-1 occurrences.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param int $skipMod
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getDailyInRange(Carbon $start, Carbon $end, int $skipMod): array
|
||||
{
|
||||
$return = [];
|
||||
$attempts = 0;
|
||||
Log::debug('Rep is daily. Start of loop.');
|
||||
while ($start <= $end) {
|
||||
Log::debug(sprintf('Mutator is now: %s', $start->format('Y-m-d')));
|
||||
if (0 === $attempts % $skipMod) {
|
||||
Log::debug(sprintf('Attempts modulo skipmod is zero, include %s', $start->format('Y-m-d')));
|
||||
$return[] = clone $start;
|
||||
}
|
||||
$start->addDay();
|
||||
$attempts++;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Get the number of daily occurrences for a recurring transaction until date $end is reached. Will skip every $skipMod-1 occurrences.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getMonthlyInRange(Carbon $start, Carbon $end, int $skipMod, string $moment): array
|
||||
{
|
||||
$return = [];
|
||||
$attempts = 0;
|
||||
$dayOfMonth = (int)$moment;
|
||||
Log::debug(sprintf('Day of month in repetition is %d', $dayOfMonth));
|
||||
Log::debug(sprintf('Start is %s.', $start->format('Y-m-d')));
|
||||
Log::debug(sprintf('End is %s.', $end->format('Y-m-d')));
|
||||
if ($start->day > $dayOfMonth) {
|
||||
Log::debug('Add a month.');
|
||||
// day has passed already, add a month.
|
||||
$start->addMonth();
|
||||
}
|
||||
Log::debug(sprintf('Start is now %s.', $start->format('Y-m-d')));
|
||||
Log::debug('Start loop.');
|
||||
while ($start < $end) {
|
||||
Log::debug(sprintf('Mutator is now %s.', $start->format('Y-m-d')));
|
||||
$domCorrected = min($dayOfMonth, $start->daysInMonth);
|
||||
Log::debug(sprintf('DoM corrected is %d', $domCorrected));
|
||||
$start->day = $domCorrected;
|
||||
Log::debug(sprintf('Mutator is now %s.', $start->format('Y-m-d')));
|
||||
Log::debug(sprintf('$attempts %% $skipMod === 0 is %s', var_export(0 === $attempts % $skipMod, true)));
|
||||
Log::debug(sprintf('$start->lte($mutator) is %s', var_export($start->lte($start), true)));
|
||||
Log::debug(sprintf('$end->gte($mutator) is %s', var_export($end->gte($start), true)));
|
||||
if (0 === $attempts % $skipMod && $start->lte($start) && $end->gte($start)) {
|
||||
Log::debug(sprintf('ADD %s to return!', $start->format('Y-m-d')));
|
||||
$return[] = clone $start;
|
||||
}
|
||||
$attempts++;
|
||||
$start->endOfMonth()->startOfDay()->addDay();
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Get the number of daily occurrences for a recurring transaction until date $end is reached. Will skip every $skipMod-1 occurrences.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getNdomInRange(Carbon $start, Carbon $end, int $skipMod, string $moment): array
|
||||
{
|
||||
$return = [];
|
||||
$attempts = 0;
|
||||
$start->startOfMonth();
|
||||
// this feels a bit like a cop out but why reinvent the wheel?
|
||||
$counters = [1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth',];
|
||||
$daysOfWeek = [1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday',];
|
||||
$parts = explode(',', $moment);
|
||||
while ($start <= $end) {
|
||||
$string = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $start->format('F'), $start->format('Y'));
|
||||
$newCarbon = new Carbon($string);
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $newCarbon;
|
||||
}
|
||||
$attempts++;
|
||||
$start->endOfMonth()->addDay();
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Get the number of daily occurrences for a recurring transaction until date $end is reached. Will skip every $skipMod-1 occurrences.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getWeeklyInRange(Carbon $start, Carbon $end, int $skipMod, string $moment): array
|
||||
{
|
||||
$return = [];
|
||||
$attempts = 0;
|
||||
Log::debug('Rep is weekly.');
|
||||
// monday = 1
|
||||
// sunday = 7
|
||||
$dayOfWeek = (int)$moment;
|
||||
Log::debug(sprintf('DoW in repetition is %d, in mutator is %d', $dayOfWeek, $start->dayOfWeekIso));
|
||||
if ($start->dayOfWeekIso > $dayOfWeek) {
|
||||
// day has already passed this week, add one week:
|
||||
$start->addWeek();
|
||||
Log::debug(sprintf('Jump to next week, so mutator is now: %s', $start->format('Y-m-d')));
|
||||
}
|
||||
// today is wednesday (3), expected is friday (5): add two days.
|
||||
// today is friday (5), expected is monday (1), subtract four days.
|
||||
Log::debug(sprintf('Mutator is now: %s', $start->format('Y-m-d')));
|
||||
$dayDifference = $dayOfWeek - $start->dayOfWeekIso;
|
||||
$start->addDays($dayDifference);
|
||||
Log::debug(sprintf('Mutator is now: %s', $start->format('Y-m-d')));
|
||||
while ($start <= $end) {
|
||||
if (0 === $attempts % $skipMod && $start->lte($start) && $end->gte($start)) {
|
||||
Log::debug('Date is in range of start+end, add to set.');
|
||||
$return[] = clone $start;
|
||||
}
|
||||
$attempts++;
|
||||
$start->addWeek();
|
||||
Log::debug(sprintf('Mutator is now (end of loop): %s', $start->format('Y-m-d')));
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the number of daily occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip
|
||||
* over $skipMod -1 recurrences.
|
||||
*
|
||||
* @param Carbon $date
|
||||
* @param int $count
|
||||
* @param int $skipMod
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getXDailyOccurrences(Carbon $date, int $count, int $skipMod): array
|
||||
{
|
||||
$return = [];
|
||||
$mutator = clone $date;
|
||||
$total = 0;
|
||||
$attempts = 0;
|
||||
while ($total < $count) {
|
||||
$mutator->addDay();
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $mutator;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Calculates the number of monthly occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip
|
||||
* over $skipMod -1 recurrences.
|
||||
*
|
||||
* @param Carbon $date
|
||||
* @param int $count
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getXMonthlyOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array
|
||||
{
|
||||
$return = [];
|
||||
$mutator = clone $date;
|
||||
$total = 0;
|
||||
$attempts = 0;
|
||||
$mutator->addDay(); // always assume today has passed.
|
||||
$dayOfMonth = (int)$moment;
|
||||
if ($mutator->day > $dayOfMonth) {
|
||||
// day has passed already, add a month.
|
||||
$mutator->addMonth();
|
||||
}
|
||||
|
||||
while ($total < $count) {
|
||||
$domCorrected = min($dayOfMonth, $mutator->daysInMonth);
|
||||
$mutator->day = $domCorrected;
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $mutator;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->endOfMonth()->addDay();
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Calculates the number of NDOM occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip
|
||||
* over $skipMod -1 recurrences.
|
||||
*
|
||||
* @param Carbon $date
|
||||
* @param int $count
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getXNDomOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array
|
||||
{
|
||||
$return = [];
|
||||
$total = 0;
|
||||
$attempts = 0;
|
||||
$mutator = clone $date;
|
||||
$mutator->addDay(); // always assume today has passed.
|
||||
$mutator->startOfMonth();
|
||||
// this feels a bit like a cop out but why reinvent the wheel?
|
||||
$counters = [1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth',];
|
||||
$daysOfWeek = [1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday',];
|
||||
$parts = explode(',', $moment);
|
||||
|
||||
while ($total < $count) {
|
||||
$string = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $mutator->format('F'), $mutator->format('Y'));
|
||||
$newCarbon = new Carbon($string);
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $newCarbon;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->endOfMonth()->addDay();
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Calculates the number of weekly occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip
|
||||
* over $skipMod -1 recurrences.
|
||||
*
|
||||
* @param Carbon $date
|
||||
* @param int $count
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getXWeeklyOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array
|
||||
{
|
||||
$return = [];
|
||||
$total = 0;
|
||||
$attempts = 0;
|
||||
$mutator = clone $date;
|
||||
// monday = 1
|
||||
// sunday = 7
|
||||
$mutator->addDay(); // always assume today has passed.
|
||||
$dayOfWeek = (int)$moment;
|
||||
if ($mutator->dayOfWeekIso > $dayOfWeek) {
|
||||
// day has already passed this week, add one week:
|
||||
$mutator->addWeek();
|
||||
}
|
||||
// today is wednesday (3), expected is friday (5): add two days.
|
||||
// today is friday (5), expected is monday (1), subtract four days.
|
||||
$dayDifference = $dayOfWeek - $mutator->dayOfWeekIso;
|
||||
$mutator->addDays($dayDifference);
|
||||
|
||||
while ($total < $count) {
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $mutator;
|
||||
$total++;
|
||||
}
|
||||
$attempts++;
|
||||
$mutator->addWeek();
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Calculates the number of yearly occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip
|
||||
* over $skipMod -1 recurrences.
|
||||
*
|
||||
* @param Carbon $date
|
||||
* @param int $count
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getXYearlyOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array
|
||||
{
|
||||
$return = [];
|
||||
$mutator = clone $date;
|
||||
$total = 0;
|
||||
$attempts = 0;
|
||||
$date = new Carbon($moment);
|
||||
$date->year = $mutator->year;
|
||||
if ($mutator > $date) {
|
||||
$date->addYear();
|
||||
}
|
||||
$obj = clone $date;
|
||||
while ($total < $count) {
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $obj;
|
||||
$total++;
|
||||
}
|
||||
$obj->addYears(1);
|
||||
$attempts++;
|
||||
}
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Get the number of daily occurrences for a recurring transaction until date $end is reached. Will skip every $skipMod-1 occurrences.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param int $skipMod
|
||||
* @param string $moment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getYearlyInRange(Carbon $start, Carbon $end, int $skipMod, string $moment): array
|
||||
{
|
||||
$attempts = 0;
|
||||
$date = new Carbon($moment);
|
||||
$date->year = $start->year;
|
||||
$return = [];
|
||||
if ($start > $date) {
|
||||
$date->addYear();
|
||||
|
||||
}
|
||||
|
||||
// is $date between $start and $end?
|
||||
$obj = clone $date;
|
||||
$count = 0;
|
||||
while ($obj <= $end && $obj >= $start && $count < 10) {
|
||||
if (0 === $attempts % $skipMod) {
|
||||
$return[] = clone $obj;
|
||||
}
|
||||
$obj->addYears(1);
|
||||
$count++;
|
||||
$attempts++;
|
||||
}
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ use DB;
|
||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -101,6 +100,7 @@ class TagRepository implements TagRepositoryInterface
|
||||
* @param string $tag
|
||||
*
|
||||
* @return Tag
|
||||
* @deprecated
|
||||
*/
|
||||
public function findByTag(string $tag): Tag
|
||||
{
|
||||
@@ -129,6 +129,7 @@ class TagRepository implements TagRepositoryInterface
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return Carbon
|
||||
* @deprecated
|
||||
*/
|
||||
public function firstUseDate(Tag $tag): Carbon
|
||||
{
|
||||
@@ -160,6 +161,7 @@ class TagRepository implements TagRepositoryInterface
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return Carbon
|
||||
* @deprecated
|
||||
*/
|
||||
public function lastUseDate(Tag $tag): Carbon
|
||||
{
|
||||
@@ -306,9 +308,7 @@ class TagRepository implements TagRepositoryInterface
|
||||
}
|
||||
if (null !== $year) {
|
||||
Log::debug(sprintf('Get tags with year %s.', $year));
|
||||
$start = $year . '-01-01 00:00:00';
|
||||
$end = $year . '-12-31 23:59:59';
|
||||
$tagQuery->where('tags.date', '>=', $start)->where('tags.date', '<=', $end);
|
||||
$tagQuery->where('tags.date', '>=', $year . '-01-01 00:00:00')->where('tags.date', '<=', $year . '-12-31 23:59:59');
|
||||
}
|
||||
|
||||
$result = $tagQuery->get(['tags.id', 'tags.tag', DB::raw('SUM(transactions.amount) as amount_sum')]);
|
||||
|
||||
@@ -24,7 +24,6 @@ namespace FireflyIII\Repositories\Tag;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@@ -68,6 +67,7 @@ interface TagRepositoryInterface
|
||||
* @param string $tag
|
||||
*
|
||||
* @return Tag
|
||||
* @deprecated
|
||||
*/
|
||||
public function findByTag(string $tag): Tag;
|
||||
|
||||
@@ -82,6 +82,7 @@ interface TagRepositoryInterface
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return Carbon
|
||||
* @deprecated
|
||||
*/
|
||||
public function firstUseDate(Tag $tag): Carbon;
|
||||
|
||||
@@ -96,6 +97,7 @@ interface TagRepositoryInterface
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return Carbon
|
||||
* @deprecated
|
||||
*/
|
||||
public function lastUseDate(Tag $tag): Carbon;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user