mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-18 12:28:46 +00:00
Some more work done. Mainly accounts. [skip ci]
This commit is contained in:
41
app/lib/Firefly/Helper/Form/FormHelper.php
Normal file
41
app/lib/Firefly/Helper/Form/FormHelper.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Firefly\Helper\Form;
|
||||
|
||||
/**
|
||||
* Class FormHelper
|
||||
*
|
||||
* @package Firefly\Form
|
||||
*/
|
||||
class FormHelper
|
||||
{
|
||||
public function budget($value = null)
|
||||
{
|
||||
|
||||
$str = '<select name="budget_id" class="form-control">';
|
||||
|
||||
$str .= '<option value="0" label="(no budget)"';
|
||||
if (is_null($value) || intval($value) == 0) {
|
||||
$str .= ' selected="selected"';
|
||||
}
|
||||
$str .= '</option>';
|
||||
|
||||
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgets */
|
||||
$budgets = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
|
||||
$list = $budgets->getAsSelectList();
|
||||
foreach ($list as $id => $name) {
|
||||
$str .= '<option value="' . e($id) . '" label="' . e($name) . '"';
|
||||
if ($id == intval($value)) {
|
||||
$str .= ' selected="selected"';
|
||||
}
|
||||
$str .= '>' . e($name) . '</option>';
|
||||
}
|
||||
|
||||
|
||||
$str .= '</select>';
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
37
app/lib/Firefly/Helper/Form/FormTrigger.php
Normal file
37
app/lib/Firefly/Helper/Form/FormTrigger.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sander
|
||||
* Date: 25/07/14
|
||||
* Time: 21:04
|
||||
*/
|
||||
|
||||
namespace Firefly\Helper\Form;
|
||||
|
||||
use Illuminate\Events\Dispatcher;
|
||||
/**
|
||||
* Class FormTrigger
|
||||
*
|
||||
* @package Firefly\Helper\Form
|
||||
*/
|
||||
class FormTrigger {
|
||||
|
||||
public function registerFormExtensions() {
|
||||
\Form::macro(
|
||||
'budget', function () {
|
||||
$helper = new \Firefly\Helper\Form\FormHelper;
|
||||
return $helper->budget();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen('laravel.booted', 'Firefly\Helper\Form\FormTrigger@registerFormExtensions');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -59,6 +59,13 @@ interface AccountRepositoryInterface
|
||||
*/
|
||||
public function getDefault();
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function findOpeningBalanceTransaction(\Account $account);
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
@@ -72,18 +79,25 @@ interface AccountRepositoryInterface
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return mixed
|
||||
* @return \Account
|
||||
*/
|
||||
public function store($data);
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param Carbon $date
|
||||
* @param int $amount
|
||||
* @param $accountId
|
||||
*
|
||||
* @return mixed
|
||||
* @return bool
|
||||
*/
|
||||
public function storeWithInitialBalance($data, Carbon $date, $amount = 0);
|
||||
public function destroy($accountId);
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return \Account
|
||||
*/
|
||||
public function update($data);
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
namespace Firefly\Storage\Account;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Firefly\Exception\FireflyException;
|
||||
use Illuminate\Database\QueryException;
|
||||
|
||||
/**
|
||||
* Class EloquentAccountRepository
|
||||
@@ -14,8 +12,6 @@ use Illuminate\Database\QueryException;
|
||||
*/
|
||||
class EloquentAccountRepository implements AccountRepositoryInterface
|
||||
{
|
||||
public $validator;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -45,16 +41,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $accountId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function find($accountId)
|
||||
{
|
||||
return \Auth::user()->accounts()->where('id', $accountId)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $ids
|
||||
*
|
||||
@@ -118,73 +104,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param Carbon $date
|
||||
* @param int $amount
|
||||
*
|
||||
* @return \Account|mixed
|
||||
* @throws \Firefly\Exception\FireflyException
|
||||
*/
|
||||
public function storeWithInitialBalance($data, Carbon $date, $amount = 0)
|
||||
{
|
||||
|
||||
$account = $this->store($data);
|
||||
|
||||
$initialBalanceAT = \AccountType::where('description', 'Initial balance account')->first();
|
||||
$initial = new \Account;
|
||||
$initial->accountType()->associate($initialBalanceAT);
|
||||
$initial->user()->associate(\Auth::user());
|
||||
$initial->name = $data['name'] . ' initial balance';
|
||||
$initial->active = 0;
|
||||
try {
|
||||
$initial->save();
|
||||
} catch (QueryException $e) {
|
||||
\Log::error('DB ERROR: ' . $e->getMessage());
|
||||
throw new FireflyException('Could not save counterbalance account for ' . $data['name']);
|
||||
}
|
||||
|
||||
// create new transaction journal (and transactions):
|
||||
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
|
||||
$transactionJournal = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
|
||||
|
||||
$transactionJournal->createSimpleJournal(
|
||||
$initial, $account, 'Initial Balance for ' . $data['name'], $amount, $date
|
||||
);
|
||||
|
||||
|
||||
return $account;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return \Account|mixed
|
||||
* @throws \Firefly\Exception\FireflyException
|
||||
*/
|
||||
public function store($data)
|
||||
{
|
||||
$defaultAT = \AccountType::where('description', 'Default account')->first();
|
||||
|
||||
$at = isset($data['account_type']) ? $data['account_type'] : $defaultAT;
|
||||
|
||||
$account = new \Account;
|
||||
$account->accountType()->associate($at);
|
||||
$account->user()->associate(\Auth::user());
|
||||
$account->name = $data['name'];
|
||||
$account->active = isset($data['active']) ? $data['active'] : 1;
|
||||
try {
|
||||
$account->save();
|
||||
} catch (QueryException $e) {
|
||||
\Log::error('DB ERROR: ' . $e->getMessage());
|
||||
throw new FireflyException('Could not save account ' . $data['name']);
|
||||
}
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
@@ -210,7 +129,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
||||
$beneficiary = $this->findByName($name);
|
||||
if (!$beneficiary) {
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'name' => $name,
|
||||
'account_type' => $type
|
||||
];
|
||||
return $this->store($data);
|
||||
@@ -228,6 +147,142 @@ class EloquentAccountRepository implements AccountRepositoryInterface
|
||||
return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return \Account
|
||||
* @throws \Firefly\Exception\FireflyException
|
||||
*/
|
||||
public function store($data)
|
||||
{
|
||||
$defaultAccountType = \AccountType::where('description', 'Default account')->first();
|
||||
$accountType = isset($data['account_type']) ? $data['account_type'] : $defaultAccountType;
|
||||
|
||||
// create Account:
|
||||
$account = new \Account;
|
||||
$account->accountType()->associate($accountType);
|
||||
$account->user()->associate(\Auth::user());
|
||||
$account->name = $data['name'];
|
||||
$account->active
|
||||
= isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
|
||||
$data['active']
|
||||
) : 1;
|
||||
|
||||
// try to save it:
|
||||
if ($account->save()) {
|
||||
// create initial balance, if necessary:
|
||||
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
|
||||
$amount = floatval($data['openingbalance']);
|
||||
$date = new Carbon($data['openingbalancedate']);
|
||||
$this->_createInitialBalance($account, $amount, $date);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// whatever the result, return the account.
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
* @param int $amount
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
|
||||
{
|
||||
// get account type:
|
||||
$initialBalanceAT = \AccountType::where('description', 'Initial balance account')->first();
|
||||
|
||||
// create new account:
|
||||
$initial = new \Account;
|
||||
$initial->accountType()->associate($initialBalanceAT);
|
||||
$initial->user()->associate(\Auth::user());
|
||||
$initial->name = $account->name . ' initial balance';
|
||||
$initial->active = 0;
|
||||
if ($initial->validate()) {
|
||||
$initial->save();
|
||||
// create new transaction journal (and transactions):
|
||||
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
|
||||
$transactionJournal = \App::make(
|
||||
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
|
||||
);
|
||||
|
||||
$transactionJournal->createSimpleJournal(
|
||||
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return \Account|void
|
||||
*/
|
||||
public function update($data)
|
||||
{
|
||||
$account = $this->find($data['id']);
|
||||
if ($account) {
|
||||
// update account accordingly:
|
||||
$account->name = $data['name'];
|
||||
if ($account->validate()) {
|
||||
$account->save();
|
||||
}
|
||||
// update initial balance if necessary:
|
||||
if ($account->accounttype->description == 'Default account') {
|
||||
$journal = $this->findOpeningBalanceTransaction($account);
|
||||
$journal->date = new Carbon($data['openingbalancedate']);
|
||||
$journal->transactions[0]->amount = floatval($data['openingbalance']) * -1;
|
||||
$journal->transactions[1]->amount = floatval($data['openingbalance']);
|
||||
$journal->transactions[0]->save();
|
||||
$journal->transactions[1]->save();
|
||||
$journal->save();
|
||||
}
|
||||
}
|
||||
return $account;
|
||||
}
|
||||
|
||||
public function destroy($accountId) {
|
||||
$account = $this->find($accountId);
|
||||
if($account) {
|
||||
$account->delete();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $accountId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function find($accountId)
|
||||
{
|
||||
return \Auth::user()->accounts()->where('id', $accountId)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function findOpeningBalanceTransaction(\Account $account)
|
||||
{
|
||||
$transactionType = \TransactionType::where('type', 'Opening balance')->first();
|
||||
$journal = \TransactionJournal::
|
||||
with(
|
||||
['transactions' => function ($q) {
|
||||
$q->orderBy('amount', 'ASC');
|
||||
}]
|
||||
)->where('transaction_type_id', $transactionType->id)
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)->first(['transaction_journals.*']);
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
|
||||
@@ -40,6 +40,9 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
|
||||
*/
|
||||
public function findByName($name)
|
||||
{
|
||||
if($name == '') {
|
||||
return null;
|
||||
}
|
||||
return \Auth::user()->categories()->where('name', 'LIKE', '%' . $name . '%')->first();
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
public function find($journalId)
|
||||
{
|
||||
return \Auth::user()->transactionjournals()->with(
|
||||
['transactions', 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
|
||||
['transactions' => function ($q) {
|
||||
return $q->orderBy('amount', 'ASC');
|
||||
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
|
||||
'transactions.account.accounttype']
|
||||
)
|
||||
->where('id', $journalId)->first();
|
||||
|
||||
Reference in New Issue
Block a user