2018-02-21 08:51:30 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JournalUpdateService.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);
|
|
|
|
|
2018-02-21 18:42:15 +01:00
|
|
|
namespace FireflyIII\Services\Internal\Update;
|
2018-02-21 08:51:30 +01:00
|
|
|
|
|
|
|
use FireflyIII\Factory\BillFactory;
|
|
|
|
use FireflyIII\Factory\TagFactory;
|
|
|
|
use FireflyIII\Factory\TransactionFactory;
|
|
|
|
use FireflyIII\Factory\TransactionJournalMetaFactory;
|
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-02-23 15:12:47 +01:00
|
|
|
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
|
2018-02-21 08:51:30 +01:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to centralise code that updates a journal given the input by system.
|
|
|
|
*
|
|
|
|
* Class JournalUpdateService
|
|
|
|
*/
|
|
|
|
class JournalUpdateService
|
|
|
|
{
|
2018-02-23 15:12:47 +01:00
|
|
|
use JournalServiceTrait;
|
2018-02-21 08:51:30 +01:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException
|
2018-02-21 08:58:06 +01:00
|
|
|
* @throws \Exception
|
2018-02-21 08:51:30 +01:00
|
|
|
*/
|
|
|
|
public function update(TransactionJournal $journal, array $data): TransactionJournal
|
|
|
|
{
|
|
|
|
// update journal:
|
|
|
|
$journal->description = $data['description'];
|
|
|
|
$journal->date = $data['date'];
|
|
|
|
$journal->save();
|
|
|
|
|
|
|
|
// update transactions:
|
|
|
|
/** @var TransactionUpdateService $service */
|
|
|
|
$service = app(TransactionUpdateService::class);
|
|
|
|
$service->setUser($this->user);
|
|
|
|
|
|
|
|
// create transactions
|
|
|
|
/** @var TransactionFactory $factory */
|
|
|
|
$factory = app(TransactionFactory::class);
|
|
|
|
$factory->setUser($this->user);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int $identifier
|
|
|
|
* @var array $trData
|
|
|
|
*/
|
|
|
|
foreach ($data['transactions'] as $identifier => $trData) {
|
|
|
|
// exists transaction(s) with this identifier? update!
|
|
|
|
/** @var Collection $existing */
|
|
|
|
$existing = $journal->transactions()->where('identifier', $identifier)->get();
|
|
|
|
if ($existing->count() > 0) {
|
|
|
|
$existing->each(
|
|
|
|
function (Transaction $transaction) use ($service, $trData) {
|
|
|
|
$service->update($transaction, $trData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// otherwise, create!
|
|
|
|
$factory->createPair($journal, $trData);
|
|
|
|
}
|
2018-02-21 08:58:06 +01:00
|
|
|
// could be that journal has more transactions than submitted (remove split)
|
|
|
|
$transactions = $journal->transactions()->where('amount', '>', 0)->get();
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
if (!isset($data['transactions'][$transaction->identifier])) {
|
|
|
|
$journal->transactions()->where('identifier', $transaction->identifier)->delete();
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 08:51:30 +01:00
|
|
|
|
|
|
|
// connect bill:
|
|
|
|
$this->connectBill($journal, $data);
|
|
|
|
|
|
|
|
// connect tags:
|
|
|
|
$this->connectTags($journal, $data);
|
|
|
|
|
|
|
|
// update or create custom fields:
|
|
|
|
// store date meta fields (if present):
|
|
|
|
$this->storeMeta($journal, $data, 'interest_date');
|
|
|
|
$this->storeMeta($journal, $data, 'book_date');
|
|
|
|
$this->storeMeta($journal, $data, 'process_date');
|
|
|
|
$this->storeMeta($journal, $data, 'due_date');
|
|
|
|
$this->storeMeta($journal, $data, 'payment_date');
|
|
|
|
$this->storeMeta($journal, $data, 'invoice_date');
|
|
|
|
$this->storeMeta($journal, $data, 'internal_reference');
|
|
|
|
|
|
|
|
// store note:
|
|
|
|
$this->storeNote($journal, $data['notes']);
|
|
|
|
|
|
|
|
|
|
|
|
return $journal;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|