More CSV related updates.

This commit is contained in:
James Cole
2016-07-23 21:37:06 +02:00
parent a4a723cfc6
commit 87c0f1d86e
31 changed files with 684 additions and 263 deletions

View File

@@ -74,6 +74,7 @@ class AccountCrud implements AccountCrudInterface
*/
public function find(int $accountId): Account
{
Log::debug('Searching for user ', ['user' => $this->user->id]);
$account = $this->user->accounts()->find($accountId);
if (is_null($account)) {
return new Account;
@@ -82,6 +83,33 @@ class AccountCrud implements AccountCrudInterface
return $account;
}
/**
* @param string $number
* @param array $types
*
* @return Account
*/
public function findByAccountNumber(string $number, array $types): Account
{
$query = $this->user->accounts()
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
->where('account_meta.name', 'accountNumber')
->where('account_meta.data', json_encode($number));
if (count($types) > 0) {
$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
$query->whereIn('account_types.type', $types);
}
/** @var Collection $accounts */
$accounts = $query->get();
if ($accounts->count() > 0) {
return $accounts->first();
}
return new Account;
}
/**
* @param string $iban
* @param array $types

View File

@@ -53,6 +53,14 @@ interface AccountCrudInterface
*/
public function findByName(string $name, array $types): Account;
/**
* @param string $number
* @param array $types
*
* @return Account
*/
public function findByAccountNumber(string $number, array $types): Account;
/**
* @param array $accountIds
*

View File

@@ -117,7 +117,7 @@ class ImportController extends Controller
/**
* This is step 1. Upload a file.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return View
*/
public function index()
{

View File

@@ -62,7 +62,8 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
$account = $repository->store(
['name' => $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0, 'active' => true]
['name' => 'Account with IBAN ' . $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0,
'active' => true]
);
return $account;

View File

@@ -11,7 +11,9 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account;
use Log;
/**
* Class AssetAccountId
@@ -24,11 +26,41 @@ class AssetAccountId extends BasicConverter implements ConverterInterface
/**
* @param $value
*
* @throws FireflyException
* @return Account
*/
public function convert($value)
{
throw new FireflyException('Importer with name AssetAccountId has not yet been configured.');
$value = intval(trim($value));
Log::debug('Going to convert using AssetAccountId', ['value' => $value]);
if ($value === 0) {
return new Account;
}
/** @var AccountCrudInterface $repository */
$repository = app(AccountCrudInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$account = $repository->find(intval($this->mapping[$value]));
if (!is_null($account->id)) {
Log::debug('Found account by ID', ['id' => $account->id]);
return $account;
}
}
// not mapped? Still try to find it first:
$account = $repository->find($value);
if (!is_null($account->id)) {
Log::debug('Found account by ID ', ['id' => $account->id]);
return $account;
}
// should not really happen. If the ID does not match FF, what is FF supposed to do?
return new Account;
}
}

View File

@@ -11,7 +11,10 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Log;
/**
* Class AssetAccountName
@@ -24,11 +27,47 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
/**
* @param $value
*
* @throws FireflyException
* @return Account
*/
public function convert($value)
{
throw new FireflyException('Importer with name AssetAccountName has not yet been configured.');
$value = trim($value);
Log::debug('Going to convert using AssetAccountName', ['value' => $value]);
if (strlen($value) === 0) {
return new Account;
}
/** @var AccountCrudInterface $repository */
$repository = app(AccountCrudInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$account = $repository->find(intval($this->mapping[$value]));
if (!is_null($account->id)) {
Log::debug('Found account by ID', ['id' => $account->id]);
return $account;
}
}
// not mapped? Still try to find it first:
$account = $repository->findByName($value, [AccountType::ASSET]);
if (!is_null($account->id)) {
Log::debug('Found account by name', ['id' => $account->id]);
return $account;
}
$account = $repository->store(
['name' => $value, 'iban' => null, 'openingBalance' => 0, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0,
'active' => true]
);
return $account;
}
}

View File

@@ -11,7 +11,11 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Crud\Account\AccountCrudInterface;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Log;
/**
* Class AssetAccountNumber
@@ -24,11 +28,46 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface
/**
* @param $value
*
* @throws FireflyException
* @return Account
*/
public function convert($value)
{
throw new FireflyException('Importer with name AssetAccountNumber has not yet been configured.');
$value = trim($value);
Log::debug('Going to convert using AssetAccountName', ['value' => $value]);
if (strlen($value) === 0) {
return new Account;
}
/** @var AccountCrudInterface $repository */
$repository = app(AccountCrudInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$account = $repository->find(intval($this->mapping[$value]));
if (!is_null($account->id)) {
Log::debug('Found account by ID', ['id' => $account->id]);
return $account;
}
}
// not mapped? Still try to find it first:
$account = $repository->findByAccountNumber($value, [AccountType::ASSET]);
if (!is_null($account->id)) {
Log::debug('Found account by name', ['id' => $account->id]);
return $account;
}
$account = $repository->store(
['name' => 'Account with number ' . $value, 'openingBalance' => 0, 'iban' => null, 'user' => $this->user->id, 'accountType' => 'asset',
'virtualBalance' => 0, 'active' => true]
);
return $account;
}
}

