Removed a bunch of methods and code that wasn't used. Also added a repository for repeated expenses.

This commit is contained in:
James Cole 2014-11-22 23:30:45 +01:00
parent 34454261d2
commit c6b6ed7fa8
18 changed files with 250 additions and 388 deletions

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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

View File

@ -1,13 +0,0 @@
<?php
namespace FireflyIII\Database\Ifaces;
/**
* Interface AccountTypeInterface
*
* @package FireflyIII\Database
*/
interface AccountTypeInterface
{
}

View File

@ -44,14 +44,4 @@ interface CUD
*/
public function validate(array $model);
/**
* Validates a model. Returns an array containing MessageBags
* errors/warnings/successes.
*
* @param Ardent $model
*
* @return array
*/
public function validateObject(Ardent $model);
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace FireflyIII\Database\Ifaces;
/**
* Interface CategoryInterface
*
* @package FireflyIII\Database
*/
interface CategoryInterface
{
}

View File

@ -1,12 +0,0 @@
<?php
namespace FireflyIII\Database\Ifaces;
/**
* Interface AccountInterface
*
* @package FireflyIII\Database
*/
interface RecurringTransactionInterface
{
}

View File

@ -1,14 +0,0 @@
<?php
namespace FireflyIII\Database\Ifaces;
/**
* Interface TransactionInterface
*
* @package FireflyIII\Database
*/
interface TransactionInterface
{
}

View File

@ -1,14 +0,0 @@
<?php
namespace FireflyIII\Database\Ifaces;
/**
* Interface TransactionTypeInterface
*
* @package FireflyIII\Database
*/
interface TransactionTypeInterface
{
}

View File

@ -138,7 +138,7 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
try {
new Carbon($model['targetdate']);
} catch (\Exception $e) {
$errors->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;

View File

@ -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.
*

View File

@ -0,0 +1,229 @@
<?php
namespace FireflyIII\Database;
use Carbon\Carbon;
use FireflyIII\Database\Ifaces\CommonDatabaseCalls;
use FireflyIII\Database\Ifaces\CUD;
use FireflyIII\Database\Ifaces\PiggybankInterface;
use FireflyIII\Exception\NotImplementedException;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use LaravelBook\Ardent\Ardent;
class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
{
use SwitchUser;
/**
*
*/
public function __construct()
{
$this->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;
}
}

View File

@ -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.
*

View File

@ -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
*

View File

@ -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.
*

View File

@ -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.
*