mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 20:22:07 +00:00
Refactor some code for recurrences.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user