. */ declare(strict_types=1); namespace FireflyIII\Services\Internal\Support; use FireflyIII\Factory\BillFactory; use FireflyIII\Factory\TagFactory; use FireflyIII\Factory\TransactionJournalMetaFactory; use FireflyIII\Models\Note; use FireflyIII\Models\TransactionJournal; /** * Trait JournalServiceTrait * * @package FireflyIII\Services\Internal\Support */ trait JournalServiceTrait { /** * Connect bill if present. * * @param TransactionJournal $journal * @param array $data */ protected function connectBill(TransactionJournal $journal, array $data): void { /** @var BillFactory $factory */ $factory = app(BillFactory::class); $factory->setUser($journal->user); $bill = $factory->find($data['bill_id'], $data['bill_name']); if (!is_null($bill)) { $journal->bill_id = $bill->id; $journal->save(); return; } $journal->bill_id = null; $journal->save(); return; } /** * @param TransactionJournal $journal * @param array $data */ protected function connectTags(TransactionJournal $journal, array $data): void { /** @var TagFactory $factory */ $factory = app(TagFactory::class); $factory->setUser($journal->user); $set = []; if (!is_array($data['tags'])) { return; // @codeCoverageIgnore } foreach ($data['tags'] as $string) { if (strlen($string) > 0) { $tag = $factory->findOrCreate($string); $set[] = $tag->id; } } $journal->tags()->sync($set); } /** * @param TransactionJournal $journal * @param array $data * @param string $field */ protected function storeMeta(TransactionJournal $journal, array $data, string $field): void { if (!isset($data[$field])) { return; } $set = [ 'journal' => $journal, 'name' => $field, 'data' => $data[$field], ]; /** @var TransactionJournalMetaFactory $factory */ $factory = app(TransactionJournalMetaFactory::class); $factory->updateOrCreate($set); } /** * @param TransactionJournal $journal * @param string $notes */ protected function storeNote(TransactionJournal $journal, ?string $notes): void { $notes = strval($notes); if (strlen($notes) > 0) { $note = $journal->notes()->first(); if (is_null($note)) { $note = new Note; $note->noteable()->associate($journal); } $note->text = $notes; $note->save(); return; } $note = $journal->notes()->first(); if (!is_null($note)) { $note->delete(); } return; } }