Moved some import routines to the repositories.

This commit is contained in:
James Cole 2014-09-14 21:07:43 +02:00
parent 19402b9022
commit 69cee59e23
14 changed files with 1852 additions and 490 deletions

View File

@ -2,6 +2,7 @@
namespace Firefly\Storage\Account;
use Illuminate\Queue\Jobs\Job;
/**
* Interface AccountRepositoryInterface
@ -11,6 +12,14 @@ namespace Firefly\Storage\Account;
interface AccountRepositoryInterface
{
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importAccount(Job $job, array $payload);
/**
* @return mixed
*/
@ -61,6 +70,16 @@ interface AccountRepositoryInterface
*/
public function findAccountType($type);
/**
* Takes a transaction/account component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransaction(Job $job, array $payload);
/**
* @return mixed
*/

View File

@ -4,6 +4,8 @@
namespace Firefly\Storage\Account;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Queue\Jobs\Job;
/**
* Class EloquentAccountRepository
@ -15,8 +17,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
protected $_user = null;
/**
*
*/
@ -25,41 +25,312 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$this->_user = \Auth::user();
}
public function firstOrCreate(array $data) {
return \Account::firstOrCreate($data);
}
/**
* Gets a list of accounts that have the mentioned type. Will automatically convert
* strings in this array to actual (model) account types.
* Takes a transaction/account component and updates the transaction journal to match.
*
* @param array $types
* @param Job $job
* @param array $payload
*
* @return Collection
*/
public function getOfTypes(array $types)
{
$accounts = $this->_user->accounts()->accountTypeIn($types)->get(['accounts.*']);
return $accounts;
}
/**
* @param $name
* @param \AccountType $type
*
* @return \Account
*/
public function findByNameAndAccountType($name, \AccountType $type) {
return $this->_user->accounts()->where('name',$name)->where('account_type_id',$type->id)->first();
}
/**
* @return mixed
*/
public function count()
public function importUpdateTransaction(Job $job, array $payload)
{
return $this->_user->accounts()->count();
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('Never found budget/account combination "' . $payload['data']['transaction_id'] . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed.
return;
}
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */
$journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$journals->overruleUser($user);
/*
* Prep some vars from the payload
*/
$transactionId = intval($payload['data']['transaction_id']);
$componentId = intval($payload['data']['component_id']);
/*
* Find the import map for both:
*/
$accountMap = $repository->findImportEntry($importMap, 'Account', $componentId);
$transactionMap = $repository->findImportEntry($importMap, 'Transaction', $transactionId);
/*
* Either may be null:
*/
if (is_null($accountMap) || is_null($transactionMap)) {
\Log::notice('No map found in account/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Find the account and the transaction:
*/
$account = $this->find($accountMap->new);
/** @var \TransactionJournal $journal */
$journal = $journals->find($transactionMap->new);
/*
* If either is null, release:
*/
if (is_null($account) || is_null($journal)) {
\Log::notice('Map is incorrect in account/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Update one of the journal's transactions to have the right account:
*/
$importType = $this->findAccountType('Import account');
/** @var \Transaction $transaction */
foreach($journal->transactions as $transaction) {
/*
* If it's of the right type, update it!
*/
if($transaction->account->account_type_id == $importType->id) {
$transaction->account()->associate($account);
$transaction->save();
}
}
$journal->save();
\Log::debug('Connected expense account "' . $account->name . '" to journal "' . $journal->description.'"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
}
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importAccount(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
/*
* maybe Account is already imported:
*/
$importEntry = $repository->findImportEntry($importMap, 'Account', intval($payload['data']['id']));
/*
* if so, delete job and return:
*/
if (!is_null($importEntry)) {
\Log::debug('Already imported ' . $payload['data']['name'] . ' of type ' . $payload['account_type']);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/*
* find the payload's account type:
*/
$payload['account_type'] = isset($payload['account_type']) ? $payload['account_type'] : 'Expense account';
$type = $this->findAccountType($payload['account_type']);
/*
* Create data array for store() procedure.
*/
$data = [
'account_type' => $type,
'name' => $payload['data']['name'],
];
if (isset($payload['data']['openingbalance'])) {
$data['openingbalance'] = floatval($payload['data']['openingbalance']);
$data['openingbalancedate'] = $payload['data']['openingbalancedate'];
}
if (isset($payload['data']['inactive'])) {
$data['active'] = intval($payload['data']['inactive']) == 0 ? 1 : 0;
}
/*
* Try to store:
*/
$account = $this->store($data);
/*
* Check for failure.
*/
if (count($account->errors()) > 0) {
\Log::error('Account creation error: ' . $account->errors()->first());
$importMap->jobsdone++;
$importMap->save();
$job->delete();// count fixed
return;
}
\Log::debug('Imported ' . $payload['account_type'] . ': ' . $payload['data']['name']);
/*
* Save meta data
*/
$repository->store($importMap, 'Account', intval($payload['data']['id']), $account->id);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed.
return;
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param $type
*
* @return mixed
*/
public function findAccountType($type)
{
return \AccountType::where('type', $type)->first();
}
/**
* @param $data
*
* @return \Account
* @throws \Firefly\Exception\FireflyException
*/
public function store($data)
{
/**
* If the AccountType has been passed through, use it:
*/
if (isset($data['account_type']) && is_object($data['account_type'])
&& get_class($data['account_type']) == 'AccountType'
) {
$accountType = $data['account_type'];
} else if (isset($data['account_type']) && is_string($data['account_type'])) {
// if it isnt but set as string, find it:
$accountType = \AccountType::where('type', $data['account_type'])->first();
} else {
$accountType = \AccountType::where('type', 'Asset account')->first();
}
$active = isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
$data['active']
) : 1;
/**
* Create new account:
*/
$account = new \Account;
$account->accountType()->associate($accountType);
$account->user()->associate($this->_user);
$account->name = $data['name'];
$account->active
=
isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
$data['active']
) : 1;
// try to save it:
try {
if ($account->save()) {
// create initial balance, if necessary:
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
$amount = floatval($data['openingbalance']);
$date = new Carbon($data['openingbalancedate']);
if ($amount != 0) {
$this->_createInitialBalance($account, $amount, $date);
}
}
}
} catch (QueryException $e) {
// do nothing
}
// whatever the result, return the account.
return $account;
}
/**
* @param \Account $account
* @param int $amount
* @param Carbon $date
*
* @return bool
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
{
// get account type:
$initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first();
// create new account:
$initial = new \Account;
$initial->accountType()->associate($initialBalanceAT);
$initial->user()->associate($this->_user);
$initial->name = $account->name . ' initial balance';
$initial->active = 0;
if ($initial->validate()) {
$initial->save();
// create new transaction journal (and transactions):
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
$transactionJournal = \App::make(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
);
$transactionJournal->overruleUser($this->_user);
$transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
);
return true;
}
return false;
}
//
// /**
@ -112,96 +383,43 @@ class EloquentAccountRepository implements AccountRepositoryInterface
// ->first();
// }
/**
* @param $data
*
* @return \Account
* @throws \Firefly\Exception\FireflyException
*/
public function store($data)
public function firstOrCreate(array $data)
{
/**
* If the AccountType has been passed through, use it:
*/
if (isset($data['account_type']) && is_object($data['account_type'])
&& get_class($data['account_type']) == 'AccountType'
) {
$accountType = $data['account_type'];
} else if (isset($data['account_type']) && is_string($data['account_type'])) {
// if it isnt but set as string, find it:
$accountType = \AccountType::where('type', $data['account_type'])->first();
} else {
$accountType = \AccountType::where('type', 'Asset account')->first();
}
/**
* Create new account:
*/
$account = new \Account;
$account->accountType()->associate($accountType);
$account->user()->associate($this->_user);
$account->name = $data['name'];
$account->active
=
isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
$data['active']
) : 1;
// try to save it:
if ($account->save()) {
// create initial balance, if necessary:
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
$amount = floatval($data['openingbalance']);
$date = new Carbon($data['openingbalancedate']);
if ($amount != 0) {
$this->_createInitialBalance($account, $amount, $date);
}
}
}
// whatever the result, return the account.
return $account;
return \Account::firstOrCreate($data);
}
/**
* @param \Account $account
* @param int $amount
* @param Carbon $date
* Gets a list of accounts that have the mentioned type. Will automatically convert
* strings in this array to actual (model) account types.
*
* @return bool
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
* @param array $types
*
* @return Collection
*/
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
public function getOfTypes(array $types)
{
// get account type:
$initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first();
$accounts = $this->_user->accounts()->accountTypeIn($types)->get(['accounts.*']);
return $accounts;
}
// create new account:
$initial = new \Account;
$initial->accountType()->associate($initialBalanceAT);
$initial->user()->associate($this->_user);
$initial->name = $account->name . ' initial balance';
$initial->active = 0;
if ($initial->validate()) {
$initial->save();
// create new transaction journal (and transactions):
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
$transactionJournal = \App::make(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
);
$transactionJournal->overruleUser($this->_user);
/**
* @param $name
* @param \AccountType $type
*
* @return \Account
*/
public function findByNameAndAccountType($name, \AccountType $type)
{
return $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first();
}
$transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
);
/**
* @return mixed
*/
public function count()
{
return $this->_user->accounts()->count();
return true;
}
return false;
}
/**
@ -245,26 +463,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return true;
}
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
return $this->_user->accounts()->where('id', $accountId)->first();
}
/**
* @param $type
*
* @return mixed
*/
public function findAccountType($type)
{
return \AccountType::where('type', $type)->first();
}
// /**
// * Used for import
// *
@ -280,11 +478,13 @@ class EloquentAccountRepository implements AccountRepositoryInterface
// }
/**
* @param $accountId
*
* @return mixed
*/
public function get()
public function find($accountId)
{
return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
return $this->_user->accounts()->where('id', $accountId)->first();
}
// /**
@ -304,13 +504,9 @@ class EloquentAccountRepository implements AccountRepositoryInterface
/**
* @return mixed
*/
public function getActiveDefault()
public function get()
{
return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])->where(
'accounts.active', 1
)
->get(['accounts.*']);
return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
}
// /**
@ -347,6 +543,18 @@ class EloquentAccountRepository implements AccountRepositoryInterface
}
}
/**
* @return mixed
*/
public function getActiveDefault()
{
return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])->where(
'accounts.active', 1
)
->get(['accounts.*']);
}
/**
* @return mixed
*/
@ -376,17 +584,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param \Account $account
* @param $data
@ -424,4 +621,5 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $account;
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Firefly\Storage\Budget;
use Illuminate\Queue\Jobs\Job;
/**
* Interface BudgetRepositoryInterface
@ -9,6 +10,34 @@ namespace Firefly\Storage\Budget;
*/
interface BudgetRepositoryInterface
{
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importBudget(Job $job, array $payload);
/**
* Takes a transaction/budget component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransaction(Job $job, array $payload);
/**
* Takes a transfer/budget component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransfer(Job $job, array $payload);
/**
* @param \Budget $budget
*

View File

@ -4,6 +4,7 @@ namespace Firefly\Storage\Budget;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Queue\Jobs\Job;
/**
* Class EloquentBudgetRepository
@ -15,6 +16,7 @@ use Illuminate\Database\Eloquent\Collection;
*/
class EloquentBudgetRepository implements BudgetRepositoryInterface
{
protected $_user = null;
/**
@ -26,79 +28,70 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
}
/**
* @param \Budget $budget
* @param Job $job
* @param array $payload
*
* @return bool
* @return mixed
*/
public function destroy(\Budget $budget)
public function importBudget(Job $job, array $payload)
{
$budget->delete();
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
return true;
}
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
/**
* @param $budgetId
*
* @return \Budget|null
*/
public function find($budgetId)
{
/*
* maybe Budget is already imported:
*/
$importEntry = $repository->findImportEntry($importMap, 'Budget', intval($payload['data']['id']));
return $this->_user->budgets()->find($budgetId);
}
/*
* if so, delete job and return:
*/
if (!is_null($importEntry)) {
\Log::debug('Already imported budget ' . $payload['data']['name']);
/**
* @param $budgetName
* @return \Budget|null
*/
public function findByName($budgetName)
{
$importMap->jobsdone++;
$importMap->save();
return $this->_user->budgets()->whereName($budgetName)->first();
}
/**
* @return Collection
*/
public function get()
{
$set = $this->_user->budgets()->with(
['limits' => function ($q) {
$q->orderBy('limits.startdate', 'DESC');
}, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
}]
)->orderBy('name', 'ASC')->get();
foreach ($set as $budget) {
foreach ($budget->limits as $limit) {
foreach ($limit->limitrepetitions as $rep) {
$rep->left = $rep->left();
}
}
$job->delete(); // count fixed
return;
}
return $set;
}
/*
* maybe Budget is already imported.
*/
$budget = $this->findByName($payload['data']['name']);
/**
* @return array
*/
public function getAsSelectList()
{
$list = $this->_user->budgets()->with(
['limits', 'limits.limitrepetitions']
)->orderBy('name', 'ASC')->get();
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
if (is_null($budget)) {
/*
* Not imported yet.
*/
$budget = $this->store($payload['data']);
$repository->store($importMap, 'Budget', $payload['data']['id'], $budget->id);
\Log::debug('Imported budget "' . $payload['data']['name'] . '".');
} else {
/*
* already imported.
*/
$repository->store($importMap, 'Budget', $payload['data']['id'], $budget->id);
\Log::debug('Already had budget "' . $payload['data']['name'] . '".');
}
return $return;
// update map:
$importMap->jobsdone++;
$importMap->save();
// delete job.
$job->delete(); // count fixed
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
@ -107,6 +100,17 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return true;
}
/**
* @param $budgetName
*
* @return \Budget|null
*/
public function findByName($budgetName)
{
return $this->_user->budgets()->whereName($budgetName)->first();
}
/**
* @param $data
*
@ -135,7 +139,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$limit = $limitRepository->store($limitData);
\Event::fire('limits.store', [$limit]);
}
if ($budget->validate()) {
$budget->save();
}
@ -143,6 +147,264 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return $budget;
}
/**
* Takes a transfer/budget component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransfer(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('Never found budget/transfer combination "' . $payload['data']['transfer_id'] . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */
$journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$journals->overruleUser($user);
/*
* Prep some vars from the payload
*/
$transferId = intval($payload['data']['transfer_id']);
$componentId = intval($payload['data']['component_id']);
/*
* Find the import map for both:
*/
$budgetMap = $repository->findImportEntry($importMap, 'Budget', $componentId);
$transferMap = $repository->findImportEntry($importMap, 'Transfer', $transferId);
/*
* Either may be null:
*/
if (is_null($budgetMap) || is_null($transferMap)) {
\Log::notice('No map found in budget/transfer mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Find the budget and the transaction:
*/
$budget = $this->find($budgetMap->new);
/** @var \TransactionJournal $journal */
$journal = $journals->find($transferMap->new);
/*
* If either is null, release:
*/
if (is_null($budget) || is_null($journal)) {
\Log::notice('Map is incorrect in budget/transfer mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Update journal to have budget:
*/
$journal->budgets()->save($budget);
$journal->save();
\Log::debug('Connected budget "' . $budget->name . '" to journal "' . $journal->description . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/**
* Takes a transaction/budget component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransaction(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('Never found budget/transaction combination "' . $payload['data']['transaction_id'] . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */
$journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$journals->overruleUser($user);
/*
* Prep some vars from the payload
*/
$transactionId = intval($payload['data']['transaction_id']);
$componentId = intval($payload['data']['component_id']);
/*
* Find the import map for both:
*/
$budgetMap = $repository->findImportEntry($importMap, 'Budget', $componentId);
$transactionMap = $repository->findImportEntry($importMap, 'Transaction', $transactionId);
/*
* Either may be null:
*/
if (is_null($budgetMap) || is_null($transactionMap)) {
\Log::notice('No map found in budget/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Find the budget and the transaction:
*/
$budget = $this->find($budgetMap->new);
/** @var \TransactionJournal $journal */
$journal = $journals->find($transactionMap->new);
/*
* If either is null, release:
*/
if (is_null($budget) || is_null($journal)) {
\Log::notice('Map is incorrect in budget/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Update journal to have budget:
*/
$journal->budgets()->save($budget);
$journal->save();
\Log::debug('Connected budget "' . $budget->name . '" to journal "' . $journal->description . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/**
* @param $budgetId
*
* @return \Budget|null
*/
public function find($budgetId)
{
return $this->_user->budgets()->find($budgetId);
}
/**
* @param \Budget $budget
*
* @return bool
*/
public function destroy(\Budget $budget)
{
$budget->delete();
return true;
}
/**
* @return Collection
*/
public function get()
{
$set = $this->_user->budgets()->with(
['limits' => function ($q) {
$q->orderBy('limits.startdate', 'DESC');
}, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
}]
)->orderBy('name', 'ASC')->get();
foreach ($set as $budget) {
foreach ($budget->limits as $limit) {
foreach ($limit->limitrepetitions as $rep) {
$rep->left = $rep->left();
}
}
}
return $set;
}
/**
* @return array
*/
public function getAsSelectList()
{
$list = $this->_user->budgets()->with(
['limits', 'limits.limitrepetitions']
)->orderBy('name', 'ASC')->get();
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
}
return $return;
}
/**
* @param \Budget $budget
* @param $data

View File

@ -1,6 +1,7 @@
<?php
namespace Firefly\Storage\Category;
use Illuminate\Queue\Jobs\Job;
/**
* Interface CategoryRepositoryInterface
@ -9,6 +10,33 @@ namespace Firefly\Storage\Category;
*/
interface CategoryRepositoryInterface
{
/**
* Takes a transaction/category component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransaction(Job $job, array $payload);
/**
* Takes a transfer/category component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransfer(Job $job, array $payload);
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importCategory(Job $job, array $payload);
/**
* @return mixed

View File

@ -2,6 +2,8 @@
namespace Firefly\Storage\Category;
use Illuminate\Queue\Jobs\Job;
/**
* Class EloquentCategoryRepository
*
@ -19,6 +21,314 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
$this->_user = \Auth::user();
}
/**
* Takes a transfer/category component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransfer(Job $job, array $payload) {
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('Never found category/transfer combination "' . $payload['data']['transfer_id'] . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed.
return;
}
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */
$journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$journals->overruleUser($user);
/*
* Prep some vars from the payload
*/
$transferId = intval($payload['data']['transfer_id']);
$componentId = intval($payload['data']['component_id']);
/*
* Find the import map for both:
*/
$categoryMap = $repository->findImportEntry($importMap, 'Category', $componentId);
$transferMap = $repository->findImportEntry($importMap, 'Transfer', $transferId);
/*
* Either may be null:
*/
if (is_null($categoryMap) || is_null($transferMap)) {
\Log::notice('No map found in category/transfer mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Find the budget and the transaction:
*/
$category = $this->find($categoryMap->new);
/** @var \TransactionJournal $journal */
$journal = $journals->find($transferMap->new);
/*
* If either is null, release:
*/
if (is_null($category) || is_null($journal)) {
\Log::notice('Map is incorrect in category/transfer mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Update journal to have budget:
*/
$journal->categories()->save($category);
$journal->save();
\Log::debug('Connected category "' . $category->name . '" to journal "' . $journal->description . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/**
* Takes a transaction/category component and updates the transaction journal to match.
*
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importUpdateTransaction(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('Never found category/transaction combination "' . $payload['data']['transaction_id'] . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed.
return;
}
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $journals */
$journals = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$journals->overruleUser($user);
/*
* Prep some vars from the payload
*/
$transactionId = intval($payload['data']['transaction_id']);
$componentId = intval($payload['data']['component_id']);
/*
* Find the import map for both:
*/
$categoryMap = $repository->findImportEntry($importMap, 'Category', $componentId);
$transactionMap = $repository->findImportEntry($importMap, 'Transaction', $transactionId);
/*
* Either may be null:
*/
if (is_null($categoryMap) || is_null($transactionMap)) {
\Log::notice('No map found in category/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Find the budget and the transaction:
*/
$category = $this->find($categoryMap->new);
/** @var \TransactionJournal $journal */
$journal = $journals->find($transactionMap->new);
/*
* If either is null, release:
*/
if (is_null($category) || is_null($journal)) {
\Log::notice('Map is incorrect in category/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Update journal to have budget:
*/
$journal->categories()->save($category);
$journal->save();
\Log::debug('Connected category "' . $category->name . '" to journal "' . $journal->description . '"');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param $categoryId
*
* @return mixed
*/
public function find($categoryId)
{
return $this->_user->categories()->find($categoryId);
}
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importCategory(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
/*
* Maybe the category has already been imported
*/
$importEntry = $repository->findImportEntry($importMap, 'Category', intval($payload['data']['id']));
/*
* if so, delete job and return:
*/
if (!is_null($importEntry)) {
\Log::debug('Already imported category ' . $payload['data']['name']);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/*
* try to find category first
*/
$current = $this->findByName($payload['data']['name']);
/*
* If not found, create it:
*/
if (is_null($current)) {
$category = $this->store($payload['data']);
$repository->store($importMap, 'Category', $payload['data']['id'], $category->id);
\Log::debug('Imported category "' . $payload['data']['name'] . '".');
} else {
$repository->store($importMap, 'Category', $payload['data']['id'], $current->id);
\Log::debug('Already had category "' . $payload['data']['name'] . '".');
}
// update map:
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/**
* @param $name
*
* @return mixed
*/
public function findByName($name)
{
if ($name == '' || strlen($name) == 0) {
return null;
}
return $this->_user->categories()->where('name', $name)->first();
}
/**
* @param $data
*
* @return \Category|mixed
*/
public function store($data)
{
$category = new \Category;
$category->name = $data['name'];
$category->user()->associate($this->_user);
$category->save();
return $category;
}
/**
* @param $name
*
@ -51,31 +361,6 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
return true;
}
/**
* @param $categoryId
*
* @return mixed
*/
public function find($categoryId)
{
return $this->_user->categories()->find($categoryId);
}
/**
* @param $name
*
* @return mixed
*/
public function findByName($name)
{
if ($name == '' || strlen($name) == 0) {
return null;
}
return $this->_user->categories()->where('name', 'LIKE', '%' . $name . '%')->first();
}
/**
* @return mixed
*/
@ -84,22 +369,6 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
return $this->_user->categories()->orderBy('name', 'ASC')->get();
}
/**
* @param $data
*
* @return \Category|mixed
*/
public function store($data)
{
$category = new \Category;
$category->name = $data['name'];
$category->user()->associate($this->_user);
$category->save();
return $category;
}
/**
* @param $category
* @param $data
@ -116,14 +385,4 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
return $category;
}
/**
* @param \User $user
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
}

View File

@ -4,6 +4,7 @@ namespace Firefly\Storage\Limit;
use Carbon\Carbon;
use Illuminate\Queue\Jobs\Job;
/**
* Class EloquentLimitRepository
@ -23,14 +24,108 @@ class EloquentLimitRepository implements LimitRepositoryInterface
}
/**
* @param \Limit $limit
* @param Job $job
* @param array $payload
*
* @return bool
* @return mixed
*/
public function destroy(\Limit $limit)
public function importLimit(Job $job, array $payload)
{
$limit->delete();
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error(
'No budget found for limit #' . $payload['data']['id'] . '. Prob. for another component. KILL!'
);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed.
return;
}
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgets */
$budgets = \App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets->overruleUser($user);
/*
* Find the budget this limit is part of:
*/
$importEntry = $repository->findImportEntry($importMap, 'Budget', intval($payload['data']['component_id']));
/*
* There is no budget (yet?)
*/
if (is_null($importEntry)) {
$componentId = intval($payload['data']['component_id']);
\Log::warning('Budget #' . $componentId . ' not found. Requeue import job.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Find budget import limit is for:
*/
$budget = $budgets->find($importEntry->new);
if (!is_null($budget)) {
/*
* Is actual limit already imported?
*/
$limit = $this->findByBudgetAndDate($budget, new Carbon($payload['data']['date']));
if (is_null($limit)) {
/*
* It isn't imported yet.
*/
$payload['data']['budget_id'] = $budget->id;
$payload['data']['startdate'] = $payload['data']['date'];
$payload['data']['period'] = 'monthly';
/*
* Store limit, and fire event for LimitRepetition.
*/
$limit = $this->store($payload['data']);
$repository->store($importMap, 'Limit', $payload['data']['id'], $limit->id);
\Event::fire('limits.store', [$limit]);
\Log::debug('Imported limit for budget ' . $budget->name);
} else {
/*
* Limit already imported:
*/
$repository->store($importMap, 'Budget', $payload['data']['id'], $limit->id);
}
} else {
\Log::error(print_r($importEntry,true));
\Log::error('Cannot import limit! Big bad error!');
}
// update map:
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
@ -42,9 +137,9 @@ class EloquentLimitRepository implements LimitRepositoryInterface
public function find($limitId)
{
return \Limit::with('limitrepetitions')->where('limits.id', $limitId)->leftJoin(
'components', 'components.id', '=', 'limits.component_id'
'components', 'components.id', '=', 'limits.component_id'
)
->where('components.user_id', $this->_user->id)->first(['limits.*']);
->where('components.user_id', $this->_user->id)->first(['limits.*']);
}
public function findByBudgetAndDate(\Budget $budget, Carbon $date)
@ -52,31 +147,6 @@ class EloquentLimitRepository implements LimitRepositoryInterface
return \Limit::whereComponentId($budget->id)->where('startdate', $date->format('Y-m-d'))->first();
}
/**
* @param \Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end)
{
$result = $budget->transactionjournals()->with('transactions')->after($start)->before($end)->get();
return $result;
}
/**
* @param \User $user
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param $data
*
@ -121,9 +191,9 @@ class EloquentLimitRepository implements LimitRepositoryInterface
// find existing:
$count = \Limit::
leftJoin('components', 'components.id', '=', 'limits.component_id')->where(
'components.user_id', $this->_user->id
'components.user_id', $this->_user->id
)->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where(
'repeat_freq', $data['period']
'repeat_freq', $data['period']
)->count();
if ($count > 0) {
\Session::flash('error', 'There already is an entry for these parameters.');
@ -144,6 +214,33 @@ class EloquentLimitRepository implements LimitRepositoryInterface
return $limit;
}
/**
* @param \Limit $limit
*
* @return bool
*/
public function destroy(\Limit $limit)
{
$limit->delete();
return true;
}
/**
* @param \Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end)
{
$result = $budget->transactionjournals()->with('transactions')->after($start)->before($end)->get();
return $result;
}
/**
* @param \Limit $limit
* @param $data

View File

@ -3,6 +3,7 @@
namespace Firefly\Storage\Limit;
use Carbon\Carbon;
use Illuminate\Queue\Jobs\Job;
/**
* Interface LimitRepositoryInterface
@ -12,6 +13,14 @@ use Carbon\Carbon;
interface LimitRepositoryInterface
{
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importLimit(Job $job, array $payload);
/**
* @param \Limit $limit
*
@ -28,15 +37,16 @@ interface LimitRepositoryInterface
/**
* @param \Budget $budget
* @param Carbon $date
* @param Carbon $date
*
* @return mixed
*/
public function findByBudgetAndDate(\Budget $budget, Carbon $date);
/**
* @param \Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
@ -59,6 +69,7 @@ interface LimitRepositoryInterface
/**
* @param \User $user
*
* @return mixed
*/
public function overruleUser(\User $user);

View File

@ -4,6 +4,8 @@ namespace Firefly\Storage\Piggybank;
use Carbon\Carbon;
use Firefly\Exception\FireflyException;
use Illuminate\Queue\Jobs\Job;
use Illuminate\Support\Collection;
/**
@ -24,20 +26,151 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
$this->_user = \Auth::user();
}
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importPiggybank(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('No account available for piggy bank "' . $payload['data']['name'] . '". KILL!');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->overruleUser($user);
/*
* Maybe the piggy bank has already been imported
*/
$importEntry = $repository->findImportEntry($importMap, 'Piggybank', intval($payload['data']['id']));
/*
* if so, delete job and return:
*/
if (!is_null($importEntry)) {
\Log::debug('Already imported piggy bank ' . $payload['data']['name']);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/*
* Try to find related piggybank:
*/
$piggyBank = $this->findByName($payload['data']['name']);
/*
* Find an account (any account, really, at this point).
*/
$accountType = $accounts->findAccountType('Asset account');
/** @var Collection $set */
$set = $accounts->getByAccountType($accountType);
/*
* If there is an account to attach to this piggy bank, simply use that one.
*/
if ($set->count() > 0) {
/** @var \Account $account */
$account = $set->first();
$payload['data']['account_id'] = $account->id;
} else {
\Log::notice('No account available yet for piggy bank "' . $payload['data']['name'] . '".');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* No existing piggy bank, create it:
*/
if (is_null($piggyBank)) {
$payload['data']['targetamount'] = floatval($payload['data']['target']);
$payload['data']['repeats'] = 0;
$payload['data']['rep_every'] = 1;
$payload['data']['reminder_skip'] = 1;
$payload['data']['rep_times'] = 1;
$piggyBank = $this->store($payload['data']);
/*
* Store and fire event.
*/
$repository->store($importMap, 'Piggybank', intval($payload['data']['id']), $piggyBank->id);
\Log::debug('Imported piggy "' . $payload['data']['name'] . '".');
\Event::fire('piggybanks.store', [$piggyBank]);
} else {
/*
* Already have a piggy bank with this name, we skip it.
*/
$this->_repository->store($importMap, 'Piggybank', $payload['data']['id'], $piggyBank->id);
\Log::debug('Already imported piggy "' . $payload['data']['name'] . '".');
}
// update map:
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
public function findByName($piggyBankName)
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', $this->_user->id
)->where('piggybanks.name', $piggyBankName)->first(['piggybanks.*']);
}
/**
* @return mixed
*/
public function count()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', $this->_user->id
'accounts.user_id', $this->_user->id
)->count();
}
public function countNonrepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', $this->_user->id
'accounts.user_id', $this->_user->id
)->where('repeats', 0)->count();
}
@ -45,7 +178,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
public function countRepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', $this->_user->id
'accounts.user_id', $this->_user->id
)->where('repeats', 1)->count();
}
@ -69,17 +202,10 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
public function find($piggyBankId)
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', $this->_user->id
'accounts.user_id', $this->_user->id
)->where('piggybanks.id', $piggyBankId)->first(['piggybanks.*']);
}
public function findByName($piggyBankName)
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', $this->_user->id
)->where('piggybanks.name', $piggyBankName)->first(['piggybanks.*']);
}
/**
* @return mixed
*/
@ -130,16 +256,6 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
}
/**
* @param \User $user
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param $data
*
@ -160,7 +276,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->overruleUser($this->_user);
$account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null;
$account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null;
$piggyBank = new \Piggybank($data);
@ -216,7 +332,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
}
if ($firstReminder > $piggyBank->targetdate) {
$piggyBank->errors()->add(
'reminder', 'The reminder has been set to remind you after the piggy bank will expire.'
'reminder', 'The reminder has been set to remind you after the piggy bank will expire.'
);
\Log::error('PiggyBank create-error: ' . $piggyBank->errors()->first());
@ -241,7 +357,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->overruleUser($this->_user);
$account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null;
$account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null;
if (!is_null($account)) {
$piggy->account()->associate($account);
@ -253,7 +369,8 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
$piggy->reminder_skip = $data['reminder_skip'];
$piggy->targetdate = strlen($data['targetdate']) > 0 ? new Carbon($data['targetdate']) : null;
$piggy->startdate
= isset($data['startdate']) && strlen($data['startdate']) > 0 ? new Carbon($data['startdate']) : null;
=
isset($data['startdate']) && strlen($data['startdate']) > 0 ? new Carbon($data['startdate']) : null;
foreach ($piggy->piggybankrepetitions()->get() as $rep) {

View File

@ -2,6 +2,8 @@
namespace Firefly\Storage\Piggybank;
use Illuminate\Queue\Jobs\Job;
/**
* Interface LimitRepositoryInterface
@ -11,6 +13,14 @@ namespace Firefly\Storage\Piggybank;
interface PiggybankRepositoryInterface
{
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importPiggybank(Job $job, array $payload);
/**
* @return mixed
*/
@ -81,6 +91,7 @@ interface PiggybankRepositoryInterface
/**
* @param \User $user
*
* @return mixed
*/
public function overruleUser(\User $user);

View File

@ -4,6 +4,7 @@
namespace Firefly\Storage\RecurringTransaction;
use Carbon\Carbon;
use Illuminate\Queue\Jobs\Job;
/**
* Class EloquentRecurringTransactionRepository
@ -13,16 +14,6 @@ use Carbon\Carbon;
class EloquentRecurringTransactionRepository implements RecurringTransactionRepositoryInterface
{
/**
* @param \User $user
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
protected $_user = null;
/**
@ -34,14 +25,81 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
}
/**
* @param \RecurringTransaction $recurringTransaction
* @param Job $job
* @param array $payload
*
* @return bool|mixed
* @return mixed
*/
public function destroy(\RecurringTransaction $recurringTransaction)
public function importPredictable(Job $job, array $payload)
{
$recurringTransaction->delete();
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
/*
* maybe the recurring transaction is already imported:
*/
$oldId = intval($payload['data']['id']);
$description = $payload['data']['description'];
$importEntry = $repository->findImportEntry($importMap, 'RecurringTransaction', $oldId);
/*
* if so, delete job and return:
*/
if (!is_null($importEntry)) {
\Log::debug('Already imported recurring transaction #' . $payload['data']['id']);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
// try to find related recurring transaction:
$recurringTransaction = $this->findByName($payload['data']['description']);
if (is_null($recurringTransaction)) {
$amount = floatval($payload['data']['amount']);
$pct = intval($payload['data']['pct']);
$set = [
'name' => $description,
'match' => join(',', explode(' ', $description)),
'amount_min' => $amount * ($pct / 100) * -1,
'amount_max' => $amount * (1 + ($pct / 100)) * -1,
'date' => date('Y-m-') . $payload['data']['dom'],
'repeat_freq' => 'monthly',
'active' => intval($payload['data']['inactive']) == 1 ? 0 : 1,
'automatch' => 1,
];
$recurringTransaction = $this->store($set);
$this->store($importMap, 'RecurringTransaction', $oldId, $recurringTransaction->id);
\Log::debug('Imported predictable ' . $description);
} else {
$this->store($importMap, 'RecurringTransaction', $oldId, $recurringTransaction->id);
\Log::debug('Already had predictable ' . $description);
}
// update map:
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
@ -50,14 +108,6 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
return $this->_user->recurringtransactions()->where('name', 'LIKE', '%' . $name . '%')->first();
}
/**
* @return mixed
*/
public function get()
{
return $this->_user->recurringtransactions()->get();
}
/**
* @param $data
*
@ -92,6 +142,26 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
return $recurringTransaction;
}
/**
* @param \RecurringTransaction $recurringTransaction
*
* @return bool|mixed
*/
public function destroy(\RecurringTransaction $recurringTransaction)
{
$recurringTransaction->delete();
return true;
}
/**
* @return mixed
*/
public function get()
{
return $this->_user->recurringtransactions()->get();
}
/**
* @param \RecurringTransaction $recurringTransaction
* @param $data

View File

@ -2,6 +2,7 @@
namespace Firefly\Storage\RecurringTransaction;
use Illuminate\Queue\Jobs\Job;
/**
* Interface RecurringTransactionRepositoryInterface
@ -11,6 +12,14 @@ namespace Firefly\Storage\RecurringTransaction;
interface RecurringTransactionRepositoryInterface
{
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importPredictable(Job $job, array $payload);
/**
* @return mixed
*/

View File

@ -5,6 +5,7 @@ namespace Firefly\Storage\TransactionJournal;
use Carbon\Carbon;
use Firefly\Exception\FireflyException;
use Illuminate\Queue\Jobs\Job;
/**
* Class EloquentTransactionJournalRepository
@ -13,7 +14,6 @@ use Firefly\Exception\FireflyException;
*/
class EloquentTransactionJournalRepository implements TransactionJournalRepositoryInterface
{
protected $_user = null;
/**
@ -24,6 +24,390 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$this->_user = \Auth::user();
}
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importTransfer(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('Never found accounts for transfer "' . $payload['data']['description'] . '". KILL!');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->overruleUser($user);
/*
* Prep some variables from the payload:
*/
$fromAccountId = intval($payload['data']['accountfrom_id']);
$toAccountId = intval($payload['data']['accountto_id']);
$description = $payload['data']['description'];
$transferId = intval($payload['data']['id']);
$amount = floatval($payload['data']['amount']);
$date = new Carbon($payload['data']['date']);
/*
* maybe Journal is already imported:
*/
$importEntry = $repository->findImportEntry($importMap, 'Transfer', $transferId);
/*
* if so, delete job and return:
*/
if (!is_null($importEntry)) {
\Log::debug('Already imported transfer ' . $description);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/*
* Find the 'from' account:
*/
$oldFromAccountEntry = $repository->findImportEntry($importMap, 'Account', $fromAccountId);
$accountFrom = $accounts->find($oldFromAccountEntry->new);
/*
* Find the 'to' account:
*/
$oldToAccountEntry = $repository->findImportEntry($importMap, 'Account', $toAccountId);
$accountTo = $accounts->find($oldToAccountEntry->new);
/*
* If either is NULL, wait a bit and then reschedule.
*/
if (is_null($accountTo) || is_null($accountFrom)) {
\Log::notice('No account to, or account from. Release transfer ' . $description);
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* Import transfer:
*/
$journal = $this->createSimpleJournal($accountFrom, $accountTo, $description, $amount, $date);
$repository->store($importMap, 'Transfer', $transferId, $journal->id);
\Log::debug('Imported transfer "' . $description . '" (' . $amount . ') (' . $date->format('Y-m-d') . ')');
// update map:
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed.
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
*
* We're building this thinking the money goes from A to B.
* If the amount is negative however, the money still goes
* from A to B but the balances are reversed.
*
* Aka:
*
* Amount = 200
* A loses 200 (-200). * -1
* B gains 200 (200). * 1
*
* Final balance: -200 for A, 200 for B.
*
* When the amount is negative:
*
* Amount = -200
* A gains 200 (200). * -1
* B loses 200 (-200). * 1
*
* @param \Account $from
* @param \Account $toAccount
* @param $description
* @param $amount
* @param \Carbon\Carbon $date
*
* @return \TransactionJournal
* @throws \Firefly\Exception\FireflyException
*/
public function createSimpleJournal(\Account $fromAccount, \Account $toAccount, $description, $amount, Carbon $date)
{
$journal = new \TransactionJournal;
$journal->completed = false;
$journal->description = $description;
$journal->date = $date;
$amountFrom = $amount * -1;
$amountTo = $amount;
if (round(floatval($amount), 2) == 0.00) {
$journal->errors()->add('amount', 'Amount must not be zero.');
return $journal;
}
// account types for both:
$toAT = $toAccount->accountType->type;
$fromAT = $fromAccount->accountType->type;
$journalType = null;
switch (true) {
case ($fromAccount->transactions()->count() == 0 && $toAccount->transactions()->count() == 0):
$journalType = \TransactionType::where('type', 'Opening balance')->first();
break;
case (in_array($fromAT, ['Default account', 'Asset account'])
&& in_array(
$toAT, ['Default account', 'Asset account']
)): // both are yours:
// determin transaction type. If both accounts are new, it's an initial balance transfer.
$journalType = \TransactionType::where('type', 'Transfer')->first();
break;
case ($amount < 0):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is deposit into one of your own accounts:
case ($toAT == 'Default account' || $toAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is withdrawal from one of your own accounts:
case ($fromAT == 'Default account' || $fromAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Withdrawal')->first();
break;
}
if (is_null($journalType)) {
throw new FireflyException('Could not figure out transaction type.');
}
// always the same currency:
$currency = \TransactionCurrency::where('code', 'EUR')->first();
if (is_null($currency)) {
throw new FireflyException('No currency for journal!');
}
// new journal:
$journal->transactionType()->associate($journalType);
$journal->transactionCurrency()->associate($currency);
$journal->user()->associate($this->_user);
// same account:
if ($fromAccount->id == $toAccount->id) {
$journal->errors()->add('account_to_id', 'Must be different from the "account from".');
$journal->errors()->add('account_from_id', 'Must be different from the "account to".');
return $journal;
}
if (!$journal->validate()) {
return $journal;
}
$journal->save();
// create transactions:
$fromTransaction = new \Transaction;
$fromTransaction->account()->associate($fromAccount);
$fromTransaction->transactionJournal()->associate($journal);
$fromTransaction->description = null;
$fromTransaction->amount = $amountFrom;
if (!$fromTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()
->first());
}
$fromTransaction->save();
$toTransaction = new \Transaction;
$toTransaction->account()->associate($toAccount);
$toTransaction->transactionJournal()->associate($journal);
$toTransaction->description = null;
$toTransaction->amount = $amountTo;
if (!$toTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (to): ' . $toTransaction->errors()->first()
. ': ' . print_r($toAccount->toArray(), true));
}
$toTransaction->save();
$journal->completed = true;
$journal->save();
return $journal;
}
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importTransaction(Job $job, array $payload)
{
/** @var \Firefly\Storage\Import\ImportRepositoryInterface $repository */
$repository = \App::make('Firefly\Storage\Import\ImportRepositoryInterface');
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$this->overruleUser($user);
if ($job->attempts() > 10) {
\Log::error('Never found asset account for transaction "' . $payload['data']['description'] . '". KILL!');
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->overruleUser($user);
/*
* Prep some vars coming out of the pay load:
*/
$amount = floatval($payload['data']['amount']);
$date = new Carbon($payload['data']['date']);
$description = $payload['data']['description'];
$transactionId = intval($payload['data']['id']);
$accountId = intval($payload['data']['account_id']);
/*
* maybe Journal is already imported:
*/
$importEntry = $repository->findImportEntry($importMap, 'Transaction', $transactionId);
/*
* if so, delete job and return:
*/
if (!is_null($importEntry)) {
\Log::debug('Already imported transaction ' . $description);
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/*
* Find or create the "import account" which is used because at this point, Firefly
* doesn't know which beneficiary (expense account) should be connected to this transaction.
*/
$accountType = $accounts->findAccountType('Import account');
$importAccount = $accounts->firstOrCreate(
[
'account_type_id' => $accountType->id,
'name' => 'Import account',
'user_id' => $user->id,
'active' => 1,
]
);
unset($accountType);
/*
* Find the asset account this transaction is paid from / paid to:
*/
$accountEntry = $repository->findImportEntry($importMap, 'Account', $accountId);
$assetAccount = $accounts->find($accountEntry->new);
unset($accountEntry);
/*
* If $assetAccount is null, we release this job and try later.
*/
if (is_null($assetAccount)) {
\Log::notice('No asset account for "' . $description . '", try again later.');
if(\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
} else {
$job->release(300); // proper release.
}
return;
}
/*
* If the amount is less than zero, we move money to the $importAccount. Otherwise,
* we move it from the $importAccount.
*/
if ($amount < 0) {
// if amount is less than zero, move to $importAccount
$accountFrom = $assetAccount;
$accountTo = $importAccount;
} else {
$accountFrom = $importAccount;
$accountTo = $assetAccount;
}
/*
* Modify the amount so it will work with or new transaction journal structure.
*/
$amount = $amount < 0 ? $amount * -1 : $amount;
/*
* Import it:
*/
$journal = $this->createSimpleJournal($accountFrom, $accountTo, $description, $amount, $date);
$repository->store($importMap, 'Transaction', $transactionId, $journal->id);
\Log::debug('Imported transaction "' . $description . '" (' . $amount . ') (' . $date->format('Y-m-d') . ')');
// update map:
$importMap->jobsdone++;
$importMap->save();
$job->delete(); // count fixed
return;
}
/**
* @param $journalId
*
@ -124,17 +508,6 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
return $query;
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param \TransactionType $type
* @param int $count
@ -161,7 +534,6 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
return $result;
}
/**
* @param $what
* @param $data
@ -285,143 +657,6 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
return $transactionJournal;
}
/**
*
* We're building this thinking the money goes from A to B.
* If the amount is negative however, the money still goes
* from A to B but the balances are reversed.
*
* Aka:
*
* Amount = 200
* A loses 200 (-200). * -1
* B gains 200 (200). * 1
*
* Final balance: -200 for A, 200 for B.
*
* When the amount is negative:
*
* Amount = -200
* A gains 200 (200). * -1
* B loses 200 (-200). * 1
*
* @param \Account $from
* @param \Account $toAccount
* @param $description
* @param $amount
* @param \Carbon\Carbon $date
*
* @return \TransactionJournal
* @throws \Firefly\Exception\FireflyException
*/
public function createSimpleJournal(\Account $fromAccount, \Account $toAccount, $description, $amount, Carbon $date)
{
$journal = new \TransactionJournal;
$journal->completed = false;
$journal->description = $description;
$journal->date = $date;
$amountFrom = $amount * -1;
$amountTo = $amount;
if (round(floatval($amount), 2) == 0.00) {
$journal->errors()->add('amount', 'Amount must not be zero.');
return $journal;
}
// account types for both:
$toAT = $toAccount->accountType->type;
$fromAT = $fromAccount->accountType->type;
$journalType = null;
switch (true) {
case ($fromAccount->transactions()->count() == 0 && $toAccount->transactions()->count() == 0):
$journalType = \TransactionType::where('type', 'Opening balance')->first();
break;
case (in_array($fromAT, ['Default account', 'Asset account'])
&& in_array(
$toAT, ['Default account', 'Asset account']
)): // both are yours:
// determin transaction type. If both accounts are new, it's an initial balance transfer.
$journalType = \TransactionType::where('type', 'Transfer')->first();
break;
case ($amount < 0):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is deposit into one of your own accounts:
case ($toAT == 'Default account' || $toAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is withdrawal from one of your own accounts:
case ($fromAT == 'Default account' || $fromAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Withdrawal')->first();
break;
}
if (is_null($journalType)) {
throw new FireflyException('Could not figure out transaction type.');
}
// always the same currency:
$currency = \TransactionCurrency::where('code', 'EUR')->first();
if (is_null($currency)) {
throw new FireflyException('No currency for journal!');
}
// new journal:
$journal->transactionType()->associate($journalType);
$journal->transactionCurrency()->associate($currency);
$journal->user()->associate($this->_user);
// same account:
if ($fromAccount->id == $toAccount->id) {
$journal->errors()->add('account_to_id', 'Must be different from the "account from".');
$journal->errors()->add('account_from_id', 'Must be different from the "account to".');
return $journal;
}
if (!$journal->validate()) {
return $journal;
}
$journal->save();
// create transactions:
$fromTransaction = new \Transaction;
$fromTransaction->account()->associate($fromAccount);
$fromTransaction->transactionJournal()->associate($journal);
$fromTransaction->description = null;
$fromTransaction->amount = $amountFrom;
if (!$fromTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()
->first());
}
$fromTransaction->save();
$toTransaction = new \Transaction;
$toTransaction->account()->associate($toAccount);
$toTransaction->transactionJournal()->associate($journal);
$toTransaction->description = null;
$toTransaction->amount = $amountTo;
if (!$toTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (to): ' . $toTransaction->errors()->first().': ' . print_r($toAccount->toArray(),true));
}
$toTransaction->save();
$journal->completed = true;
$journal->save();
return $journal;
}
/**
* @param \TransactionJournal $journal
* @param $data

View File

@ -3,6 +3,7 @@
namespace Firefly\Storage\TransactionJournal;
use Carbon\Carbon;
use Illuminate\Queue\Jobs\Job;
/**
* Interface TransactionJournalRepositoryInterface
@ -11,6 +12,22 @@ use Carbon\Carbon;
*/
interface TransactionJournalRepositoryInterface
{
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importTransaction(Job $job, array $payload);
/**
* @param Job $job
* @param array $payload
*
* @return mixed
*/
public function importTransfer(Job $job, array $payload);
/**
* @param \Account $from
* @param \Account $toAccount