firefly-iii/app/Services/Internal/Support/JournalServiceTrait.php

131 lines
3.6 KiB
PHP
Raw Normal View History

2018-02-23 08:12:47 -06:00
<?php
/**
* JournalServiceTrait.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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
*
*/
trait JournalServiceTrait
{
/**
* @param TransactionJournal $journal
* @param array $data
*/
public function connectTags(TransactionJournal $journal, array $data): void
{
/** @var TagFactory $factory */
$factory = app(TagFactory::class);
$factory->setUser($journal->user);
$set = [];
2018-04-27 23:23:13 -05:00
if (!\is_array($data['tags'])) {
return; // @codeCoverageIgnore
}
foreach ($data['tags'] as $string) {
2018-04-07 15:23:16 -05:00
if (\strlen($string) > 0) {
$tag = $factory->findOrCreate($string);
$set[] = $tag->id;
}
}
$journal->tags()->sync($set);
}
2018-02-23 08:12:47 -06:00
/**
* 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);
2018-03-02 09:31:02 -06:00
$factory->setUser($journal->user);
2018-02-23 08:12:47 -06:00
$bill = $factory->find($data['bill_id'], $data['bill_name']);
2018-04-02 07:50:17 -05:00
if (null !== $bill) {
2018-02-23 08:12:47 -06:00
$journal->bill_id = $bill->id;
$journal->save();
return;
}
$journal->bill_id = null;
$journal->save();
}
/**
* @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);
2018-03-01 13:54:50 -06:00
$factory->updateOrCreate($set);
2018-02-23 08:12:47 -06:00
}
/**
* @param TransactionJournal $journal
* @param string $notes
*/
protected function storeNote(TransactionJournal $journal, ?string $notes): void
{
2018-04-02 07:50:17 -05:00
$notes = (string)$notes;
2018-04-27 23:23:13 -05:00
if (\strlen($notes) > 0) {
2018-02-23 08:12:47 -06:00
$note = $journal->notes()->first();
2018-04-02 07:50:17 -05:00
if (null === $note) {
2018-02-23 08:12:47 -06:00
$note = new Note;
$note->noteable()->associate($journal);
}
$note->text = $notes;
$note->save();
return;
}
$note = $journal->notes()->first();
2018-04-02 07:50:17 -05:00
if (null !== $note) {
2018-02-23 08:12:47 -06:00
$note->delete();
}
}
2018-03-05 12:35:58 -06:00
}