mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix for #835
This commit is contained in:
parent
3e8fe70915
commit
1d3ace5f21
@ -420,7 +420,7 @@ class CategoryController extends Controller
|
|||||||
$accountRepository = app(AccountRepositoryInterface::class);
|
$accountRepository = app(AccountRepositoryInterface::class);
|
||||||
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
||||||
$first = $repository->firstUseDate($category);
|
$first = $repository->firstUseDate($category);
|
||||||
if ($first->year === 1900) {
|
if (is_null($first)) {
|
||||||
$first = new Carbon;
|
$first = new Carbon;
|
||||||
}
|
}
|
||||||
$range = Preferences::get('viewRange', '1M')->data;
|
$range = Preferences::get('viewRange', '1M')->data;
|
||||||
|
@ -67,7 +67,7 @@ class CategoryController extends Controller
|
|||||||
|
|
||||||
$start = $repository->firstUseDate($category);
|
$start = $repository->firstUseDate($category);
|
||||||
|
|
||||||
if ($start->year === 1900) {
|
if (is_null($first)) {
|
||||||
$start = new Carbon;
|
$start = new Carbon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ use Carbon\Carbon;
|
|||||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||||
use FireflyIII\Models\Category;
|
use FireflyIII\Models\Category;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@ -105,33 +104,28 @@ class CategoryRepository implements CategoryRepositoryInterface
|
|||||||
/**
|
/**
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return Carbon|null
|
||||||
*/
|
*/
|
||||||
public function firstUseDate(Category $category): Carbon
|
public function firstUseDate(Category $category): ?Carbon
|
||||||
{
|
{
|
||||||
$first = new Carbon;
|
$firstJournalDate = $this->getFirstJournalDate($category);
|
||||||
|
$firstTransactionDate = $this->getFirstTransactionDate($category);
|
||||||
|
|
||||||
/** @var TransactionJournal $firstJournal */
|
if (is_null($firstTransactionDate) && is_null($firstJournalDate)) {
|
||||||
$firstJournal = $category->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.date']);
|
return null;
|
||||||
|
}
|
||||||
// if transaction journal exists and date is before $first, then
|
if (is_null($firstTransactionDate)) {
|
||||||
// new date:
|
return $firstJournalDate;
|
||||||
if (!is_null($firstJournal) && $firstJournal->date->lessThanOrEqualTo($first)) {
|
}
|
||||||
$first = $firstJournal->date;
|
if (is_null($firstJournalDate)) {
|
||||||
|
return $firstTransactionDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check transactions:
|
if ($firstTransactionDate < $firstJournalDate) {
|
||||||
$firstTransaction = $category->transactions()
|
return $firstTransactionDate;
|
||||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
||||||
->orderBy('transaction_journals.date', 'ASC')->first(['transaction_journals.date']);
|
|
||||||
|
|
||||||
|
|
||||||
// transaction exists, and date is before $first, this date becomes first.
|
|
||||||
if (!is_null($firstTransaction) && Carbon::parse($firstTransaction->date)->lessThanOrEqualTo($first)) {
|
|
||||||
$first = new Carbon($firstTransaction->date);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $first;
|
return $firstJournalDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,26 +150,28 @@ class CategoryRepository implements CategoryRepositoryInterface
|
|||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return Carbon|null
|
||||||
*/
|
*/
|
||||||
public function lastUseDate(Category $category, Collection $accounts): Carbon
|
public function lastUseDate(Category $category, Collection $accounts): ?Carbon
|
||||||
{
|
{
|
||||||
$last = new Carbon('1900-01-01');
|
$lastJournalDate = $this->getLastJournalDate($category, $accounts);
|
||||||
$lastJournalDate = $this->getLastJournalDate($category, $accounts);
|
|
||||||
|
|
||||||
if ($lastJournalDate->year !== 1900) {
|
|
||||||
$last = clone $lastJournalDate;
|
|
||||||
unset($lastJournalDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
$lastTransactionDate = $this->getLastTransactionDate($category, $accounts);
|
$lastTransactionDate = $this->getLastTransactionDate($category, $accounts);
|
||||||
|
|
||||||
if ($lastTransactionDate->year !== 1900 && $lastTransactionDate < $last) {
|
if (is_null($lastTransactionDate) && is_null($lastJournalDate)) {
|
||||||
$last = clone $lastTransactionDate;
|
return null;
|
||||||
unset($lastTransactionDate);
|
}
|
||||||
|
if (is_null($lastTransactionDate)) {
|
||||||
|
return $lastJournalDate;
|
||||||
|
}
|
||||||
|
if (is_null($lastJournalDate)) {
|
||||||
|
return $lastTransactionDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $last;
|
if ($lastTransactionDate < $lastJournalDate) {
|
||||||
|
return $lastTransactionDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $lastJournalDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -456,13 +452,50 @@ class CategoryRepository implements CategoryRepositoryInterface
|
|||||||
return $category;
|
return $category;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Category $category
|
||||||
|
*
|
||||||
|
* @return Carbon|null
|
||||||
|
*/
|
||||||
|
private function getFirstJournalDate(Category $category): ?Carbon
|
||||||
|
{
|
||||||
|
$query = $category->transactionJournals()->orderBy('date', 'ASC');
|
||||||
|
$result = $query->first(['transaction_journals.*']);
|
||||||
|
|
||||||
|
if (!is_null($result)) {
|
||||||
|
return $result->date;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Category $category
|
||||||
|
*
|
||||||
|
* @return Carbon|null
|
||||||
|
*/
|
||||||
|
private function getFirstTransactionDate(Category $category): ?Carbon
|
||||||
|
{
|
||||||
|
// check transactions:
|
||||||
|
$query = $category->transactions()
|
||||||
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||||
|
->orderBy('transaction_journals.date', 'DESC');
|
||||||
|
|
||||||
|
$lastTransaction = $query->first(['transaction_journals.*']);
|
||||||
|
if (!is_null($lastTransaction)) {
|
||||||
|
return new Carbon($lastTransaction->date);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return Carbon|null
|
||||||
*/
|
*/
|
||||||
private function getLastJournalDate(Category $category, Collection $accounts): Carbon
|
private function getLastJournalDate(Category $category, Collection $accounts): ?Carbon
|
||||||
{
|
{
|
||||||
$query = $category->transactionJournals()->orderBy('date', 'DESC');
|
$query = $category->transactionJournals()->orderBy('date', 'DESC');
|
||||||
|
|
||||||
@ -477,16 +510,16 @@ class CategoryRepository implements CategoryRepositoryInterface
|
|||||||
return $result->date;
|
return $result->date;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Carbon('1900-01-01');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return Carbon|null
|
||||||
*/
|
*/
|
||||||
private function getLastTransactionDate(Category $category, Collection $accounts): Carbon
|
private function getLastTransactionDate(Category $category, Collection $accounts): ?Carbon
|
||||||
{
|
{
|
||||||
// check transactions:
|
// check transactions:
|
||||||
$query = $category->transactions()
|
$query = $category->transactions()
|
||||||
@ -502,7 +535,7 @@ class CategoryRepository implements CategoryRepositoryInterface
|
|||||||
return new Carbon($lastTransaction->date);
|
return new Carbon($lastTransaction->date);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Carbon('1900-01-01');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -63,9 +63,9 @@ interface CategoryRepositoryInterface
|
|||||||
/**
|
/**
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return Carbon|null
|
||||||
*/
|
*/
|
||||||
public function firstUseDate(Category $category): Carbon;
|
public function firstUseDate(Category $category): ?Carbon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of all the categories belonging to a user.
|
* Returns a list of all the categories belonging to a user.
|
||||||
@ -75,14 +75,14 @@ interface CategoryRepositoryInterface
|
|||||||
public function getCategories(): Collection;
|
public function getCategories(): Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return most recent transaction(journal) date.
|
* Return most recent transaction(journal) date or null when never used before.
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
*
|
*
|
||||||
* @return Carbon
|
* @return Carbon|null
|
||||||
*/
|
*/
|
||||||
public function lastUseDate(Category $category, Collection $accounts): Carbon;
|
public function lastUseDate(Category $category, Collection $accounts): ?Carbon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $categories
|
* @param Collection $categories
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
<td data-value="{{ category.name }}">
|
<td data-value="{{ category.name }}">
|
||||||
<a href="{{ route('categories.show', category.id) }}" title="{{ category.name }}">{{ category.name }}</a>
|
<a href="{{ route('categories.show', category.id) }}" title="{{ category.name }}">{{ category.name }}</a>
|
||||||
</td>
|
</td>
|
||||||
{% if category.lastActivity.year != "1900" %}
|
{% if category.lastActivity %}
|
||||||
<td class="hidden-sm hidden-xs" data-value="{{ category.lastActivity.format('Y-m-d H-i-s') }}">
|
<td class="hidden-sm hidden-xs" data-value="{{ category.lastActivity.format('Y-m-d H-i-s') }}">
|
||||||
{{ category.lastActivity.formatLocalized(monthAndDayFormat) }}
|
{{ category.lastActivity.formatLocalized(monthAndDayFormat) }}
|
||||||
</td>
|
</td>
|
||||||
|
Loading…
Reference in New Issue
Block a user