2018-06-21 18:57:51 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RecurringTransactionTrait.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 Exception;
|
2018-06-30 06:14:39 +02:00
|
|
|
use FireflyIII\Factory\BudgetFactory;
|
|
|
|
use FireflyIII\Factory\CategoryFactory;
|
|
|
|
use FireflyIII\Factory\PiggyBankFactory;
|
|
|
|
use FireflyIII\Factory\TransactionCurrencyFactory;
|
2018-06-21 18:57:51 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\Recurrence;
|
|
|
|
use FireflyIII\Models\RecurrenceMeta;
|
|
|
|
use FireflyIII\Models\RecurrenceRepetition;
|
|
|
|
use FireflyIII\Models\RecurrenceTransaction;
|
|
|
|
use FireflyIII\Models\RecurrenceTransactionMeta;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait RecurringTransactionTrait
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
trait RecurringTransactionTrait
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
* @param array $repetitions
|
|
|
|
*/
|
|
|
|
public function createRepetitions(Recurrence $recurrence, array $repetitions): void
|
|
|
|
{
|
|
|
|
/** @var array $array */
|
|
|
|
foreach ($repetitions as $array) {
|
|
|
|
RecurrenceRepetition::create(
|
|
|
|
[
|
|
|
|
'recurrence_id' => $recurrence->id,
|
|
|
|
'repetition_type' => $array['type'],
|
|
|
|
'repetition_moment' => $array['moment'],
|
|
|
|
'repetition_skip' => $array['skip'],
|
2018-06-27 05:37:56 +02:00
|
|
|
'weekend' => $array['weekend'] ?? 1,
|
2018-06-21 18:57:51 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
* @param array $transactions
|
|
|
|
*/
|
|
|
|
public function createTransactions(Recurrence $recurrence, array $transactions): void
|
|
|
|
{
|
|
|
|
foreach ($transactions as $array) {
|
|
|
|
$source = null;
|
|
|
|
$destination = null;
|
|
|
|
switch ($recurrence->transactionType->type) {
|
|
|
|
case TransactionType::WITHDRAWAL:
|
2018-06-30 06:14:39 +02:00
|
|
|
$source = $this->findAccount(AccountType::ASSET, $array['source_id'], $array['source_name']);
|
|
|
|
$destination = $this->findAccount(AccountType::EXPENSE, $array['destination_id'], $array['destination_name']);
|
2018-06-21 18:57:51 +02:00
|
|
|
break;
|
|
|
|
case TransactionType::DEPOSIT:
|
2018-06-30 06:14:39 +02:00
|
|
|
$source = $this->findAccount(AccountType::REVENUE, $array['source_id'], $array['source_name']);
|
|
|
|
$destination = $this->findAccount(AccountType::ASSET, $array['destination_id'], $array['destination_name']);
|
2018-06-21 18:57:51 +02:00
|
|
|
break;
|
|
|
|
case TransactionType::TRANSFER:
|
2018-06-30 06:14:39 +02:00
|
|
|
$source = $this->findAccount(AccountType::ASSET, $array['source_id'], $array['source_name']);
|
|
|
|
$destination = $this->findAccount(AccountType::ASSET, $array['destination_id'], $array['destination_name']);
|
2018-06-21 18:57:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-30 06:14:39 +02:00
|
|
|
/** @var TransactionCurrencyFactory $factory */
|
|
|
|
$factory = app(TransactionCurrencyFactory::class);
|
|
|
|
$currency = $factory->find($array['currency_id'], $array['currency_code']);
|
|
|
|
$foreignCurrency = $factory->find($array['foreign_currency_id'], $array['foreign_currency_code']);
|
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($recurrence->user);
|
|
|
|
if (null === $currency) {
|
|
|
|
$currency = $defaultCurrency;
|
|
|
|
}
|
2018-06-21 18:57:51 +02:00
|
|
|
$transaction = new RecurrenceTransaction(
|
|
|
|
[
|
|
|
|
'recurrence_id' => $recurrence->id,
|
2018-06-30 06:14:39 +02:00
|
|
|
'transaction_currency_id' => $currency->id,
|
|
|
|
'foreign_currency_id' => null === $foreignCurrency ? null : $foreignCurrency->id,
|
|
|
|
'source_id' => $source->id,
|
|
|
|
'destination_id' => $destination->id,
|
2018-06-21 18:57:51 +02:00
|
|
|
'amount' => $array['amount'],
|
|
|
|
'foreign_amount' => '' === (string)$array['foreign_amount'] ? null : (string)$array['foreign_amount'],
|
|
|
|
'description' => $array['description'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$transaction->save();
|
|
|
|
|
2018-06-30 06:14:39 +02:00
|
|
|
/** @var BudgetFactory $budgetFactory */
|
|
|
|
$budgetFactory = app(BudgetFactory::class);
|
|
|
|
$budgetFactory->setUser($recurrence->user);
|
|
|
|
$budget = $budgetFactory->find($array['budget_id'], $array['budget_name']);
|
|
|
|
|
|
|
|
/** @var CategoryFactory $categoryFactory */
|
|
|
|
$categoryFactory = app(CategoryFactory::class);
|
|
|
|
$categoryFactory->setUser($recurrence->user);
|
|
|
|
$category = $categoryFactory->findOrCreate($array['category_id'], $array['category_name']);
|
|
|
|
|
2018-06-21 18:57:51 +02:00
|
|
|
// create recurrence transaction meta:
|
2018-06-30 06:14:39 +02:00
|
|
|
if (null !== $budget) {
|
2018-06-21 18:57:51 +02:00
|
|
|
RecurrenceTransactionMeta::create(
|
|
|
|
[
|
|
|
|
'rt_id' => $transaction->id,
|
|
|
|
'name' => 'budget_id',
|
2018-06-30 06:14:39 +02:00
|
|
|
'value' => $budget->id,
|
2018-06-21 18:57:51 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2018-06-30 06:14:39 +02:00
|
|
|
if (null !== $category) {
|
2018-06-21 18:57:51 +02:00
|
|
|
RecurrenceTransactionMeta::create(
|
|
|
|
[
|
|
|
|
'rt_id' => $transaction->id,
|
|
|
|
'name' => 'category_name',
|
2018-06-30 06:14:39 +02:00
|
|
|
'value' => $category->name,
|
2018-06-21 18:57:51 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
*/
|
|
|
|
public function deleteRepetitions(Recurrence $recurrence): void
|
|
|
|
{
|
|
|
|
$recurrence->recurrenceRepetitions()->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
*/
|
|
|
|
public function deleteTransactions(Recurrence $recurrence): void
|
|
|
|
{
|
|
|
|
/** @var RecurrenceTransaction $transaction */
|
|
|
|
foreach ($recurrence->recurrenceTransactions as $transaction) {
|
|
|
|
$transaction->recurrenceTransactionMeta()->delete();
|
|
|
|
try {
|
|
|
|
$transaction->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::debug($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function updateMetaData(Recurrence $recurrence, array $data): void
|
|
|
|
{
|
|
|
|
// only two special meta fields right now. Let's just hard code them.
|
2018-06-30 06:14:39 +02:00
|
|
|
$piggyId = (int)($data['meta']['piggy_bank_id'] ?? 0.0);
|
|
|
|
$piggyName = $data['meta']['piggy_bank_name'] ?? '';
|
|
|
|
/** @var PiggyBankFactory $factory */
|
|
|
|
$factory = app(PiggyBankFactory::class);
|
|
|
|
$factory->setUser($recurrence->user);
|
|
|
|
$piggyBank = $factory->find($piggyId, $piggyName);
|
|
|
|
if (null !== $piggyBank) {
|
2018-06-21 18:57:51 +02:00
|
|
|
/** @var RecurrenceMeta $entry */
|
|
|
|
$entry = $recurrence->recurrenceMeta()->where('name', 'piggy_bank_id')->first();
|
|
|
|
if (null === $entry) {
|
2018-06-30 06:14:39 +02:00
|
|
|
$entry = RecurrenceMeta::create(['recurrence_id' => $recurrence->id, 'name' => 'piggy_bank_id', 'value' => $piggyBank->id]);
|
2018-06-21 18:57:51 +02:00
|
|
|
}
|
2018-06-30 06:14:39 +02:00
|
|
|
$entry->value = $piggyBank->id;
|
2018-06-21 18:57:51 +02:00
|
|
|
$entry->save();
|
|
|
|
}
|
2018-06-30 06:14:39 +02:00
|
|
|
if (null === $piggyBank) {
|
2018-06-21 18:57:51 +02:00
|
|
|
// delete if present
|
|
|
|
$recurrence->recurrenceMeta()->where('name', 'piggy_bank_id')->delete();
|
|
|
|
}
|
2018-06-30 06:14:39 +02:00
|
|
|
|
|
|
|
|
2018-06-21 18:57:51 +02:00
|
|
|
$tags = $data['meta']['tags'] ?? [];
|
|
|
|
if (\count($tags) > 0) {
|
|
|
|
/** @var RecurrenceMeta $entry */
|
|
|
|
$entry = $recurrence->recurrenceMeta()->where('name', 'tags')->first();
|
|
|
|
if (null === $entry) {
|
|
|
|
$entry = RecurrenceMeta::create(['recurrence_id' => $recurrence->id, 'name' => 'tags', 'value' => implode(',', $tags)]);
|
|
|
|
}
|
|
|
|
$entry->value = implode(',', $tags);
|
|
|
|
$entry->save();
|
|
|
|
}
|
|
|
|
if (\count($tags) === 0) {
|
|
|
|
// delete if present
|
|
|
|
$recurrence->recurrenceMeta()->where('name', 'tags')->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|