mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-18 04:19:12 +00:00
Code clean up and reformat.
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -14,7 +14,7 @@ interface BudgetInterface
|
||||
{
|
||||
/**
|
||||
* @param \Budget $budget
|
||||
* @param Carbon $date
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return \LimitRepetition|null
|
||||
*/
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Database\Ifaces;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
use LaravelBook\Ardent\Ardent;
|
||||
|
||||
/**
|
||||
* Interface CUD
|
||||
*
|
||||
* @package FireflyIII\Database
|
||||
*/
|
||||
interface CUD
|
||||
@@ -18,27 +19,6 @@ interface CUD
|
||||
*/
|
||||
public function destroy(Ardent $model);
|
||||
|
||||
|
||||
/**
|
||||
* Validates a model. Returns an array containing MessageBags
|
||||
* errors/warnings/successes.
|
||||
*
|
||||
* @param Ardent $model
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validateObject(Ardent $model);
|
||||
|
||||
/**
|
||||
* Validates an array. Returns an array containing MessageBags
|
||||
* errors/warnings/successes.
|
||||
*
|
||||
* @param array $model
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validate(array $model);
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
@@ -54,4 +34,24 @@ interface CUD
|
||||
*/
|
||||
public function update(Ardent $model, array $data);
|
||||
|
||||
/**
|
||||
* Validates an array. Returns an array containing MessageBags
|
||||
* errors/warnings/successes.
|
||||
*
|
||||
* @param array $model
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
|
||||
}
|
||||
@@ -2,9 +2,6 @@
|
||||
|
||||
namespace FireflyIII\Database\Ifaces;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Interface CategoryInterface
|
||||
*
|
||||
|
||||
@@ -22,21 +22,6 @@ interface CommonDatabaseCalls
|
||||
*/
|
||||
public function find($id);
|
||||
|
||||
/**
|
||||
* Returns all objects.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function get();
|
||||
|
||||
|
||||
/**
|
||||
* @param array $ids
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getByIds(array $ids);
|
||||
|
||||
/**
|
||||
* Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
|
||||
*
|
||||
@@ -46,4 +31,18 @@ interface CommonDatabaseCalls
|
||||
*/
|
||||
public function findByWhat($what);
|
||||
|
||||
/**
|
||||
* Returns all objects.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function get();
|
||||
|
||||
/**
|
||||
* @param array $ids
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getByIds(array $ids);
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Database\Ifaces;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace FireflyIII\Database\Ifaces;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Interface AccountInterface
|
||||
*
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Database\Ifaces;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@@ -11,6 +12,13 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
interface TransactionJournalInterface
|
||||
{
|
||||
/**
|
||||
* Get the very first transaction journal.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function first();
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -20,10 +28,11 @@ interface TransactionJournalInterface
|
||||
public function getInDateRange(Carbon $start, Carbon $end);
|
||||
|
||||
/**
|
||||
* Get the very first transaction journal.
|
||||
* @return mixed
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function first();
|
||||
public function getSumOfExpensesByMonth(Carbon $date);
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
@@ -32,11 +41,4 @@ interface TransactionJournalInterface
|
||||
*/
|
||||
public function getSumOfIncomesByMonth(Carbon $date);
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getSumOfExpensesByMonth(Carbon $date);
|
||||
|
||||
}
|
||||
@@ -27,23 +27,6 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
|
||||
$this->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;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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.*']);
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user