First attempt, trying to build an import stuff routine.

This commit is contained in:
James Cole
2014-09-02 08:58:56 +02:00
parent 420b5790e3
commit f472a01a80
25 changed files with 1050 additions and 101 deletions

View File

@@ -46,11 +46,23 @@ interface AccountRepositoryInterface
public function find($accountId);
/**
* @param $name
*
* @param $type
* @return mixed
*/
public function findByName($name);
public function findAccountType($type);
/**
* @param $name
* @param \AccountType $type
* @return mixed
*/
public function findByName($name, \AccountType $type = null);
/**
* @param $name
* @return mixed
*/
public function findByNameAny($name);
/**
* @return mixed

View File

@@ -148,6 +148,14 @@ class EloquentAccountRepository implements AccountRepositoryInterface
->get(['accounts.*']);
}
/**
* @param $type
* @return mixed
*/
public function findAccountType($type) {
return \AccountType::where('type',$type)->first();
}
/**
* @return array|mixed
*/
@@ -203,6 +211,14 @@ class EloquentAccountRepository implements AccountRepositoryInterface
{
$type = \AccountType::where('type', 'Cash account')->first();
$cash = \Auth::user()->accounts()->where('account_type_id', $type->id)->first();
if(is_null($cash)) {
$cash = new \Account;
$cash->accountType()->associate($type);
$cash->user()->associate(\Auth::user());
$cash->name = 'Cash account';
$cash->active = 1;
$cash->save();
}
return $cash;
@@ -234,6 +250,9 @@ class EloquentAccountRepository implements AccountRepositoryInterface
&& get_class($data['account_type']) == 'AccountType'
) {
$accountType = $data['account_type'];
} else if (isset($data['account_type']) && is_string($data['account_type'])) {
$accountType = \AccountType::where('type', $data['account_type'])->first();
} else {
$accountType = \AccountType::where('type', 'Default account')->first();
}
@@ -243,7 +262,12 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*/
$account = new \Account;
$account->accountType()->associate($accountType);
$account->user()->associate(\Auth::user());
if (\Auth::check()) {
$account->user()->associate(\Auth::user());
} else {
$account->user_id = $data['user_id'];
}
$account->name = $data['name'];
$account->active
= isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
@@ -256,7 +280,9 @@ class EloquentAccountRepository implements AccountRepositoryInterface
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
$amount = floatval($data['openingbalance']);
$date = new Carbon($data['openingbalancedate']);
$this->_createInitialBalance($account, $amount, $date);
if ($amount != 0) {
$this->_createInitialBalance($account, $amount, $date);
}
}
}
@@ -302,6 +328,20 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $account;
}
/**
* Used for import
*
* @param $name
*
* @return mixed
*/
public function findByNameAny($name)
{
return \Auth::user()->accounts()
->where('name', 'like', '%' . $name . '%')
->first();
}
/**
* @param \Account $account
* @param int $amount

View File

@@ -23,6 +23,13 @@ interface BudgetRepositoryInterface
*/
public function find($budgetId);
/**
* @param $budgetName
* @return mixed
*/
public function findByName($budgetName);
/**
* @return mixed
*/

View File

