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;
|
2018-06-02 11:19:35 -05:00
|
|
|
use Log;
|
2018-02-23 08:12:47 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait JournalServiceTrait
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
trait JournalServiceTrait
|
|
|
|
{
|
|
|
|
|
2018-03-04 02:12:33 -06:00
|
|
|
/**
|
2018-07-27 23:27:30 -05:00
|
|
|
* Link tags to journal.
|
|
|
|
*
|
2018-03-04 02:12:33 -06:00
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $data
|
2018-07-27 23:27:30 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2018-03-04 02:12:33 -06:00
|
|
|
*/
|
|
|
|
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'])) {
|
2018-03-04 02:12:33 -06:00
|
|
|
return; // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
foreach ($data['tags'] as $string) {
|
2018-04-07 15:23:16 -05:00
|
|
|
if (\strlen($string) > 0) {
|
2018-07-25 23:27:52 -05:00
|
|
|
$tag = $factory->findOrCreate($string);
|
|
|
|
if (null !== $tag) {
|
|
|
|
$set[] = $tag->id;
|
|
|
|
}
|
2018-03-04 02:12:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$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-07-29 09:04:22 -05:00
|
|
|
$bill = $factory->find((int)$data['bill_id'], $data['bill_name']);
|
2018-02-23 08:12:47 -06:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
$set = [
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => $field,
|
2018-08-03 09:55:10 -05:00
|
|
|
'data' => (string)($data[$field] ?? ''),
|
2018-02-23 08:12:47 -06:00
|
|
|
];
|
2018-06-02 11:19:35 -05:00
|
|
|
|
2018-08-03 09:55:10 -05:00
|
|
|
Log::debug(sprintf('Going to store meta-field "%s", with value "%s".', $set['name'], $set['data']));
|
2018-06-02 11:19:35 -05:00
|
|
|
|
2018-02-23 08:12:47 -06:00
|
|
|
/** @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
|
|
|
}
|