diff --git a/app/lib/FireflyIII/Database/Account.php b/app/lib/FireflyIII/Database/Account.php index b4960b41dd..1b598ac76f 100644 --- a/app/lib/FireflyIII/Database/Account.php +++ b/app/lib/FireflyIII/Database/Account.php @@ -81,11 +81,10 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface /** * @param array $types - * @param array $parameters * * @return Collection */ - public function getAccountsByType(array $types, array $parameters = []) + public function getAccountsByType(array $types) { /* * Basic query: @@ -103,10 +102,9 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface /* * Not used, but useful for the balance within a certain month / year. */ - $balanceOnDate = isset($parameters['date']) ? $parameters['date'] : Carbon::now(); $query->where( - function ($q) use ($balanceOnDate) { - $q->where('transaction_journals.date', '<=', $balanceOnDate->format('Y-m-d')); + function ($q) { + $q->where('transaction_journals.date', '<=', Carbon::now()->format('Y-m-d')); $q->orWhereNull('transaction_journals.date'); } ); @@ -118,23 +116,6 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface */ $query->orderBy('name', 'ASC'); - /* - * If present, process parameters for searching. - */ - if (isset($parameters['search'])) { - $query->where('name', 'LIKE', '%' . e($parameters['search']['value'] . '%')); - } - - /* - * If present, start at $start: - */ - if (isset($parameters['start'])) { - $query->skip(intval($parameters['start'])); - } - if (isset($parameters['length'])) { - $query->take(intval($parameters['length'])); - } - return $query->get(['accounts.*', \DB::Raw('SUM(`transactions`.`amount`) as `balance`')]); } @@ -145,43 +126,28 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface * * @return Collection */ - public function getAssetAccounts(array $parameters = []) + public function getAssetAccounts() { - return $this->getAccountsByType(['Default account', 'Asset account'], $parameters); + return $this->getAccountsByType(['Default account', 'Asset account']); } /** - * Get all default accounts. - * * @return Collection */ - public function getDefaultAccounts() + public function getExpenseAccounts() { - // TODO: Implement getDefaultAccounts() method. - throw new NotImplementedException; - } - - /** - * @param array $parameters - * - * @return Collection - */ - public function getExpenseAccounts(array $parameters = []) - { - return $this->getAccountsByType(['Expense account', 'Beneficiary account'], $parameters); + return $this->getAccountsByType(['Expense account', 'Beneficiary account']); } /** * Get all revenue accounts. * - * @param array $parameters - * * @return Collection */ - public function getRevenueAccounts(array $parameters = []) + public function getRevenueAccounts() { - return $this->getAccountsByType(['Revenue account'], $parameters); + return $this->getAccountsByType(['Revenue account']); } /** @@ -397,20 +363,6 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; } - /** - * 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. * diff --git a/app/lib/FireflyIII/Database/AccountType.php b/app/lib/FireflyIII/Database/AccountType.php index 302b8b2335..9d945faffa 100644 --- a/app/lib/FireflyIII/Database/AccountType.php +++ b/app/lib/FireflyIII/Database/AccountType.php @@ -15,7 +15,7 @@ use LaravelBook\Ardent\Ardent; * * @package FireflyIII\Database */ -class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls +class AccountType implements CUD, CommonDatabaseCalls { /** @@ -66,20 +66,6 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls 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. * diff --git a/app/lib/FireflyIII/Database/Budget.php b/app/lib/FireflyIII/Database/Budget.php index 2d4a0474e5..5fc1eeb4f0 100644 --- a/app/lib/FireflyIII/Database/Budget.php +++ b/app/lib/FireflyIII/Database/Budget.php @@ -119,20 +119,6 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; } - /** - * 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. * diff --git a/app/lib/FireflyIII/Database/Category.php b/app/lib/FireflyIII/Database/Category.php index 5857f84326..c04d4aa6b7 100644 --- a/app/lib/FireflyIII/Database/Category.php +++ b/app/lib/FireflyIII/Database/Category.php @@ -15,7 +15,7 @@ use LaravelBook\Ardent\Ardent; * * @package FireflyIII\Database */ -class Category implements CUD, CommonDatabaseCalls, CategoryInterface +class Category implements CUD, CommonDatabaseCalls { use SwitchUser; @@ -118,20 +118,6 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; } - /** - * 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. * diff --git a/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php b/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php index 2871e988f7..614c476556 100644 --- a/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php +++ b/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php @@ -42,12 +42,6 @@ interface AccountInterface */ public function countRevenueAccounts(); - /** - * @param array $parameters - * - * @return Collection - */ - /** * @param \Account $account * @@ -59,38 +53,29 @@ interface AccountInterface * 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 = []); + public function getAccountsByType(array $types); /** * 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 = []); + public function getAssetAccounts(); /** - * Get all default accounts. - * * @return Collection */ - public function getDefaultAccounts(); - - public function getExpenseAccounts(array $parameters = []); + public function getExpenseAccounts(); /** * Get all revenue accounts. * - * @param array $parameters - * * @return Collection */ - public function getRevenueAccounts(array $parameters = []); + public function getRevenueAccounts(); /** * @param \Account $account diff --git a/app/lib/FireflyIII/Database/Ifaces/AccountTypeInterface.php b/app/lib/FireflyIII/Database/Ifaces/AccountTypeInterface.php deleted file mode 100644 index d06621d702..0000000000 --- a/app/lib/FireflyIII/Database/Ifaces/AccountTypeInterface.php +++ /dev/null @@ -1,13 +0,0 @@ -add('date', 'Invalid date.'); + $errors->add('targetdate', 'Invalid date.'); } } if (floatval($model['targetamount']) < 0.01) { @@ -183,20 +183,6 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; } - /** - * 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. * @@ -264,7 +250,7 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface */ public function leftOnAccount(\Account $account) { - $balance = Steam::balance($account); + $balance = \Steam::balance($account); /** @var \Piggybank $p */ foreach ($account->piggybanks()->get() as $p) { $balance -= $p->currentRelevantRep()->currentamount; diff --git a/app/lib/FireflyIII/Database/Recurring.php b/app/lib/FireflyIII/Database/Recurring.php index 6329cde2ec..b781b4e611 100644 --- a/app/lib/FireflyIII/Database/Recurring.php +++ b/app/lib/FireflyIII/Database/Recurring.php @@ -177,20 +177,6 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; } - /** - * 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. * diff --git a/app/lib/FireflyIII/Database/RepeatedExpense.php b/app/lib/FireflyIII/Database/RepeatedExpense.php new file mode 100644 index 0000000000..2b50bf6bf1 --- /dev/null +++ b/app/lib/FireflyIII/Database/RepeatedExpense.php @@ -0,0 +1,229 @@ +setUser(\Auth::user()); + } + + /** + * @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) + { + + $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; + $data['account_id'] = intval($data['account_id']); + + + if ($data['remind_me'] == 0) { + $data['reminder'] = null; + } + + + $repeated = new \Piggybank($data); + if (!$repeated->validate()) { + var_dump($repeated->errors()->all()); + exit; + } + $repeated->save(); + + return $repeated; + } + + /** + * @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) + { + $warnings = new MessageBag; + $successes = new MessageBag; + $errors = new MessageBag; + + /* + * Name validation: + */ + if (!isset($model['name'])) { + $errors->add('name', 'Name is mandatory'); + } + + if (isset($model['name']) && strlen($model['name']) == 0) { + $errors->add('name', 'Name is too short'); + } + if (isset($model['name']) && strlen($model['name']) > 100) { + $errors->add('name', 'Name is too long'); + } + + if (intval($model['account_id']) == 0) { + $errors->add('account_id', 'Account is mandatory'); + } + if ($model['targetdate'] == '' && isset($model['remind_me']) && intval($model['remind_me']) == 1) { + $errors->add('targetdate', 'Target date is mandatory when setting reminders.'); + } + if ($model['targetdate'] != '') { + try { + new Carbon($model['targetdate']); + } catch (\Exception $e) { + $errors->add('targetdate', 'Invalid date.'); + } + } else { + $errors->add('targetdate', 'Invalid target date.'); + } + if (floatval($model['targetamount']) < 0.01) { + $errors->add('targetamount', 'Amount should be above 0.01.'); + } + if (!in_array(ucfirst($model['reminder']), \Config::get('firefly.piggybank_periods'))) { + $errors->add('reminder', 'Invalid reminder period (' . $model['reminder'] . ')'); + } + + if (!in_array(ucfirst($model['rep_length']), \Config::get('firefly.piggybank_periods'))) { + $errors->add('rep_length', 'Invalid repeat period (' . $model['rep_length'] . ')'); + } + + // check period. + if (!$errors->has('reminder') && !$errors->has('targetdate') && isset($model['remind_me']) && intval($model['remind_me']) == 1) { + $today = new Carbon; + $target = new Carbon($model['targetdate']); + switch ($model['reminder']) { + case 'week': + $today->addWeek(); + break; + case 'month': + $today->addMonth(); + break; + case 'year': + $today->addYear(); + break; + } + if ($today > $target) { + $errors->add('reminder', 'Target date is too close to today to set reminders.'); + } + } + + $validator = \Validator::make($model, \Piggybank::$rules); + if ($validator->invalid()) { + $errors->merge($errors); + } + + // add ok messages. + $list = ['name', 'account_id', 'rep_every', 'rep_times', 'rep_length', 'targetamount', 'targetdate', 'remind_me', 'reminder']; + foreach ($list as $entry) { + if (!$errors->has($entry) && !$warnings->has($entry)) { + $successes->add($entry, 'OK'); + } + } + + return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; + } + + /** + * 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. + * + * @param $what + * + * @return \AccountType|null + */ + public function findByWhat($what) + { + // TODO: Implement findByWhat() method. + throw new NotImplementedException; + } + + /** + * Returns all objects. + * + * @return Collection + */ + public function get() + { + return $this->getUser()->piggybanks()->where('repeats', 1)->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) + { + // TODO: Implement leftOnAccount() 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 26a3d252d5..3ab54aea75 100644 --- a/app/lib/FireflyIII/Database/Transaction.php +++ b/app/lib/FireflyIII/Database/Transaction.php @@ -16,7 +16,7 @@ use LaravelBook\Ardent\Ardent; * * @package FireflyIII\Database */ -class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls +class Transaction implements CUD, CommonDatabaseCalls { use SwitchUser; @@ -135,20 +135,6 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; } - /** - * 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. * diff --git a/app/lib/FireflyIII/Database/TransactionCurrency.php b/app/lib/FireflyIII/Database/TransactionCurrency.php index 82a4639345..6de3860ada 100644 --- a/app/lib/FireflyIII/Database/TransactionCurrency.php +++ b/app/lib/FireflyIII/Database/TransactionCurrency.php @@ -3,132 +3,16 @@ namespace FireflyIII\Database; -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 * * @package FireflyIII\Database */ -class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDatabaseCalls +class TransactionCurrency implements TransactionCurrencyInterface { - - /** - * @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. - * - * @param $what - * - * @return \AccountType|null - */ - public function findByWhat($what) - { - // TODO: Implement findByWhat() method. - throw new NotImplementedException; - } - - /** - * Returns all objects. - * - * @return Collection - */ - public function get() - { - // TODO: Implement get() method. - throw new NotImplementedException; - } - - /** - * @param array $ids - * - * @return Collection - */ - public function getByIds(array $ids) - { - // TODO: Implement getByIds() method. - throw new NotImplementedException; - } - /** * @param string $code * diff --git a/app/lib/FireflyIII/Database/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal.php index b7c2ce5a75..aa6c8a8044 100644 --- a/app/lib/FireflyIII/Database/TransactionJournal.php +++ b/app/lib/FireflyIII/Database/TransactionJournal.php @@ -428,20 +428,6 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData } - /** - * 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. * diff --git a/app/lib/FireflyIII/Database/TransactionType.php b/app/lib/FireflyIII/Database/TransactionType.php index 8b4aad2fef..a0c215783d 100644 --- a/app/lib/FireflyIII/Database/TransactionType.php +++ b/app/lib/FireflyIII/Database/TransactionType.php @@ -16,7 +16,7 @@ use LaravelBook\Ardent\Ardent; * * @package FireflyIII\Database */ -class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCalls +class TransactionType implements CUD, CommonDatabaseCalls { /** @@ -67,20 +67,6 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa 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. *