mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Refactor some code for recurrences.
This commit is contained in:
@@ -184,7 +184,6 @@ class AccountController extends Controller
|
||||
* @param Request $request
|
||||
* @param Account $account
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show(Request $request, Account $account): JsonResponse
|
||||
|
||||
@@ -162,7 +162,6 @@ class AttachmentController extends Controller
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Attachment $attachment
|
||||
* @codeCoverageIgnore
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show(Request $request, Attachment $attachment): JsonResponse
|
||||
|
||||
@@ -41,7 +41,8 @@ class Request extends FireflyIIIRequest
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getAllAccountData(): array {
|
||||
public function getAllAccountData(): array
|
||||
{
|
||||
$active = true;
|
||||
$includeNetWorth = true;
|
||||
if (null !== $this->get('active')) {
|
||||
@@ -194,7 +195,8 @@ class Request extends FireflyIIIRequest
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getRecurrenceTransactionData(): array {
|
||||
protected function getRecurrenceTransactionData(): array
|
||||
{
|
||||
$return = [];
|
||||
// transaction data:
|
||||
/** @var array $transactions */
|
||||
@@ -217,6 +219,7 @@ class Request extends FireflyIIIRequest
|
||||
'destination_id' => isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null,
|
||||
'destination_name' => isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null,
|
||||
'description' => $transaction['description'],
|
||||
'type' => $this->string('type'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -112,7 +112,7 @@ class AccountFactory
|
||||
$data['currency_id'] = $currency->id;
|
||||
// remove virtual balance when not an asset account or a liability
|
||||
$canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
|
||||
if (!\in_array($type->type, $canHaveVirtual, true)) {
|
||||
if (!in_array($type->type, $canHaveVirtual, true)) {
|
||||
$databaseData['virtual_balance'] = '0';
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ class AccountFactory
|
||||
$return = Account::create($databaseData);
|
||||
$this->updateMetaData($return, $data);
|
||||
|
||||
if (\in_array($type->type, $canHaveVirtual, true)) {
|
||||
if (in_array($type->type, $canHaveVirtual, true)) {
|
||||
if ($this->validIBData($data)) {
|
||||
$this->updateIB($return, $data);
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ class BillFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $billId
|
||||
* @param int|null $billId
|
||||
* @param null|string $billName
|
||||
*
|
||||
* @return Bill|null
|
||||
@@ -126,8 +126,10 @@ class BillFactory
|
||||
public function findByName(string $name): ?Bill
|
||||
{
|
||||
$query = sprintf('%%%s%%', $name);
|
||||
/** @var Bill $first */
|
||||
$first = $this->user->bills()->where('name', 'LIKE', $query)->first();
|
||||
|
||||
return $this->user->bills()->where('name', 'LIKE', $query)->first();
|
||||
return $first;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -246,6 +246,10 @@ class TransactionJournalFactory
|
||||
$destinationAccount = $this->getAccount($type->type, 'destination', (int)$row['destination_id'], $row['destination_name']);
|
||||
|
||||
/** double check currencies. */
|
||||
$sourceCurrency = $currency;
|
||||
$destCurrency = $currency;
|
||||
$sourceForeignCurrency = $foreignCurrency;
|
||||
$destForeignCurrency = $foreignCurrency;
|
||||
|
||||
if ($type->type === 'Withdrawal') {
|
||||
// make sure currency is correct.
|
||||
|
||||
@@ -61,7 +61,7 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
$amounts = array_column($data, 'amount');
|
||||
$next = next($amounts);
|
||||
$sortFlag = SORT_ASC;
|
||||
if (!\is_bool($next) && 1 === bccomp((string)$next, '0')) {
|
||||
if (!is_bool($next) && 1 === bccomp((string)$next, '0')) {
|
||||
$sortFlag = SORT_DESC;
|
||||
}
|
||||
array_multisort($amounts, $sortFlag, $data);
|
||||
@@ -118,7 +118,7 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
{
|
||||
reset($data);
|
||||
$first = current($data);
|
||||
$labels = \is_array($first['entries']) ? array_keys($first['entries']) : [];
|
||||
$labels = is_array($first['entries']) ? array_keys($first['entries']) : [];
|
||||
|
||||
$chartData = [
|
||||
'count' => count($data),
|
||||
@@ -173,7 +173,7 @@ class ChartJsGenerator implements GeneratorInterface
|
||||
// different sort when values are positive and when they're negative.
|
||||
asort($data);
|
||||
$next = next($data);
|
||||
if (!\is_bool($next) && 1 === bccomp((string)$next, '0')) {
|
||||
if (!is_bool($next) && 1 === bccomp((string)$next, '0')) {
|
||||
// next is positive, sort other way around.
|
||||
arsort($data);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Generator\Report\ReportGeneratorInterface;
|
||||
use FireflyIII\Generator\Report\Support;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
@@ -101,82 +100,6 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get expense collection for report.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getExpenses(): array
|
||||
{
|
||||
if (count($this->expenses) > 0) {
|
||||
Log::debug('Return previous set of expenses.');
|
||||
|
||||
return $this->expenses;
|
||||
}
|
||||
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
|
||||
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||
->setTags($this->tags)->withAccountInformation();
|
||||
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$this->expenses = $journals;
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the income for this report.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getIncome(): array
|
||||
{
|
||||
if (count($this->income) > 0) {
|
||||
return $this->income;
|
||||
}
|
||||
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
|
||||
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
||||
->setTags($this->tags)->withAccountInformation();
|
||||
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$this->income = $journals;
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize by tag.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function summarizeByTag(array $array): array
|
||||
{
|
||||
$tagIds = array_map('\intval', $this->tags->pluck('id')->toArray());
|
||||
$result = [];
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
/**
|
||||
* @var int $id
|
||||
* @var array $tag
|
||||
*/
|
||||
foreach ($journal['tags'] as $id => $tag) {
|
||||
if (in_array($id, $tagIds, true)) {
|
||||
$result[$id] = $result[$id] ?? '0';
|
||||
$result[$id] = bcadd($journal['amount'], $result[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the accounts.
|
||||
*
|
||||
@@ -268,4 +191,80 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get expense collection for report.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getExpenses(): array
|
||||
{
|
||||
if (count($this->expenses) > 0) {
|
||||
Log::debug('Return previous set of expenses.');
|
||||
|
||||
return $this->expenses;
|
||||
}
|
||||
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
|
||||
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||
->setTags($this->tags)->withAccountInformation();
|
||||
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$this->expenses = $journals;
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the income for this report.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getIncome(): array
|
||||
{
|
||||
if (count($this->income) > 0) {
|
||||
return $this->income;
|
||||
}
|
||||
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
|
||||
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
||||
->setTags($this->tags)->withAccountInformation();
|
||||
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$this->income = $journals;
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize by tag.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function summarizeByTag(array $array): array
|
||||
{
|
||||
$tagIds = array_map('\intval', $this->tags->pluck('id')->toArray());
|
||||
$result = [];
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
/**
|
||||
* @var int $id
|
||||
* @var array $tag
|
||||
*/
|
||||
foreach ($journal['tags'] as $id => $tag) {
|
||||
if (in_array($id, $tagIds, true)) {
|
||||
$result[$id] = $result[$id] ?? '0';
|
||||
$result[$id] = bcadd($journal['amount'], $result[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Handlers\Events;
|
||||
|
||||
use FireflyIII\Events\StoredTransactionGroup;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleGroup;
|
||||
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
||||
@@ -39,7 +40,7 @@ class StoredGroupEventHandler
|
||||
* @param StoredTransactionGroup $storedJournalEvent
|
||||
*
|
||||
* @return bool
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function processRules(StoredTransactionGroup $storedJournalEvent): bool
|
||||
{
|
||||
@@ -47,6 +48,7 @@ class StoredGroupEventHandler
|
||||
if(false === $storedJournalEvent->applyRules) {
|
||||
return true;
|
||||
}
|
||||
// TODO fix this
|
||||
die('cannot apply rules yet');
|
||||
// create objects:
|
||||
/** @var RuleGroupRepositoryInterface $ruleGroupRepos */
|
||||
|
||||
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Handlers\Events;
|
||||
|
||||
use FireflyIII\Events\UpdatedTransactionGroup;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleGroup;
|
||||
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
||||
@@ -39,13 +40,14 @@ class UpdatedGroupEventHandler
|
||||
* @param UpdatedTransactionGroup $updatedJournalEvent
|
||||
*
|
||||
* @return bool
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function processRules(UpdatedTransactionGroup $updatedJournalEvent): bool
|
||||
{
|
||||
// get all the user's rule groups, with the rules, order by 'order'.
|
||||
$journals = $updatedJournalEvent->transactionGroup->transactionJournals;
|
||||
|
||||
// TODO fix this
|
||||
die('cannot apply rules yet');
|
||||
/** @var RuleGroupRepositoryInterface $ruleGroupRepos */
|
||||
$ruleGroupRepos = app(RuleGroupRepositoryInterface::class);
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Handlers\Events;
|
||||
|
||||
|
||||
use FireflyConfig;
|
||||
use FireflyIII\Events\RequestedVersionCheckStatus;
|
||||
use FireflyIII\Helpers\Update\UpdateTrait;
|
||||
use FireflyIII\Models\Configuration;
|
||||
@@ -71,7 +70,7 @@ class VersionCheckEventHandler
|
||||
}
|
||||
|
||||
/** @var Configuration $lastCheckTime */
|
||||
$lastCheckTime = FireflyConfig::get('last_update_check', time());
|
||||
$lastCheckTime = app('fireflyconfig')->get('last_update_check', time());
|
||||
$now = time();
|
||||
$diff = $now - $lastCheckTime->data;
|
||||
Log::debug(sprintf('Last check time is %d, current time is %d, difference is %d', $lastCheckTime->data, $now, $diff));
|
||||
@@ -90,6 +89,6 @@ class VersionCheckEventHandler
|
||||
// flash info
|
||||
session()->flash('info', $resultString);
|
||||
}
|
||||
FireflyConfig::set('last_update_check', time());
|
||||
app('fireflyconfig')->set('last_update_check', time());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@ trait BillServiceTrait
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function updateNote(Bill $bill, string $note): bool
|
||||
{
|
||||
|
||||
@@ -57,7 +57,7 @@ trait RecurringTransactionTrait
|
||||
[
|
||||
'recurrence_id' => $recurrence->id,
|
||||
'repetition_type' => $array['type'],
|
||||
'repetition_moment' => $array['moment'],
|
||||
'repetition_moment' => $array['moment'] ?? '',
|
||||
'repetition_skip' => $array['skip'],
|
||||
'weekend' => $array['weekend'] ?? 1,
|
||||
]
|
||||
|
||||
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Services\Internal\Support;
|
||||
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\AccountFactory;
|
||||
use FireflyIII\Factory\TransactionCurrencyFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
@@ -75,11 +76,12 @@ trait TransactionServiceTrait
|
||||
|
||||
/**
|
||||
* @param string|null $expectedType
|
||||
* @param int|null $accountId
|
||||
* @param Account|null $account
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return Account|null
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @throws FireflyException
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function findAccount(?string $expectedType, ?Account $account, ?int $accountId, ?string $accountName): ?Account
|
||||
|
||||
@@ -130,7 +130,7 @@ class AccountValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
@@ -195,19 +195,23 @@ class AccountValidator
|
||||
*/
|
||||
private function canCreateTypes(array $accountTypes): bool
|
||||
{
|
||||
Log::debug('Can we create any of these types?', $accountTypes);
|
||||
/** @var string $accountType */
|
||||
foreach ($accountTypes as $accountType) {
|
||||
if ($this->canCreateType($accountType)) {
|
||||
Log::debug(sprintf('YES, we can create a %s', $accountType));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Log::debug('NO, we cant create any of those.');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $validTypes
|
||||
* @param int|null $accountId
|
||||
* @param array $validTypes
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return Account|null
|
||||
@@ -282,7 +286,7 @@ class AccountValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
@@ -360,7 +364,7 @@ class AccountValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
@@ -391,7 +395,7 @@ class AccountValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
@@ -409,6 +413,20 @@ class AccountValidator
|
||||
return false;
|
||||
}
|
||||
|
||||
// if there's an ID it must be of the "validTypes".
|
||||
if (null !== $accountId && 0 !== $accountId) {
|
||||
$found = $this->accountRepository->findNull($accountId);
|
||||
if (null !== $found) {
|
||||
$type = $found->accountType->type;
|
||||
if (in_array($type, $validTypes)) {
|
||||
return true;
|
||||
}
|
||||
$this->destError = (string)trans('validation.withdrawal_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// if the account can be created anyway don't need to search.
|
||||
if (true === $this->canCreateTypes($validTypes)) {
|
||||
|
||||
@@ -420,7 +438,7 @@ class AccountValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return bool
|
||||
|
||||
@@ -26,8 +26,8 @@ namespace FireflyIII\Validation;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Illuminate\Validation\Validator;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Trait TransactionValidation
|
||||
@@ -41,15 +41,16 @@ trait TransactionValidation
|
||||
*/
|
||||
public function validateAccountInformation(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
Log::debug('Now in validateAccountInformation()');
|
||||
$data = $validator->getData();
|
||||
$transactionType = $data['type'] ?? 'invalid';
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
|
||||
/** @var AccountValidator $accountValidator */
|
||||
$accountValidator = app(AccountValidator::class);
|
||||
|
||||
|
||||
Log::debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$transactionType = $transaction['type'] ?? 'invalid';
|
||||
$accountValidator->setTransactionType($transactionType);
|
||||
|
||||
// validate source account.
|
||||
@@ -224,7 +225,7 @@ trait TransactionValidation
|
||||
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
$originalType = $this->getOriginalType($transaction['transaction_journal_id'] ?? 0);
|
||||
$originalData = $this->getOriginalData($transaction['transaction_journal_id'] ?? 0);
|
||||
$originalData = $this->getOriginalData($transaction['transaction_journal_id'] ?? 0);
|
||||
$transactionType = $transaction['type'] ?? $originalType;
|
||||
$accountValidator->setTransactionType($transactionType);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user