View File

@@ -12,6 +12,9 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Log;
/**
* Class BillId
@@ -24,11 +27,40 @@ class BillId extends BasicConverter implements ConverterInterface
/**
* @param $value
*
* @throws FireflyException
* @return Bill
*/
public function convert($value)
{
throw new FireflyException('Importer with name BillId has not yet been configured.');
$value = intval(trim($value));
Log::debug('Going to convert using BillId', ['value' => $value]);
if ($value === 0) {
return new Bill;
}
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found bill in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$bill = $repository->find(intval($this->mapping[$value]));
if (!is_null($bill->id)) {
Log::debug('Found bill by ID', ['id' => $bill->id]);
return $bill;
}
}
// not mapped? Still try to find it first:
$bill = $repository->find($value);
if (!is_null($bill->id)) {
Log::debug('Found bill by ID ', ['id' => $bill->id]);
return $bill;
}
// should not really happen. If the ID does not match FF, what is FF supposed to do?
return new Bill;
}
}

View File

@@ -12,6 +12,9 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Log;
/**
* Class BillName
@@ -28,7 +31,53 @@ class BillName extends BasicConverter implements ConverterInterface
*/
public function convert($value)
{
throw new FireflyException('Importer with name BillName has not yet been configured.');
$value = trim($value);
Log::debug('Going to convert using BillName', ['value' => $value]);
if (strlen($value) === 0) {
return new Bill;
}
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found bill in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$bill = $repository->find(intval($this->mapping[$value]));
if (!is_null($bill->id)) {
Log::debug('Found bill by ID', ['id' => $bill->id]);
return $bill;
}
}
// not mapped? Still try to find it first:
$bill = $repository->findByName($value);
if (!is_null($bill->id)) {
Log::debug('Found bill by name ', ['id' => $bill->id]);
return $bill;
}
// create new bill. Use a lot of made up values.
$bill = $repository->store(
[
'name' => $value,
'match' => $value,
'amount_min' => 1,
'user_id' => $this->user->id,
'amount_max' => 10,
'date' => date('Ymd'),
'repeat_freq' => 'monthly',
'skip' => 0,
'automatch' => 0,
'active' => 1,
]
);
return $bill;
}
}

View File

@@ -65,40 +65,7 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface
$account = $repository->store(
['name' => $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'import', 'virtualBalance' => 0, 'active' => true,
'openingBalance' => 0<?php
/**
* AssetAccountId.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
/**
* Class AssetAccountId
*
* @package FireflyIII\Import\Converter
*/
class AssetAccountId extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @throws FireflyException
*/
public function convert($value)
{
throw new FireflyException('Importer with name AssetAccountId has not yet been configured.');
}
}]
'openingBalance' => 0]
);
return $account;

View File

