First set of code for #956

This commit is contained in:
James Cole
2017-12-01 20:11:29 +01:00
parent 382c5e5760
commit 8a59380c6d
5 changed files with 125 additions and 7 deletions

View File

@@ -29,6 +29,7 @@ use FireflyIII\Import\Object\ImportJournal;
use FireflyIII\Models\ImportJob; use FireflyIII\Models\ImportJob;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -44,14 +45,22 @@ class ImportStorage
public $errors; public $errors;
/** @var Collection */ /** @var Collection */
public $journals; public $journals;
/** @var int */ /** @var BillRepositoryInterface */
protected $defaultCurrencyId = 1; // yes, hard coded protected $billRepository; // yes, hard coded
/** @var Collection */
protected $bills;
/** @var int */
protected $defaultCurrencyId = 1;
/** @var ImportJob */ /** @var ImportJob */
protected $job; protected $job;
/** @var Collection */ /** @var Collection */
protected $rules; protected $rules;
/** @var bool */
private $applyRules = false;
/** @var string */ /** @var string */
private $dateFormat = 'Ymd'; private $dateFormat = 'Ymd';
/** @var bool */
private $matchBills = false;
/** @var Collection */ /** @var Collection */
private $objects; private $objects;
/** @var array */ /** @var array */
@@ -84,7 +93,21 @@ class ImportStorage
$currency = app('amount')->getDefaultCurrencyByUser($this->job->user); $currency = app('amount')->getDefaultCurrencyByUser($this->job->user);
$this->defaultCurrencyId = $currency->id; $this->defaultCurrencyId = $currency->id;
$this->transfers = $this->getTransfers(); $this->transfers = $this->getTransfers();
$this->rules = $this->getRules(); $config = $job->configuration;
$this->applyRules = $config['apply_rules'] ?? false;
$this->matchBills = $config['match_bills'] ?? false;
if ($this->applyRules === true) {
Log::debug('applyRules seems to be true, get the rules.');
$this->rules = $this->getRules();
}
if ($this->matchBills === true) {
Log::debug('matchBills seems to be true, get the bills');
$this->bills = $this->getBills();
$this->billRepository = app(BillRepositoryInterface::class);
$this->billRepository->setUser($job->user);
}
Log::debug(sprintf('Value of apply rules is %s', var_export($this->applyRules, true)));
Log::debug(sprintf('Value of match bills is %s', var_export($this->matchBills, true)));
} }
/** /**
@@ -202,8 +225,26 @@ class ImportStorage
// Another step done! // Another step done!
$this->job->addStepsDone(1); $this->job->addStepsDone(1);
// run rules: // run rules if config calls for it:
$this->applyRules($journal); if ($this->applyRules === true) {
Log::info('Will apply rules to this journal.');
$this->applyRules($journal);
}
if (!($this->applyRules === true)) {
Log::info('Will NOT apply rules to this journal.');
}
// match bills if config calls for it.
if ($this->matchBills === true) {
//$this->/applyRules($journal);
Log::info('Cannot match bills (yet).');
$this->matchBills($journal);
}
if (!($this->matchBills === true)) {
Log::info('Cannot match bills (yet), but do not have to.');
}
// Another step done! // Another step done!
$this->job->addStepsDone(1); $this->job->addStepsDone(1);
$this->journals->push($journal); $this->journals->push($journal);

View File

@@ -38,6 +38,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalMeta; use FireflyIII\Models\TransactionJournalMeta;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\TransactionRules\Processor; use FireflyIII\TransactionRules\Processor;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
@@ -49,6 +50,10 @@ use Log;
*/ */
trait ImportSupport trait ImportSupport
{ {
/** @var BillRepositoryInterface */
protected $billRepository;
/** @var Collection */
protected $bills;
/** @var int */ /** @var int */
protected $defaultCurrencyId = 1; protected $defaultCurrencyId = 1;
/** @var ImportJob */ /** @var ImportJob */
@@ -81,6 +86,29 @@ trait ImportSupport
return true; return true;
} }
/**
* @param TransactionJournal $journal
*
* @return bool
*/
protected function matchBills(TransactionJournal $journal): bool
{
if(!is_null($journal->bill_id)) {
Log::debug('Journal is already linked to a bill, will not scan.');
return true;
}
if ($this->bills->count() > 0) {
$this->bills->each(
function (Bill $bill) use ($journal) {
Log::debug(sprintf('Going to match bill #%d to journal %d.', $bill->id, $journal->id));
$this->billRepository->scan($bill, $journal);
}
);
}
return true;
}
/** /**
* @param array $parameters * @param array $parameters
* *
@@ -107,6 +135,17 @@ trait ImportSupport
return true; return true;
} }
/**
* @return Collection
*/
private function getBills(): Collection
{
$set = Bill::where('user_id', $this->job->user->id)->where('active', 1)->where('automatch', 1)->get(['bills.*']);
Log::debug(sprintf('Found %d user bills.', $set->count()));
return $set;
}
/** /**
* This method finds out what the import journal's currency should be. The account itself * This method finds out what the import journal's currency should be. The account itself
* is favoured (and usually it stops there). If no preference is found, the journal has a say * is favoured (and usually it stops there). If no preference is found, the journal has a say
@@ -224,7 +263,7 @@ trait ImportSupport
* @param Account $account * @param Account $account
* *
* @return string * @return string
* *x
* @throws FireflyException * @throws FireflyException
* *
* @see ImportSupport::getOpposingAccount() * @see ImportSupport::getOpposingAccount()

View File

@@ -113,6 +113,8 @@ class Initial implements ConfigurationInterface
$config['date-format'] = $data['date_format']; $config['date-format'] = $data['date_format'];
$config['delimiter'] = $data['csv_delimiter']; $config['delimiter'] = $data['csv_delimiter'];
$config['delimiter'] = 'tab' === $config['delimiter'] ? "\t" : $config['delimiter']; $config['delimiter'] = 'tab' === $config['delimiter'] ? "\t" : $config['delimiter'];
$config['apply_rules'] = isset($data['apply_rules']) && 1 === intval($data['apply_rules']) ? true : false;
$config['match_bills'] = isset($data['match_bills']) && 1 === intval($data['match_bills']) ? true : false;
Log::debug('Entered import account.', ['id' => $importId]); Log::debug('Entered import account.', ['id' => $importId]);

View File

@@ -24,6 +24,12 @@ return [
'initial_import_account_help' => 'If your CSV file does NOT contain information about your asset account(s), use this dropdown to select to which account the transactions in the CSV belong to.', 'initial_import_account_help' => 'If your CSV file does NOT contain information about your asset account(s), use this dropdown to select to which account the transactions in the CSV belong to.',
'initial_submit' => 'Continue with step 2/3', 'initial_submit' => 'Continue with step 2/3',
// new options:
'apply_rules_title' => 'Apply rules',
'apply_rules_description' => 'Apply your rules. Note that this slows the import significantly.',
'match_bills_title' => 'Match bills',
'match_bills_description' => 'Match your bills to newly created withdrawals. Note that this slows the import significantly.',
// roles config // roles config
'roles_title' => 'Import setup (2/3) - Define each column\'s role', 'roles_title' => 'Import setup (2/3) - Define each column\'s role',
'roles_text' => 'Each column in your CSV file contains certain data. Please indicate what kind of data the importer should expect. The option to "map" data means that you will link each entry found in the column to a value in your database. An often mapped column is the column that contains the IBAN of the opposing account. That can be easily matched to IBAN\'s present in your database already.', 'roles_text' => 'Each column in your CSV file contains certain data. Please indicate what kind of data the importer should expect. The option to "map" data means that you will link each entry found in the column to a value in your database. An often mapped column is the column that contains the IBAN of the opposing account. That can be easily matched to IBAN\'s present in your database already.',

View File

@@ -34,12 +34,42 @@
<div class="box-body"> <div class="box-body">
<div class="row"> <div class="row">
<div class="col-lg-6"> <div class="col-lg-6">
<h4>{{ 'mandatoryFields'|_ }}</h4>
{{ ExpandedForm.checkbox('has_headers',1,job.configuration['has-headers'],{helpText: trans('csv.initial_header_help')}) }} {{ ExpandedForm.checkbox('has_headers',1,job.configuration['has-headers'],{helpText: trans('csv.initial_header_help')}) }}
{{ ExpandedForm.text('date_format',job.configuration['date-format'],{helpText: trans('csv.initial_date_help', {dateExample: phpdate('Ymd')}) }) }} {{ ExpandedForm.text('date_format',job.configuration['date-format'],{helpText: trans('csv.initial_date_help', {dateExample: phpdate('Ymd')}) }) }}
{{ ExpandedForm.select('csv_delimiter', data.delimiters, job.configuration['delimiter'], {helpText: trans('csv.initial_delimiter_help') } ) }} {{ ExpandedForm.select('csv_delimiter', data.delimiters, job.configuration['delimiter'], {helpText: trans('csv.initial_delimiter_help') } ) }}
{{ ExpandedForm.select('csv_import_account', data.accounts, job.configuration['import-account'], {helpText: trans('csv.initial_import_account_help')} ) }} {{ ExpandedForm.select('csv_import_account', data.accounts, job.configuration['import-account'], {helpText: trans('csv.initial_import_account_help')} ) }}
<h4>{{ 'optionalFields'|_ }}</h4>
<div class="form-group">
<label for="apply_rules_label" class="col-sm-4 control-label">
{{ trans('csv.apply_rules_title') }}
</label>
<div class="col-sm-8">
<div class="radio"><label>
{{ Form.checkbox('apply_rules', '1',
job.configuration.apply_rules == '1', {'id': 'apply_rules_label'}) }}
{{ trans('csv.apply_rules_description') }}
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="match_bills_label" class="col-sm-4 control-label">
{{ trans('csv.match_bills_title') }}
</label>
<div class="col-sm-8">
<div class="radio"><label>
{{ Form.checkbox('match_bills', '1',
job.configuration.match_bills == '1', {'id': 'match_bills_label'}) }}
{{ trans('csv.match_bills_description') }}
</label>
</div>
</div>
</div>
{% for type, specific in data.specifics %} {% for type, specific in data.specifics %}
<div class="form-group"> <div class="form-group">
<label for="{{ type }}_label" class="col-sm-4 control-label"> <label for="{{ type }}_label" class="col-sm-4 control-label">