Code clean up and reformat.

This commit is contained in:
James Cole
2014-11-12 22:37:09 +01:00
parent 258d6a1688
commit 4aa9a04516
33 changed files with 1543 additions and 1611 deletions

View File

@@ -31,97 +31,111 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
}
/**
* @param Carbon $date
* @param Ardent $model
*
* @return float
* @return bool
*/
public function getSumOfIncomesByMonth(Carbon $date)
public function destroy(Ardent $model)
{
$end = clone $date;
$date->startOfMonth();
$end->endOfMonth();
$sum = \DB::table('transactions')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')
->where('amount', '>', 0)
->where('transaction_types.type', '=', 'Deposit')
->where('transaction_journals.date', '>=', $date->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount');
$sum = floatval($sum);
return $sum;
// TODO: Implement destroy() method.
throw new NotImplementedException;
}
/**
* @param Carbon $date
* @param array $data
*
* @return float
* @return Ardent
*/
public function getSumOfExpensesByMonth(Carbon $date)
public function store(array $data)
{
$end = clone $date;
$date->startOfMonth();
$end->endOfMonth();
$sum = \DB::table('transactions')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')
->where('amount', '>', 0)
->where('transaction_types.type', '=', 'Withdrawal')
->where('transaction_journals.date', '>=', $date->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount');
$sum = floatval($sum);
return $sum;
/** @var \FireflyIII\Database\TransactionType $typeRepository */
$typeRepository = \App::make('FireflyIII\Database\TransactionType');
/** @var \FireflyIII\Database\Account $accountRepository */
$accountRepository = \App::make('FireflyIII\Database\Account');
/** @var \FireflyIII\Database\TransactionCurrency $currencyRepository */
$currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency');
/** @var \FireflyIII\Database\Transaction $transactionRepository */
$transactionRepository = \App::make('FireflyIII\Database\Transaction');
$journalType = $typeRepository->findByWhat($data['what']);
$currency = $currencyRepository->findByCode($data['currency']);
$journal = new \TransactionJournal;
$journal->transactionType()->associate($journalType);
$journal->transactionCurrency()->associate($currency);
$journal->user()->associate($this->getUser());
$journal->description = $data['description'];
$journal->date = $data['date'];
$journal->completed = 0;
/*
* This must be enough to store the journal:
*/
if (!$journal->validate()) {
\Log::error($journal->errors()->all());
throw new FireflyException('store() transaction journal failed, but it should not!');
}
$journal->save();
/*
* Still need to find the accounts related to the transactions.
* This depends on the type of transaction.
*/
switch ($data['what']) {
case 'withdrawal':
$data['from'] = $accountRepository->find($data['account_id']);
$data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
break;
case 'opening':
break;
default:
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
break;
}
/*
* Then store both transactions.
*/
$first = ['account' => $data['from'], 'transaction_journal' => $journal, 'amount' => ($data['amount'] * -1),];
$validate = $transactionRepository->validate($first);
if ($validate['errors']->count() == 0) {
$transactionRepository->store($first);
} else {
throw new FireflyException($validate['errors']->first());
}
$second = ['account' => $data['to'], 'transaction_journal' => $journal, 'amount' => floatval($data['amount']),];
$validate = $transactionRepository->validate($second);
if ($validate['errors']->count() == 0) {
$transactionRepository->store($second);
} else {
throw new FireflyException($validate['errors']->first());
}
$journal->completed = 1;
$journal->save();
return $journal;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Ardent $model
* @param array $data
*
* @return Collection
* @return bool
*/
public function getInDateRange(Carbon $start, Carbon $end)
public function update(Ardent $model, array $data)
{
return $this->getuser()->transactionjournals()->withRelevantData()->before($end)->after($start)->get();
// TODO: Implement update() method.
throw new NotImplementedException;
}
/**
* @param \Account $account
* @param int $count
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getInDateRangeAccount(\Account $account, $count = 20, Carbon $start, Carbon $end)
{
$accountID = $account->id;
$query = $this->_user
->transactionjournals()
->with(['transactions', 'transactioncurrency', 'transactiontype'])
->leftJoin('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.*']);
return $query;
}
/**
* @return TransactionJournal
*/
public function first()
{
return $this->getUser()->transactionjournals()->orderBy('date', 'ASC')->first();
}
/**
* Validates an array. Returns an array containing MessageBags
* errors/warnings/successes.
@@ -133,9 +147,9 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
public function validate(array $model)
{
$warnings = new MessageBag;
$warnings = new MessageBag;
$successes = new MessageBag;
$errors = new MessageBag;
$errors = new MessageBag;
if (!isset($model['what'])) {
@@ -173,10 +187,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
*/
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!');
} else {
$successes->add('amount', 'OK');
if (!isset($model['amount'])) {
$errors->add('amount', 'Amount must be set!');
} else {
$successes->add('amount', 'OK');
}
}
/*
@@ -240,26 +256,26 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
break;
}
// if (isset($model['to_id']) && intval($model['to_id']) < 1) {
// $errors->add('account_to', 'Invalid to-account');
// }
//
// if (isset($model['from_id']) && intval($model['from_id']) < 1) {
// $errors->add('account_from', 'Invalid from-account');
//
// }
// if (isset($model['account_id']) && intval($model['account_id']) < 1) {
// $errors->add('account_id', 'Invalid account!');
// }
// if (isset($model['to']) && !($model['to'] instanceof \Account)) {
// $errors->add('account_to', 'Invalid to-account');
// }
// if (isset($model['from']) && !($model['from'] instanceof \Account)) {
// $errors->add('account_from', 'Invalid from-account');
// }
// if (!isset($model['amount']) || (isset($model['amount']) && floatval($model['amount']) < 0)) {
// $errors->add('amount', 'Invalid amount');
// }
// if (isset($model['to_id']) && intval($model['to_id']) < 1) {
// $errors->add('account_to', 'Invalid to-account');
// }
//
// if (isset($model['from_id']) && intval($model['from_id']) < 1) {
// $errors->add('account_from', 'Invalid from-account');
//
// }
// if (isset($model['account_id']) && intval($model['account_id']) < 1) {
// $errors->add('account_id', 'Invalid account!');
// }
// if (isset($model['to']) && !($model['to'] instanceof \Account)) {
// $errors->add('account_to', 'Invalid to-account');
// }
// if (isset($model['from']) && !($model['from'] instanceof \Account)) {
// $errors->add('account_from', 'Invalid from-account');
// }
// if (!isset($model['amount']) || (isset($model['amount']) && floatval($model['amount']) < 0)) {
// $errors->add('amount', 'Invalid amount');
// }
$validator = \Validator::make([$model], \Transaction::$rules);
@@ -277,169 +293,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
if (!$errors->has('date')) {
$successes->add('date', 'OK');
}
return [
'errors' => $errors,
'warnings' => $warnings,
'successes' => $successes
];
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
/**
* @param array $data
*
* @return Ardent
*/
public function store(array $data)
{
/** @var \FireflyIII\Database\TransactionType $typeRepository */
$typeRepository = \App::make('FireflyIII\Database\TransactionType');
/** @var \FireflyIII\Database\Account $accountRepository */
$accountRepository = \App::make('FireflyIII\Database\Account');
/** @var \FireflyIII\Database\TransactionCurrency $currencyRepository */
$currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency');
/** @var \FireflyIII\Database\Transaction $transactionRepository */
$transactionRepository = \App::make('FireflyIII\Database\Transaction');
$journalType = $typeRepository->findByWhat($data['what']);
$currency = $currencyRepository->findByCode($data['currency']);
$journal = new \TransactionJournal;
$journal->transactionType()->associate($journalType);
$journal->transactionCurrency()->associate($currency);
$journal->user()->associate($this->getUser());
$journal->description = $data['description'];
$journal->date = $data['date'];
$journal->completed = 0;
/*
* This must be enough to store the journal:
*/
if (!$journal->validate()) {
\Log::error($journal->errors()->all());
throw new FireflyException('store() transaction journal failed, but it should not!');
}
$journal->save();
/*
* Still need to find the accounts related to the transactions.
* This depends on the type of transaction.
*/
switch ($data['what']) {
case 'withdrawal':
$data['from'] = $accountRepository->find($data['account_id']);
$data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
break;
case 'opening':
break;
default:
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
break;
}
/*
* Then store both transactions.
*/
$first = [
'account' => $data['from'],
'transaction_journal' => $journal,
'amount' => ($data['amount'] * -1),
];
$validate = $transactionRepository->validate($first);
if ($validate['errors']->count() == 0) {
$transactionRepository->store($first);
} else {
throw new FireflyException($validate['errors']->first());
}
$second = [
'account' => $data['to'],
'transaction_journal' => $journal,
'amount' => floatval($data['amount']),
];
$validate = $transactionRepository->validate($second);
if ($validate['errors']->count() == 0) {
$transactionRepository->store($second);
} else {
throw new FireflyException($validate['errors']->first());
}
$journal->completed = 1;
$journal->save();
return $journal;
}
/**
* Returns an object with id $id.
*
* @param int $id
*
* @return Ardent
*/
public function find($id)
{
return $this->getUser()->transactionjournals()->find($id);
}
/**
* Returns all objects.
*
* @return Collection
*/
public function get()
{
return $this->getUser()->transactionjournals()->get();
}
/**
* Some objects.
*
* @return Collection
*/
public function getTransfers()
{
return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Transfer'])->get(['transaction_journals.*']);
}
/**
* Some objects.
*
* @return Collection
*/
public function getDeposits()
{
return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Deposit'])->get(['transaction_journals.*']);
}
/**
* Some objects.
*
* @return Collection
*/
public function getWithdrawals()
{
return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Withdrawal'])->get(['transaction_journals.*']);
}
/**
* @param Ardent $model
*
* @return bool
*/
public function destroy(Ardent $model)
{
// TODO: Implement destroy() method.
throw new NotImplementedException;
}
/**
* Validates a model. Returns an array containing MessageBags
* errors/warnings/successes.
@@ -455,26 +314,15 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
}
/**
* @param Ardent $model
* @param array $data
* Returns an object with id $id.
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
throw new NotImplementedException;
}
/**
* @param array $ids
* @param int $id
*
* @return Collection
* @return Ardent
*/
public function getByIds(array $ids)
public function find($id)
{
// TODO: Implement getByIds() method.
throw new NotImplementedException;
return $this->getUser()->transactionjournals()->find($id);
}
/**
@@ -489,4 +337,137 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
// TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
/**
* Returns all objects.
*
* @return Collection
*/
public function get()
{
return $this->getUser()->transactionjournals()->get();
}
/**
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids)
{
// TODO: Implement getByIds() method.
throw new NotImplementedException;
}
/**
* @return TransactionJournal
*/
public function first()
{
return $this->getUser()->transactionjournals()->orderBy('date', 'ASC')->first();
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getInDateRange(Carbon $start, Carbon $end)
{
return $this->getuser()->transactionjournals()->withRelevantData()->before($end)->after($start)->get();
}
/**
* @param Carbon $date
*
* @return float
*/
public function getSumOfExpensesByMonth(Carbon $date)
{
$end = clone $date;
$date->startOfMonth();
$end->endOfMonth();
$sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin(
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Withdrawal')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount');
$sum = floatval($sum);
return $sum;
}
/**
* @param Carbon $date
*
* @return float
*/
public function getSumOfIncomesByMonth(Carbon $date)
{
$end = clone $date;
$date->startOfMonth();
$end->endOfMonth();
$sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin(
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Deposit')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount');
$sum = floatval($sum);
return $sum;
}
/**
* Some objects.
*
* @return Collection
*/
public function getDeposits()
{
return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Deposit'])->get(['transaction_journals.*']);
}
/**
* @param \Account $account
* @param int $count
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getInDateRangeAccount(\Account $account, $count = 20, Carbon $start, Carbon $end)
{
$accountID = $account->id;
$query = $this->_user->transactionjournals()->with(['transactions', 'transactioncurrency', 'transactiontype'])->leftJoin(
'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.*']);
return $query;
}
/**
* Some objects.
*
* @return Collection
*/
public function getTransfers()
{
return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Transfer'])->get(['transaction_journals.*']);
}
/**
* Some objects.
*
* @return Collection
*/
public function getWithdrawals()
{
return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Withdrawal'])->get(['transaction_journals.*']);
}
}