firefly-iii/app/Repositories/Journal/JournalRepository.php

485 lines
16 KiB
PHP
Raw Normal View History

2015-02-09 00:23:39 -06:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-02-09 00:23:39 -06:00
namespace FireflyIII\Repositories\Journal;
use Carbon\Carbon;
2015-04-28 03:36:13 -05:00
use DB;
2016-02-17 08:52:46 -06:00
use FireflyIII\Exceptions\FireflyException;
2015-02-24 15:53:38 -06:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
2015-04-28 03:36:13 -05:00
use FireflyIII\Models\Tag;
2015-02-24 15:53:38 -06:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Pagination\LengthAwarePaginator;
2015-02-25 14:19:06 -06:00
use Illuminate\Support\Collection;
2015-04-03 15:54:21 -05:00
use Log;
2015-02-24 15:53:38 -06:00
2015-02-11 00:35:10 -06:00
/**
* Class JournalRepository
*
* @package FireflyIII\Repositories\Journal
*/
class JournalRepository implements JournalRepositoryInterface
{
/** @var User */
private $user;
/**
2016-04-26 02:21:57 -05:00
* JournalRepository constructor.
*
* @param User $user
*/
public function __construct(User $user)
{
$this->user = $user;
}
2015-02-09 00:23:39 -06:00
/**
* @param TransactionJournal $journal
*
* @return bool
*/
2016-04-06 02:27:45 -05:00
public function delete(TransactionJournal $journal): bool
{
$journal->delete();
return true;
}
2016-04-23 02:33:54 -05:00
/**
* @param int $journalId
*
* @return TransactionJournal
*/
public function find(int $journalId) : TransactionJournal
{
$journal = $this->user->transactionjournals()->where('id', $journalId)->first();
if (is_null($journal)) {
return new TransactionJournal;
}
return $journal;
}
2015-04-03 15:54:21 -05:00
/**
* Get users first transaction journal
*
* @return TransactionJournal
*/
2016-04-06 02:27:45 -05:00
public function first(): TransactionJournal
2015-04-03 15:54:21 -05:00
{
$entry = $this->user->transactionjournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
2016-04-10 14:31:00 -05:00
if (is_null($entry)) {
return new TransactionJournal;
}
2015-12-25 10:11:55 -06:00
return $entry;
2015-04-03 15:54:21 -05:00
}
/**
* @param TransactionJournal $journal
* @param Transaction $transaction
*
2016-04-06 02:27:45 -05:00
* @return string
*/
2016-04-06 02:27:45 -05:00
public function getAmountBefore(TransactionJournal $journal, Transaction $transaction): string
{
$set = $transaction->account->transactions()->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)
->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d'))
->where('transaction_journals.order', '>=', $journal->order)
->where('transaction_journals.id', '!=', $journal->id)
2015-05-24 04:41:52 -05:00
->get(['transactions.*']);
$sum = '0';
2015-05-24 04:41:52 -05:00
foreach ($set as $entry) {
$sum = bcadd($entry->amount, $sum);
}
2015-05-24 04:41:52 -05:00
return $sum;
}
2016-02-17 13:31:18 -06:00
/**
* @param array $types
* @param int $offset
* @param int $count
*
* @return Collection
*/
2016-04-06 02:27:45 -05:00
public function getCollectionOfTypes(array $types, int $offset, int $count): Collection
2016-02-17 13:31:18 -06:00
{
2016-03-11 23:58:39 -06:00
$set = $this->user->transactionJournals()
->expanded()
->transactionTypes($types)
->take($count)->offset($offset)
->orderBy('date', 'DESC')
->orderBy('order', 'ASC')
->orderBy('id', 'DESC')
2016-03-11 23:58:39 -06:00
->get(TransactionJournal::QUERYFIELDS);
2016-02-17 13:31:18 -06:00
return $set;
}
2015-04-11 12:59:41 -05:00
/**
* @param TransactionType $dbType
*
* @return Collection
*/
2016-04-06 02:27:45 -05:00
public function getJournalsOfType(TransactionType $dbType): Collection
2015-04-11 12:59:41 -05:00
{
return $this->user->transactionjournals()->where('transaction_type_id', $dbType->id)->orderBy('id', 'DESC')->take(50)->get();
2015-04-11 12:59:41 -05:00
}
/**
* @param array $types
* @param int $page
2016-04-21 03:34:16 -05:00
* @param int $pageSize
2016-02-17 14:14:32 -06:00
*
* @return LengthAwarePaginator
*/
2016-04-21 03:34:16 -05:00
public function getJournalsOfTypes(array $types, int $page, int $pageSize = 50): LengthAwarePaginator
{
2016-04-21 03:34:16 -05:00
$offset = ($page - 1) * $pageSize;
2016-04-23 02:33:54 -05:00
$query = $this->user
->transactionJournals()
->expanded()
2016-04-21 03:34:16 -05:00
->transactionTypes($types);
$count = $query->count();
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::QUERYFIELDS);
$journals = new LengthAwarePaginator($set, $count, $pageSize, $page);
return $journals;
}
2015-04-11 12:59:41 -05:00
/**
2016-02-05 02:25:15 -06:00
* @param string $type
2015-04-11 12:59:41 -05:00
*
* @return TransactionType
*/
2016-04-06 02:27:45 -05:00
public function getTransactionType(string $type): TransactionType
2015-04-11 12:59:41 -05:00
{
return TransactionType::whereType($type)->first();
}
/**
2015-07-06 15:23:34 -05:00
* @param int $journalId
* @param Carbon $date
*
* @return TransactionJournal
*/
2016-04-06 02:27:45 -05:00
public function getWithDate(int $journalId, Carbon $date): TransactionJournal
{
return $this->user->transactionjournals()->where('id', $journalId)->where('date', $date->format('Y-m-d 00:00:00'))->first();
}
2015-04-28 03:36:13 -05:00
/**
2015-04-28 06:47:13 -05:00
*
* * Remember: a balancingAct takes at most one expense and one transfer.
* an advancePayment takes at most one expense, infinite deposits and NO transfers.
*
2015-04-28 03:36:13 -05:00
* @param TransactionJournal $journal
* @param array $array
*
2016-04-06 02:27:45 -05:00
* @return bool
2015-04-28 03:36:13 -05:00
*/
2016-04-06 02:27:45 -05:00
public function saveTags(TransactionJournal $journal, array $array): bool
2015-04-28 03:36:13 -05:00
{
2015-04-28 10:10:52 -05:00
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
2015-07-07 12:09:45 -05:00
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
2015-04-28 10:10:52 -05:00
2015-04-28 03:36:13 -05:00
foreach ($array as $name) {
2015-04-28 14:01:54 -05:00
if (strlen(trim($name)) > 0) {
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
if (!is_null($tag)) {
$tagRepository->connect($journal, $tag);
}
2015-04-28 14:01:54 -05:00
}
2015-04-28 03:36:13 -05:00
}
2016-04-06 02:27:45 -05:00
return true;
2015-04-28 03:36:13 -05:00
}
2015-02-24 15:53:38 -06:00
/**
* @param array $data
*
* @return TransactionJournal
*/
2016-04-06 02:27:45 -05:00
public function store(array $data): TransactionJournal
2015-02-24 15:53:38 -06:00
{
// find transaction type.
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
// store actual journal.
$journal = new TransactionJournal(
[
'user_id' => $data['user'],
'transaction_type_id' => $transactionType->id,
'transaction_currency_id' => $data['amount_currency_id_amount'],
'description' => $data['description'],
'completed' => 0,
'date' => $data['date'],
'interest_date' => $data['interest_date'],
'book_date' => $data['book_date'],
'process_date' => $data['process_date'],
2015-02-24 15:53:38 -06:00
]
);
$journal->save();
// store or get category
if (strlen($data['category']) > 0) {
$category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]);
2015-02-24 15:53:38 -06:00
$journal->categories()->save($category);
}
// store or get budget
if (intval($data['budget_id']) > 0) {
2015-05-05 03:23:01 -05:00
/** @var \FireflyIII\Models\Budget $budget */
2015-02-24 15:53:38 -06:00
$budget = Budget::find($data['budget_id']);
$journal->budgets()->save($budget);
}
// store accounts (depends on type)
2016-04-29 10:29:13 -05:00
list($sourceAccount, $destinationAccount) = $this->storeAccounts($transactionType, $data);
2015-02-24 15:53:38 -06:00
// store accompanying transactions.
Transaction::create( // first transaction.
[
2016-04-29 10:29:13 -05:00
'account_id' => $sourceAccount->id,
2015-02-24 15:53:38 -06:00
'transaction_journal_id' => $journal->id,
2016-01-15 16:12:52 -06:00
'amount' => $data['amount'] * -1,
2015-02-24 15:53:38 -06:00
]
);
2015-04-03 15:54:21 -05:00
Transaction::create( // second transaction.
2015-02-24 15:53:38 -06:00
[
2016-04-29 10:29:13 -05:00
'account_id' => $destinationAccount->id,
2015-02-24 15:53:38 -06:00
'transaction_journal_id' => $journal->id,
2016-01-15 16:12:52 -06:00
'amount' => $data['amount'],
2015-02-24 15:53:38 -06:00
]
);
2015-02-27 07:27:04 -06:00
$journal->completed = 1;
$journal->save();
2015-02-24 15:53:38 -06:00
2015-04-28 07:08:58 -05:00
// store tags
if (isset($data['tags']) && is_array($data['tags'])) {
$this->saveTags($journal, $data['tags']);
}
2015-02-24 15:53:38 -06:00
return $journal;
}
2015-02-27 07:27:04 -06:00
/**
* @param TransactionJournal $journal
* @param array $data
*
* @return TransactionJournal
2015-02-27 07:27:04 -06:00
*/
2016-04-06 02:27:45 -05:00
public function update(TransactionJournal $journal, array $data): TransactionJournal
2015-02-27 07:27:04 -06:00
{
// update actual journal.
$journal->transaction_currency_id = $data['amount_currency_id_amount'];
2015-02-27 07:27:04 -06:00
$journal->description = $data['description'];
$journal->date = $data['date'];
$journal->interest_date = $data['interest_date'];
$journal->book_date = $data['book_date'];
$journal->process_date = $data['process_date'];
2015-02-27 07:27:04 -06:00
// unlink all categories, recreate them:
$journal->categories()->detach();
if (strlen($data['category']) > 0) {
$category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]);
2015-02-27 07:27:04 -06:00
$journal->categories()->save($category);
}
// unlink all budgets and recreate them:
$journal->budgets()->detach();
if (intval($data['budget_id']) > 0) {
2015-05-05 03:23:01 -05:00
/** @var \FireflyIII\Models\Budget $budget */
2015-02-27 07:27:04 -06:00
$budget = Budget::find($data['budget_id']);
$journal->budgets()->save($budget);
}
// store accounts (depends on type)
2015-07-06 11:57:15 -05:00
list($fromAccount, $toAccount) = $this->storeAccounts($journal->transactionType, $data);
2015-03-29 05:32:00 -05:00
// update the from and to transaction.
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
if ($transaction->amount < 0) {
2015-03-29 05:32:00 -05:00
// this is the from transaction, negative amount:
$transaction->amount = $data['amount'] * -1;
2015-07-06 11:57:15 -05:00
$transaction->account_id = $fromAccount->id;
2015-03-29 05:32:00 -05:00
$transaction->save();
}
if ($transaction->amount > 0) {
2015-03-29 05:32:00 -05:00
$transaction->amount = $data['amount'];
2015-07-06 11:57:15 -05:00
$transaction->account_id = $toAccount->id;
2015-03-29 05:32:00 -05:00
$transaction->save();
}
}
$journal->save();
2015-04-28 03:36:13 -05:00
// update tags:
if (isset($data['tags']) && is_array($data['tags'])) {
$this->updateTags($journal, $data['tags']);
}
2015-03-29 05:32:00 -05:00
return $journal;
}
2015-04-28 03:36:13 -05:00
/**
* @param TransactionJournal $journal
* @param array $array
*
2016-04-06 02:27:45 -05:00
* @return bool
2015-04-28 03:36:13 -05:00
*/
2016-04-06 02:27:45 -05:00
public function updateTags(TransactionJournal $journal, array $array): bool
2015-04-28 03:36:13 -05:00
{
2015-04-28 10:10:52 -05:00
// create tag repository
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
2015-07-07 12:09:45 -05:00
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
2015-04-28 10:10:52 -05:00
2015-04-28 03:36:13 -05:00
// find or create all tags:
$tags = [];
$ids = [];
foreach ($array as $name) {
2015-04-28 14:01:54 -05:00
if (strlen(trim($name)) > 0) {
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
$tags[] = $tag;
$ids[] = $tag->id;
}
2015-04-28 03:36:13 -05:00
}
// delete all tags connected to journal not in this array:
if (count($ids) > 0) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
}
2015-05-02 15:30:59 -05:00
// if count is zero, delete them all:
2015-05-03 05:58:55 -05:00
if (count($ids) == 0) {
2015-05-02 15:30:59 -05:00
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
}
2015-04-28 03:36:13 -05:00
// connect each tag to journal (if not yet connected):
2015-04-28 06:47:13 -05:00
/** @var Tag $tag */
foreach ($tags as $tag) {
2015-04-28 10:10:52 -05:00
$tagRepository->connect($journal, $tag);
2015-04-28 03:36:13 -05:00
}
2016-04-06 02:27:45 -05:00
return true;
2015-04-28 03:36:13 -05:00
}
2015-03-29 05:32:00 -05:00
/**
* @param TransactionType $type
* @param array $data
*
* @return array
2016-02-17 14:14:32 -06:00
* @throws FireflyException
2015-03-29 05:32:00 -05:00
*/
2016-04-06 02:27:45 -05:00
protected function storeAccounts(TransactionType $type, array $data): array
2015-03-29 05:32:00 -05:00
{
2016-04-29 10:29:13 -05:00
$sourceAccount = null;
$destinationAccount = null;
2015-03-29 05:32:00 -05:00
switch ($type->type) {
case TransactionType::WITHDRAWAL:
2016-04-29 10:29:13 -05:00
list($sourceAccount, $destinationAccount) = $this->storeWithdrawalAccounts($data);
2015-02-27 07:27:04 -06:00
break;
case TransactionType::DEPOSIT:
2016-04-29 10:29:13 -05:00
list($sourceAccount, $destinationAccount) = $this->storeDepositAccounts($data);
2015-02-27 07:27:04 -06:00
break;
case TransactionType::TRANSFER:
$sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_from_id'])->first();
$destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_to_id'])->first();
2015-02-27 07:27:04 -06:00
break;
2016-04-24 00:18:39 -05:00
default:
throw new FireflyException('Did not recognise transaction type.');
2015-02-27 07:27:04 -06:00
}
2015-05-17 05:49:09 -05:00
2016-04-29 10:29:13 -05:00
if (is_null($destinationAccount)) {
2016-04-24 00:18:39 -05:00
Log::error('"to"-account is null, so we cannot continue!', ['data' => $data]);
2016-02-17 08:52:46 -06:00
throw new FireflyException('"to"-account is null, so we cannot continue!');
2015-04-03 15:54:21 -05:00
}
2015-05-09 11:30:58 -05:00
2016-04-29 10:29:13 -05:00
if (is_null($sourceAccount)) {
2016-04-24 00:18:39 -05:00
Log::error('"from"-account is null, so we cannot continue!', ['data' => $data]);
2016-02-17 08:52:46 -06:00
throw new FireflyException('"from"-account is null, so we cannot continue!');
2015-07-07 12:09:45 -05:00
2015-04-03 15:54:21 -05:00
}
2015-05-14 02:51:54 -05:00
2015-02-27 07:27:04 -06:00
2016-04-29 10:29:13 -05:00
return [$sourceAccount, $destinationAccount];
2015-05-17 05:49:09 -05:00
}
/**
* @param array $data
*
* @return array
*/
2016-04-06 02:27:45 -05:00
protected function storeDepositAccounts(array $data): array
2015-05-17 05:49:09 -05:00
{
2016-04-29 10:29:13 -05:00
$destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_destination_id'])->first(['accounts.*']);
2015-05-17 05:49:09 -05:00
2016-04-29 10:29:13 -05:00
if (strlen($data['source_account_name']) > 0) {
2016-01-20 08:23:36 -06:00
$fromType = AccountType::where('type', 'Revenue account')->first();
$fromAccount = Account::firstOrCreateEncrypted(
2016-04-29 10:29:13 -05:00
['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => $data['source_account_name'], 'active' => 1]
2015-05-17 05:49:09 -05:00
);
2016-04-29 10:29:13 -05:00
return [$fromAccount, $destinationAccount];
2015-05-17 05:49:09 -05:00
} else {
$fromType = AccountType::where('type', 'Cash account')->first();
2016-01-20 08:23:36 -06:00
$fromAccount = Account::firstOrCreateEncrypted(
2016-04-29 10:29:13 -05:00
['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => 'Cash account', 'active' => 1]
2015-05-17 05:49:09 -05:00
);
}
2016-04-29 10:29:13 -05:00
return [$fromAccount, $destinationAccount];
2015-05-17 05:49:09 -05:00
}
/**
* @param array $data
*
* @return array
*/
2016-04-06 02:27:45 -05:00
protected function storeWithdrawalAccounts(array $data): array
2015-05-17 05:49:09 -05:00
{
2016-04-29 10:29:13 -05:00
$sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first(['accounts.*']);
if (strlen($data['destination_account_name']) > 0) {
$destinationType = AccountType::where('type', 'Expense account')->first();
$destinationAccount = Account::firstOrCreateEncrypted(
[
'user_id' => $data['user'],
'account_type_id' => $destinationType->id,
'name' => $data['destination_account_name'],
'active' => 1,
]
2015-05-17 05:49:09 -05:00
);
2016-04-29 10:29:13 -05:00
return [$sourceAccount, $destinationAccount];
2015-05-17 05:49:09 -05:00
}
2016-04-29 10:29:13 -05:00
$destinationType = AccountType::where('type', 'Cash account')->first();
$destinationAccount = Account::firstOrCreateEncrypted(
['user_id' => $data['user'], 'account_type_id' => $destinationType->id, 'name' => 'Cash account', 'active' => 1]
);
return [$sourceAccount, $destinationAccount];
2015-05-17 05:49:09 -05:00
2015-02-27 07:27:04 -06:00
}
}