mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 18:54:58 +00:00 
			
		
		
		
	Fix for #835
This commit is contained in:
		| @@ -420,7 +420,7 @@ class CategoryController extends Controller | ||||
|         $accountRepository = app(AccountRepositoryInterface::class); | ||||
|         $accounts          = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); | ||||
|         $first             = $repository->firstUseDate($category); | ||||
|         if ($first->year === 1900) { | ||||
|         if (is_null($first)) { | ||||
|             $first = new Carbon; | ||||
|         } | ||||
|         $range   = Preferences::get('viewRange', '1M')->data; | ||||
|   | ||||
| @@ -67,7 +67,7 @@ class CategoryController extends Controller | ||||
|  | ||||
|         $start = $repository->firstUseDate($category); | ||||
|  | ||||
|         if ($start->year === 1900) { | ||||
|         if (is_null($first)) { | ||||
|             $start = new Carbon; | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -17,7 +17,6 @@ use Carbon\Carbon; | ||||
| use FireflyIII\Helpers\Collector\JournalCollectorInterface; | ||||
| use FireflyIII\Models\Category; | ||||
| use FireflyIII\Models\Transaction; | ||||
| use FireflyIII\Models\TransactionJournal; | ||||
| use FireflyIII\Models\TransactionType; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Support\Collection; | ||||
| @@ -105,33 +104,28 @@ class CategoryRepository implements CategoryRepositoryInterface | ||||
|     /** | ||||
|      * @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 */ | ||||
|         $firstJournal = $category->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.date']); | ||||
|  | ||||
|         // if transaction journal exists and date is before $first, then | ||||
|         // new date: | ||||
|         if (!is_null($firstJournal) && $firstJournal->date->lessThanOrEqualTo($first)) { | ||||
|             $first = $firstJournal->date; | ||||
|         if (is_null($firstTransactionDate) && is_null($firstJournalDate)) { | ||||
|             return null; | ||||
|         } | ||||
|         if (is_null($firstTransactionDate)) { | ||||
|             return $firstJournalDate; | ||||
|         } | ||||
|         if (is_null($firstJournalDate)) { | ||||
|             return $firstTransactionDate; | ||||
|         } | ||||
|  | ||||
|         // check transactions: | ||||
|         $firstTransaction = $category->transactions() | ||||
|                                      ->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); | ||||
|         if ($firstTransactionDate < $firstJournalDate) { | ||||
|             return $firstTransactionDate; | ||||
|         } | ||||
|  | ||||
|         return $first; | ||||
|         return $firstJournalDate; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -156,26 +150,28 @@ class CategoryRepository implements CategoryRepositoryInterface | ||||
|      * @param Category   $category | ||||
|      * @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); | ||||
|  | ||||
|         if ($lastJournalDate->year !== 1900) { | ||||
|             $last = clone $lastJournalDate; | ||||
|             unset($lastJournalDate); | ||||
|         } | ||||
|  | ||||
|         $lastTransactionDate = $this->getLastTransactionDate($category, $accounts); | ||||
|  | ||||
|         if ($lastTransactionDate->year !== 1900 && $lastTransactionDate < $last) { | ||||
|             $last = clone $lastTransactionDate; | ||||
|             unset($lastTransactionDate); | ||||
|         if (is_null($lastTransactionDate) && is_null($lastJournalDate)) { | ||||
|             return null; | ||||
|         } | ||||
|         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; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @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 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'); | ||||
|  | ||||
| @@ -477,16 +510,16 @@ class CategoryRepository implements CategoryRepositoryInterface | ||||
|             return $result->date; | ||||
|         } | ||||
|  | ||||
|         return new Carbon('1900-01-01'); | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param Category   $category | ||||
|      * @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: | ||||
|         $query = $category->transactions() | ||||
| @@ -502,7 +535,7 @@ class CategoryRepository implements CategoryRepositoryInterface | ||||
|             return new Carbon($lastTransaction->date); | ||||
|         } | ||||
|  | ||||
|         return new Carbon('1900-01-01'); | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -63,9 +63,9 @@ interface CategoryRepositoryInterface | ||||
|     /** | ||||
|      * @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. | ||||
| @@ -75,14 +75,14 @@ interface CategoryRepositoryInterface | ||||
|     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 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 | ||||
|   | ||||
| @@ -23,7 +23,7 @@ | ||||
|             <td data-value="{{ category.name }}"> | ||||
|                 <a href="{{ route('categories.show', category.id) }}" title="{{ category.name }}">{{ category.name }}</a> | ||||
|             </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') }}"> | ||||
|                     {{ category.lastActivity.formatLocalized(monthAndDayFormat) }} | ||||
|                 </td> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user