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']); + } + + /** + * Counts the number of total revenue accounts. Useful for DataTables. + * + * @return int + */ + public function countRevenueAccounts() + { + return $this->countAccountsByType(['Revenue account']); } /** * @param \Account $account * - * @return \TransactionJournal|null + * @return \Account|null */ - public function openingBalanceTransaction(\Account $account) + public function findInitialBalanceAccount(\Account $account) { - 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.*']); - } + /** @var \FireflyIII\Database\AccountType $acctType */ + $acctType = \App::make('FireflyIII\Database\AccountType'); - /** - * Get all asset accounts. Optional JSON based parameters. - * - * @param array $parameters - * - * @return Collection - */ - public function getAssetAccounts(array $parameters = []) - { - return $this->getAccountsByType(['Default account', 'Asset account'], $parameters); + $accountType = $acctType->findByWhat('initial'); + return $this->getUser()->accounts()->where('account_type_id', $accountType->id)->where('name', 'LIKE', $account->name . '%')->first(); } /** @@ -149,44 +139,27 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface } /** - * @param \Account $account + * Get all asset accounts. Optional JSON based parameters. * - * @return \Account|null - */ - public function findInitialBalanceAccount(\Account $account) - { - /** @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(); - } - - /** - * @return int - */ - public function countAssetAccounts() - { - return $this->countAccountsByType(['Default account', 'Asset account']); - } - - /** - * @param array $types + * @param array $parameters * - * @return int + * @return Collection */ - public function countAccountsByType(array $types) + public function getAssetAccounts(array $parameters = []) { - return $this->getUser()->accounts()->accountTypeIn($types)->count(); + return $this->getAccountsByType(['Default account', 'Asset account'], $parameters); + } /** - * @return int + * Get all default accounts. + * + * @return Collection */ - public function countExpenseAccounts() + public function getDefaultAccounts() { - return $this->countAccountsByType(['Expense account', 'Beneficiary account']); + // TODO: Implement getDefaultAccounts() method. + throw new NotImplementedException; } /** @@ -199,45 +172,6 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface return $this->getAccountsByType(['Expense account', 'Beneficiary account'], $parameters); } - /** - * Returns an object with id $id. - * - * @param int $id - * - * @return Ardent - */ - public function find($id) - { - return $this->getUser()->accounts()->find($id); - } - - public function firstExpenseAccountOrCreate($name) - { - /** @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); - - } - - /** - * Counts the number of total revenue accounts. Useful for DataTables. - * - * @return int - */ - public function countRevenueAccounts() - { - return $this->countAccountsByType(['Revenue account']); - } - /** * Get all revenue accounts. * @@ -250,6 +184,65 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface return $this->getAccountsByType(['Revenue account'], $parameters); } + /** + * @param \Account $account + * + * @return \TransactionJournal|null + */ + public function openingBalanceTransaction(\Account $account) + { + 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.*']); + } + + /** + * @param \Account $account + * @param array $data + * + * @return bool + */ + public function storeInitialBalance(\Account $account, array $data) + { + $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; + } + /** * @param Ardent $model * @@ -258,10 +251,83 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface public function destroy(Ardent $model) { $model->delete(); + return true; } + /** + * @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, ['_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 update(Ardent $model, array $data) + { + $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; + } + /** * Validates a model. Returns an array containing MessageBags * errors/warnings/successes. @@ -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,139 +393,34 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface if (!$errors->has('openingbalancedate')) { $successes->add('openingbalancedate', '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. + * + * @param Ardent $model + * + * @return array + */ + public function validateObject(Ardent $model) + { + // TODO: Implement validateObject() method. + throw new NotImplementedException; + } + + /** + * Returns an object with id $id. + * + * @param int $id * * @return Ardent */ - public function store(array $data) + public function find($id) { - - /* - * 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; - - } - - /** - * @param \Account $account - * @param array $data - * - * @return bool - */ - public function storeInitialBalance(\Account $account, array $data) - { - $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; - } - - /** - * @param array $ids - * - * @return Collection - */ - public function getByIds(array $ids) - { - return $this->getUser()->accounts()->whereIn('id', $ids)->get(); - } - - /** - * Get all default accounts. - * - * @return Collection - */ - public function getDefaultAccounts() - { - // TODO: Implement getDefaultAccounts() method. - throw new NotImplementedException; - } - - /** - * Returns all objects. - * - * @return Collection - */ - public function get() - { - // TODO: Implement get() method. - throw new NotImplementedException; + return $this->getUser()->accounts()->find($id); } /** @@ -476,16 +437,36 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface } /** - * Validates a model. Returns an array containing MessageBags - * errors/warnings/successes. + * Returns all objects. * - * @param Ardent $model - * - * @return array + * @return Collection */ - public function validateObject(Ardent $model) + public function get() { - // TODO: Implement validateObject() method. + // TODO: Implement get() method. throw new NotImplementedException; } + + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids) + { + return $this->getUser()->accounts()->whereIn('id', $ids)->get(); + } + + public function firstExpenseAccountOrCreate($name) + { + /** @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 @@ -18,6 +18,81 @@ use FireflyIII\Database\Ifaces\CUD; class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls { + /** + * @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) + { + // TODO: Implement store() method. + throw new NotImplementedException; + } + + /** + * @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. + * + * @param array $model + * + * @return array + */ + public function validate(array $model) + { + // TODO: Implement validate() method. + throw new NotImplementedException; + } + + /** + * Validates a model. Returns an array containing MessageBags + * errors/warnings/successes. + * + * @param Ardent $model + * + * @return array + */ + public function validateObject(Ardent $model) + { + // TODO: Implement validateObject() method. + throw new NotImplementedException; + } + + /** + * Returns an object with id $id. + * + * @param int $id + * + * @return Ardent + */ + public function find($id) + { + // TODO: Implement find() method. + throw new NotImplementedException; + } + /** * Finds an account type using one of the "$what"'s: expense, asset, revenue * @@ -46,84 +121,10 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls break; } + return null; } - /** - * @param Ardent $model - * - * @return bool - */ - public function destroy(Ardent $model) - { - // TODO: Implement destroy() method. - throw new NotImplementedException; - } - - /** - * Validates a model. Returns an array containing MessageBags - * errors/warnings/successes. - * - * @param Ardent $model - * - * @return array - */ - public function validateObject(Ardent $model) - { - // TODO: Implement validateObject() method. - throw new NotImplementedException; - } - - /** - * Validates an array. Returns an array containing MessageBags - * errors/warnings/successes. - * - * @param array $model - * - * @return array - */ - public function validate(array $model) - { - // TODO: Implement validate() method. - throw new NotImplementedException; - } - - /** - * @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) - { - // TODO: Implement update() method. - throw new NotImplementedException; - } - - /** - * Returns an object with id $id. - * - * @param int $id - * - * @return Ardent - */ - public function find($id) - { - // TODO: Implement find() method. - throw new NotImplementedException; - } - /** * 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 @@ -27,22 +27,6 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface $this->setUser(\Auth::user()); } - /** - * @param \Budget $budget - * @param Carbon $date - * - * @return \LimitRepetition|null - */ - public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date) - { - 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 Ardent $model * @@ -51,9 +35,50 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface public function destroy(Ardent $model) { $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 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 @@ -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,103 +116,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface $successes->add('name', 'OK'); } - return [ - 'errors' => $errors, - 'warnings' => $warnings, - 'successes' => $successes - ]; - } - - /** - * @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; - } - - /** - * Returns all objects. - * - * @return Collection - */ - public function get() - { - $budgets = $this->getUser()->budgets()->get(); - - return $budgets; - } - - /** - * @param \Budget $budget - * @param Carbon $date - * - * @return float - */ - public function spentInMonth(\Budget $budget, Carbon $date) - { - $end = clone $date; - $date->startOfMonth(); - $end->endOfMonth(); - $sum = floatval($budget->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1; - return $sum; - } - - - /** - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end) - { - // 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(); - } - - /** - * @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]; } /** @@ -217,17 +146,6 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface throw new NotImplementedException; } - /** - * @param array $ids - * - * @return Collection - */ - public function getByIds(array $ids) - { - // TODO: Implement getByIds() method. - throw new NotImplementedException; - } - /** * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc. * @@ -240,4 +158,79 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface // TODO: Implement findByWhat() method. throw new NotImplementedException; } + + /** + * Returns all objects. + * + * @return Collection + */ + public function get() + { + $budgets = $this->getUser()->budgets()->get(); + + return $budgets; + } + + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids) + { + // TODO: Implement getByIds() method. + throw new NotImplementedException; + } + + /** + * @param \Budget $budget + * @param Carbon $date + * + * @return \LimitRepetition|null + */ + public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date) + { + 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 Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end) + { + // 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(); + } + + /** + * @param \Budget $budget + * @param Carbon $date + * + * @return float + */ + public function spentInMonth(\Budget $budget, Carbon $date) + { + $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]; } /** @@ -153,17 +123,6 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface throw new NotImplementedException; } - /** - * @param array $data - * - * @return Ardent - */ - public function store(array $data) - { - // TODO: Implement store() method. - throw new NotImplementedException; - } - /** * Returns an object with id $id. * @@ -177,17 +136,6 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface throw new NotImplementedException; } - /** - * @param array $ids - * - * @return Collection - */ - public function getByIds(array $ids) - { - // TODO: Implement getByIds() method. - throw new NotImplementedException; - } - /** * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc. * @@ -200,4 +148,52 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface // 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 + * + * @return Collection + */ + public function getByIds(array $ids) + { + // TODO: Implement getByIds() method. + throw new NotImplementedException; + } + + /** + * @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; + } } \ 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,6 +55,32 @@ interface AccountInterface */ public function findInitialBalanceAccount(\Account $account); + /** + * Get all accounts of the selected types. Is also capable of handling DataTables' parameters. + * + * @param array $types + * @param array $parameters + * + * @return Collection + */ + public function getAccountsByType(array $types, array $parameters = []); + + /** + * Get all asset accounts. The parameters are optional and are provided by the DataTables plugin. + * + * @param array $parameters + * + * @return Collection + */ + public function getAssetAccounts(array $parameters = []); + + /** + * Get all default accounts. + * + * @return Collection + */ + public function getDefaultAccounts(); + public function getExpenseAccounts(array $parameters = []); /** @@ -66,32 +92,6 @@ interface AccountInterface */ public function getRevenueAccounts(array $parameters = []); - /** - * Get all accounts of the selected types. Is also capable of handling DataTables' parameters. - * - * @param array $types - * @param array $parameters - * - * @return Collection - */ - public function getAccountsByType(array $types, array $parameters = []); - - /** - * Counts the number of accounts found with the included types. - * - * @param array $types - * - * @return int - */ - public function countAccountsByType(array $types); - - /** - * Get all default accounts. - * - * @return Collection - */ - public function getDefaultAccounts(); - /** * @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 @@ setUser(\Auth::user()); } - /** - * @param \Account $account - * - * @return float - */ - public function leftOnAccount(\Account $account) - { - $balance = $account->balance(); - /** @var \Piggybank $p */ - foreach ($account->piggybanks()->get() as $p) { - $balance -= $p->currentRelevantRep()->currentamount; - } - - return $balance; - - } - /** * @param Ardent $model * @@ -54,6 +37,56 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface $model->delete(); } + /** + * @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 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; + } /** * 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,17 +198,6 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface throw new NotImplementedException; } - /** - * @param array $ids - * - * @return Collection - */ - public function getByIds(array $ids) - { - // TODO: Implement getByIds() method. - throw new NotImplementedException; - } - /** * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc. * @@ -253,4 +210,42 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface // 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 + * + * @return Collection + */ + public function getByIds(array $ids) + { + // TODO: Implement getByIds() method. + throw new NotImplementedException; + } + + /** + * @param \Account $account + * + * @return float + */ + public function leftOnAccount(\Account $account) + { + $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 @@ -28,30 +28,6 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface $this->setUser(\Auth::user()); } - /** - * @param \RecurringTransaction $recurring - * @param Carbon $start - * @param Carbon $end - * - * @return \TransactionJournal|null - */ - public function getJournalForRecurringInRange(\RecurringTransaction $recurring, Carbon $start, Carbon $end) - { - return $this->getUser()->transactionjournals()->where('recurring_transaction_id', $recurring->id)->after($start)->before($end)->first(); - - } - - - /** - * Returns all objects. - * - * @return Collection - */ - public function get() - { - return $this->getUser()->recurringtransactions()->get(); - } - /** * @param Ardent $model * @@ -64,16 +40,25 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface } /** - * Validates a model. Returns an array containing MessageBags - * errors/warnings/successes. + * @param array $data * - * @param Ardent $model - * - * @return array + * @return Ardent */ - public function validateObject(Ardent $model) + public function store(array $data) { - // TODO: Implement validateObject() method. + // TODO: Implement store() method. + throw new NotImplementedException; + } + + /** + * @param Ardent $model + * @param array $data + * + * @return bool + */ + public function update(Ardent $model, array $data) + { + // TODO: Implement update() method. throw new NotImplementedException; } @@ -92,25 +77,16 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface } /** - * @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; } @@ -127,17 +103,6 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface throw new NotImplementedException; } - /** - * @param array $ids - * - * @return Collection - */ - public function getByIds(array $ids) - { - // TODO: Implement getByIds() method. - throw new NotImplementedException; - } - /** * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc. * @@ -150,4 +115,38 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface // TODO: Implement findByWhat() method. throw new NotImplementedException; } + + /** + * Returns all objects. + * + * @return Collection + */ + public function get() + { + return $this->getUser()->recurringtransactions()->get(); + } + + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids) + { + // TODO: Implement getByIds() method. + throw new NotImplementedException; + } + + /** + * @param \RecurringTransaction $recurring + * @param Carbon $start + * @param Carbon $end + * + * @return \TransactionJournal|null + */ + public function getJournalForRecurringInRange(\RecurringTransaction $recurring, Carbon $start, Carbon $end) + { + 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,17 +26,6 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac $this->setUser(\Auth::user()); } - - /** - * Returns all objects. - * - * @return Collection - */ - public function get() - { - return $this->getUser()->recurringtransactions()->get(); - } - /** * @param Ardent $model * @@ -49,16 +38,25 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac } /** - * Validates a model. Returns an array containing MessageBags - * errors/warnings/successes. + * @param array $data * - * @param Ardent $model - * - * @return array + * @return Ardent */ - public function validateObject(Ardent $model) + public function store(array $data) { - // TODO: Implement validateObject() method. + // TODO: Implement store() method. + throw new NotImplementedException; + } + + /** + * @param Ardent $model + * @param array $data + * + * @return bool + */ + public function update(Ardent $model, array $data) + { + // 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; } @@ -112,17 +101,6 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac throw new NotImplementedException; } - /** - * @param array $ids - * - * @return Collection - */ - public function getByIds(array $ids) - { - // TODO: Implement getByIds() method. - throw new NotImplementedException; - } - /** * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc. * @@ -135,4 +113,25 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac // TODO: Implement findByWhat() method. throw new NotImplementedException; } + + /** + * Returns all objects. + * + * @return Collection + */ + public function get() + { + return $this->getUser()->recurringtransactions()->get(); + } + + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids) + { + // 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]; } /** @@ -140,18 +149,6 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls throw new NotImplementedException; } - /** - * @param Ardent $model - * @param array $data - * - * @return bool - */ - public function update(Ardent $model, array $data) - { - // TODO: Implement update() method. - throw new NotImplementedException; - } - /** * Returns an object with id $id. * @@ -165,6 +162,19 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls 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. * @@ -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 @@ -19,16 +19,6 @@ class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDa { - /** - * @param string $code - * - * @return \TransactionCurrency|null - */ - public function findByCode($code) - { - return \TransactionCurrency::whereCode($code)->first(); - } - /** * @param Ardent $model * @@ -41,16 +31,25 @@ class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDa } /** - * Validates a model. Returns an array containing MessageBags - * errors/warnings/successes. + * @param array $data * - * @param Ardent $model - * - * @return array + * @return Ardent */ - public function validateObject(Ardent $model) + public function store(array $data) { - // TODO: Implement validateObject() method. + // TODO: Implement store() method. + throw new NotImplementedException; + } + + /** + * @param Ardent $model + * @param array $data + * + * @return bool + */ + public function update(Ardent $model, array $data) + { + // 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 string $code * - * @param $what - * - * @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'); + + /** @var \FireflyIII\Database\Account $accountRepository */ + $accountRepository = \App::make('FireflyIII\Database\Account'); + + /** @var \FireflyIII\Database\TransactionCurrency $currencyRepository */ + $currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency'); + + /** @var \FireflyIII\Database\Transaction $transactionRepository */ + $transactionRepository = \App::make('FireflyIII\Database\Transaction'); + + $journalType = $typeRepository->findByWhat($data['what']); + $currency = $currencyRepository->findByCode($data['currency']); + + $journal = new \TransactionJournal; + $journal->transactionType()->associate($journalType); + $journal->transactionCurrency()->associate($currency); + $journal->user()->associate($this->getUser()); + $journal->description = $data['description']; + $journal->date = $data['date']; + $journal->completed = 0; + + /* + * This must be enough to store the journal: + */ + if (!$journal->validate()) { + \Log::error($journal->errors()->all()); + throw new FireflyException('store() transaction journal failed, but it should not!'); + } + $journal->save(); + + /* + * Still need to find the accounts related to the transactions. + * This depends on the type of transaction. + */ + switch ($data['what']) { + case 'withdrawal': + $data['from'] = $accountRepository->find($data['account_id']); + $data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']); + break; + case 'opening': + break; + + default: + throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".'); + break; + } + + /* + * Then store both transactions. + */ + $first = ['account' => $data['from'], 'transaction_journal' => $journal, 'amount' => ($data['amount'] * -1),]; + $validate = $transactionRepository->validate($first); + if ($validate['errors']->count() == 0) { + $transactionRepository->store($first); + } else { + throw new FireflyException($validate['errors']->first()); + } + + $second = ['account' => $data['to'], 'transaction_journal' => $journal, 'amount' => floatval($data['amount']),]; + + $validate = $transactionRepository->validate($second); + if ($validate['errors']->count() == 0) { + $transactionRepository->store($second); + } else { + throw new FireflyException($validate['errors']->first()); + } + + $journal->completed = 1; + $journal->save(); + + return $journal; } /** - * @param Carbon $start - * @param Carbon $end + * @param Ardent $model + * @param array $data * - * @return Collection + * @return bool */ - public function getInDateRange(Carbon $start, Carbon $end) + public function update(Ardent $model, array $data) { - return $this->getuser()->transactionjournals()->withRelevantData()->before($end)->after($start)->get(); + // TODO: Implement update() method. + throw new NotImplementedException; } - /** - * @param \Account $account - * @param int $count - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getInDateRangeAccount(\Account $account, $count = 20, Carbon $start, Carbon $end) - { - - $accountID = $account->id; - $query = $this->_user - ->transactionjournals() - ->with(['transactions', 'transactioncurrency', 'transactiontype']) - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') - ->where('accounts.id', $accountID) - ->where('date', '>=', $start->format('Y-m-d')) - ->where('date', '<=', $end->format('Y-m-d')) - ->orderBy('transaction_journals.date', 'DESC') - ->orderBy('transaction_journals.id', 'DESC') - ->take($count) - ->get(['transaction_journals.*']); - - return $query; - } - - /** - * @return TransactionJournal - */ - public function first() - { - return $this->getUser()->transactionjournals()->orderBy('date', 'ASC')->first(); - } - - /** * Validates an array. Returns an array containing MessageBags * errors/warnings/successes. @@ -133,9 +147,9 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData public function validate(array $model) { - $warnings = new MessageBag; + $warnings = new MessageBag; $successes = new MessageBag; - $errors = new MessageBag; + $errors = new MessageBag; if (!isset($model['what'])) { @@ -173,10 +187,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData */ if (isset($model['amount']) && floatval($model['amount']) < 0.01) { $errors->add('amount', 'Amount must be > 0.01'); - } else if (!isset($model['amount'])) { - $errors->add('amount', 'Amount must be set!'); } else { - $successes->add('amount', 'OK'); + if (!isset($model['amount'])) { + $errors->add('amount', 'Amount must be set!'); + } else { + $successes->add('amount', 'OK'); + } } /* @@ -240,26 +256,26 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData break; } -// if (isset($model['to_id']) && intval($model['to_id']) < 1) { -// $errors->add('account_to', 'Invalid to-account'); -// } -// -// if (isset($model['from_id']) && intval($model['from_id']) < 1) { -// $errors->add('account_from', 'Invalid from-account'); -// -// } -// if (isset($model['account_id']) && intval($model['account_id']) < 1) { -// $errors->add('account_id', 'Invalid account!'); -// } -// if (isset($model['to']) && !($model['to'] instanceof \Account)) { -// $errors->add('account_to', 'Invalid to-account'); -// } -// if (isset($model['from']) && !($model['from'] instanceof \Account)) { -// $errors->add('account_from', 'Invalid from-account'); -// } -// if (!isset($model['amount']) || (isset($model['amount']) && floatval($model['amount']) < 0)) { -// $errors->add('amount', 'Invalid amount'); -// } + // if (isset($model['to_id']) && intval($model['to_id']) < 1) { + // $errors->add('account_to', 'Invalid to-account'); + // } + // + // if (isset($model['from_id']) && intval($model['from_id']) < 1) { + // $errors->add('account_from', 'Invalid from-account'); + // + // } + // if (isset($model['account_id']) && intval($model['account_id']) < 1) { + // $errors->add('account_id', 'Invalid account!'); + // } + // if (isset($model['to']) && !($model['to'] instanceof \Account)) { + // $errors->add('account_to', 'Invalid to-account'); + // } + // if (isset($model['from']) && !($model['from'] instanceof \Account)) { + // $errors->add('account_from', 'Invalid from-account'); + // } + // if (!isset($model['amount']) || (isset($model['amount']) && floatval($model['amount']) < 0)) { + // $errors->add('amount', 'Invalid amount'); + // } $validator = \Validator::make([$model], \Transaction::$rules); @@ -277,169 +293,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData if (!$errors->has('date')) { $successes->add('date', 'OK'); } - return [ - 'errors' => $errors, - 'warnings' => $warnings, - 'successes' => $successes - ]; + + return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; } - /** - * @param array $data - * - * @return Ardent - */ - public function store(array $data) - { - - /** @var \FireflyIII\Database\TransactionType $typeRepository */ - $typeRepository = \App::make('FireflyIII\Database\TransactionType'); - - /** @var \FireflyIII\Database\Account $accountRepository */ - $accountRepository = \App::make('FireflyIII\Database\Account'); - - /** @var \FireflyIII\Database\TransactionCurrency $currencyRepository */ - $currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency'); - - /** @var \FireflyIII\Database\Transaction $transactionRepository */ - $transactionRepository = \App::make('FireflyIII\Database\Transaction'); - - $journalType = $typeRepository->findByWhat($data['what']); - $currency = $currencyRepository->findByCode($data['currency']); - - $journal = new \TransactionJournal; - $journal->transactionType()->associate($journalType); - $journal->transactionCurrency()->associate($currency); - $journal->user()->associate($this->getUser()); - $journal->description = $data['description']; - $journal->date = $data['date']; - $journal->completed = 0; - - /* - * This must be enough to store the journal: - */ - if (!$journal->validate()) { - \Log::error($journal->errors()->all()); - throw new FireflyException('store() transaction journal failed, but it should not!'); - } - $journal->save(); - - /* - * Still need to find the accounts related to the transactions. - * This depends on the type of transaction. - */ - switch ($data['what']) { - case 'withdrawal': - $data['from'] = $accountRepository->find($data['account_id']); - $data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']); - break; - case 'opening': - break; - - default: - throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".'); - break; - } - - /* - * Then store both transactions. - */ - $first = [ - 'account' => $data['from'], - 'transaction_journal' => $journal, - 'amount' => ($data['amount'] * -1), - ]; - $validate = $transactionRepository->validate($first); - if ($validate['errors']->count() == 0) { - $transactionRepository->store($first); - } else { - throw new FireflyException($validate['errors']->first()); - } - - $second = [ - 'account' => $data['to'], - 'transaction_journal' => $journal, - 'amount' => floatval($data['amount']), - ]; - - $validate = $transactionRepository->validate($second); - if ($validate['errors']->count() == 0) { - $transactionRepository->store($second); - } else { - throw new FireflyException($validate['errors']->first()); - } - - $journal->completed = 1; - $journal->save(); - return $journal; - } - - /** - * Returns an object with id $id. - * - * @param int $id - * - * @return Ardent - */ - public function find($id) - { - return $this->getUser()->transactionjournals()->find($id); - } - - /** - * Returns all objects. - * - * @return Collection - */ - public function get() - { - return $this->getUser()->transactionjournals()->get(); - } - - /** - * Some objects. - * - * @return Collection - */ - public function getTransfers() - { - return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Transfer'])->get(['transaction_journals.*']); - } - - /** - * Some objects. - * - * @return Collection - */ - public function getDeposits() - { - return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Deposit'])->get(['transaction_journals.*']); - } - - /** - * Some objects. - * - * @return Collection - */ - public function getWithdrawals() - { - return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Withdrawal'])->get(['transaction_journals.*']); - } - - - /** - * @param Ardent $model - * - * @return bool - */ - public function destroy(Ardent $model) - { - // TODO: Implement destroy() method. - throw new NotImplementedException; - } - /** * Validates a model. Returns an array containing MessageBags * errors/warnings/successes. @@ -455,26 +314,15 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData } /** - * @param Ardent $model - * @param array $data + * Returns an object with id $id. * - * @return bool - */ - public function update(Ardent $model, array $data) - { - // TODO: Implement update() method. - throw new NotImplementedException; - } - - /** - * @param array $ids + * @param int $id * - * @return Collection + * @return Ardent */ - public function getByIds(array $ids) + public function find($id) { - // TODO: Implement getByIds() method. - throw new NotImplementedException; + return $this->getUser()->transactionjournals()->find($id); } /** @@ -489,4 +337,137 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData // TODO: Implement findByWhat() method. throw new NotImplementedException; } + + /** + * Returns all objects. + * + * @return Collection + */ + public function get() + { + return $this->getUser()->transactionjournals()->get(); + } + + /** + * @param array $ids + * + * @return Collection + */ + public function getByIds(array $ids) + { + // TODO: Implement getByIds() method. + throw new NotImplementedException; + } + + /** + * @return TransactionJournal + */ + public function first() + { + return $this->getUser()->transactionjournals()->orderBy('date', 'ASC')->first(); + } + + /** + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getInDateRange(Carbon $start, Carbon $end) + { + return $this->getuser()->transactionjournals()->withRelevantData()->before($end)->after($start)->get(); + } + + /** + * @param Carbon $date + * + * @return float + */ + public function getSumOfExpensesByMonth(Carbon $date) + { + $end = clone $date; + $date->startOfMonth(); + $end->endOfMonth(); + + $sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin( + 'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id' + )->where('amount', '>', 0)->where('transaction_types.type', '=', 'Withdrawal')->where('transaction_journals.date', '>=', $date->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount'); + $sum = floatval($sum); + + return $sum; + } + + /** + * @param Carbon $date + * + * @return float + */ + public function getSumOfIncomesByMonth(Carbon $date) + { + $end = clone $date; + $date->startOfMonth(); + $end->endOfMonth(); + + $sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin( + 'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id' + )->where('amount', '>', 0)->where('transaction_types.type', '=', 'Deposit')->where('transaction_journals.date', '>=', $date->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('transactions.amount'); + $sum = floatval($sum); + + return $sum; + } + + /** + * Some objects. + * + * @return Collection + */ + public function getDeposits() + { + return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Deposit'])->get(['transaction_journals.*']); + } + + /** + * @param \Account $account + * @param int $count + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getInDateRangeAccount(\Account $account, $count = 20, Carbon $start, Carbon $end) + { + + $accountID = $account->id; + $query = $this->_user->transactionjournals()->with(['transactions', 'transactioncurrency', 'transactiontype'])->leftJoin( + 'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id' + )->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $accountID)->where( + 'date', '>=', $start->format('Y-m-d') + )->where('date', '<=', $end->format('Y-m-d'))->orderBy('transaction_journals.date', 'DESC')->orderBy('transaction_journals.id', 'DESC')->take( + $count + )->get(['transaction_journals.*']); + + return $query; + } + + /** + * Some objects. + * + * @return Collection + */ + public function getTransfers() + { + return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Transfer'])->get(['transaction_journals.*']); + } + + /** + * Some objects. + * + * @return Collection + */ + public function getWithdrawals() + { + return $this->getUser()->transactionjournals()->withRelevantData()->transactionTypes(['Withdrawal'])->get(['transaction_journals.*']); + } } \ 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 @@ -19,6 +19,81 @@ use LaravelBook\Ardent\Ardent; class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCalls { + /** + * @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) + { + // TODO: Implement store() method. + throw new NotImplementedException; + } + + /** + * @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. + * + * @param array $model + * + * @return array + */ + public function validate(array $model) + { + // TODO: Implement validate() method. + throw new NotImplementedException; + } + + /** + * Validates a model. Returns an array containing MessageBags + * errors/warnings/successes. + * + * @param Ardent $model + * + * @return array + */ + public function validateObject(Ardent $model) + { + // TODO: Implement validateObject() method. + throw new NotImplementedException; + } + + /** + * Returns an object with id $id. + * + * @param int $id + * + * @return Ardent + */ + public function find($id) + { + // TODO: Implement find() method. + throw new NotImplementedException; + } + /** * Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc. * @@ -44,84 +119,10 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa } + return null; } - /** - * @param Ardent $model - * - * @return bool - */ - public function destroy(Ardent $model) - { - // TODO: Implement destroy() method. - throw new NotImplementedException; - } - - /** - * Validates a model. Returns an array containing MessageBags - * errors/warnings/successes. - * - * @param Ardent $model - * - * @return array - */ - public function validateObject(Ardent $model) - { - // TODO: Implement validateObject() method. - throw new NotImplementedException; - } - - /** - * Validates an array. Returns an array containing MessageBags - * errors/warnings/successes. - * - * @param array $model - * - * @return array - */ - public function validate(array $model) - { - // TODO: Implement validate() method. - throw new NotImplementedException; - } - - /** - * @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) - { - // TODO: Implement update() method. - throw new NotImplementedException; - } - - /** - * Returns an object with id $id. - * - * @param int $id - * - * @return Ardent - */ - public function find($id) - { - // TODO: Implement find() method. - throw new NotImplementedException; - } - /** * 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,10 +42,27 @@ class Form public static function ffBalance($name, $value = null, array $options = []) { $options['step'] = 'any'; + return self::ffInput('amount', $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 @@ -96,126 +84,12 @@ class Form * @return string * @throws FireflyException */ - public static function ffTags($name, $value = null, array $options = []) + public static function ffInteger($name, $value = null, array $options = []) { - $options['data-role'] = 'tagsinput'; - return self::ffInput('text', $name, $value, $options); - } + $options['step'] = '1'; - /** - * @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); - } + return self::ffInput('number', $name, $value, $options); - /** - * @param $name - * @param null $value - * @param array $options - * - * @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) - { - $previousValue = \Input::old('post_submit_action'); - $previousValue = is_null($previousValue) ? 'store' : $previousValue; - /* - * Store. - */ - $store = ''; - switch ($type) { - case 'create': - $store = '
'; - $store .= '
'; - break; - case 'update': - $store = '
'; - $store .= '
'; - break; - default: - throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type); - break; - } - - /* - * validate is always the same: - */ - $validate = '
'; - - /* - * Store & return: - */ - switch ($type) { - case 'create': - $return = '
'; - break; - case 'update': - $return = '
'; - 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 = '
'; + $store .= '
'; + break; + case 'update': + $store = '
'; + $store .= '
'; + break; + default: + throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type); + break; + } + + /* + * validate is always the same: + */ + $validate = '
'; + + /* + * Store & return: + */ + switch ($type) { + case 'create': + $return = '
'; + break; + case 'update': + $return = '
'; + 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 @@ -8,27 +8,6 @@ namespace FireflyIII\Shared\Mail; */ class Registration implements RegistrationInterface { - /** - * @param \User $user - * - * @return mixed|void - */ - public function sendVerificationMail(\User $user) - { - - $reset = \Str::random(32); - $user->reset = $reset; - $user->forceSave(); - $email = $user->email; - $data = ['reset' => $reset]; - - \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.'); - } - ); - } - /** * @param \User $user * @@ -37,9 +16,9 @@ class Registration implements RegistrationInterface public function sendPasswordMail(\User $user) { - $password = \Str::random(12); + $password = \Str::random(12); $user->password = $password; - $user->reset = \Str::random(32); // new one. + $user->reset = \Str::random(32); // new one. $user->forceSave(); $email = $user->email; @@ -59,7 +38,7 @@ class Registration implements RegistrationInterface */ public function sendResetVerification(\User $user) { - $reset = \Str::random(32); + $reset = \Str::random(32); $user->reset = $reset; $user->forceSave(); $email = $user->email; @@ -74,4 +53,25 @@ class Registration implements RegistrationInterface } + /** + * @param \User $user + * + * @return mixed|void + */ + public function sendVerificationMail(\User $user) + { + + $reset = \Str::random(32); + $user->reset = $reset; + $user->forceSave(); + $email = $user->email; + $data = ['reset' => $reset]; + + \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.'); + } + ); + } + } \ 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 @@ -10,13 +10,6 @@ namespace FireflyIII\Shared\Mail; interface RegistrationInterface { - /** - * @param \User $user - * - * @return mixed - */ - public function sendVerificationMail(\User $user); - /** * @param \User $user * @@ -31,4 +24,11 @@ interface RegistrationInterface */ public function sendResetVerification(\User $user); + /** + * @param \User $user + * + * @return mixed + */ + 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 @@ -12,39 +12,6 @@ use Firefly\Exception\FireflyException; */ class Date { - /** - * @param Carbon $currentEnd - * @param $repeatFreq - * - * @throws FireflyException - */ - public function endOfPeriod(Carbon $currentEnd, $repeatFreq) - { - switch ($repeatFreq) { - default: - throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq); - break; - case 'daily': - $currentEnd->addDay(); - break; - case 'weekly': - $currentEnd->addWeek()->subDay(); - break; - case 'monthly': - $currentEnd->addMonth()->subDay(); - break; - case 'quarterly': - $currentEnd->addMonths(3)->subDay(); - break; - case 'half-year': - $currentEnd->addMonths(6)->subDay(); - break; - case 'yearly': - $currentEnd->addYear()->subDay(); - break; - } - } - /** * @param Carbon $date * @param $repeatFreq @@ -81,6 +48,40 @@ class Date $date->addYears($add); break; } + return $date; } + + /** + * @param Carbon $currentEnd + * @param $repeatFreq + * + * @throws FireflyException + */ + public function endOfPeriod(Carbon $currentEnd, $repeatFreq) + { + switch ($repeatFreq) { + default: + throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq); + break; + case 'daily': + $currentEnd->addDay(); + break; + case 'weekly': + $currentEnd->addWeek()->subDay(); + break; + case 'monthly': + $currentEnd->addMonth()->subDay(); + break; + case 'quarterly': + $currentEnd->addMonths(3)->subDay(); + break; + case 'half-year': + $currentEnd->addMonths(6)->subDay(); + break; + case 'yearly': + $currentEnd->addYear()->subDay(); + break; + } + } } \ 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