Files
firefly-iii/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php

616 lines
19 KiB
PHP
Raw Normal View History

<?php
2014-12-13 22:11:51 +01:00
namespace FireflyIII\Database\TransactionJournal;
use Carbon\Carbon;
2015-01-02 06:05:40 +01:00
use FireflyIII\Database\CommonDatabaseCallsInterface;
use FireflyIII\Database\CUDInterface;
2014-12-13 22:11:51 +01:00
use FireflyIII\Database\SwitchUser;
2014-12-06 12:12:55 +01:00
use FireflyIII\Exception\FireflyException;
2014-11-12 22:21:48 +01:00
use FireflyIII\Exception\NotImplementedException;
2014-12-20 15:00:53 +01:00
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
2014-11-02 18:46:01 +01:00
/**
* Class TransactionJournal
*
* @package FireflyIII\Database
*/
2015-01-02 06:05:40 +01:00
class TransactionJournal implements TransactionJournalInterface, CUDInterface, CommonDatabaseCallsInterface
{
use SwitchUser;
/**
*
*/
public function __construct()
{
$this->setUser(\Auth::user());
}
2014-11-02 18:46:01 +01:00
/**
2014-12-20 15:00:53 +01:00
* @param Eloquent $model
2014-11-02 18:46:01 +01:00
*
2014-11-12 22:37:09 +01:00
* @return bool
2014-11-02 18:46:01 +01:00
*/
2014-12-20 15:00:53 +01:00
public function destroy(Eloquent $model)
2014-11-02 18:46:01 +01:00
{
/*
* Trigger deletion.
*/
\Event::fire('transactionJournal.destroy', [$model]); // new and used.
/*
* Since this event will also destroy both transactions, trigger on those as
* well because we might want to update some caches and what-not.
*/
/** @var Transaction $transaction */
foreach ($model->transactions as $transaction) {
\Event::fire('transaction.destroy', [$transaction]);
}
2014-11-14 10:39:34 +01:00
$model->delete();
2014-11-17 07:33:18 +01:00
2014-11-14 10:39:34 +01:00
return true;
2014-11-02 18:46:01 +01:00
}
/**
2014-11-12 22:37:09 +01:00
* @param array $data
2014-11-02 18:46:01 +01:00
*
2014-12-06 12:12:55 +01:00
* @return \Eloquent
2014-12-13 21:59:02 +01:00
* @throws FireflyException
2014-11-02 18:46:01 +01:00
*/
2014-11-12 22:37:09 +01:00
public function store(array $data)
2014-11-08 10:16:12 +01:00
{
2014-12-31 16:45:12 +01:00
$currency = $this->getJournalCurrency($data['currency']);
$journal = new \TransactionJournal(
[
'transaction_type_id' => $data['transaction_type_id'],
'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id,
'description' => $data['description'], 'date' => $data['date'], 'completed' => 0]
2014-12-14 20:40:02 +01:00
);
2014-11-12 22:37:09 +01:00
$journal->save();
2014-12-14 20:40:02 +01:00
list($fromAccount, $toAccount) = $this->storeAccounts($data);
2014-11-12 22:37:09 +01:00
2015-01-13 20:43:54 +01:00
$this->storeTransaction(
['account_id' => $fromAccount->id, 'account' => $fromAccount, 'transaction_journal' => $journal, 'transaction_journal_id' => $journal->id,
'amount' => floatval($data['amount'] * -1)]
);
$this->storeTransaction(
['account_id' => $toAccount->id, 'account' => $toAccount, 'transaction_journal' => $journal, 'transaction_journal_id' => $journal->id,
'amount' => floatval($data['amount'])]
);
2014-12-14 20:40:02 +01:00
$this->storeBudget($data, $journal);
$this->storeCategory($data, $journal);
2014-11-13 17:01:09 +01:00
2014-11-12 22:37:09 +01:00
$journal->completed = 1;
$journal->save();
return $journal;
}
2014-11-02 18:46:01 +01:00
/**
2014-12-20 15:00:53 +01:00
* @param Eloquent $model
* @param array $data
2014-11-12 22:37:09 +01:00
*
* @return bool
2014-12-13 21:59:02 +01:00
* @throws FireflyException
2014-11-02 18:46:01 +01:00
*/
2014-12-20 15:00:53 +01:00
public function update(Eloquent $model, array $data)
2014-11-02 18:46:01 +01:00
{
$journalType = $this->getJournalType($data['what']);
$currency = $this->getJournalCurrency($data['currency']);
$model->description = $data['description'];
$model->date = $data['date'];
2014-11-13 17:01:09 +01:00
$model->transactionType()->associate($journalType);
$model->transactionCurrency()->associate($currency);
$model->user()->associate($this->getUser());
$model->save();
list($fromAccount, $toAccount) = $this->storeAccounts($data);
2014-12-17 21:32:27 +01:00
2015-01-01 22:32:25 +01:00
/** @noinspection PhpParamsInspection */
$this->storeBudget($data, $model);
$this->storeCategory($data, $model);
2014-11-14 19:33:50 +01:00
2014-11-13 17:01:09 +01:00
$amount = floatval($data['amount']);
/** @var \Transaction $transaction */
foreach ($model->transactions()->get() as $transaction) {
if (floatval($transaction->amount) > 0) {
// the TO transaction.
$transaction->account()->associate($toAccount);
2014-11-13 17:01:09 +01:00
$transaction->amount = $amount;
} else {
$transaction->account()->associate($fromAccount);
2014-11-13 17:01:09 +01:00
$transaction->amount = $amount * -1;
}
2014-12-06 12:12:55 +01:00
if (!$transaction->isValid()) {
2014-11-13 17:01:09 +01:00
throw new FireflyException('Could not validate transaction while saving.');
}
$transaction->save();
}
return true;
2014-11-02 18:46:01 +01:00
}
/**
* Validates an array. Returns an array containing MessageBags
* errors/warnings/successes.
*
* ignored because this method will be gone soon.
*
* @param array $model
*
* @return array
2014-12-13 21:59:02 +01:00
* @throws FireflyException
*/
public function validate(array $model)
{
2014-11-12 22:37:09 +01:00
$warnings = new MessageBag;
$successes = new MessageBag;
2014-12-31 16:45:12 +01:00
$journal = new \TransactionJournal($model);
$journal->isValid();
$errors = $journal->getErrors();
/*
* Is not in rules.
*/
if (!isset($model['what'])) {
$errors->add('description', 'Internal error: need to know type of transaction!');
}
/*
* Is not in rules.
*/
$errors = $errors->merge($this->_validateAmount($model));
$errors = $errors->merge($this->_validateBudget($model));
$errors = $errors->merge($this->_validateAccount($model));
/*
* Add "OK"
*/
/**
* else {
* $successes->add('account_from_id', 'OK');
* $successes->add('account_to_id', 'OK');
* }
* else {
*/
$list = ['date', 'description', 'amount', 'budget_id', 'from', 'to', 'account_from_id', 'account_to_id', 'category', 'account_id', 'expense_account',
'revenue_account'];
foreach ($list as $entry) {
if (!$errors->has($entry)) {
$successes->add($entry, 'OK');
}
}
2014-11-12 22:37:09 +01:00
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
2014-12-14 20:40:02 +01:00
/**
* @param $currency
*
* @return null|\TransactionCurrency
*/
public function getJournalCurrency($currency)
{
/** @var \FireflyIII\Database\TransactionCurrency\TransactionCurrency $currencyRepository */
$currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency\TransactionCurrency');
return $currencyRepository->findByCode($currency);
}
/**
2015-01-17 10:06:12 +01:00
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
2014-12-14 20:40:02 +01:00
* @param array $data
*
* @return array
* @throws FireflyException
*/
public function storeAccounts(array $data)
{
/** @var \FireflyIII\Database\Account\Account $accountRepository */
$accountRepository = \App::make('FireflyIII\Database\Account\Account');
$fromAccount = null;
$toAccount = null;
switch ($data['what']) {
case 'withdrawal':
$fromAccount = $accountRepository->find($data['account_id']);
$toAccount = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
break;
case 'opening':
2015-01-13 20:43:54 +01:00
$fromAccount = $data['from'];
$toAccount = $data['to'];
2014-12-14 20:40:02 +01:00
break;
case 'deposit':
$fromAccount = $accountRepository->firstRevenueAccountOrCreate($data['revenue_account']);
$toAccount = $accountRepository->find($data['account_id']);
break;
case 'transfer':
$fromAccount = $accountRepository->find($data['account_from_id']);
$toAccount = $accountRepository->find($data['account_to_id']);
break;
default:
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
break;
}
return [$fromAccount, $toAccount];
}
/**
* @param array $data
*
* @return \Eloquent
* @throws FireflyException
*/
public function storeTransaction($data)
{
/** @var \FireflyIII\Database\Transaction\Transaction $repository */
$repository = \App::make('FireflyIII\Database\Transaction\Transaction');
$errors = $repository->validate($data);
if ($errors->count() > 0) {
\Log::error('Could not store transaction: ' . $errors->toJson());
throw new FireflyException('store() transaction failed, but it should not!');
}
return $repository->store($data);
}
/**
* @param array $data
* @param \TransactionJournal|\Eloquent $journal
2014-12-14 20:40:02 +01:00
*/
public function storeBudget($data, \TransactionJournal $journal)
{
if (isset($data['budget_id']) && intval($data['budget_id']) > 0) {
/** @var \FireflyIII\Database\Budget\Budget $budgetRepository */
$budgetRepository = \App::make('FireflyIII\Database\Budget\Budget');
$budget = $budgetRepository->find(intval($data['budget_id']));
if ($budget) {
$journal->budgets()->sync([$budget->id]);
2014-12-14 20:40:02 +01:00
}
}
}
/**
* @param array $data
* @param \TransactionJournal|\Eloquent $journal
2014-12-14 20:40:02 +01:00
*/
public function storeCategory(array $data, \TransactionJournal $journal)
{
if (isset($data['category']) && strlen($data['category']) > 0) {
/** @var \FireflyIII\Database\Category\Category $categoryRepository */
$categoryRepository = \App::make('FireflyIII\Database\Category\Category');
$category = $categoryRepository->firstOrCreate($data['category']);
if ($category) {
$journal->categories()->sync([$category->id]);
2014-12-14 20:40:02 +01:00
}
2014-12-27 17:21:15 +01:00
return;
2014-12-14 20:40:02 +01:00
}
2014-12-27 17:21:15 +01:00
$journal->categories()->sync([]);
return;
2014-12-14 20:40:02 +01:00
}
2014-12-31 16:45:12 +01:00
/**
* @param $type
*
* @return \TransactionType|null
* @throws FireflyException
*/
public function getJournalType($type)
{
/** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */
$typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType');
return $typeRepository->findByWhat($type);
}
/**
2015-01-17 10:06:12 +01:00
* @SuppressWarnings("CamelCase") // I'm fine with this.
*
* @param array $model
*
* @return MessageBag
*/
protected function _validateAmount(array $model)
{
$errors = new MessageBag;
if (isset($model['amount']) && floatval($model['amount']) < 0.01) {
$errors->add('amount', 'Amount must be > 0.01');
} else {
if (!isset($model['amount'])) {
$errors->add('amount', 'Amount must be set!');
}
}
return $errors;
}
/**
2015-01-17 10:06:12 +01:00
* @SuppressWarnings("CamelCase") // I'm fine with this.
*
* @param array $model
*
* @return MessageBag
*/
protected function _validateBudget(array $model)
{
/*
* Budget (is not in rules)
*/
$errors = new MessageBag;
if (isset($model['budget_id']) && !ctype_digit($model['budget_id'])) {
$errors->add('budget_id', 'Invalid budget');
}
return $errors;
}
/**
2015-01-17 10:06:12 +01:00
* @SuppressWarnings("CamelCase") // I'm fine with this.
*
* @param array $model
*
* @return MessageBag
* @throws FireflyException
*/
protected function _validateAccount(array $model)
{
$errors = new MessageBag;
switch (true) {
// this combination is often seen in withdrawals.
case (isset($model['account_id']) && isset($model['expense_account'])):
if (intval($model['account_id']) < 1) {
$errors->add('account_id', 'Invalid account.');
}
break;
case (isset($model['account_id']) && isset($model['revenue_account'])):
if (intval($model['account_id']) < 1) {
$errors->add('account_id', 'Invalid account.');
}
break;
case (isset($model['account_from_id']) && isset($model['account_to_id'])):
if (intval($model['account_from_id']) < 1 || intval($model['account_from_id']) < 1) {
$errors->add('account_from_id', 'Invalid account selected.');
$errors->add('account_to_id', 'Invalid account selected.');
} else {
if (intval($model['account_from_id']) == intval($model['account_to_id'])) {
$errors->add('account_to_id', 'Cannot be the same as "from" account.');
$errors->add('account_from_id', 'Cannot be the same as "to" account.');
}
}
break;
default:
throw new FireflyException('Cannot validate accounts for transaction journal.');
break;
}
return $errors;
}
/**
* Returns an object with id $id.
*
2014-12-19 21:18:42 +01:00
* @param int $objectId
*
2014-12-06 12:12:55 +01:00
* @return \Eloquent
*/
2014-12-19 21:18:42 +01:00
public function find($objectId)
{
2014-12-19 21:18:42 +01:00
return $this->getUser()->transactionjournals()->find($objectId);
}
2014-11-12 22:37:09 +01:00
/**
2015-01-17 10:06:12 +01:00
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
2014-11-12 22:37:09 +01:00
* Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
* @param $what
*
* @return \AccountType|null
2014-12-13 21:59:02 +01:00
* @throws NotImplementedException
2014-11-12 22:37:09 +01:00
*/
public function findByWhat($what)
{
throw new NotImplementedException;
}
/**
* Returns all objects.
*
* @return Collection
*/
public function get()
{
2014-12-04 20:38:45 +01:00
return $this->getUser()->transactionjournals()->with(['TransactionType', 'transactions', 'transactions.account', 'transactions.account.accountType'])
->get();
}
/**
2014-11-12 22:37:09 +01:00
* @param array $ids
*
* @return Collection
*/
2014-11-12 22:37:09 +01:00
public function getByIds(array $ids)
{
2014-11-13 16:13:32 +01:00
return $this->getUser()->transactionjournals()->with('transactions')->whereIn('id', $ids)->orderBy('date', 'ASC')->get();
}
2014-12-07 15:37:53 +01:00
/**
* @return Carbon
*/
public function firstDate()
{
$journal = $this->first();
if ($journal) {
return $journal->date;
}
return Carbon::now();
}
/**
2014-11-12 22:37:09 +01:00
* @return TransactionJournal
*/
2014-11-12 22:37:09 +01:00
public function first()
{
2014-11-12 22:37:09 +01:00
return $this->getUser()->transactionjournals()->orderBy('date', 'ASC')->first();
}
/**
2014-11-12 22:37:09 +01:00
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
2014-11-12 22:37:09 +01:00
public function getInDateRange(Carbon $start, Carbon $end)
{
2014-11-12 22:37:09 +01:00
return $this->getuser()->transactionjournals()->withRelevantData()->before($end)->after($start)->get();
}
/**
2014-11-12 22:37:09 +01:00
* @param Carbon $date
*
2014-11-12 22:37:09 +01:00
* @return float
*/
2014-11-12 22:37:09 +01:00
public function getSumOfExpensesByMonth(Carbon $date)
{
/** @var \FireflyIII\Report\ReportInterface $reportRepository */
$reportRepository = \App::make('FireflyIII\Report\ReportInterface');
$set = $reportRepository->getExpenseGroupedForMonth($date, 200);
$sum = 0;
foreach ($set as $entry) {
$sum += $entry['amount'];
}
2014-11-12 22:37:09 +01:00
return $sum;
}
2014-10-29 10:30:52 +01:00
/**
2014-11-12 22:37:09 +01:00
* @param Carbon $date
2014-11-12 22:21:48 +01:00
*
2014-11-12 22:37:09 +01:00
* @return float
2014-10-29 10:30:52 +01:00
*/
2014-11-12 22:37:09 +01:00
public function getSumOfIncomesByMonth(Carbon $date)
2014-10-29 10:30:52 +01:00
{
/** @var \FireflyIII\Report\ReportInterface $reportRepository */
$reportRepository = \App::make('FireflyIII\Report\ReportInterface');
2014-11-12 22:37:09 +01:00
$incomes = $reportRepository->getIncomeForMonth($date);
$totalIn = 0;
/** @var \TransactionJournal $entry */
foreach ($incomes as $entry) {
$totalIn += $entry->getAmount();
}
2014-11-12 22:37:09 +01:00
return $totalIn;
2014-10-29 10:30:52 +01:00
}
2014-12-13 21:59:02 +01:00
/**
* @param int $limit
*
* @return \Illuminate\Pagination\Paginator
*/
2014-11-17 07:33:18 +01:00
public function getDepositsPaginated($limit = 50)
{
2014-12-04 20:38:45 +01:00
$offset = intval(\Input::get('page')) > 0 ? (intval(\Input::get('page')) - 1) * $limit : 0;
2014-11-17 07:33:18 +01:00
$set = $this->getUser()->transactionJournals()->transactionTypes(['Deposit'])->withRelevantData()->take($limit)->offset($offset)->orderBy(
'date', 'DESC'
)->get(['transaction_journals.*']);
$count = $this->getUser()->transactionJournals()->transactionTypes(['Deposit'])->count();
$items = [];
foreach ($set as $entry) {
$items[] = $entry;
}
return \Paginator::make($items, $count, $limit);
}
2014-11-12 22:21:48 +01:00
/**
2014-11-12 22:37:09 +01:00
* @param \Account $account
* @param Carbon $start
* @param Carbon $end
* @param int $count
2014-11-12 22:21:48 +01:00
*
* @return Collection
*/
public function getInDateRangeAccount(\Account $account, Carbon $start, Carbon $end, $count = 20)
2014-11-12 22:21:48 +01:00
{
2014-11-12 22:37:09 +01:00
$accountID = $account->id;
$query = $this->_user->transactionjournals()->with(['transactions', 'transactioncurrency', 'transactiontype'])->leftJoin(
2014-11-13 16:13:32 +01:00
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
)->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $accountID)->where(
'date', '>=', $start->format('Y-m-d')
)->where('date', '<=', $end->format('Y-m-d'))->orderBy('transaction_journals.date', 'DESC')->orderBy('transaction_journals.id', 'DESC')->take(
$count
)->get(['transaction_journals.*']);
2014-11-12 22:37:09 +01:00
return $query;
2014-11-12 22:21:48 +01:00
}
2014-12-13 21:59:02 +01:00
/**
* @param int $limit
*
* @return \Illuminate\Pagination\Paginator
*/
2014-11-17 07:33:18 +01:00
public function getTransfersPaginated($limit = 50)
{
2014-12-04 20:38:45 +01:00
$offset = intval(\Input::get('page')) > 0 ? (intval(\Input::get('page')) - 1) * $limit : 0;
2014-11-17 07:33:18 +01:00
$set = $this->getUser()->transactionJournals()->transactionTypes(['Transfer'])->withRelevantData()->take($limit)->offset($offset)->orderBy(
'date', 'DESC'
)->get(['transaction_journals.*']);
$count = $this->getUser()->transactionJournals()->transactionTypes(['Transfer'])->count();
$items = [];
2014-11-14 10:28:18 +01:00
foreach ($set as $entry) {
$items[] = $entry;
}
2014-11-14 10:28:18 +01:00
return \Paginator::make($items, $count, $limit);
}
2014-12-13 21:59:02 +01:00
/**
* @param int $limit
*
* @return \Illuminate\Pagination\Paginator
*/
2014-11-17 07:33:18 +01:00
public function getWithdrawalsPaginated($limit = 50)
{
2014-12-04 20:38:45 +01:00
$offset = intval(\Input::get('page')) > 0 ? (intval(\Input::get('page')) - 1) * $limit : 0;
2014-11-14 10:28:18 +01:00
2014-11-17 07:33:18 +01:00
$set = $this->getUser()->transactionJournals()->transactionTypes(['Withdrawal'])->withRelevantData()->take($limit)->offset($offset)->orderBy(
'date', 'DESC'
)->get(['transaction_journals.*']);
$count = $this->getUser()->transactionJournals()->transactionTypes(['Withdrawal'])->count();
$items = [];
2014-11-14 10:28:18 +01:00
foreach ($set as $entry) {
$items[] = $entry;
}
2014-11-14 10:28:18 +01:00
return \Paginator::make($items, $count, $limit);
}
2015-01-02 06:16:49 +01:00
}