@@ -17,6 +17,7 @@ use FireflyIII\Crud\Account\AccountCrud;
use FireflyIII\Import\Converter\ConverterInterface;
use FireflyIII\Import\ImportEntry;
use FireflyIII\Import\Mapper\MapperInterface;
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ImportJob;
use Illuminate\Http\Request;
@@ -180,16 +181,23 @@ class CsvImporter implements ImporterInterface
public function saveImportConfiguration(array $data, FileBag $files): bool
{
/** @var AccountCrud $repository */
$repository = app(AccountCrud::class);
$account = $repository->find(intval($data['csv_import_account']));
$repository = app(AccountCrud::class, [auth()->user()]);
$account = $repository->find(intval($data['csv_import_account']));
$hasHeaders = isset($data['has_headers']) && intval($data['has_headers']) === 1 ? true : false;
$config = $this->job->configuration;
$config['has-headers'] = $hasHeaders;
$config['date-format'] = $data['date_format'];
$config['delimiter'] = $data['csv_delimiter'];
Log::debug('Entered import account.', ['id' => $data['csv_import_account']]);
if (!is_null($account->id)) {
Log::debug('Found account.', ['id' => $account->id, 'name' => $account->name]);
$config['import-account'] = $account->id;
} else {
Log::error('Could not find anything for csv_import_account.', ['id' => $data['csv_import_account']]);
}
// loop specifics.
if (isset($data['specifics']) && is_array($data['specifics'])) {
@@ -335,21 +343,28 @@ class CsvImporter implements ImporterInterface
foreach ($config['column-do-mapping'] as $index => $mustBeMapped) {
if ($mustBeMapped) {
$column = $config['column-roles'][$index] ?? '_ignore';
$canBeMapped = config('csv.import_roles.' . $column . '.mappable');
$column = $config['column-roles'][$index] ?? '_ignore';
$canBeMapped = config('csv.import_roles.' . $column . '.mappable');
$preProcessMap = config('csv.import_roles.' . $column . '.pre-process-map');
if ($canBeMapped) {
$mapperName = '\FireflyIII\Import\Mapper\\' . config('csv.import_roles.' . $column . '.mapper');
/** @var MapperInterface $mapper */
$mapper = new $mapperName;
$indexes[] = $index;
$data[$index] = [
'name' => $column,
'mapper' => $mapperName,
'index' => $index,
'options' => $mapper->getMap(),
'values' => [],
'name' => $column,
'mapper' => $mapperName,
'index' => $index,
'options' => $mapper->getMap(),
'preProcessMap' => null,
'values' => [],
];
if ($preProcessMap) {
$data[$index]['preProcessMap'] = '\FireflyIII\Import\MapperPreProcess\\' .
config('csv.import_roles.' . $column . '.pre-process-mapper');
}
}
}
}
@@ -358,12 +373,29 @@ class CsvImporter implements ImporterInterface
$reader = Reader::createFromString($content);
$results = $reader->fetch();
foreach ($results as $row) {
foreach ($results as $rowIndex => $row) {
//do something here
foreach ($indexes as $index) {
foreach ($indexes as $index) { // this is simply 1, 2, 3, etc.
$value = $row[$index];
if (strlen($value) > 0) {
$data[$index]['values'][] = $row[$index];
// we can do some preprocessing here,
// which is exclusively to fix the tags:
if (!is_null($data[$index]['preProcessMap'])) {
/** @var PreProcessorInterface $preProcessor */
$preProcessor = app($data[$index]['preProcessMap']);
$result = $preProcessor->run($value);
$data[$index]['values'] = array_merge($data[$index]['values'], $result);
Log::debug($rowIndex . ':' . $index . 'Value before preprocessor', ['value' => $value]);
Log::debug($rowIndex . ':' . $index . 'Value after preprocessor', ['value-new' => $result]);
Log::debug($rowIndex . ':' . $index . 'Value after joining', ['value-complete' => $data[$index]['values']]);
continue;
}
$data[$index]['values'][] = $value;
}
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* PreProcessorInterface.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\MapperPreProcess;
/**
* Interface PreProcessorInterface
*
* @package FireflyIII\Import\MapperPreProcess
*/
interface PreProcessorInterface
{
/**
* @param string $value
*
* @return array
*/
public function run(string $value): array;
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* TagsComma.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\MapperPreProcess;
/**
* Class TagsComma
*
* @package FireflyIII\Import\MapperPreProcess
*/
class TagsComma implements PreProcessorInterface
{
/**
* @param string $value
*
* @return array
*/
public function run(string $value): array
{
return explode(',', $value);
}
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* TagsSpace.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\MapperPreProcess;
/**
* Class TagsSpace
*
* @package FireflyIII\Import\MapperPreProcess
*/
class TagsSpace implements PreProcessorInterface
{
/**
* @param string $value
*
* @return array
*/
public function run(string $value): array
{
return explode(' ', $value);
}
}

View File

@@ -44,7 +44,7 @@ class AccountServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Account\AccountRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Account\AccountRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Account\AccountRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -44,7 +44,7 @@ class AttachmentServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Attachment\AttachmentRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Attachment\AttachmentRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -44,7 +44,7 @@ class BillServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Bill\BillRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Bill\BillRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Bill\BillRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -44,7 +44,7 @@ class BudgetServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Budget\BudgetRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Budget\BudgetRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Budget\BudgetRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -44,7 +44,7 @@ class CategoryServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Category\CategoryRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Category\CategoryRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Category\CategoryRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -12,8 +12,10 @@ declare(strict_types = 1);
namespace FireflyIII\Providers;
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Auth\AuthManager;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Log;
/**
* Class CrudServiceProvider
@@ -49,12 +51,14 @@ class CrudServiceProvider extends ServiceProvider
$this->app->bind(
'FireflyIII\Crud\Account\AccountCrudInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Crud\Account\AccountCrud', [$app->auth->user()]);
if (!isset($arguments[0]) && auth()->check()) {
return app('FireflyIII\Crud\Account\AccountCrud', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');
}
Log::debug('AccountCrud constructor, run with default arguments.', $arguments);
return app('FireflyIII\Crud\Account\AccountCrud', $arguments);
}
@@ -67,7 +71,7 @@ class CrudServiceProvider extends ServiceProvider
'FireflyIII\Crud\Split\JournalInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Crud\Split\Journal', [$app->auth->user()]);
return app('FireflyIII\Crud\Split\Journal', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -56,7 +56,7 @@ class ExportJobServiceProvider extends ServiceProvider
'FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\ExportJob\ExportJobRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\ExportJob\ExportJobRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');
@@ -73,7 +73,7 @@ class ExportJobServiceProvider extends ServiceProvider
'FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\ImportJob\ImportJobRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\ImportJob\ImportJobRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -44,7 +44,7 @@ class JournalServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Journal\JournalRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Journal\JournalRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Journal\JournalRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -45,7 +45,7 @@ class PiggyBankServiceProvider extends ServiceProvider
'FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\PiggyBank\PiggyBankRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\PiggyBank\PiggyBankRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -45,7 +45,7 @@ class RuleGroupServiceProvider extends ServiceProvider
'FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\RuleGroup\RuleGroupRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\RuleGroup\RuleGroupRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -44,7 +44,7 @@ class RuleServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Rule\RuleRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Rule\RuleRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Rule\RuleRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -44,7 +44,7 @@ class TagServiceProvider extends ServiceProvider
'FireflyIII\Repositories\Tag\TagRepositoryInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Tag\TagRepository', [$app->auth->user()]);
return app('FireflyIII\Repositories\Tag\TagRepository', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');

View File

@@ -72,6 +72,27 @@ class BillRepository implements BillRepositoryInterface
return $bill;
}
/**
* Find a bill by name.
*
* @param string $name
*
* @return Bill
*/
public function findByName(string $name) : Bill
{
$bills = $this->user->bills()->get();
/** @var Bill $bill */
foreach ($bills as $bill) {
if ($bill->name === $name) {
return $bill;
}
}
return new Bill;
}
/**
* @return Collection
*/
@@ -293,6 +314,28 @@ class BillRepository implements BillRepositoryInterface
return $bill->transactionjournals()->before($end)->after($start)->get();
}
/**
* @param $bill
*
* @return string
*/
public function getOverallAverage($bill): string
{
$journals = $bill->transactionjournals()->get();
$sum = '0';
$count = strval($journals->count());
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
}
return $avg;
}
/**
* @param Bill $bill
*
@@ -358,6 +401,32 @@ class BillRepository implements BillRepositoryInterface
return $validRanges;
}
/**
* @param Bill $bill
* @param Carbon $date
*
* @return string
*/
public function getYearAverage(Bill $bill, Carbon $date): string
{
$journals = $bill->transactionjournals()
->where('date', '>=', $date->year . '-01-01')
->where('date', '<=', $date->year . '-12-31')
->get();
$sum = '0';
$count = strval($journals->count());
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
}
return $avg;
}
/**
* @param Bill $bill
*
@@ -557,52 +626,4 @@ class BillRepository implements BillRepositoryInterface
return $wordMatch;
}
/**
* @param Bill $bill
* @param Carbon $date
*
* @return string
*/
public function getYearAverage(Bill $bill, Carbon $date): string
{
$journals = $bill->transactionjournals()
->where('date', '>=', $date->year . '-01-01')
->where('date', '<=', $date->year . '-12-31')
->get();
$sum = '0';
$count = strval($journals->count());
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
}
return $avg;
}
/**
* @param $bill
*
* @return string
*/
public function getOverallAverage($bill): string
{
$journals = $bill->transactionjournals()->get();
$sum = '0';
$count = strval($journals->count());
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
}
return $avg;
}
}

View File

@@ -32,21 +32,6 @@ interface BillRepositoryInterface
*/
public function destroy(Bill $bill): bool;
/**
* @param Bill $bill
* @param Carbon $date
*
* @return string
*/
public function getYearAverage(Bill $bill, Carbon $date): string;
/**
* @param $bill
*
* @return string
*/
public function getOverallAverage($bill): string;
/**
* Find a bill by ID.
*
@@ -56,6 +41,15 @@ interface BillRepositoryInterface
*/
public function find(int $billId) : Bill;
/**
* Find a bill by name.
*
* @param string $name
*
* @return Bill
*/
public function findByName(string $name) : Bill;
/**
* @return Collection
*/
@@ -128,6 +122,13 @@ interface BillRepositoryInterface
*/
public function getJournalsInRange(Bill $bill, Carbon $start, Carbon $end): Collection;
/**
* @param $bill
*
* @return string
*/
public function getOverallAverage($bill): string;
/**
* @param Bill $bill
*
@@ -148,6 +149,14 @@ interface BillRepositoryInterface
*/
public function getRanges(Bill $bill, Carbon $start, Carbon $end): array;
/**
* @param Bill $bill
* @param Carbon $date
*
* @return string
*/
public function getYearAverage(Bill $bill, Carbon $date): string;
/**
* @param Bill $bill
*