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

188 lines
5.2 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;
2018-09-06 00:38:51 -05:00
use Exception;
2018-02-23 08:12:47 -06:00
use FireflyIII\Factory\BillFactory;
2019-03-05 10:26:49 -06:00
use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory;
2018-02-23 08:12:47 -06:00
use FireflyIII\Factory\TagFactory;
use FireflyIII\Factory\TransactionJournalMetaFactory;
2019-03-07 22:47:51 -06:00
use FireflyIII\Models\Bill;
2019-03-05 10:26:49 -06:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
2018-02-23 08:12:47 -06:00
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-07-27 23:27:30 -05:00
* Link tags to journal.
*
* @param TransactionJournal $journal
* @param array $data
2018-07-27 23:27:30 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
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) {
2019-03-07 22:47:51 -06:00
if ('' !== $string) {
2018-07-25 23:27:52 -05:00
$tag = $factory->findOrCreate($string);
if (null !== $tag) {
$set[] = $tag->id;
}
}
}
$journal->tags()->sync($set);
}
2018-02-23 08:12:47 -06:00
2019-03-05 10:26:49 -06:00
/**
* @param int|null $budgetId
* @param null|string $budgetName
*
* @return Budget|null
*/
protected function findBudget(?int $budgetId, ?string $budgetName): ?Budget
{
/** @var BudgetFactory $factory */
$factory = app(BudgetFactory::class);
$factory->setUser($this->user);
return $factory->find($budgetId, $budgetName);
}
/**
* @param int|null $categoryId
* @param null|string $categoryName
*
* @return Category|null
*/
protected function findCategory(?int $categoryId, ?string $categoryName): ?Category
{
Log::debug(sprintf('Going to find or create category #%d, with name "%s"', $categoryId, $categoryName));
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user);
return $factory->findOrCreate($categoryId, $categoryName);
}
/**
* @param TransactionJournal $journal
* @param Budget|null $budget
*/
protected function setBudget(TransactionJournal $journal, ?Budget $budget): void
{
if (null === $budget) {
$journal->budgets()->sync([]);
return;
}
$journal->budgets()->sync([$budget->id]);
}
/**
* @param TransactionJournal $journal
* @param Category|null $category
*/
protected function setCategory(TransactionJournal $journal, ?Category $category): void
{
if (null === $category) {
$journal->categories()->sync([]);
return;
}
$journal->categories()->sync([$category->id]);
}
2018-02-23 08:12:47 -06:00
/**
* @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;
if ('' !== $notes) {
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-09-06 00:38:51 -05:00
try {
$note->delete();
} catch (Exception $e) {
Log::debug(sprintf('Journal service trait could not delete note: %s', $e->getMessage()));
}
2018-02-23 08:12:47 -06:00
}
}
2018-03-05 12:35:58 -06:00
}