@@ -35,6 +35,12 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return \Auth::user()->budgets()->find($budgetId);
}
public function findByName($budgetName)
{
return \Auth::user()->budgets()->whereName($budgetName)->first();
}
/**
* @return mixed
*/
@@ -88,7 +94,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$budget->save();
// if limit, create limit (repetition itself will be picked up elsewhere).
if (floatval($data['amount']) > 0) {
if (isset($data['amount']) && floatval($data['amount']) > 0) {
$limit = new \Limit;
$limit->budget()->associate($budget);
$startDate = new Carbon;

View File

@@ -0,0 +1,39 @@
<?php
namespace Firefly\Storage\Import;
class EloquentImportRepository implements ImportRepositoryInterface
{
public function findImportComponentMap(\Importmap $map, $oldComponentId)
{
$entry = \Importentry::where('importmap_id', $map->id)
->whereIn('class', ['Budget', 'Category', 'Account', 'Component'])
->where('old', intval($oldComponentId))->first();
return $entry;
}
public function findImportEntry(\Importmap $map, $class, $oldID)
{
return \Importentry::where('importmap_id', $map->id)->where('class', $class)->where('old', $oldID)->first();
}
public function findImportMap($id)
{
return \Importmap::find($id);
}
public function store(\Importmap $map, $class, $oldID, $newID)
{
$entry = new \Importentry;
$entry->importmap()->associate($map);
$entry->class = $class;
$entry->old = intval($oldID);
$entry->new = intval($newID);
$entry->save();
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Firefly\Storage\Import;
/**
* Interface ImportRepositoryInterface
* @package Firefly\Storage\Import
*/
interface ImportRepositoryInterface
{
/**
* @param \Importmap $map
* @param $class
* @param $oldID
* @param $newID
* @return mixed
*/
public function store(\Importmap $map, $class, $oldID, $newID);
public function findImportMap($id);
public function findImportEntry(\Importmap $map, $class, $oldID);
public function findImportComponentMap(\Importmap $map, $oldComponentId);
}

View File

@@ -13,6 +13,9 @@ use Carbon\Carbon;
class EloquentLimitRepository implements LimitRepositoryInterface
{
public function findByBudgetAndDate(\Budget $budget, Carbon $date) {
return \Limit::whereComponentId($budget->id)->where('startdate',$date->format('Y-m-d'))->first();
}
/**
* @param \Limit $limit

View File

@@ -12,6 +12,36 @@ use Carbon\Carbon;
interface LimitRepositoryInterface
{
/**
* @param \Limit $limit
*
* @return mixed
*/
public function destroy(\Limit $limit);
/**
* @param $limitId
*
* @return mixed
*/
public function find($limitId);
/**
* @param \Budget $budget
* @param Carbon $date
* @return mixed
*/
public function findByBudgetAndDate(\Budget $budget, Carbon $date);
/**
* @param \Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end);
/**
* @param $data
*
@@ -26,27 +56,4 @@ interface LimitRepositoryInterface
* @return mixed
*/
public function update(\Limit $limit, $data);
/**
* @param \Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end);
/**
* @param $limitId
*
* @return mixed
*/
public function find($limitId);
/**
* @param \Limit $limit
*
* @return mixed
*/
public function destroy(\Limit $limit);
}

View File

@@ -21,14 +21,14 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
public function count()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
'accounts.user_id', \Auth::user()->id
)->count();
}
public function countNonrepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
'accounts.user_id', \Auth::user()->id
)->where('repeats', 0)->count();
}
@@ -36,7 +36,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
public function countRepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
'accounts.user_id', \Auth::user()->id
)->where('repeats', 1)->count();
}
@@ -60,10 +60,17 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
public function find($piggyBankId)
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
'accounts.user_id', \Auth::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', \Auth::user()->id
)->where('piggybanks.name', $piggyBankName)->first(['piggybanks.*']);
}
/**
* @return mixed
*/
@@ -122,19 +129,19 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
*/
public function store($data)
{
if ($data['targetdate'] == '') {
if (isset($data['targetdate']) && $data['targetdate'] == '') {
unset($data['targetdate']);
}
if ($data['reminder'] == 'none') {
if (isset($data['reminder']) && $data['reminder'] == 'none') {
unset($data['reminder']);
}
if ($data['startdate'] == '') {
if (isset($data['startdate']) && $data['startdate'] == '') {
unset($data['startdate']);
}
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$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);
@@ -142,7 +149,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
if (!is_null($piggyBank->reminder) && is_null($piggyBank->startdate) && is_null($piggyBank->targetdate)) {
$piggyBank->errors()->add('reminder', 'Cannot create reminders without start ~ AND target date.');
\Log::error('PiggyBank create-error: ' . $piggyBank->errors()->first());
return $piggyBank;
}
@@ -150,6 +157,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
if ($piggyBank->repeats && !isset($data['targetdate'])) {
$piggyBank->errors()->add('targetdate', 'Target date is mandatory!');
\Log::error('PiggyBank create-error: ' . $piggyBank->errors()->first());
return $piggyBank;
}
@@ -161,13 +169,14 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
if ($piggyBank->validate()) {
if (!is_null($piggyBank->targetdate) && $piggyBank->targetdate < $today) {
$piggyBank->errors()->add('targetdate', 'Target date cannot be in the past.');
\Log::error('PiggyBank create-error: ' . $piggyBank->errors()->first());
return $piggyBank;
}
if (!is_null($piggyBank->reminder) && !is_null($piggyBank->targetdate)) {
// first period for reminder is AFTER target date.
$reminderSkip = $piggyBank->reminder_skip < 1 ? 1 : intval($piggyBank->reminder_skip);
$reminderSkip = $piggyBank->reminder_skip < 1 ? 1 : intval($piggyBank->reminder_skip);
$firstReminder = new Carbon;
switch ($piggyBank->reminder) {
case 'day':
@@ -188,8 +197,9 @@ 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());
return $piggyBank;
}
@@ -197,6 +207,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
$piggyBank->save();
}
return $piggyBank;
}
@@ -210,19 +221,19 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
{
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$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);
}
$piggy->name = $data['name'];
$piggy->targetamount = floatval($data['targetamount']);
$piggy->reminder = isset($data['reminder']) && $data['reminder'] != 'none' ? $data['reminder'] : null;
$piggy->name = $data['name'];
$piggy->targetamount = floatval($data['targetamount']);
$piggy->reminder = isset($data['reminder']) && $data['reminder'] != 'none' ? $data['reminder'] : null;
$piggy->reminder_skip = $data['reminder_skip'];
$piggy->targetdate = strlen($data['targetdate']) > 0 ? new Carbon($data['targetdate']) : null;
$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) {
@@ -230,7 +241,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
}
if ($piggy->repeats == 1) {
$piggy->rep_every = intval($data['rep_every']);
$piggy->rep_every = intval($data['rep_every']);
$piggy->rep_length = $data['rep_length'];
}

View File

