Skip to content

Firefly III: a personal finances manager

Code clean up and reformat.

James Cole4aa9a0451619
diff --git a/app/lib/FireflyIII/Database/Account.php b/app/lib/FireflyIII/Database/Account.php
index e345a1c6d0..414f046379 100644
--- a/app/lib/FireflyIII/Database/Account.php
+++ b/app/lib/FireflyIII/Database/Account.php
@@ -29,64 +29,54 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
}
/**
- * @param Ardent $model
- * @param array $data
+ * @param array $types
*
- * @return bool
+ * @return int
*/
- public function update(Ardent $model, array $data)
+ public function countAccountsByType(array $types)
{
- $model->name = $data['name'];
- $model->active = isset($data['active']) ? intval($data['active']) : 0;
- $model->save();
+ return $this->getUser()->accounts()->accountTypeIn($types)->count();
+ }
- if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
- $openingBalance = $this->openingBalanceTransaction($model);
+ /**
+ * @return int
+ */
+ public function countAssetAccounts()
+ {
+ return $this->countAccountsByType(['Default account', 'Asset account']);
+ }
- $openingBalance->date = new Carbon($data['openingbalancedate']);
- $openingBalance->save();
- $amount = floatval($data['openingbalance']);
- /** @var \Transaction $transaction */
- foreach ($openingBalance->transactions as $transaction) {
- if ($transaction->account_id == $model->id) {
- $transaction->amount = $amount;
- } else {
- $transaction->amount = $amount * -1;
- }
- $transaction->save();
- }
- }
- return true;
+ /**
+ * @return int
+ */
+ public function countExpenseAccounts()
+ {
+ return $this->countAccountsByType(['Expense account', 'Beneficiary account']);
}
/**
- * @param \Account $account
+ * Counts the number of total revenue accounts. Useful for DataTables.
*
- * @return \TransactionJournal|null
+ * @return int
*/
- public function openingBalanceTransaction(\Account $account)
+ public function countRevenueAccounts()
{
- return \TransactionJournal::withRelevantData()
- ->accountIs($account)
- ->leftJoin(
- 'transaction_types', 'transaction_types.id', '=',
- 'transaction_journals.transaction_type_id'
- )
- ->where('transaction_types.type', 'Opening balance')
- ->first(['transaction_journals.*']);
+ return $this->countAccountsByType(['Revenue account']);
}
/**
- * Get all asset accounts. Optional JSON based parameters.
- *
- * @param array $parameters
+ * @param \Account $account
*
- * @return Collection
+ * @return \Account|null
*/
- public function getAssetAccounts(array $parameters = [])
+ public function findInitialBalanceAccount(\Account $account)
{
- return $this->getAccountsByType(['Default account', 'Asset account'], $parameters);
+ /** @var \FireflyIII\Database\AccountType $acctType */
+ $acctType = \App::make('FireflyIII\Database\AccountType');
+ $accountType = $acctType->findByWhat('initial');
+
+ return $this->getUser()->accounts()->where('account_type_id', $accountType->id)->where('name', 'LIKE', $account->name . '%')->first();
}
/**
@@ -149,117 +139,193 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
}
/**
- * @param \Account $account
+ * Get all asset accounts. Optional JSON based parameters.
*
- * @return \Account|null
+ * @param array $parameters
+ *
+ * @return Collection
*/
- public function findInitialBalanceAccount(\Account $account)
+ public function getAssetAccounts(array $parameters = [])
{
- /** @var \FireflyIII\Database\AccountType $acctType */
- $acctType = \App::make('FireflyIII\Database\AccountType');
-
- $accountType = $acctType->findByWhat('initial');
+ return $this->getAccountsByType(['Default account', 'Asset account'], $parameters);
- return $this->getUser()->accounts()->where('account_type_id', $accountType->id)->where('name', 'LIKE', $account->name . '%')->first();
}
/**
- * @return int
+ * Get all default accounts.
+ *
+ * @return Collection
*/
- public function countAssetAccounts()
+ public function getDefaultAccounts()
{
- return $this->countAccountsByType(['Default account', 'Asset account']);
+ // TODO: Implement getDefaultAccounts() method.
+ throw new NotImplementedException;
}
/**
- * @param array $types
+ * @param array $parameters
*
- * @return int
+ * @return Collection
*/
- public function countAccountsByType(array $types)
+ public function getExpenseAccounts(array $parameters = [])
{
- return $this->getUser()->accounts()->accountTypeIn($types)->count();
+ return $this->getAccountsByType(['Expense account', 'Beneficiary account'], $parameters);
}
/**
- * @return int
+ * Get all revenue accounts.
+ *
+ * @param array $parameters
+ *
+ * @return Collection
*/
- public function countExpenseAccounts()
+ public function getRevenueAccounts(array $parameters = [])
{
- return $this->countAccountsByType(['Expense account', 'Beneficiary account']);
+ return $this->getAccountsByType(['Revenue account'], $parameters);
}
/**
- * @param array $parameters
+ * @param \Account $account
*
- * @return Collection
+ * @return \TransactionJournal|null
*/
- public function getExpenseAccounts(array $parameters = [])
+ public function openingBalanceTransaction(\Account $account)
{
- return $this->getAccountsByType(['Expense account', 'Beneficiary account'], $parameters);
+ return \TransactionJournal::withRelevantData()->accountIs($account)->leftJoin(
+ 'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
+ )->where('transaction_types.type', 'Opening balance')->first(['transaction_journals.*']);
}
/**
- * Returns an object with id $id.
- *
- * @param int $id
+ * @param \Account $account
+ * @param array $data
*
- * @return Ardent
+ * @return bool
*/
- public function find($id)
+ public function storeInitialBalance(\Account $account, array $data)
{
- return $this->getUser()->accounts()->find($id);
- }
+ $opposingData = ['name' => $account->name . ' Initial Balance', 'active' => 0, 'what' => 'initial'];
+ $opposingAccount = $this->store($opposingData);
- public function firstExpenseAccountOrCreate($name)
- {
- /** @var \FireflyIII\Database\AccountType $accountTypeRepos */
- $accountTypeRepos = \App::make('FireflyIII\Database\AccountType');
+ /*
+ * Create a journal from opposing to account or vice versa.
+ */
+ $balance = floatval($data['openingbalance']);
+ $date = new Carbon($data['openingbalancedate']);
+ /** @var \FireflyIII\Database\TransactionJournal $tj */
+ $tj = \App::make('FireflyIII\Database\TransactionJournal');
+ if ($balance < 0) {
+ // first transaction draws money from the new account to the opposing
+ $from = $account;
+ $to = $opposingAccount;
+ } else {
+ // first transaction puts money into account
+ $from = $opposingAccount;
+ $to = $account;
+ }
- $accountType = $accountTypeRepos->findByWhat('expense');
+ // data for transaction journal:
+ $balance = $balance < 0 ? $balance * -1 : $balance;
+ $opening = ['what' => 'opening', 'currency' => 'EUR', 'amount' => $balance, 'from' => $from, 'to' => $to, 'date' => $date,
+ 'description' => 'Opening balance for new account ' . $account->name,];
- $data = [
- 'user_id' => $this->getUser()->id,
- 'account_type_id' => $accountType->id,
- 'name' => $name,
- 'active' => 1
- ];
- return \Account::firstOrCreate($data);
+ $validation = $tj->validate($opening);
+ if ($validation['errors']->count() == 0) {
+ $tj->store($opening);
+
+ return true;
+ } else {
+ var_dump($validation['errors']);
+ exit;
+ }
+
+ return false;
}
/**
- * Counts the number of total revenue accounts. Useful for DataTables.
+ * @param Ardent $model
*
- * @return int
+ * @return bool
*/
- public function countRevenueAccounts()
+ public function destroy(Ardent $model)
{
- return $this->countAccountsByType(['Revenue account']);
+ $model->delete();
+
+ return true;
+
}
/**
- * Get all revenue accounts.
- *
- * @param array $parameters
+ * @param array $data
*
- * @return Collection
+ * @return Ardent
*/
- public function getRevenueAccounts(array $parameters = [])
+ public function store(array $data)
{
- return $this->getAccountsByType(['Revenue account'], $parameters);
+
+ /*
+ * Find account type.
+ */
+ /** @var \FireflyIII\Database\AccountType $acctType */
+ $acctType = \App::make('FireflyIII\Database\AccountType');
+
+ $accountType = $acctType->findByWhat($data['what']);
+
+ $data['user_id'] = $this->getUser()->id;
+ $data['account_type_id'] = $accountType->id;
+ $data['active'] = isset($data['active']) && $data['active'] === '1' ? 1 : 0;
+
+
+ $data = array_except($data, ['_token', 'what']);
+ $account = new \Account($data);
+ if (!$account->validate()) {
+ var_dump($account->errors()->all());
+ exit;
+ }
+ $account->save();
+ if (isset($data['openingbalance']) && floatval($data['openingbalance']) != 0) {
+ $this->storeInitialBalance($account, $data);
+ }
+
+
+ /* Tell transaction journal to store a new one.*/
+
+
+ return $account;
+
}
/**
* @param Ardent $model
+ * @param array $data
*
* @return bool
*/
- public function destroy(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- $model->delete();
- return true;
+ $model->name = $data['name'];
+ $model->active = isset($data['active']) ? intval($data['active']) : 0;
+ $model->save();
+
+ if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
+ $openingBalance = $this->openingBalanceTransaction($model);
+
+ $openingBalance->date = new Carbon($data['openingbalancedate']);
+ $openingBalance->save();
+ $amount = floatval($data['openingbalance']);
+ /** @var \Transaction $transaction */
+ foreach ($openingBalance->transactions as $transaction) {
+ if ($transaction->account_id == $model->id) {
+ $transaction->amount = $amount;
+ } else {
+ $transaction->amount = $amount * -1;
+ }
+ $transaction->save();
+ }
+ }
+ return true;
}
/**
@@ -272,9 +338,9 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
*/
public function validate(array $model)
{
- $warnings = new MessageBag;
+ $warnings = new MessageBag;
$successes = new MessageBag;
- $errors = new MessageBag;
+ $errors = new MessageBag;
/*
* Name validation:
@@ -327,127 +393,46 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
if (!$errors->has('openingbalancedate')) {
$successes->add('openingbalancedate', 'OK');
}
- return [
- 'errors' => $errors,
- 'warnings' => $warnings,
- 'successes' => $successes
- ];
- }
-
- /**
- * @param array $data
- *
- * @return Ardent
- */
- public function store(array $data)
- {
-
- /*
- * Find account type.
- */
- /** @var \FireflyIII\Database\AccountType $acctType */
- $acctType = \App::make('FireflyIII\Database\AccountType');
-
- $accountType = $acctType->findByWhat($data['what']);
-
- $data['user_id'] = $this->getUser()->id;
- $data['account_type_id'] = $accountType->id;
- $data['active'] = isset($data['active']) && $data['active'] === '1' ? 1 : 0;
-
-
- $data = array_except($data, array('_token', 'what'));
- $account = new \Account($data);
- if (!$account->validate()) {
- var_dump($account->errors()->all());
- exit;
- }
- $account->save();
- if (isset($data['openingbalance']) && floatval($data['openingbalance']) != 0) {
- $this->storeInitialBalance($account, $data);
- }
-
-
- /* Tell transaction journal to store a new one.*/
-
-
- return $account;
+ return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
/**
- * @param \Account $account
- * @param array $data
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return bool
+ * @param Ardent $model
+ *
+ * @return array
*/
- public function storeInitialBalance(\Account $account, array $data)
+ public function validateObject(Ardent $model)
{
- $opposingData = [
- 'name' => $account->name . ' Initial Balance',
- 'active' => 0,
- 'what' => 'initial'
- ];
- $opposingAccount = $this->store($opposingData);
-
- /*
- * Create a journal from opposing to account or vice versa.
- */
- $balance = floatval($data['openingbalance']);
- $date = new Carbon($data['openingbalancedate']);
- /** @var \FireflyIII\Database\TransactionJournal $tj */
- $tj = \App::make('FireflyIII\Database\TransactionJournal');
- if ($balance < 0) {
- // first transaction draws money from the new account to the opposing
- $from = $account;
- $to = $opposingAccount;
- } else {
- // first transaction puts money into account
- $from = $opposingAccount;
- $to = $account;
- }
-
- // data for transaction journal:
- $balance = $balance < 0 ? $balance * -1 : $balance;
- $opening = [
- 'what' => 'opening',
- 'currency' => 'EUR',
- 'amount' => $balance,
- 'from' => $from,
- 'to' => $to,
- 'date' => $date,
- 'description' => 'Opening balance for new account ' . $account->name,
- ];
-
-
- $validation = $tj->validate($opening);
- if ($validation['errors']->count() == 0) {
- $tj->store($opening);
- return true;
- } else {
- var_dump($validation['errors']);
- exit;
- }
- return false;
+ // TODO: Implement validateObject() method.
+ throw new NotImplementedException;
}
/**
- * @param array $ids
+ * Returns an object with id $id.
*
- * @return Collection
+ * @param int $id
+ *
+ * @return Ardent
*/
- public function getByIds(array $ids)
+ public function find($id)
{
- return $this->getUser()->accounts()->whereIn('id', $ids)->get();
+ return $this->getUser()->accounts()->find($id);
}
/**
- * Get all default accounts.
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
- * @return Collection
+ * @param $what
+ *
+ * @return \AccountType|null
*/
- public function getDefaultAccounts()
+ public function findByWhat($what)
{
- // TODO: Implement getDefaultAccounts() method.
+ // TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
@@ -463,29 +448,25 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
+ * @param array $ids
*
- * @return \AccountType|null
+ * @return Collection
*/
- public function findByWhat($what)
+ public function getByIds(array $ids)
{
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
+ return $this->getUser()->accounts()->whereIn('id', $ids)->get();
}
- /**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
- *
- * @param Ardent $model
- *
- * @return array
- */
- public function validateObject(Ardent $model)
+ public function firstExpenseAccountOrCreate($name)
{
- // TODO: Implement validateObject() method.
- throw new NotImplementedException;
+ /** @var \FireflyIII\Database\AccountType $accountTypeRepos */
+ $accountTypeRepos = \App::make('FireflyIII\Database\AccountType');
+
+ $accountType = $accountTypeRepos->findByWhat('expense');
+
+ $data = ['user_id' => $this->getUser()->id, 'account_type_id' => $accountType->id, 'name' => $name, 'active' => 1];
+
+ return \Account::firstOrCreate($data);
+
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/AccountType.php b/app/lib/FireflyIII/Database/AccountType.php
index 5d101c1653..0c5341d1c5 100644
--- a/app/lib/FireflyIII/Database/AccountType.php
+++ b/app/lib/FireflyIII/Database/AccountType.php
@@ -3,12 +3,12 @@
namespace FireflyIII\Database;
use Firefly\Exception\FireflyException;
-use FireflyIII\Exception\NotImplementedException;
-use Illuminate\Support\Collection;
-use LaravelBook\Ardent\Ardent;
use FireflyIII\Database\Ifaces\AccountTypeInterface;
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
+use FireflyIII\Exception\NotImplementedException;
+use Illuminate\Support\Collection;
+use LaravelBook\Ardent\Ardent;
/**
* Class AccountType
@@ -19,58 +19,36 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls
{
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue
- *
- * @param $what
+ * @param Ardent $model
*
- * @return \AccountType|null
- * @throws FireflyException
+ * @return bool
*/
- public function findByWhat($what)
+ public function destroy(Ardent $model)
{
- switch ($what) {
- case 'expense':
- return \AccountType::whereType('Expense account')->first();
- break;
- case 'asset':
- return \AccountType::whereType('Asset account')->first();
- break;
- case 'revenue':
- return \AccountType::whereType('Revenue account')->first();
- break;
- case 'initial':
- return \AccountType::whereType('Initial balance account')->first();
- break;
- default:
- throw new FireflyException('Cannot find account type described as "' . e($what) . '".');
- break;
-
- }
- return null;
+ // TODO: Implement destroy() method.
+ throw new NotImplementedException;
}
/**
- * @param Ardent $model
+ * @param array $data
*
- * @return bool
+ * @return Ardent
*/
- public function destroy(Ardent $model)
+ public function store(array $data)
{
- // TODO: Implement destroy() method.
+ // TODO: Implement store() method.
throw new NotImplementedException;
}
/**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
- *
* @param Ardent $model
+ * @param array $data
*
- * @return array
+ * @return bool
*/
- public function validateObject(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- // TODO: Implement validateObject() method.
+ // TODO: Implement update() method.
throw new NotImplementedException;
}
@@ -89,25 +67,16 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls
}
/**
- * @param array $data
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return Ardent
- */
- public function store(array $data)
- {
- // TODO: Implement store() method.
- throw new NotImplementedException;
- }
-
- /**
* @param Ardent $model
- * @param array $data
*
- * @return bool
+ * @return array
*/
- public function update(Ardent $model, array $data)
+ public function validateObject(Ardent $model)
{
- // TODO: Implement update() method.
+ // TODO: Implement validateObject() method.
throw new NotImplementedException;
}
@@ -124,6 +93,38 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls
throw new NotImplementedException;
}
+ /**
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue
+ *
+ * @param $what
+ *
+ * @return \AccountType|null
+ * @throws FireflyException
+ */
+ public function findByWhat($what)
+ {
+ switch ($what) {
+ case 'expense':
+ return \AccountType::whereType('Expense account')->first();
+ break;
+ case 'asset':
+ return \AccountType::whereType('Asset account')->first();
+ break;
+ case 'revenue':
+ return \AccountType::whereType('Revenue account')->first();
+ break;
+ case 'initial':
+ return \AccountType::whereType('Initial balance account')->first();
+ break;
+ default:
+ throw new FireflyException('Cannot find account type described as "' . e($what) . '".');
+ break;
+
+ }
+
+ return null;
+ }
+
/**
* Returns all objects.
*
diff --git a/app/lib/FireflyIII/Database/Budget.php b/app/lib/FireflyIII/Database/Budget.php
index 0d0eda4be3..03aa341f7b 100644
--- a/app/lib/FireflyIII/Database/Budget.php
+++ b/app/lib/FireflyIII/Database/Budget.php
@@ -28,33 +28,58 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
}
/**
- * @param \Budget $budget
- * @param Carbon $date
+ * @param Ardent $model
*
- * @return \LimitRepetition|null
+ * @return bool
*/
- public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date)
+ public function destroy(Ardent $model)
{
- return \LimitRepetition::
- leftJoin('limits', 'limit_repetitions.limit_id', '=', 'limits.id')->leftJoin(
- 'components', 'limits.component_id', '=', 'components.id'
- )->where('limit_repetitions.startdate', $date->format('Y-m-d'))->where(
- 'components.id', $budget->id
- )->first(['limit_repetitions.*']);
+ $model->delete();
+
+ return true;
+ }
+
+ /**
+ * @param array $data
+ *
+ * @return Ardent
+ */
+ public function store(array $data)
+ {
+ $data['user_id'] = $this->getUser()->id;
+
+ $budget = new \Budget($data);
+ $budget->class = 'Budget';
+
+ if (!$budget->validate()) {
+ var_dump($budget->errors()->all());
+ exit;
+ }
+ $budget->save();
+
+ return $budget;
}
/**
* @param Ardent $model
+ * @param array $data
*
* @return bool
*/
- public function destroy(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- $model->delete();
+ $model->name = $data['name'];
+ if (!$model->validate()) {
+ var_dump($model->errors()->all());
+ exit;
+ }
+
+
+ $model->save();
+
return true;
}
-
/**
* Validates an array. Returns an array containing MessageBags
* errors/warnings/successes.
@@ -65,9 +90,9 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
*/
public function validate(array $model)
{
- $warnings = new MessageBag;
+ $warnings = new MessageBag;
$successes = new MessageBag;
- $errors = new MessageBag;
+ $errors = new MessageBag;
if (isset($model['name'])) {
if (strlen($model['name']) < 1) {
@@ -91,153 +116,121 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
$successes->add('name', 'OK');
}
- return [
- 'errors' => $errors,
- 'warnings' => $warnings,
- 'successes' => $successes
- ];
+ return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
/**
- * @param array $data
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return Ardent
- */
- public function store(array $data)
- {
- $data['user_id'] = $this->getUser()->id;
-
- $budget = new \Budget($data);
- $budget->class = 'Budget';
-
- if (!$budget->validate()) {
- var_dump($budget->errors()->all());
- exit;
- }
- $budget->save();
- return $budget;
- }
-
- /**
- * Returns all objects.
+ * @param Ardent $model
*
- * @return Collection
+ * @return array
*/
- public function get()
+ public function validateObject(Ardent $model)
{
- $budgets = $this->getUser()->budgets()->get();
-
- return $budgets;
+ // TODO: Implement validateObject() method.
+ throw new NotImplementedException;
}
/**
- * @param \Budget $budget
- * @param Carbon $date
+ * Returns an object with id $id.
*
- * @return float
+ * @param int $id
+ *
+ * @return Ardent
*/
- public function spentInMonth(\Budget $budget, Carbon $date)
+ public function find($id)
{
- $end = clone $date;
- $date->startOfMonth();
- $end->endOfMonth();
- $sum = floatval($budget->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1;
- return $sum;
+ // TODO: Implement find() method.
+ throw new NotImplementedException;
}
-
/**
- * @param Carbon $start
- * @param Carbon $end
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
- * @return Collection
+ * @param $what
+ *
+ * @return \AccountType|null
*/
- public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end)
+ public function findByWhat($what)
{
- // Add expenses that have no budget:
- return \Auth::user()->transactionjournals()->whereNotIn(
- 'transaction_journals.id', function ($query) use ($start, $end) {
- $query->select('transaction_journals.id')->from('transaction_journals')
- ->leftJoin(
- 'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=',
- 'transaction_journals.id'
- )
- ->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')
- ->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
- ->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
- ->where('components.class', 'Budget');
- }
- )->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->get();
+ // TODO: Implement findByWhat() method.
+ throw new NotImplementedException;
}
/**
- * @param Ardent $model
- * @param array $data
+ * Returns all objects.
*
- * @return bool
+ * @return Collection
*/
- public function update(Ardent $model, array $data)
+ public function get()
{
- $model->name = $data['name'];
- if (!$model->validate()) {
- var_dump($model->errors()->all());
- exit;
- }
-
-
- $model->save();
+ $budgets = $this->getUser()->budgets()->get();
- return true;
+ return $budgets;
}
/**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
- *
- * @param Ardent $model
+ * @param array $ids
*
- * @return array
+ * @return Collection
*/
- public function validateObject(Ardent $model)
+ public function getByIds(array $ids)
{
- // TODO: Implement validateObject() method.
+ // TODO: Implement getByIds() method.
throw new NotImplementedException;
}
/**
- * Returns an object with id $id.
- *
- * @param int $id
+ * @param \Budget $budget
+ * @param Carbon $date
*
- * @return Ardent
+ * @return \LimitRepetition|null
*/
- public function find($id)
+ public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date)
{
- // TODO: Implement find() method.
- throw new NotImplementedException;
+ return \LimitRepetition::
+ leftJoin('limits', 'limit_repetitions.limit_id', '=', 'limits.id')->leftJoin(
+ 'components', 'limits.component_id', '=', 'components.id'
+ )->where('limit_repetitions.startdate', $date->format('Y-m-d'))->where(
+ 'components.id', $budget->id
+ )->first(['limit_repetitions.*']);
}
/**
- * @param array $ids
+ * @param Carbon $start
+ * @param Carbon $end
*
* @return Collection
*/
- public function getByIds(array $ids)
+ public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end)
{
- // TODO: Implement getByIds() method.
- throw new NotImplementedException;
+ // Add expenses that have no budget:
+ return \Auth::user()->transactionjournals()->whereNotIn(
+ 'transaction_journals.id', function ($query) use ($start, $end) {
+ $query->select('transaction_journals.id')->from('transaction_journals')->leftJoin(
+ 'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
+ )->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')->where(
+ 'transaction_journals.date', '>=', $start->format('Y-m-d')
+ )->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->where('components.class', 'Budget');
+ }
+ )->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->get();
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
+ * @param \Budget $budget
+ * @param Carbon $date
*
- * @return \AccountType|null
+ * @return float
*/
- public function findByWhat($what)
+ public function spentInMonth(\Budget $budget, Carbon $date)
{
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
+ $end = clone $date;
+ $date->startOfMonth();
+ $end->endOfMonth();
+ $sum = floatval($budget->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1;
+
+ return $sum;
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Category.php b/app/lib/FireflyIII/Database/Category.php
index 5c378a2704..0e4085e439 100644
--- a/app/lib/FireflyIII/Database/Category.php
+++ b/app/lib/FireflyIII/Database/Category.php
@@ -35,9 +35,40 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface
public function destroy(Ardent $model)
{
$model->delete();
+
return true;
}
+ /**
+ * @param array $data
+ *
+ * @return Ardent
+ */
+ public function store(array $data)
+ {
+ // TODO: Implement store() method.
+ throw new NotImplementedException;
+ }
+
+ /**
+ * @param Ardent $model
+ * @param array $data
+ *
+ * @return bool
+ */
+ public function update(Ardent $model, array $data)
+ {
+ $model->name = $data['name'];
+ if (!$model->validate()) {
+ var_dump($model->errors()->all());
+ exit;
+ }
+
+
+ $model->save();
+
+ return true;
+ }
/**
* Validates an array. Returns an array containing MessageBags
@@ -49,9 +80,9 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface
*/
public function validate(array $model)
{
- $warnings = new MessageBag;
+ $warnings = new MessageBag;
$successes = new MessageBag;
- $errors = new MessageBag;
+ $errors = new MessageBag;
if (isset($model['name'])) {
if (strlen($model['name']) < 1) {
@@ -75,68 +106,7 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface
$successes->add('name', 'OK');
}
- return [
- 'errors' => $errors,
- 'warnings' => $warnings,
- 'successes' => $successes
- ];
- }
-
- /**
- * Returns all objects.
- *
- * @return Collection
- */
- public function get()
- {
- return $this->getUser()->categories()->orderBy('name', 'ASC')->get();
- }
-
-
- /**
- * @param \Category $budget
- * @param Carbon $date
- *
- * @return null
- */
- public function repetitionOnStartingOnDate(\Category $category, Carbon $date)
- {
- return null;
- }
-
- /**
- * @param \Category $category
- * @param Carbon $date
- *
- * @return float
- */
- public function spentInMonth(\Category $category, Carbon $date)
- {
- $end = clone $date;
- $date->startOfMonth();
- $end->endOfMonth();
- $sum = floatval($category->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1;
- return $sum;
- }
-
- /**
- * @param Ardent $model
- * @param array $data
- *
- * @return bool
- */
- public function update(Ardent $model, array $data)
- {
- $model->name = $data['name'];
- if (!$model->validate()) {
- var_dump($model->errors()->all());
- exit;
- }
-
-
- $model->save();
-
- return true;
+ return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
/**
@@ -154,29 +124,41 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface
}
/**
- * @param array $data
+ * Returns an object with id $id.
+ *
+ * @param int $id
*
* @return Ardent
*/
- public function store(array $data)
+ public function find($id)
{
- // TODO: Implement store() method.
+ // TODO: Implement find() method.
throw new NotImplementedException;
}
/**
- * Returns an object with id $id.
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
- * @param int $id
+ * @param $what
*
- * @return Ardent
+ * @return \AccountType|null
*/
- public function find($id)
+ public function findByWhat($what)
{
- // TODO: Implement find() method.
+ // TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
+ /**
+ * Returns all objects.
+ *
+ * @return Collection
+ */
+ public function get()
+ {
+ return $this->getUser()->categories()->orderBy('name', 'ASC')->get();
+ }
+
/**
* @param array $ids
*
@@ -189,15 +171,29 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ * @param \Category $budget
+ * @param Carbon $date
*
- * @param $what
+ * @return null
+ */
+ public function repetitionOnStartingOnDate(\Category $category, Carbon $date)
+ {
+ return null;
+ }
+
+ /**
+ * @param \Category $category
+ * @param Carbon $date
*
- * @return \AccountType|null
+ * @return float
*/
- public function findByWhat($what)
+ public function spentInMonth(\Category $category, Carbon $date)
{
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
+ $end = clone $date;
+ $date->startOfMonth();
+ $end->endOfMonth();
+ $sum = floatval($category->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1;
+
+ return $sum;
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php b/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php
index a24fa07421..2871e988f7 100644
--- a/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php
+++ b/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php
@@ -13,13 +13,13 @@ interface AccountInterface
{
/**
- * Get all asset accounts. The parameters are optional and are provided by the DataTables plugin.
+ * Counts the number of accounts found with the included types.
*
- * @param array $parameters
+ * @param array $types
*
- * @return Collection
+ * @return int
*/
- public function getAssetAccounts(array $parameters = []);
+ public function countAccountsByType(array $types);
/**
* Counts the number of total asset accounts. Useful for DataTables.
@@ -55,42 +55,42 @@ interface AccountInterface
*/
public function findInitialBalanceAccount(\Account $account);
- public function getExpenseAccounts(array $parameters = []);
-
/**
- * Get all revenue accounts.
+ * Get all accounts of the selected types. Is also capable of handling DataTables' parameters.
*
+ * @param array $types
* @param array $parameters
*
* @return Collection
*/
- public function getRevenueAccounts(array $parameters = []);
+ public function getAccountsByType(array $types, array $parameters = []);
/**
- * Get all accounts of the selected types. Is also capable of handling DataTables' parameters.
+ * Get all asset accounts. The parameters are optional and are provided by the DataTables plugin.
*
- * @param array $types
* @param array $parameters
*
* @return Collection
*/
- public function getAccountsByType(array $types, array $parameters = []);
+ public function getAssetAccounts(array $parameters = []);
/**
- * Counts the number of accounts found with the included types.
- *
- * @param array $types
+ * Get all default accounts.
*
- * @return int
+ * @return Collection
*/
- public function countAccountsByType(array $types);
+ public function getDefaultAccounts();
+
+ public function getExpenseAccounts(array $parameters = []);
/**
- * Get all default accounts.
+ * Get all revenue accounts.
+ *
+ * @param array $parameters
*
* @return Collection
*/
- public function getDefaultAccounts();
+ public function getRevenueAccounts(array $parameters = []);
/**
* @param \Account $account
@@ -101,7 +101,7 @@ interface AccountInterface
/**
* @param \Account $account
- * @param array $data
+ * @param array $data
*
* @return bool
*/
diff --git a/app/lib/FireflyIII/Database/Ifaces/BudgetInterface.php b/app/lib/FireflyIII/Database/Ifaces/BudgetInterface.php
index 17fd25dbe9..149970cbc2 100644
--- a/app/lib/FireflyIII/Database/Ifaces/BudgetInterface.php
+++ b/app/lib/FireflyIII/Database/Ifaces/BudgetInterface.php
@@ -14,7 +14,7 @@ interface BudgetInterface
{
/**
* @param \Budget $budget
- * @param Carbon $date
+ * @param Carbon $date
*
* @return \LimitRepetition|null
*/
diff --git a/app/lib/FireflyIII/Database/Ifaces/CUD.php b/app/lib/FireflyIII/Database/Ifaces/CUD.php
index 3c457f8629..70578007e7 100644
--- a/app/lib/FireflyIII/Database/Ifaces/CUD.php
+++ b/app/lib/FireflyIII/Database/Ifaces/CUD.php
@@ -1,11 +1,12 @@
<?php
namespace FireflyIII\Database\Ifaces;
-use Illuminate\Support\MessageBag;
+
use LaravelBook\Ardent\Ardent;
/**
* Interface CUD
+ *
* @package FireflyIII\Database
*/
interface CUD
@@ -18,16 +19,20 @@ interface CUD
*/
public function destroy(Ardent $model);
-
/**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
+ * @param array $data
*
+ * @return Ardent
+ */
+ public function store(array $data);
+
+ /**
* @param Ardent $model
+ * @param array $data
*
- * @return array
+ * @return bool
*/
- public function validateObject(Ardent $model);
+ public function update(Ardent $model, array $data);
/**
* Validates an array. Returns an array containing MessageBags
@@ -40,18 +45,13 @@ interface CUD
public function validate(array $model);
/**
- * @param array $data
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return Ardent
- */
- public function store(array $data);
-
- /**
* @param Ardent $model
- * @param array $data
*
- * @return bool
+ * @return array
*/
- public function update(Ardent $model, array $data);
+ public function validateObject(Ardent $model);
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Ifaces/CategoryInterface.php b/app/lib/FireflyIII/Database/Ifaces/CategoryInterface.php
index 4c8dd0d3b0..1ba540f38f 100644
--- a/app/lib/FireflyIII/Database/Ifaces/CategoryInterface.php
+++ b/app/lib/FireflyIII/Database/Ifaces/CategoryInterface.php
@@ -2,9 +2,6 @@
namespace FireflyIII\Database\Ifaces;
-use Carbon\Carbon;
-use Illuminate\Support\Collection;
-
/**
* Interface CategoryInterface
*
diff --git a/app/lib/FireflyIII/Database/Ifaces/CommonDatabaseCalls.php b/app/lib/FireflyIII/Database/Ifaces/CommonDatabaseCalls.php
index fb3ba77adc..977e151c82 100644
--- a/app/lib/FireflyIII/Database/Ifaces/CommonDatabaseCalls.php
+++ b/app/lib/FireflyIII/Database/Ifaces/CommonDatabaseCalls.php
@@ -22,6 +22,15 @@ interface CommonDatabaseCalls
*/
public function find($id);
+ /**
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ *
+ * @param $what
+ *
+ * @return \AccountType|null
+ */
+ public function findByWhat($what);
+
/**
* Returns all objects.
*
@@ -29,7 +38,6 @@ interface CommonDatabaseCalls
*/
public function get();
-
/**
* @param array $ids
*
@@ -37,13 +45,4 @@ interface CommonDatabaseCalls
*/
public function getByIds(array $ids);
- /**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
- *
- * @return \AccountType|null
- */
- public function findByWhat($what);
-
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Ifaces/RecurringInterface.php b/app/lib/FireflyIII/Database/Ifaces/RecurringInterface.php
index 98619d7799..9f9184db52 100644
--- a/app/lib/FireflyIII/Database/Ifaces/RecurringInterface.php
+++ b/app/lib/FireflyIII/Database/Ifaces/RecurringInterface.php
@@ -1,6 +1,7 @@
<?php
namespace FireflyIII\Database\Ifaces;
+
use Carbon\Carbon;
/**
diff --git a/app/lib/FireflyIII/Database/Ifaces/RecurringTransactionInterface.php b/app/lib/FireflyIII/Database/Ifaces/RecurringTransactionInterface.php
index 1669902ab1..a0bb8f758b 100644
--- a/app/lib/FireflyIII/Database/Ifaces/RecurringTransactionInterface.php
+++ b/app/lib/FireflyIII/Database/Ifaces/RecurringTransactionInterface.php
@@ -2,8 +2,6 @@
namespace FireflyIII\Database\Ifaces;
-use Illuminate\Support\Collection;
-
/**
* Interface AccountInterface
*
diff --git a/app/lib/FireflyIII/Database/Ifaces/TransactionJournalInterface.php b/app/lib/FireflyIII/Database/Ifaces/TransactionJournalInterface.php
index e5085c7f19..94ed8ec016 100644
--- a/app/lib/FireflyIII/Database/Ifaces/TransactionJournalInterface.php
+++ b/app/lib/FireflyIII/Database/Ifaces/TransactionJournalInterface.php
@@ -1,6 +1,7 @@
<?php
namespace FireflyIII\Database\Ifaces;
+
use Carbon\Carbon;
use Illuminate\Support\Collection;
@@ -11,6 +12,13 @@ use Illuminate\Support\Collection;
*/
interface TransactionJournalInterface
{
+ /**
+ * Get the very first transaction journal.
+ *
+ * @return mixed
+ */
+ public function first();
+
/**
* @param Carbon $start
* @param Carbon $end
@@ -19,24 +27,18 @@ interface TransactionJournalInterface
*/
public function getInDateRange(Carbon $start, Carbon $end);
- /**
- * Get the very first transaction journal.
- * @return mixed
- */
- public function first();
-
/**
* @param Carbon $date
*
* @return float
*/
- public function getSumOfIncomesByMonth(Carbon $date);
+ public function getSumOfExpensesByMonth(Carbon $date);
/**
* @param Carbon $date
*
* @return float
*/
- public function getSumOfExpensesByMonth(Carbon $date);
+ public function getSumOfIncomesByMonth(Carbon $date);
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Piggybank.php b/app/lib/FireflyIII/Database/Piggybank.php
index 66575fcfb3..439d26f9de 100644
--- a/app/lib/FireflyIII/Database/Piggybank.php
+++ b/app/lib/FireflyIII/Database/Piggybank.php
@@ -28,32 +28,65 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
}
/**
- * @param \Account $account
+ * @param Ardent $model
*
- * @return float
+ * @return bool
*/
- public function leftOnAccount(\Account $account)
+ public function destroy(Ardent $model)
{
- $balance = $account->balance();
- /** @var \Piggybank $p */
- foreach ($account->piggybanks()->get() as $p) {
- $balance -= $p->currentRelevantRep()->currentamount;
- }
+ $model->delete();
+ }
- return $balance;
+ /**
+ * @param array $data
+ *
+ * @return Ardent
+ */
+ public function store(array $data)
+ {
+ $data['rep_every'] = isset($data['rep_every']) ? $data['rep_every'] : 0;
+ $data['reminder_skip'] = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
+ $data['order'] = isset($data['order']) ? $data['order'] : 0;
+ $data['remind_me'] = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
+ $data['startdate'] = isset($data['startdate']) ? $data['startdate'] : Carbon::now()->format('Y-m-d');
+ $data['targetdate'] = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
+
+ $piggybank = new \Piggybank($data);
+ if (!$piggybank->validate()) {
+ var_dump($piggybank->errors()->all());
+ exit;
+ }
+ $piggybank->save();
+ \Event::fire('piggybanks.store', [$piggybank]);
+ $piggybank->save();
}
/**
* @param Ardent $model
+ * @param array $data
*
* @return bool
*/
- public function destroy(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- $model->delete();
- }
+ /** @var \Piggybank $model */
+ $model->name = $data['name'];
+ $model->account_id = intval($data['account_id']);
+ $model->targetamount = floatval($data['targetamount']);
+ $model->targetdate = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
+ $model->rep_every = isset($data['rep_every']) ? $data['rep_every'] : 0;
+ $model->reminder_skip = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
+ $model->order = isset($data['order']) ? $data['order'] : 0;
+ $model->remind_me = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
+ if (!$model->validate()) {
+ var_dump($model->errors());
+ exit();
+ }
+ $model->save();
+ return true;
+ }
/**
* Validates an array. Returns an array containing MessageBags
@@ -65,9 +98,9 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
*/
public function validate(array $model)
{
- $warnings = new MessageBag;
+ $warnings = new MessageBag;
$successes = new MessageBag;
- $errors = new MessageBag;
+ $errors = new MessageBag;
/*
* Name validation:
@@ -104,7 +137,7 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
}
// check period.
if (!$errors->has('reminder') && !$errors->has('targetdate') && isset($model['remind_me']) && intval($model['remind_me']) == 1) {
- $today = new Carbon;
+ $today = new Carbon;
$target = new Carbon($model['targetdate']);
switch ($model['reminder']) {
case 'week':
@@ -135,72 +168,7 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
}
}
- return [
- 'errors' => $errors,
- 'warnings' => $warnings,
- 'successes' => $successes
- ];
- }
-
- /**
- * @param array $data
- *
- * @return Ardent
- */
- public function store(array $data)
- {
- $data['rep_every'] = isset($data['rep_every']) ? $data['rep_every'] : 0;
- $data['reminder_skip'] = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
- $data['order'] = isset($data['order']) ? $data['order'] : 0;
- $data['remind_me'] = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
- $data['startdate'] = isset($data['startdate']) ? $data['startdate'] : Carbon::now()->format('Y-m-d');
- $data['targetdate'] = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
-
-
- $piggybank = new \Piggybank($data);
- if (!$piggybank->validate()) {
- var_dump($piggybank->errors()->all());
- exit;
- }
- $piggybank->save();
- \Event::fire('piggybanks.store', [$piggybank]);
- $piggybank->save();
- }
-
-
- /**
- * Returns all objects.
- *
- * @return Collection
- */
- public function get()
- {
- return $this->getUser()->piggybanks()->where('repeats', 0)->get();
- }
-
- /**
- * @param Ardent $model
- * @param array $data
- *
- * @return bool
- */
- public function update(Ardent $model, array $data)
- {
- /** @var \Piggybank $model */
- $model->name = $data['name'];
- $model->account_id = intval($data['account_id']);
- $model->targetamount = floatval($data['targetamount']);
- $model->targetdate = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
- $model->rep_every = isset($data['rep_every']) ? $data['rep_every'] : 0;
- $model->reminder_skip = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
- $model->order = isset($data['order']) ? $data['order'] : 0;
- $model->remind_me = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
- if (!$model->validate()) {
- var_dump($model->errors());
- exit();
- }
- $model->save();
- return true;
+ return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
/**
@@ -230,6 +198,29 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
throw new NotImplementedException;
}
+ /**
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ *
+ * @param $what
+ *
+ * @return \AccountType|null
+ */
+ public function findByWhat($what)
+ {
+ // TODO: Implement findByWhat() method.
+ throw new NotImplementedException;
+ }
+
+ /**
+ * Returns all objects.
+ *
+ * @return Collection
+ */
+ public function get()
+ {
+ return $this->getUser()->piggybanks()->where('repeats', 0)->get();
+ }
+
/**
* @param array $ids
*
@@ -242,15 +233,19 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
+ * @param \Account $account
*
- * @return \AccountType|null
+ * @return float
*/
- public function findByWhat($what)
+ public function leftOnAccount(\Account $account)
{
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
+ $balance = $account->balance();
+ /** @var \Piggybank $p */
+ foreach ($account->piggybanks()->get() as $p) {
+ $balance -= $p->currentRelevantRep()->currentamount;
+ }
+
+ return $balance;
+
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Recurring.php b/app/lib/FireflyIII/Database/Recurring.php
index 410de6a3a6..d7d4bd4de3 100644
--- a/app/lib/FireflyIII/Database/Recurring.php
+++ b/app/lib/FireflyIII/Database/Recurring.php
@@ -4,12 +4,12 @@ namespace FireflyIII\Database;
use Carbon\Carbon;
-use FireflyIII\Exception\NotImplementedException;
-use Illuminate\Support\Collection;
-use LaravelBook\Ardent\Ardent;
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
use FireflyIII\Database\Ifaces\RecurringInterface;
+use FireflyIII\Exception\NotImplementedException;
+use Illuminate\Support\Collection;
+use LaravelBook\Ardent\Ardent;
/**
* Class Recurring
@@ -29,102 +29,101 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
}
/**
- * @param \RecurringTransaction $recurring
- * @param Carbon $start
- * @param Carbon $end
+ * @param Ardent $model
*
- * @return \TransactionJournal|null
+ * @return bool
*/
- public function getJournalForRecurringInRange(\RecurringTransaction $recurring, Carbon $start, Carbon $end)
+ public function destroy(Ardent $model)
{
- return $this->getUser()->transactionjournals()->where('recurring_transaction_id', $recurring->id)->after($start)->before($end)->first();
-
+ // TODO: Implement destroy() method.
+ throw new NotImplementedException;
}
-
/**
- * Returns all objects.
+ * @param array $data
*
- * @return Collection
+ * @return Ardent
*/
- public function get()
+ public function store(array $data)
{
- return $this->getUser()->recurringtransactions()->get();
+ // TODO: Implement store() method.
+ throw new NotImplementedException;
}
/**
* @param Ardent $model
+ * @param array $data
*
* @return bool
*/
- public function destroy(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- // TODO: Implement destroy() method.
+ // TODO: Implement update() method.
throw new NotImplementedException;
}
/**
- * Validates a model. Returns an array containing MessageBags
+ * Validates an array. Returns an array containing MessageBags
* errors/warnings/successes.
*
- * @param Ardent $model
+ * @param array $model
*
* @return array
*/
- public function validateObject(Ardent $model)
+ public function validate(array $model)
{
- // TODO: Implement validateObject() method.
+ // TODO: Implement validate() method.
throw new NotImplementedException;
}
/**
- * Validates an array. Returns an array containing MessageBags
+ * Validates a model. Returns an array containing MessageBags
* errors/warnings/successes.
*
- * @param array $model
+ * @param Ardent $model
*
* @return array
*/
- public function validate(array $model)
+ public function validateObject(Ardent $model)
{
- // TODO: Implement validate() method.
+ // TODO: Implement validateObject() method.
throw new NotImplementedException;
}
/**
- * @param array $data
+ * Returns an object with id $id.
+ *
+ * @param int $id
*
* @return Ardent
*/
- public function store(array $data)
+ public function find($id)
{
- // TODO: Implement store() method.
+ // TODO: Implement find() method.
throw new NotImplementedException;
}
/**
- * @param Ardent $model
- * @param array $data
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
- * @return bool
+ * @param $what
+ *
+ * @return \AccountType|null
*/
- public function update(Ardent $model, array $data)
+ public function findByWhat($what)
{
- // TODO: Implement update() method.
+ // TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
/**
- * Returns an object with id $id.
- *
- * @param int $id
+ * Returns all objects.
*
- * @return Ardent
+ * @return Collection
*/
- public function find($id)
+ public function get()
{
- // TODO: Implement find() method.
- throw new NotImplementedException;
+ return $this->getUser()->recurringtransactions()->get();
}
/**
@@ -139,15 +138,15 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
+ * @param \RecurringTransaction $recurring
+ * @param Carbon $start
+ * @param Carbon $end
*
- * @return \AccountType|null
+ * @return \TransactionJournal|null
*/
- public function findByWhat($what)
+ public function getJournalForRecurringInRange(\RecurringTransaction $recurring, Carbon $start, Carbon $end)
{
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
+ return $this->getUser()->transactionjournals()->where('recurring_transaction_id', $recurring->id)->after($start)->before($end)->first();
+
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/RecurringTransaction.php b/app/lib/FireflyIII/Database/RecurringTransaction.php
index 6eb8cac8f1..b4e80c489d 100644
--- a/app/lib/FireflyIII/Database/RecurringTransaction.php
+++ b/app/lib/FireflyIII/Database/RecurringTransaction.php
@@ -26,39 +26,37 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac
$this->setUser(\Auth::user());
}
-
/**
- * Returns all objects.
+ * @param Ardent $model
*
- * @return Collection
+ * @return bool
*/
- public function get()
+ public function destroy(Ardent $model)
{
- return $this->getUser()->recurringtransactions()->get();
+ // TODO: Implement destroy() method.
+ throw new NotImplementedException;
}
/**
- * @param Ardent $model
+ * @param array $data
*
- * @return bool
+ * @return Ardent
*/
- public function destroy(Ardent $model)
+ public function store(array $data)
{
- // TODO: Implement destroy() method.
+ // TODO: Implement store() method.
throw new NotImplementedException;
}
/**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
- *
* @param Ardent $model
+ * @param array $data
*
- * @return array
+ * @return bool
*/
- public function validateObject(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- // TODO: Implement validateObject() method.
+ // TODO: Implement update() method.
throw new NotImplementedException;
}
@@ -77,25 +75,16 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac
}
/**
- * @param array $data
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return Ardent
- */
- public function store(array $data)
- {
- // TODO: Implement store() method.
- throw new NotImplementedException;
- }
-
- /**
* @param Ardent $model
- * @param array $data
*
- * @return bool
+ * @return array
*/
- public function update(Ardent $model, array $data)
+ public function validateObject(Ardent $model)
{
- // TODO: Implement update() method.
+ // TODO: Implement validateObject() method.
throw new NotImplementedException;
}
@@ -113,26 +102,36 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac
}
/**
- * @param array $ids
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
- * @return Collection
+ * @param $what
+ *
+ * @return \AccountType|null
*/
- public function getByIds(array $ids)
+ public function findByWhat($what)
{
- // TODO: Implement getByIds() method.
+ // TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ * Returns all objects.
*
- * @param $what
+ * @return Collection
+ */
+ public function get()
+ {
+ return $this->getUser()->recurringtransactions()->get();
+ }
+
+ /**
+ * @param array $ids
*
- * @return \AccountType|null
+ * @return Collection
*/
- public function findByWhat($what)
+ public function getByIds(array $ids)
{
- // TODO: Implement findByWhat() method.
+ // TODO: Implement getByIds() method.
throw new NotImplementedException;
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Transaction.php b/app/lib/FireflyIII/Database/Transaction.php
index 09d88c00c5..26a3d252d5 100644
--- a/app/lib/FireflyIII/Database/Transaction.php
+++ b/app/lib/FireflyIII/Database/Transaction.php
@@ -20,6 +20,55 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls
{
use SwitchUser;
+ /**
+ * @param Ardent $model
+ *
+ * @return bool
+ */
+ public function destroy(Ardent $model)
+ {
+ // TODO: Implement destroy() method.
+ throw new NotImplementedException;
+ }
+
+ /**
+ * @param array $data
+ *
+ * @return Ardent
+ */
+ public function store(array $data)
+ {
+ $transaction = new \Transaction;
+ $transaction->account()->associate($data['account']);
+ $transaction->transactionJournal()->associate($data['transaction_journal']);
+ $transaction->amount = floatval($data['amount']);
+ if (isset($data['piggybank'])) {
+ $transaction->piggybank()->associate($data['piggybank']);
+ }
+ if (isset($data['description'])) {
+ $transaction->description = $data['description'];
+ }
+ if ($transaction->validate()) {
+ $transaction->save();
+ } else {
+ throw new FireflyException($transaction->errors()->first());
+ }
+
+ return $transaction;
+ }
+
+ /**
+ * @param Ardent $model
+ * @param array $data
+ *
+ * @return bool
+ */
+ public function update(Ardent $model, array $data)
+ {
+ // TODO: Implement update() method.
+ throw new NotImplementedException;
+ }
+
/**
* Validates an array. Returns an array containing MessageBags
* errors/warnings/successes.
@@ -30,9 +79,9 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls
*/
public function validate(array $model)
{
- $warnings = new MessageBag;
+ $warnings = new MessageBag;
$successes = new MessageBag;
- $errors = new MessageBag;
+ $errors = new MessageBag;
if (!isset($model['account_id']) && !isset($model['account'])) {
@@ -83,47 +132,7 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls
$successes->add('amount', 'OK');
}
- return [
- 'errors' => $errors,
- 'warnings' => $warnings,
- 'successes' => $successes
- ];
- }
-
- /**
- * @param array $data
- *
- * @return Ardent
- */
- public function store(array $data)
- {
- $transaction = new \Transaction;
- $transaction->account()->associate($data['account']);
- $transaction->transactionJournal()->associate($data['transaction_journal']);
- $transaction->amount = floatval($data['amount']);
- if (isset($data['piggybank'])) {
- $transaction->piggybank()->associate($data['piggybank']);
- }
- if (isset($data['description'])) {
- $transaction->description = $data['description'];
- }
- if ($transaction->validate()) {
- $transaction->save();
- } else {
- throw new FireflyException($transaction->errors()->first());
- }
- return $transaction;
- }
-
- /**
- * @param Ardent $model
- *
- * @return bool
- */
- public function destroy(Ardent $model)
- {
- // TODO: Implement destroy() method.
- throw new NotImplementedException;
+ return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
/**
@@ -141,27 +150,28 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls
}
/**
- * @param Ardent $model
- * @param array $data
+ * Returns an object with id $id.
*
- * @return bool
+ * @param int $id
+ *
+ * @return Ardent
*/
- public function update(Ardent $model, array $data)
+ public function find($id)
{
- // TODO: Implement update() method.
+ // TODO: Implement find() method.
throw new NotImplementedException;
}
/**
- * Returns an object with id $id.
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
*
- * @param int $id
+ * @param $what
*
- * @return Ardent
+ * @return \AccountType|null
*/
- public function find($id)
+ public function findByWhat($what)
{
- // TODO: Implement find() method.
+ // TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
@@ -186,17 +196,4 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls
// TODO: Implement getByIds() method.
throw new NotImplementedException;
}
-
- /**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
- *
- * @return \AccountType|null
- */
- public function findByWhat($what)
- {
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
- }
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/TransactionCurrency.php b/app/lib/FireflyIII/Database/TransactionCurrency.php
index 91cb15a869..82a4639345 100644
--- a/app/lib/FireflyIII/Database/TransactionCurrency.php
+++ b/app/lib/FireflyIII/Database/TransactionCurrency.php
@@ -3,12 +3,12 @@
namespace FireflyIII\Database;
-use FireflyIII\Exception\NotImplementedException;
-use Illuminate\Support\Collection;
-use LaravelBook\Ardent\Ardent;
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
use FireflyIII\Database\Ifaces\TransactionCurrencyInterface;
+use FireflyIII\Exception\NotImplementedException;
+use Illuminate\Support\Collection;
+use LaravelBook\Ardent\Ardent;
/**
* Class TransactionType
@@ -20,37 +20,36 @@ class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDa
/**
- * @param string $code
+ * @param Ardent $model
*
- * @return \TransactionCurrency|null
+ * @return bool
*/
- public function findByCode($code)
+ public function destroy(Ardent $model)
{
- return \TransactionCurrency::whereCode($code)->first();
+ // TODO: Implement destroy() method.
+ throw new NotImplementedException;
}
/**
- * @param Ardent $model
+ * @param array $data
*
- * @return bool
+ * @return Ardent
*/
- public function destroy(Ardent $model)
+ public function store(array $data)
{
- // TODO: Implement destroy() method.
+ // TODO: Implement store() method.
throw new NotImplementedException;
}
/**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
- *
* @param Ardent $model
+ * @param array $data
*
- * @return array
+ * @return bool
*/
- public function validateObject(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- // TODO: Implement validateObject() method.
+ // TODO: Implement update() method.
throw new NotImplementedException;
}
@@ -69,25 +68,16 @@ class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDa
}
/**
- * @param array $data
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return Ardent
- */
- public function store(array $data)
- {
- // TODO: Implement store() method.
- throw new NotImplementedException;
- }
-
- /**
* @param Ardent $model
- * @param array $data
*
- * @return bool
+ * @return array
*/
- public function update(Ardent $model, array $data)
+ public function validateObject(Ardent $model)
{
- // TODO: Implement update() method.
+ // TODO: Implement validateObject() method.
throw new NotImplementedException;
}
@@ -104,6 +94,19 @@ class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDa
throw new NotImplementedException;
}
+ /**
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ *
+ * @param $what
+ *
+ * @return \AccountType|null
+ */
+ public function findByWhat($what)
+ {
+ // TODO: Implement findByWhat() method.
+ throw new NotImplementedException;
+ }
+
/**
* Returns all objects.
*
@@ -127,15 +130,12 @@ class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDa
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
+ * @param string $code
*
- * @return \AccountType|null
+ * @return \TransactionCurrency|null
*/
- public function findByWhat($what)
+ public function findByCode($code)
{
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
+ return \TransactionCurrency::whereCode($code)->first();
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal.php
index c9291faf98..0c2430ae62 100644
--- a/app/lib/FireflyIII/Database/TransactionJournal.php
+++ b/app/lib/FireflyIII/Database/TransactionJournal.php
@@ -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');
- /**
- * @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();
- }
+ /** @var \FireflyIII\Database\Account $accountRepository */
+ $accountRepository = \App::make('FireflyIII\Database\Account');
- /**
- * @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)
- {
+ /** @var \FireflyIII\Database\TransactionCurrency $currencyRepository */
+ $currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency');
- $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.*']);
+ /** @var \FireflyIII\Database\Transaction $transactionRepository */
+ $transactionRepository = \App::make('FireflyIII\Database\Transaction');
- return $query;
+ $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;
}
/**
- * @return TransactionJournal
+ * @param Ardent $model
+ * @param array $data
+ *
+ * @return bool
*/
- public function first()
+ public function update(Ardent $model, array $data)
{
- return $this->getUser()->transactionjournals()->orderBy('date', 'ASC')->first();
+ // TODO: Implement update() method.
+ throw new NotImplementedException;
}
-
/**
* 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,103 +293,24 @@ 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
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return Ardent
+ * @param Ardent $model
+ *
+ * @return array
*/
- public function store(array $data)
+ public function validateObject(Ardent $model)
{
-
- /** @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;
+ // TODO: Implement validateObject() method.
+ throw new NotImplementedException;
}
/**
@@ -388,6 +325,19 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
return $this->getUser()->transactionjournals()->find($id);
}
+ /**
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ *
+ * @param $what
+ *
+ * @return \AccountType|null
+ */
+ public function findByWhat($what)
+ {
+ // TODO: Implement findByWhat() method.
+ throw new NotImplementedException;
+ }
+
/**
* Returns all objects.
*
@@ -399,94 +349,125 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
}
/**
- * Some objects.
+ * @param array $ids
*
* @return Collection
*/
- public function getTransfers()
+ public function getByIds(array $ids)
{
- return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Transfer'])->get(['transaction_journals.*']);
+ // TODO: Implement getByIds() method.
+ throw new NotImplementedException;
}
/**
- * Some objects.
- *
- * @return Collection
+ * @return TransactionJournal
*/
- public function getDeposits()
+ public function first()
{
- return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Deposit'])->get(['transaction_journals.*']);
+ return $this->getUser()->transactionjournals()->orderBy('date', 'ASC')->first();
}
/**
- * Some objects.
+ * @param Carbon $start
+ * @param Carbon $end
*
* @return Collection
*/
- public function getWithdrawals()
+ public function getInDateRange(Carbon $start, Carbon $end)
{
- return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Withdrawal'])->get(['transaction_journals.*']);
+ return $this->getuser()->transactionjournals()->withRelevantData()->before($end)->after($start)->get();
}
-
/**
- * @param Ardent $model
+ * @param Carbon $date
*
- * @return bool
+ * @return float
*/
- public function destroy(Ardent $model)
+ public function getSumOfExpensesByMonth(Carbon $date)
{
- // TODO: Implement destroy() method.
- throw new NotImplementedException;
+ $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;
}
/**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
- *
- * @param Ardent $model
+ * @param Carbon $date
*
- * @return array
+ * @return float
*/
- public function validateObject(Ardent $model)
+ public function getSumOfIncomesByMonth(Carbon $date)
{
- // TODO: Implement validateObject() method.
- throw new NotImplementedException;
+ $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;
}
/**
- * @param Ardent $model
- * @param array $data
+ * Some objects.
*
- * @return bool
+ * @return Collection
*/
- public function update(Ardent $model, array $data)
+ public function getDeposits()
{
- // TODO: Implement update() method.
- throw new NotImplementedException;
+ return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Deposit'])->get(['transaction_journals.*']);
}
/**
- * @param array $ids
+ * @param \Account $account
+ * @param int $count
+ * @param Carbon $start
+ * @param Carbon $end
*
* @return Collection
*/
- public function getByIds(array $ids)
+ public function getInDateRangeAccount(\Account $account, $count = 20, Carbon $start, Carbon $end)
{
- // TODO: Implement getByIds() method.
- throw new NotImplementedException;
+
+ $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;
}
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ * Some objects.
*
- * @param $what
+ * @return Collection
+ */
+ public function getTransfers()
+ {
+ return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Transfer'])->get(['transaction_journals.*']);
+ }
+
+ /**
+ * Some objects.
*
- * @return \AccountType|null
+ * @return Collection
*/
- public function findByWhat($what)
+ public function getWithdrawals()
{
- // TODO: Implement findByWhat() method.
- throw new NotImplementedException;
+ return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Withdrawal'])->get(['transaction_journals.*']);
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/TransactionType.php b/app/lib/FireflyIII/Database/TransactionType.php
index c6cc1c59dd..fea091ae62 100644
--- a/app/lib/FireflyIII/Database/TransactionType.php
+++ b/app/lib/FireflyIII/Database/TransactionType.php
@@ -20,55 +20,36 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa
{
/**
- * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
- *
- * @param $what
+ * @param Ardent $model
*
- * @return \AccountType|null
+ * @return bool
*/
- public function findByWhat($what)
+ public function destroy(Ardent $model)
{
- switch ($what) {
- case 'opening':
- return \TransactionType::whereType('Opening balance')->first();
- break;
- case 'transfer':
- return \TransactionType::whereType('Transfer')->first();
- break;
- case 'withdrawal':
- return \TransactionType::whereType('Withdrawal')->first();
- break;
- default:
- throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".');
- break;
-
-
- }
- return null;
+ // TODO: Implement destroy() method.
+ throw new NotImplementedException;
}
/**
- * @param Ardent $model
+ * @param array $data
*
- * @return bool
+ * @return Ardent
*/
- public function destroy(Ardent $model)
+ public function store(array $data)
{
- // TODO: Implement destroy() method.
+ // TODO: Implement store() method.
throw new NotImplementedException;
}
/**
- * Validates a model. Returns an array containing MessageBags
- * errors/warnings/successes.
- *
* @param Ardent $model
+ * @param array $data
*
- * @return array
+ * @return bool
*/
- public function validateObject(Ardent $model)
+ public function update(Ardent $model, array $data)
{
- // TODO: Implement validateObject() method.
+ // TODO: Implement update() method.
throw new NotImplementedException;
}
@@ -87,25 +68,16 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa
}
/**
- * @param array $data
+ * Validates a model. Returns an array containing MessageBags
+ * errors/warnings/successes.
*
- * @return Ardent
- */
- public function store(array $data)
- {
- // TODO: Implement store() method.
- throw new NotImplementedException;
- }
-
- /**
* @param Ardent $model
- * @param array $data
*
- * @return bool
+ * @return array
*/
- public function update(Ardent $model, array $data)
+ public function validateObject(Ardent $model)
{
- // TODO: Implement update() method.
+ // TODO: Implement validateObject() method.
throw new NotImplementedException;
}
@@ -122,6 +94,35 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa
throw new NotImplementedException;
}
+ /**
+ * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
+ *
+ * @param $what
+ *
+ * @return \AccountType|null
+ */
+ public function findByWhat($what)
+ {
+ switch ($what) {
+ case 'opening':
+ return \TransactionType::whereType('Opening balance')->first();
+ break;
+ case 'transfer':
+ return \TransactionType::whereType('Transfer')->first();
+ break;
+ case 'withdrawal':
+ return \TransactionType::whereType('Withdrawal')->first();
+ break;
+ default:
+ throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".');
+ break;
+
+
+ }
+
+ return null;
+ }
+
/**
* Returns all objects.
*
diff --git a/app/lib/FireflyIII/Database/User.php b/app/lib/FireflyIII/Database/User.php
index 1796ab8468..4cb416e42a 100644
--- a/app/lib/FireflyIII/Database/User.php
+++ b/app/lib/FireflyIII/Database/User.php
@@ -11,6 +11,26 @@ namespace FireflyIII\Database;
class User
{
+ /**
+ * @param $mail
+ *
+ * @return null|User
+ */
+ public function findByEmail($mail)
+ {
+ return \User::where('email', $mail)->first();
+ }
+
+ /**
+ * @param $reset
+ *
+ * @return null|User
+ */
+ public function findByReset($reset)
+ {
+ return \User::where('reset', $reset)->first();
+ }
+
/**
* @param array $data
*
@@ -18,10 +38,10 @@ class User
*/
public function register(array $data)
{
- $user = new \User;
- $user->email = isset($data['email']) ? $data['email'] : null;
+ $user = new \User;
+ $user->email = isset($data['email']) ? $data['email'] : null;
$user->migrated = 0;
- $user->reset = \Str::random(32);
+ $user->reset = \Str::random(32);
$user->password = \Hash::make(\Str::random(12));
if (!$user->save()) {
@@ -50,24 +70,4 @@ class User
return true;
}
- /**
- * @param $mail
- *
- * @return null|User
- */
- public function findByEmail($mail)
- {
- return \User::where('email', $mail)->first();
- }
-
- /**
- * @param $reset
- *
- * @return null|User
- */
- public function findByReset($reset)
- {
- return \User::where('reset', $reset)->first();
- }
-
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Exception/ValidationException.php b/app/lib/FireflyIII/Exception/ValidationException.php
index 4cb09c9f5d..56116a870d 100644
--- a/app/lib/FireflyIII/Exception/ValidationException.php
+++ b/app/lib/FireflyIII/Exception/ValidationException.php
@@ -6,6 +6,7 @@ namespace FireflyIII\Exception;
*
* @package Firefly\Exception
*/
-class ValidationException extends \Exception {
+class ValidationException extends \Exception
+{
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Form/Form.php b/app/lib/FireflyIII/Form/Form.php
index ad7483cc07..171c0a7d96 100644
--- a/app/lib/FireflyIII/Form/Form.php
+++ b/app/lib/FireflyIII/Form/Form.php
@@ -14,36 +14,6 @@ use Illuminate\Support\MessageBag;
class Form
{
- /**
- * @param $name
- * @param null $value
- * @param array $options
- *
- * @return string
- * @throws FireflyException
- */
- public static function ffInteger($name, $value = null, array $options = [])
- {
- $options['step'] = '1';
- return self::ffInput('number', $name, $value, $options);
-
- }
-
- /**
- * @param $name
- * @param int $value
- * @param null $checked
- * @param array $options
- *
- * @return string
- * @throws FireflyException
- */
- public static function ffCheckbox($name, $value = 1, $checked = null, $options = [])
- {
- $options['checked'] = $checked ? true : null;
- return self::ffInput('checkbox', $name, $value, $options);
- }
-
/**
* @param $name
* @param null $value
@@ -56,6 +26,7 @@ class Form
{
$options['step'] = 'any';
$options['min'] = '0.01';
+
return self::ffInput('amount', $name, $value, $options);
}
@@ -71,49 +42,38 @@ class Form
public static function ffBalance($name, $value = null, array $options = [])
{
$options['step'] = 'any';
+
return self::ffInput('amount', $name, $value, $options);
}
/**
* @param $name
- * @param null $value
+ * @param int $value
+ * @param null $checked
* @param array $options
*
* @return string
* @throws FireflyException
*/
- public static function ffDate($name, $value = null, array $options = [])
+ public static function ffCheckbox($name, $value = 1, $checked = null, $options = [])
{
- return self::ffInput('date', $name, $value, $options);
- }
+ $options['checked'] = $checked ? true : null;
- /**
- * @param $name
- * @param null $value
- * @param array $options
- *
- * @return string
- * @throws FireflyException
- */
- public static function ffTags($name, $value = null, array $options = [])
- {
- $options['data-role'] = 'tagsinput';
- return self::ffInput('text', $name, $value, $options);
+ return self::ffInput('checkbox', $name, $value, $options);
}
/**
* @param $name
- * @param array $list
- * @param null $selected
+ * @param null $value
* @param array $options
*
* @return string
* @throws FireflyException
*/
- public static function ffSelect($name, array $list = [], $selected = null, array $options = [])
+ public static function ffDate($name, $value = null, array $options = [])
{
- return self::ffInput('select', $name, $selected, $options, $list);
+ return self::ffInput('date', $name, $value, $options);
}
/**
@@ -124,98 +84,12 @@ class Form
* @return string
* @throws FireflyException
*/
- public static function ffText($name, $value = null, array $options = array())
- {
- return self::ffInput('text', $name, $value, $options);
-
- }
-
- /**
- * @param $name
- * @param $options
- *
- * @return string
- */
- public static function label($name, $options)
- {
- if (isset($options['label'])) {
- return $options['label'];
- }
- $labels = [
- 'amount_min' => 'Amount (min)',
- 'amount_max' => 'Amount (max)',
- 'match' => 'Matches on',
- 'repeat_freq' => 'Repetition',
- 'account_from_id' => 'Account from',
- 'account_to_id' => 'Account to',
- 'account_id' => 'Asset account'
- ];
-
- return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name));
-
- }
-
- /**
- * Return buttons for update/validate/return.
- *
- * @param $type
- * @param $name
- */
- public static function ffOptionsList($type, $name)
+ public static function ffInteger($name, $value = null, array $options = [])
{
- $previousValue = \Input::old('post_submit_action');
- $previousValue = is_null($previousValue) ? 'store' : $previousValue;
- /*
- * Store.
- */
- $store = '';
- switch ($type) {
- case 'create':
- $store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
- $store .= '<div class="col-sm-8"><div class="radio"><label>';
- $store .= \Form::radio('post_submit_action', 'store', $previousValue == 'store');
- $store .= 'Store ' . $name . '</label></div></div></div>';
- break;
- case 'update':
- $store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
- $store .= '<div class="col-sm-8"><div class="radio"><label>';
- $store .= \Form::radio('post_submit_action', 'update', $previousValue == 'store');
- $store .= 'Update ' . $name . '</label></div></div></div>';
- break;
- default:
- throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type);
- break;
- }
+ $options['step'] = '1';
- /*
- * validate is always the same:
- */
- $validate = '<div class="form-group"><label for="validate_only" class="col-sm-4 control-label">Validate only';
- $validate .= '</label><div class="col-sm-8"><div class="radio"><label>';
- $validate .= \Form::radio('post_submit_action', 'validate_only', $previousValue == 'validate_only');
- $validate .= 'Only validate, do not save</label></div></div></div>';
+ return self::ffInput('number', $name, $value, $options);
- /*
- * Store & return:
- */
- switch ($type) {
- case 'create':
- $return = '<div class="form-group"><label for="return_to_form" class="col-sm-4 control-label">';
- $return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
- $return .= \Form::radio('post_submit_action', 'create_another', $previousValue == 'create_another');
- $return .= 'After storing, return here to create another one.</label></div></div></div>';
- break;
- case 'update':
- $return = '<div class="form-group"><label for="return_to_edit" class="col-sm-4 control-label">';
- $return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
- $return .= \Form::radio('post_submit_action', 'return_to_edit', $previousValue == 'return_to_edit');
- $return .= 'After updating, return here.</label></div></div></div>';
- break;
- default:
- throw new FireflyException('Cannot create ffOptionsList for option (store+return) ' . $type);
- break;
- }
- return $store . $validate . $return;
}
/**
@@ -228,7 +102,7 @@ class Form
* @return string
* @throws FireflyException
*/
- public static function ffInput($type, $name, $value = null, array $options = array(), $list = [])
+ public static function ffInput($type, $name, $value = null, array $options = [], $list = [])
{
/*
* add some defaults to this method:
@@ -376,4 +250,129 @@ class Form
return $html;
}
+
+ /**
+ * @param $name
+ * @param $options
+ *
+ * @return string
+ */
+ public static function label($name, $options)
+ {
+ if (isset($options['label'])) {
+ return $options['label'];
+ }
+ $labels = ['amount_min' => 'Amount (min)', 'amount_max' => 'Amount (max)', 'match' => 'Matches on', 'repeat_freq' => 'Repetition',
+ 'account_from_id' => 'Account from', 'account_to_id' => 'Account to', 'account_id' => 'Asset account'];
+
+ return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name));
+
+ }
+
+ /**
+ * Return buttons for update/validate/return.
+ *
+ * @param $type
+ * @param $name
+ */
+ public static function ffOptionsList($type, $name)
+ {
+ $previousValue = \Input::old('post_submit_action');
+ $previousValue = is_null($previousValue) ? 'store' : $previousValue;
+ /*
+ * Store.
+ */
+ $store = '';
+ switch ($type) {
+ case 'create':
+ $store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
+ $store .= '<div class="col-sm-8"><div class="radio"><label>';
+ $store .= \Form::radio('post_submit_action', 'store', $previousValue == 'store');
+ $store .= 'Store ' . $name . '</label></div></div></div>';
+ break;
+ case 'update':
+ $store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
+ $store .= '<div class="col-sm-8"><div class="radio"><label>';
+ $store .= \Form::radio('post_submit_action', 'update', $previousValue == 'store');
+ $store .= 'Update ' . $name . '</label></div></div></div>';
+ break;
+ default:
+ throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type);
+ break;
+ }
+
+ /*
+ * validate is always the same:
+ */
+ $validate = '<div class="form-group"><label for="validate_only" class="col-sm-4 control-label">Validate only';
+ $validate .= '</label><div class="col-sm-8"><div class="radio"><label>';
+ $validate .= \Form::radio('post_submit_action', 'validate_only', $previousValue == 'validate_only');
+ $validate .= 'Only validate, do not save</label></div></div></div>';
+
+ /*
+ * Store & return:
+ */
+ switch ($type) {
+ case 'create':
+ $return = '<div class="form-group"><label for="return_to_form" class="col-sm-4 control-label">';
+ $return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
+ $return .= \Form::radio('post_submit_action', 'create_another', $previousValue == 'create_another');
+ $return .= 'After storing, return here to create another one.</label></div></div></div>';
+ break;
+ case 'update':
+ $return = '<div class="form-group"><label for="return_to_edit" class="col-sm-4 control-label">';
+ $return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
+ $return .= \Form::radio('post_submit_action', 'return_to_edit', $previousValue == 'return_to_edit');
+ $return .= 'After updating, return here.</label></div></div></div>';
+ break;
+ default:
+ throw new FireflyException('Cannot create ffOptionsList for option (store+return) ' . $type);
+ break;
+ }
+
+ return $store . $validate . $return;
+ }
+
+ /**
+ * @param $name
+ * @param array $list
+ * @param null $selected
+ * @param array $options
+ *
+ * @return string
+ * @throws FireflyException
+ */
+ public static function ffSelect($name, array $list = [], $selected = null, array $options = [])
+ {
+ return self::ffInput('select', $name, $selected, $options, $list);
+ }
+
+ /**
+ * @param $name
+ * @param null $value
+ * @param array $options
+ *
+ * @return string
+ * @throws FireflyException
+ */
+ public static function ffTags($name, $value = null, array $options = [])
+ {
+ $options['data-role'] = 'tagsinput';
+
+ return self::ffInput('text', $name, $value, $options);
+ }
+
+ /**
+ * @param $name
+ * @param null $value
+ * @param array $options
+ *
+ * @return string
+ * @throws FireflyException
+ */
+ public static function ffText($name, $value = null, array $options = [])
+ {
+ return self::ffInput('text', $name, $value, $options);
+
+ }
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Json/Account.php b/app/lib/FireflyIII/Shared/Json/Account.php
index 120ed6fad8..28724e732f 100644
--- a/app/lib/FireflyIII/Shared/Json/Account.php
+++ b/app/lib/FireflyIII/Shared/Json/Account.php
@@ -7,6 +7,7 @@ namespace FireflyIII\Shared\Json;
*
* @package FireflyIII\Shared\Json
*/
-class Account {
+class Account
+{
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Json/Json.php b/app/lib/FireflyIII/Shared/Json/Json.php
index 0a0cbc7730..2b0956de80 100644
--- a/app/lib/FireflyIII/Shared/Json/Json.php
+++ b/app/lib/FireflyIII/Shared/Json/Json.php
@@ -4,6 +4,7 @@ namespace FireflyIII\Shared\Json;
/**
* Class Json
+ *
* @package FireflyIII\Shared\Json
*/
class Json
@@ -24,11 +25,7 @@ class Json
} else {
$length = intval(\Input::get('length'));
}
- $parameters = [
- 'start' => intval(\Input::get('start')),
- 'length' => $length,
- 'draw' => intval(\Input::get('draw')),
- ];
+ $parameters = ['start' => intval(\Input::get('start')), 'length' => $length, 'draw' => intval(\Input::get('draw')),];
/*
@@ -36,16 +33,12 @@ class Json
*/
if (!is_null(\Input::get('columns')) && is_array(\Input::get('columns'))) {
foreach (\Input::get('columns') as $column) {
- $parameters['columns'][] = [
- 'data' => $column['data'],
- 'name' => $column['name'],
- 'searchable' => $column['searchable'] == 'true' ? true : false,
- 'orderable' => $column['orderable'] == 'true' ? true : false,
- 'search' => [
- 'value' => $column['search']['value'],
- 'regex' => $column['search']['regex'] == 'true' ? true : false,
- ]
- ];
+ $parameters['columns'][] = ['data' => $column['data'], 'name' => $column['name'],
+ 'searchable' => $column['searchable'] == 'true' ? true : false,
+ 'orderable' => $column['orderable'] == 'true' ? true : false, 'search' => ['value' => $column['search']['value'],
+ 'regex' =>
+ $column['search']['regex'] == 'true'
+ ? true : false,]];
}
}
@@ -58,10 +51,7 @@ class Json
foreach (\Input::get('order') as $order) {
$columnIndex = intval($order['column']);
$columnName = $parameters['columns'][$columnIndex]['name'];
- $parameters['order'][] = [
- 'name' => $columnName,
- 'dir' => strtoupper($order['dir'])
- ];
+ $parameters['order'][] = ['name' => $columnName, 'dir' => strtoupper($order['dir'])];
if ($columnName == 'to' || $columnName == 'from') {
$parameters['orderOnAccount'] = true;
}
@@ -70,17 +60,12 @@ class Json
/*
* Search parameters:
*/
- $parameters['search'] = [
- 'value' => '',
- 'regex' => false
- ];
+ $parameters['search'] = ['value' => '', 'regex' => false];
if (!is_null(\Input::get('search')) && is_array(\Input::get('search'))) {
$search = \Input::get('search');
- $parameters['search'] = [
- 'value' => $search['value'],
- 'regex' => $search['regex'] == 'true' ? true : false
- ];
+ $parameters['search'] = ['value' => $search['value'], 'regex' => $search['regex'] == 'true' ? true : false];
}
+
return $parameters;
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Mail/Registration.php b/app/lib/FireflyIII/Shared/Mail/Registration.php
index 922c67141e..3fafddbff7 100644
--- a/app/lib/FireflyIII/Shared/Mail/Registration.php
+++ b/app/lib/FireflyIII/Shared/Mail/Registration.php
@@ -13,18 +13,20 @@ class Registration implements RegistrationInterface
*
* @return mixed|void
*/
- public function sendVerificationMail(\User $user)
+ public function sendPasswordMail(\User $user)
{
- $reset = \Str::random(32);
- $user->reset = $reset;
+ $password = \Str::random(12);
+ $user->password = $password;
+ $user->reset = \Str::random(32); // new one.
$user->forceSave();
$email = $user->email;
- $data = ['reset' => $reset];
+
+ $data = ['password' => $password];
\Mail::send(
- ['emails.user.verify-html', 'emails.user.verify-text'], $data, function ($message) use ($email) {
- $message->to($email, $email)->subject('Verify your e-mail address.');
+ ['emails.user.register-html', 'emails.user.register-text'], $data, function ($message) use ($email) {
+ $message->to($email, $email)->subject('Welcome to Firefly!');
}
);
}
@@ -34,22 +36,21 @@ class Registration implements RegistrationInterface
*
* @return mixed|void
*/
- public function sendPasswordMail(\User $user)
+ public function sendResetVerification(\User $user)
{
-
- $password = \Str::random(12);
- $user->password = $password;
- $user->reset = \Str::random(32); // new one.
+ $reset = \Str::random(32);
+ $user->reset = $reset;
$user->forceSave();
$email = $user->email;
-
- $data = ['password' => $password];
+ $data = ['reset' => $reset];
\Mail::send(
- ['emails.user.register-html', 'emails.user.register-text'], $data, function ($message) use ($email) {
- $message->to($email, $email)->subject('Welcome to Firefly!');
+ ['emails.user.remindme-html', 'emails.user.remindme-text'], $data, function ($message) use ($email) {
+ $message->to($email, $email)->subject('Forgot your password?');
}
);
+
+
}
/**
@@ -57,21 +58,20 @@ class Registration implements RegistrationInterface
*
* @return mixed|void
*/
- public function sendResetVerification(\User $user)
+ public function sendVerificationMail(\User $user)
{
- $reset = \Str::random(32);
+
+ $reset = \Str::random(32);
$user->reset = $reset;
$user->forceSave();
$email = $user->email;
+ $data = ['reset' => $reset];
- $data = ['reset' => $reset];
\Mail::send(
- ['emails.user.remindme-html', 'emails.user.remindme-text'], $data, function ($message) use ($email) {
- $message->to($email, $email)->subject('Forgot your password?');
+ ['emails.user.verify-html', 'emails.user.verify-text'], $data, function ($message) use ($email) {
+ $message->to($email, $email)->subject('Verify your e-mail address.');
}
);
-
-
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Mail/RegistrationInterface.php b/app/lib/FireflyIII/Shared/Mail/RegistrationInterface.php
index 0606956120..0741757b4e 100644
--- a/app/lib/FireflyIII/Shared/Mail/RegistrationInterface.php
+++ b/app/lib/FireflyIII/Shared/Mail/RegistrationInterface.php
@@ -15,20 +15,20 @@ interface RegistrationInterface
*
* @return mixed
*/
- public function sendVerificationMail(\User $user);
+ public function sendPasswordMail(\User $user);
/**
* @param \User $user
*
* @return mixed
*/
- public function sendPasswordMail(\User $user);
+ public function sendResetVerification(\User $user);
/**
* @param \User $user
*
* @return mixed
*/
- public function sendResetVerification(\User $user);
+ public function sendVerificationMail(\User $user);
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Preferences/PreferencesInterface.php b/app/lib/FireflyIII/Shared/Preferences/PreferencesInterface.php
index 4e7389695f..27f18a7ac5 100644
--- a/app/lib/FireflyIII/Shared/Preferences/PreferencesInterface.php
+++ b/app/lib/FireflyIII/Shared/Preferences/PreferencesInterface.php
@@ -10,14 +10,6 @@ interface PreferencesInterface
{
- /**
- * @param $name
- * @param $value
- *
- * @return null|\Preference
- */
- public function set($name, $value);
-
/**
* @param $name
* @param null $default
@@ -26,4 +18,12 @@ interface PreferencesInterface
*/
public function get($name, $default = null);
+ /**
+ * @param $name
+ * @param $value
+ *
+ * @return null|\Preference
+ */
+ public function set($name, $value);
+
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/SingleTableInheritanceEntity.php b/app/lib/FireflyIII/Shared/SingleTableInheritanceEntity.php
index 9787e02e40..664813d406 100644
--- a/app/lib/FireflyIII/Shared/SingleTableInheritanceEntity.php
+++ b/app/lib/FireflyIII/Shared/SingleTableInheritanceEntity.php
@@ -10,18 +10,18 @@ use LaravelBook\Ardent\Ardent;
*/
abstract class SingleTableInheritanceEntity extends Ardent
{
- /**
- * The field that stores the subclass
- *
- * @var string
- */
- protected $subclassField = null;
/**
* must be overridden and set to true in subclasses
*
* @var bool
*/
protected $isSubclass = false;
+ /**
+ * The field that stores the subclass
+ *
+ * @var string
+ */
+ protected $subclassField = null;
/**
* @param array $attributes
@@ -36,24 +36,6 @@ abstract class SingleTableInheritanceEntity extends Ardent
return $instance;
}
-
- /**
- * if no subclass is defined, function as normal
- *
- * @param array $attributes
- *
- * @return \Illuminate\Database\Eloquent\Model|static
- */
- public function mapData(array $attributes)
- {
- if (!$this->subclassField) {
- return $this->newInstance();
- }
-
- return new $attributes[$this->subclassField];
- }
-
-
/**
*
* instead of using $this->newInstance(), call
@@ -86,14 +68,6 @@ abstract class SingleTableInheritanceEntity extends Ardent
return $builder;
}
- /**
- * @return bool
- */
- public function isSubclass()
- {
- return $this->isSubclass;
- }
-
/**
* ensure that the subclass field is assigned on save
*
@@ -106,11 +80,7 @@ abstract class SingleTableInheritanceEntity extends Ardent
* @return bool
*/
public function save(
- array $rules = [],
- array $customMessages = [],
- array $options = [],
- \Closure $beforeSave = null,
- \Closure $afterSave = null
+ array $rules = [], array $customMessages = [], array $options = [], \Closure $beforeSave = null, \Closure $afterSave = null
) {
if ($this->subclassField) {
$this->attributes[$this->subclassField] = get_class($this);
@@ -118,4 +88,28 @@ abstract class SingleTableInheritanceEntity extends Ardent
return parent::save($rules, $customMessages, $options, $beforeSave, $afterSave);
}
+
+ /**
+ * if no subclass is defined, function as normal
+ *
+ * @param array $attributes
+ *
+ * @return \Illuminate\Database\Eloquent\Model|static
+ */
+ public function mapData(array $attributes)
+ {
+ if (!$this->subclassField) {
+ return $this->newInstance();
+ }
+
+ return new $attributes[$this->subclassField];
+ }
+
+ /**
+ * @return bool
+ */
+ public function isSubclass()
+ {
+ return $this->isSubclass;
+ }
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Toolkit/Date.php b/app/lib/FireflyIII/Shared/Toolkit/Date.php
index 1abf90e321..845381346e 100644
--- a/app/lib/FireflyIII/Shared/Toolkit/Date.php
+++ b/app/lib/FireflyIII/Shared/Toolkit/Date.php
@@ -13,74 +13,75 @@ use Firefly\Exception\FireflyException;
class Date
{
/**
- * @param Carbon $currentEnd
+ * @param Carbon $date
* @param $repeatFreq
+ * @param $skip
*
+ * @return Carbon
* @throws FireflyException
*/
- public function endOfPeriod(Carbon $currentEnd, $repeatFreq)
+ public function addPeriod(Carbon $date, $repeatFreq, $skip)
{
+ $add = ($skip + 1);
switch ($repeatFreq) {
default:
- throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq);
+ throw new FireflyException('Cannot do addPeriod for $repeat_freq ' . $repeatFreq);
break;
case 'daily':
- $currentEnd->addDay();
+ $date->addDays($add);
break;
case 'weekly':
- $currentEnd->addWeek()->subDay();
+ $date->addWeeks($add);
break;
case 'monthly':
- $currentEnd->addMonth()->subDay();
+ $date->addMonths($add);
break;
case 'quarterly':
- $currentEnd->addMonths(3)->subDay();
+ $months = $add * 3;
+ $date->addMonths($months);
break;
case 'half-year':
- $currentEnd->addMonths(6)->subDay();
+ $months = $add * 6;
+ $date->addMonths($months);
break;
case 'yearly':
- $currentEnd->addYear()->subDay();
+ $date->addYears($add);
break;
}
+
+ return $date;
}
/**
- * @param Carbon $date
+ * @param Carbon $currentEnd
* @param $repeatFreq
- * @param $skip
*
- * @return Carbon
* @throws FireflyException
*/
- public function addPeriod(Carbon $date, $repeatFreq, $skip)
+ public function endOfPeriod(Carbon $currentEnd, $repeatFreq)
{
- $add = ($skip + 1);
switch ($repeatFreq) {
default:
- throw new FireflyException('Cannot do addPeriod for $repeat_freq ' . $repeatFreq);
+ throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq);
break;
case 'daily':
- $date->addDays($add);
+ $currentEnd->addDay();
break;
case 'weekly':
- $date->addWeeks($add);
+ $currentEnd->addWeek()->subDay();
break;
case 'monthly':
- $date->addMonths($add);
+ $currentEnd->addMonth()->subDay();
break;
case 'quarterly':
- $months = $add * 3;
- $date->addMonths($months);
+ $currentEnd->addMonths(3)->subDay();
break;
case 'half-year':
- $months = $add * 6;
- $date->addMonths($months);
+ $currentEnd->addMonths(6)->subDay();
break;
case 'yearly':
- $date->addYears($add);
+ $currentEnd->addYear()->subDay();
break;
}
- return $date;
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Toolkit/Filter.php b/app/lib/FireflyIII/Shared/Toolkit/Filter.php
index b3cb70686c..f6c923f15a 100644
--- a/app/lib/FireflyIII/Shared/Toolkit/Filter.php
+++ b/app/lib/FireflyIII/Shared/Toolkit/Filter.php
@@ -18,29 +18,6 @@ use Firefly\Exception\FireflyException;
*/
class Filter
{
- /**
- * Checks and sets the currently set 'range' or defaults to a session
- * and if that fails, defaults to 1M. Always returns the final value.
- *
- * @return string
- */
- public function setSessionRangeValue()
- {
- if (!is_null(\Session::get('range'))) {
- $range = \Session::get('range');
- } else {
- /** @var \FireflyIII\Shared\Preferences\PreferencesInterface $preferences */
- $preferences = \App::make('FireflyIII\Shared\Preferences\PreferencesInterface');
- $viewRange = $preferences->get('viewRange', '1M');
-
- // default range:
- $range = $viewRange->data;
- \Session::put('range', $range);
- }
- return $range;
-
- }
-
/**
* Save Session::get('start') and Session::get('end') for other methods to use.
*/
@@ -87,10 +64,35 @@ class Filter
\Session::put('period', $period);
\Session::put('prev', $this->periodName($range, $prev));
\Session::put('next', $this->periodName($range, $next));
+
return null;
}
+ /**
+ * Checks and sets the currently set 'range' or defaults to a session
+ * and if that fails, defaults to 1M. Always returns the final value.
+ *
+ * @return string
+ */
+ public function setSessionRangeValue()
+ {
+ if (!is_null(\Session::get('range'))) {
+ $range = \Session::get('range');
+ } else {
+ /** @var \FireflyIII\Shared\Preferences\PreferencesInterface $preferences */
+ $preferences = \App::make('FireflyIII\Shared\Preferences\PreferencesInterface');
+ $viewRange = $preferences->get('viewRange', '1M');
+
+ // default range:
+ $range = $viewRange->data;
+ \Session::put('range', $range);
+ }
+
+ return $range;
+
+ }
+
/**
* @param $range
* @param Carbon $start
@@ -198,12 +200,14 @@ class Filter
break;
case '3M':
$month = intval($date->format('m'));
+
return 'Q' . ceil(($month / 12) * 4) . ' ' . $date->format('Y');
break;
case '6M':
$month = intval($date->format('m'));
$half = ceil(($month / 12) * 2);
$halfName = $half == 1 ? 'first' : 'second';
+
return $halfName . ' half of ' . $date->format('d-m-Y');
break;
case '1Y':
@@ -252,6 +256,7 @@ class Filter
break;
}
+
return $date;
}
@@ -291,6 +296,7 @@ class Filter
throw new FireflyException('Cannot do _next() on ' . $range);
break;
}
+
return $date;
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Toolkit/Form.php b/app/lib/FireflyIII/Shared/Toolkit/Form.php
index 7383a0e7a2..2fa690a427 100644
--- a/app/lib/FireflyIII/Shared/Toolkit/Form.php
+++ b/app/lib/FireflyIII/Shared/Toolkit/Form.php
@@ -9,12 +9,13 @@ use Illuminate\Support\Collection;
*
* @package FireflyIII\Shared\Toolkit
*/
-class Form {
+class Form
+{
/**
* Takes any collection and tries to make a sensible select list compatible array of it.
*
* @param Collection $set
- * @param null $titleField
+ * @param null $titleField
*
* @return mixed
*/
@@ -44,6 +45,7 @@ class Form {
}
$selectList[$id] = $title;
}
+
return $selectList;
}
diff --git a/app/lib/FireflyIII/Shared/Toolkit/Navigation.php b/app/lib/FireflyIII/Shared/Toolkit/Navigation.php
index f4f40e9117..1b26fc8b51 100644
--- a/app/lib/FireflyIII/Shared/Toolkit/Navigation.php
+++ b/app/lib/FireflyIII/Shared/Toolkit/Navigation.php
@@ -33,6 +33,7 @@ class Navigation
* Save in session:
*/
\Session::put('start', $next);
+
return true;
}
@@ -59,6 +60,7 @@ class Navigation
* Save in session:
*/
\Session::put('start', $prev);
+
return true;
}
}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Validation/ValidationServiceProvider.php b/app/lib/FireflyIII/Shared/Validation/ValidationServiceProvider.php
index 5ba7a32446..96a421aeca 100644
--- a/app/lib/FireflyIII/Shared/Validation/ValidationServiceProvider.php
+++ b/app/lib/FireflyIII/Shared/Validation/ValidationServiceProvider.php
@@ -7,6 +7,7 @@ use Illuminate\Support\ServiceProvider;
/**
* Class ValidationServiceProvider
+ *
* @package FireflyIII\Shared\Validation
*/
class ValidationServiceProvider extends ServiceProvider