Some basic fixes for the transaction controller [skip ci]

This commit is contained in:
James Cole
2014-08-02 07:34:38 +02:00
parent af856a135f
commit d756324432
31 changed files with 726 additions and 341 deletions

View File

@@ -15,10 +15,9 @@ class Category implements CategoryInterface
{
public function journalsInRange(\Category $category, Carbon $start, Carbon $end)
{
return $category->transactionjournals()->
with(['transactions','transactions.account','transactiontype','components'])->
orderBy('date','DESC')->orderBy('id','DESC')->before($end)->after($start)->get();
return $category->transactionjournals()->with(
['transactions', 'transactions.account', 'transactiontype', 'components']
)->orderBy('date', 'DESC')->orderBy('id', 'DESC')->before($end)->after($start)->get();
}
}

View File

@@ -11,7 +11,8 @@ namespace Firefly\Helper\Controllers;
use Carbon\Carbon;
interface CategoryInterface {
interface CategoryInterface
{
public function journalsInRange(\Category $category, Carbon $start, Carbon $end);

View File

@@ -21,7 +21,9 @@ class Chart implements ChartInterface
$return = ['name' => $account->name, 'id' => $account->id, 'data' => []];
while ($current <= $end) {
\Log::debug('Now at day: ' . $current . '('.$current->timestamp.'), ('.($current->timestamp * 1000).') ');
\Log::debug(
'Now at day: ' . $current . '(' . $current->timestamp . '), (' . ($current->timestamp * 1000) . ') '
);
if ($current > $today) {
$return['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
} else {
@@ -38,7 +40,7 @@ class Chart implements ChartInterface
{
$result = [
'rows' => [],
'sum' => 0
'sum' => 0
];
if ($account) {
// get journals in range:
@@ -219,7 +221,6 @@ class Chart implements ChartInterface
}
// now format the current range:
$title = '';
switch ($range) {
@@ -240,7 +241,7 @@ class Chart implements ChartInterface
$title = $beginning->format('M Y') . ' - ' . $currentEnd->format('M Y');
break;
case 'custom':
$title = $beginning->format('d-m-Y').' - '.$currentEnd->format('d-m-Y');
$title = $beginning->format('d-m-Y') . ' - ' . $currentEnd->format('d-m-Y');
break;
case 'yearly':
// return $this->startdate->format('Y');
@@ -257,7 +258,10 @@ class Chart implements ChartInterface
)
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('transaction_types.type', 'Withdrawal')
->leftJoin('component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin(
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=',
'transaction_journals.id'
)
->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')
->where('components.id', '=', $category->id)
//->leftJoin()
@@ -306,6 +310,7 @@ class Chart implements ChartInterface
}
}
return $data;

View File

@@ -14,6 +14,7 @@ interface CategoryRepositoryInterface
* @return mixed
*/
public function get();
public function find($categoryId);
/**

View File

@@ -66,6 +66,7 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
$category->user()->associate(\Auth::user());
$category->save();
return $category;
}

View File

@@ -23,7 +23,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('piggybanks.id', $piggyBankId)->first('piggybanks.*');
)->where('piggybanks.id', $piggyBankId)->first(['piggybanks.*']);
}
public function get()
@@ -32,13 +32,6 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
'accounts.user_id', \Auth::user()->id
)->get(['piggybanks.*']);
}
public function updateAmount(\Piggybank $piggyBank, $amount) {
$piggyBank->amount = floatval($amount);
if($piggyBank->validate()) {
$piggyBank->save();
}
}
public function store($data)
{
@@ -62,4 +55,31 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
return $piggyBank;
}
public function update($data)
{
$piggyBank = $this->find($data['id']);
if ($piggyBank) {
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$account = $accounts->find($data['account_id']);
// update piggybank accordingly:
$piggyBank->name = $data['name'];
$piggyBank->target = floatval($data['target']);
$piggyBank->account()->associate($account);
if ($piggyBank->validate()) {
$piggyBank->save();
}
}
return $piggyBank;
}
public function updateAmount(\Piggybank $piggyBank, $amount)
{
$piggyBank->amount = floatval($amount);
if ($piggyBank->validate()) {
$piggyBank->save();
}
}
}

View File

@@ -12,9 +12,15 @@ interface PiggybankRepositoryInterface
{
public function find($piggyBankId);
public function count();
public function store($data);
public function get();
public function updateAmount(\Piggybank $piggyBank, $amount);
public function update($data);
}

View File

@@ -32,8 +32,6 @@ class StorageServiceProvider extends ServiceProvider
);
$this->app->bind(
'Firefly\Storage\Account\AccountRepositoryInterface',
'Firefly\Storage\Account\EloquentAccountRepository'

View File

@@ -14,26 +14,6 @@ use Firefly\Exception\FireflyException;
class EloquentTransactionJournalRepository implements TransactionJournalRepositoryInterface
{
/**
* @param $journalId
*
* @return mixed
*/
public function find($journalId)
{
return \Auth::user()->transactionjournals()->with(
['transactions' => function ($q) {
return $q->orderBy('amount', 'ASC');
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
'transactions.account.accounttype']
)
->where('id', $journalId)->first();
}
/*
*
*/
/**
*
* We're building this thinking the money goes from A to B.
@@ -66,20 +46,24 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
public function createSimpleJournal(\Account $from, \Account $toAccount, $description, $amount, Carbon $date)
{
\Log::debug('Creating tranaction "' . $description . '".');
$journal = new \TransactionJournal;
$amountFrom = $amount * -1;
$amountTo = $amount;
if (round(floatval($amount), 2) == 0.00) {
\Log::error('Transaction will never save: amount = 0');
\Session::flash('error', 'The amount should not be empty or zero.');
throw new FireflyException('Could not figure out transaction type.');
$journal->errors()->add('amount', 'Amount must not be zero.');
return $journal;
}
// same account:
if ($from->id == $toAccount->id) {
\Log::error('Accounts cannot be equal');
\Session::flash('error', 'Select two different accounts.');
throw new FireflyException('Select two different accounts.');
$journal->errors()->add('account_id', 'Must be different accounts.');
$journal->errors()->add('account_from_id', 'Must be different accounts.');
return $journal;
}
// account types for both:
@@ -129,18 +113,15 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
}
// new journal:
$journal = new \TransactionJournal();
$journal->transactionType()->associate($journalType);
$journal->transactionCurrency()->associate($currency);
$journal->user()->associate(\Auth::user());
$journal->completed = false;
$journal->description = $description;
$journal->date = $date;
if (!$journal->save()) {
\Log::error('Cannot create valid journal.');
\Log::error('Errors: ' . print_r($journal->errors()->all(), true));
\Session::flash('error', 'Could not create journal: ' . $journal->errors()->first());
throw new FireflyException('Cannot create valid journal.');
if (!$journal->validate()) {
return $journal;
}
$journal->save();
@@ -150,7 +131,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$fromTransaction->transactionJournal()->associate($journal);
$fromTransaction->description = null;
$fromTransaction->amount = $amountFrom;
if (!$fromTransaction->save()) {
if (!$fromTransaction->validate()) {
\Log::error('Cannot create valid transaction (from) for journal #' . $journal->id);
\Log::error('Errors: ' . print_r($fromTransaction->errors()->all(), true));
throw new FireflyException('Cannot create valid transaction (from).');
@@ -162,7 +143,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$toTransaction->transactionJournal()->associate($journal);
$toTransaction->description = null;
$toTransaction->amount = $amountTo;
if (!$toTransaction->save()) {
if (!$toTransaction->validate()) {
\Log::error('Cannot create valid transaction (to) for journal #' . $journal->id);
\Log::error('Errors: ' . print_r($toTransaction->errors()->all(), true));
throw new FireflyException('Cannot create valid transaction (to).');
@@ -174,6 +155,26 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
return $journal;
}
/*
*
*/
/**
* @param $journalId
*
* @return mixed
*/
public function find($journalId)
{
return \Auth::user()->transactionjournals()->with(
['transactions' => function ($q) {
return $q->orderBy('amount', 'ASC');
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
'transactions.account.accounttype']
)
->where('id', $journalId)->first();
}
/**
*
@@ -183,6 +184,35 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
}
/**
* @param \Account $account
* @param Carbon $date
*
* @return mixed
*/
public function getByAccountAndDate(\Account $account, Carbon $date)
{
$accountID = $account->id;
$query = \Auth::user()->transactionjournals()->with(
[
'transactions',
'transactions.account',
'transactioncurrency',
'transactiontype'
]
)
->distinct()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->where('transactions.account_id', $accountID)
->where('transaction_journals.date', $date->format('Y-m-d'))
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
return $query;
}
/**
* @param \Account $account
* @param int $count
@@ -214,6 +244,17 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
return $query;
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
public function getByDateRange(Carbon $start, Carbon $end)
{
die('no impl');
}
/**
* @param int $count
*
@@ -234,50 +275,154 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC')
->take($count)
->paginate($count);
return $query;
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
public function getByDateRange(Carbon $start, Carbon $end)
public function store($what, $data)
{
die('no impl');
// $fromAccount and $toAccount are found
// depending on the $what
$fromAccount = null;
$toAccount = null;
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accountRepository */
$accountRepository = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
/** @var \Firefly\Storage\Category\CategoryRepositoryInterface $catRepository */
$catRepository = \App::make('Firefly\Storage\Category\CategoryRepositoryInterface');
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budRepository */
$budRepository = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
switch ($what) {
case 'withdrawal':
$fromAccount = $accountRepository->find(intval($data['account_id']));
$toAccount = $accountRepository->createOrFindBeneficiary($data['beneficiary']);
break;
case 'deposit':
$fromAccount = $accountRepository->createOrFindBeneficiary($data['beneficiary']);
$toAccount = $accountRepository->find(intval($data['account_id']));
break;
case 'transfer':
$fromAccount = $accountRepository->find(intval($data['account_from_id']));
$toAccount = $accountRepository->find(intval($data['account_to_id']));
break;
}
// fall back to cash if necessary:
$fromAccount = is_null($fromAccount) ? $fromAccount = $accountRepository->getCashAccount() : $fromAccount;
$toAccount = is_null($toAccount) ? $toAccount = $accountRepository->getCashAccount() : $toAccount;
// create or find category:
$category = isset($data['category']) ? $catRepository->createOrFind($data['category']) : null;
// find budget:
$budget = isset($data['budget_id']) ? $budRepository->find(intval($data['budget_id'])) : null;
//
// // find amount & description:
$description = trim($data['description']);
$amount = floatval($data['amount']);
$date = new \Carbon\Carbon($data['date']);
// try to create a journal:
$transactionJournal = $this->createSimpleJournal($fromAccount, $toAccount, $description, $amount, $date);
if (!$transactionJournal->id || $transactionJournal->completed == 0) {
return $transactionJournal;
}
// attach:
if (!is_null($budget)) {
$transactionJournal->budgets()->save($budget);
}
if (!is_null($category)) {
$transactionJournal->categories()->save($category);
}
return $transactionJournal;
}
/**
* @param \Account $account
* @param Carbon $date
*
* @return mixed
*/
public function getByAccountAndDate(\Account $account, Carbon $date)
public function update(\TransactionJournal $journal, $data)
{
$accountID = $account->id;
$query = \Auth::user()->transactionjournals()->with(
[
'transactions',
'transactions.account',
'transactioncurrency',
'transactiontype'
]
)
->distinct()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->where('transactions.account_id', $accountID)
->where('transaction_journals.date', $date->format('Y-m-d'))
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
/** @var \Firefly\Storage\Category\CategoryRepositoryInterface $catRepository */
$catRepository = \App::make('Firefly\Storage\Category\CategoryRepositoryInterface');
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgetRepository */
$budRepository = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accountRepository */
$accountRepository = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
// update basics first:
$journal->description = $data['description'];
$journal->date = $data['date'];
$amount = floatval($data['amount']);
// remove previous category, if any:
if (!is_null($journal->categories()->first())) {
$journal->categories()->detach($journal->categories()->first()->id);
}
// remove previous budget, if any:
if (!is_null($journal->budgets()->first())) {
$journal->budgets()->detach($journal->budgets()->first()->id);
}
$category = isset($data['category']) ? $catRepository->findByName($data['category']) : null;
if (!is_null($category)) {
$journal->categories()->attach($category);
}
// update the amounts:
/** @var \Transaction $transaction */
$transactions = $journal->transactions()->orderBy('amount', 'ASC')->get();
$transactions[0]->amount = $amount;
$transactions[1]->amount = $amount;
// switch on type to properly change things:
switch ($journal->transactiontype->type) {
case 'Withdrawal':
// means transaction[0] is the users account.
$account = $accountRepository->find($data['account_id']);
$beneficiary = $accountRepository->findByName($data['beneficiary']);
$transactions[0]->account()->associate($account);
$transactions[1]->account()->associate($beneficiary);
// do budget:
$budget = $budRepository->find($data['budget_id']);
$journal->budgets()->attach($budget);
break;
case 'Deposit':
// means transaction[0] is the beneficiary.
$account = $accountRepository->find($data['account_id']);
$beneficiary = $accountRepository->findByName($data['beneficiary']);
$journal->transactions[0]->account()->associate($beneficiary);
$journal->transactions[1]->account()->associate($account);
break;
case 'Transfer':
// means transaction[0] is account that sent the money (from).
$fromAccount = $accountRepository->find($data['account_from_id']);
$toAccount = $accountRepository->find($data['account_to_id']);
$journal->transactions[0]->account()->associate($fromAccount);
$journal->transactions[1]->account()->associate($toAccount);
break;
default:
throw new \Firefly\Exception\FireflyException('Cannot edit this!');
break;
}
$transactions[0]->save();
$transactions[1]->save();
if ($journal->validate()) {
$journal->save();
}
return $journal;
return $query;
}

View File

@@ -27,6 +27,10 @@ interface TransactionJournalRepositoryInterface
*/
public function get();
public function store($what, $data);
public function update(\TransactionJournal $journal, $data);
/**
* @param $journalId
*

View File

@@ -46,14 +46,14 @@ class EloquentLimitTrigger
if ($limit->repeats == 0) {
$limit->createRepetition($limit->startdate);
}
if($limit->repeats == 1) {
if ($limit->repeats == 1) {
$start = $limit->startdate;
$end = new Carbon;
// repeat for period:
$current = clone $start;
\Log::debug('Create repeating limit for #'.$limit->id.' starting on ' . $current);
while($current <= $end) {
\Log::debug('Create repeating limit for #' . $limit->id . ' starting on ' . $current);
while ($current <= $end) {
\Log::debug('Current is now: ' . $current);
$limit->createRepetition(clone $current);
// switch period, add time:
@@ -128,10 +128,10 @@ class EloquentLimitTrigger
// }
//
// }
}
}
}
}
//
//