@@ -39,7 +39,7 @@ interface PiggybankRepositoryInterface
* @return mixed
*/
public function find($piggyBankId);
public function findByName($piggyBankName);
/**
* @return mixed
*/

View File

@@ -24,6 +24,11 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
return true;
}
public function findByName($name)
{
return \Auth::user()->recurringtransactions()->where('name', 'LIKE', '%' . $name . '%')->first();
}
/**
* @return mixed
*/
@@ -41,8 +46,8 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
{
$recurringTransaction = new \RecurringTransaction;
$recurringTransaction->user()->associate(\Auth::user());
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->amount_max = floatval($data['amount_max']);
$recurringTransaction->amount_min = floatval($data['amount_min']);
@@ -53,10 +58,10 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
return $recurringTransaction;
}
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
$recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0;
$recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0;
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
$recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0;
$recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0;
$recurringTransaction->repeat_freq = $data['repeat_freq'];
if ($recurringTransaction->validate()) {
@@ -74,8 +79,8 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
*/
public function update(\RecurringTransaction $recurringTransaction, $data)
{
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->amount_max = floatval($data['amount_max']);
$recurringTransaction->amount_min = floatval($data['amount_min']);
@@ -85,10 +90,10 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
return $recurringTransaction;
}
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
$recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0;
$recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0;
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
$recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0;
$recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0;
$recurringTransaction->repeat_freq = $data['repeat_freq'];
if ($recurringTransaction->validate()) {

View File

@@ -16,6 +16,12 @@ interface RecurringTransactionRepositoryInterface
*/
public function get();
/**
* @param $name
* @return mixed
*/
public function findByName($name);
/**
* @param $data
*

View File

@@ -18,55 +18,60 @@ class StorageServiceProvider extends ServiceProvider
public function register()
{
$this->app->bind(
'Firefly\Storage\User\UserRepositoryInterface',
'Firefly\Storage\User\EloquentUserRepository'
'Firefly\Storage\User\UserRepositoryInterface',
'Firefly\Storage\User\EloquentUserRepository'
);
$this->app->bind(
'Firefly\Storage\Transaction\TransactionRepositoryInterface',
'Firefly\Storage\Transaction\EloquentTransactionRepository'
'Firefly\Storage\Transaction\TransactionRepositoryInterface',
'Firefly\Storage\Transaction\EloquentTransactionRepository'
);
$this->app->bind(
'Firefly\Storage\Import\ImportRepositoryInterface',
'Firefly\Storage\Import\EloquentImportRepository'
);
$this->app->bind(
'Firefly\Storage\Piggybank\PiggybankRepositoryInterface',
'Firefly\Storage\Piggybank\EloquentPiggybankRepository'
);
$this->app->bind(
'Firefly\Storage\Piggybank\PiggybankRepositoryInterface',
'Firefly\Storage\Piggybank\EloquentPiggybankRepository'
'Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface',
'Firefly\Storage\RecurringTransaction\EloquentRecurringTransactionRepository'
);
$this->app->bind(
'Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface',
'Firefly\Storage\RecurringTransaction\EloquentRecurringTransactionRepository'
'Firefly\Storage\Reminder\ReminderRepositoryInterface',
'Firefly\Storage\Reminder\EloquentReminderRepository'
);
$this->app->bind(
'Firefly\Storage\Reminder\ReminderRepositoryInterface',
'Firefly\Storage\Reminder\EloquentReminderRepository'
'Firefly\Storage\Account\AccountRepositoryInterface',
'Firefly\Storage\Account\EloquentAccountRepository'
);
$this->app->bind(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface',
'Firefly\Storage\TransactionJournal\EloquentTransactionJournalRepository'
);
$this->app->bind(
'Firefly\Storage\Account\AccountRepositoryInterface',
'Firefly\Storage\Account\EloquentAccountRepository'
);
$this->app->bind(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface',
'Firefly\Storage\TransactionJournal\EloquentTransactionJournalRepository'
'Firefly\Storage\Component\ComponentRepositoryInterface',
'Firefly\Storage\Component\EloquentComponentRepository'
);
$this->app->bind(
'Firefly\Storage\Component\ComponentRepositoryInterface',
'Firefly\Storage\Component\EloquentComponentRepository'
'Firefly\Storage\Limit\LimitRepositoryInterface',
'Firefly\Storage\Limit\EloquentLimitRepository'
);
$this->app->bind(
'Firefly\Storage\Limit\LimitRepositoryInterface',
'Firefly\Storage\Limit\EloquentLimitRepository'
);
$this->app->bind(
'Firefly\Storage\Budget\BudgetRepositoryInterface',
'Firefly\Storage\Budget\EloquentBudgetRepository'
'Firefly\Storage\Budget\BudgetRepositoryInterface',
'Firefly\Storage\Budget\EloquentBudgetRepository'
);
$this->app->bind(
'Firefly\Storage\Category\CategoryRepositoryInterface',
'Firefly\Storage\Category\EloquentCategoryRepository'
'Firefly\Storage\Category\CategoryRepositoryInterface',
'Firefly\Storage\Category\EloquentCategoryRepository'
);
}