Files
firefly-iii/app/Repositories/Journal/JournalAPIRepository.php

100 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
* JournalAPIRepository.php
2020-02-16 14:00:57 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-08-17 12:09:03 +02:00
declare(strict_types=1);
namespace FireflyIII\Repositories\Journal;
2020-05-07 06:44:01 +02:00
use FireflyIII\Models\Attachment;
2025-01-03 19:07:29 +01:00
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2025-02-23 12:28:27 +01:00
use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use Illuminate\Support\Collection;
2025-02-23 12:47:04 +01:00
use Illuminate\Support\Facades\Storage;
/**
* Class JournalAPIRepository
*/
2025-02-23 12:28:27 +01:00
class JournalAPIRepository implements JournalAPIRepositoryInterface, UserGroupInterface
{
2025-02-23 12:28:27 +01:00
use UserGroupTrait;
/**
* Returns transaction by ID. Used to validate attachments.
*/
public function findTransaction(int $transactionId): ?Transaction
{
2020-10-23 19:11:25 +02:00
return Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
2023-12-20 19:35:52 +01:00
->where('transaction_journals.user_id', $this->user->id)
->where('transactions.id', $transactionId)
->first(['transactions.*'])
;
}
/**
2022-12-31 15:54:55 +01:00
* TODO pretty sure method duplicated.
*
* Return all attachments for journal.
*/
public function getAttachments(TransactionJournal $journal): Collection
{
$set = $journal->attachments;
2020-05-07 06:44:01 +02:00
2025-02-23 12:47:04 +01:00
$disk = Storage::disk('upload');
2020-05-07 06:44:01 +02:00
2020-10-23 19:11:25 +02:00
return $set->each(
2020-05-07 06:44:01 +02:00
static function (Attachment $attachment) use ($disk) {
2022-03-29 14:59:58 +02:00
$notes = $attachment->notes()->first();
2020-05-07 06:44:01 +02:00
$attachment->file_exists = $disk->exists($attachment->fileName());
2023-11-05 09:40:45 +01:00
$attachment->notes_text = null !== $notes ? $notes->text : ''; // TODO should not set notes like this.
2020-05-07 06:44:01 +02:00
return $attachment;
}
);
}
2021-03-12 06:20:01 +01:00
public function getJournalLinks(TransactionJournal $journal): Collection
{
$collection = $journal->destJournalLinks()->get();
return $journal->sourceJournalLinks()->get()->merge($collection);
}
/**
* Get all piggy bank events for a journal.
*/
public function getPiggyBankEvents(TransactionJournal $journal): Collection
{
$events = $journal->piggyBankEvents()->get();
$events->each(
static function (PiggyBankEvent $event): void { // @phpstan-ignore-line
2025-01-03 19:07:29 +01:00
$event->piggyBank = PiggyBank::withTrashed()->find($event->piggy_bank_id);
}
);
return $events;
}
2019-08-17 12:09:03 +02:00
}