mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-31 19:27:51 -06:00
Massively complex but working never the less.
This commit is contained in:
parent
601f9f86bb
commit
12ee5da872
39
app/Helpers/Csv/Converter/OpposingAccountIban.php
Normal file
39
app/Helpers/Csv/Converter/OpposingAccountIban.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Helpers\Csv\Converter;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
|
use FireflyIII\Models\Account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OpposingAccountIban
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Helpers\Csv\Converter
|
||||||
|
*/
|
||||||
|
class OpposingAccountIban extends BasicConverter implements ConverterInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If mapped, return account. Otherwise, only return the name itself.
|
||||||
|
*
|
||||||
|
* @return Account|string
|
||||||
|
*/
|
||||||
|
public function convert()
|
||||||
|
{
|
||||||
|
if (isset($this->mapped[$this->index][$this->value])) {
|
||||||
|
$account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]);
|
||||||
|
|
||||||
|
return $account;
|
||||||
|
} else {
|
||||||
|
$set = Auth::user()->accounts()->get();
|
||||||
|
/** @var Account $account */
|
||||||
|
foreach ($set as $account) {
|
||||||
|
if ($account->iban == $this->value) {
|
||||||
|
return $account;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
app/Helpers/Csv/Converter/OpposingAccountId.php
Normal file
32
app/Helpers/Csv/Converter/OpposingAccountId.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Helpers\Csv\Converter;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
|
use FireflyIII\Models\Account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OpposingName
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Helpers\Csv\Converter
|
||||||
|
*/
|
||||||
|
class OpposingAccountId extends BasicConverter implements ConverterInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Account
|
||||||
|
*/
|
||||||
|
public function convert()
|
||||||
|
{
|
||||||
|
if (isset($this->mapped[$this->index][$this->value])) {
|
||||||
|
$account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$account = Auth::user()->accounts()->find($this->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $account;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
31
app/Helpers/Csv/Converter/OpposingAccountName.php
Normal file
31
app/Helpers/Csv/Converter/OpposingAccountName.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Helpers\Csv\Converter;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
|
use FireflyIII\Models\Account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OpposingName
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Helpers\Csv\Converter
|
||||||
|
*/
|
||||||
|
class OpposingAccountName extends BasicConverter implements ConverterInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If mapped, return account. Otherwise, only return the name itself.
|
||||||
|
*
|
||||||
|
* @return Account|string
|
||||||
|
*/
|
||||||
|
public function convert()
|
||||||
|
{
|
||||||
|
if (isset($this->mapped[$this->index][$this->value])) {
|
||||||
|
$account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]);
|
||||||
|
|
||||||
|
return $account;
|
||||||
|
} else {
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace FireflyIII\Helpers\Csv\Converter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class OpposingName
|
|
||||||
*
|
|
||||||
* @package FireflyIII\Helpers\Csv\Converter
|
|
||||||
*/
|
|
||||||
class OpposingName extends BasicConverter implements ConverterInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method cannot search yet for the correct account (Expense account, Revenue account or Asset account) because simply put,
|
|
||||||
* Firefly doesn't know yet if this account needs to be an Expense account or a Revenue account. This depends
|
|
||||||
* on the amount which is in the current row and that's a big unknown.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function convert()
|
|
||||||
{
|
|
||||||
return $this->value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -150,12 +150,15 @@ class Importer
|
|||||||
{
|
{
|
||||||
$filler = [];
|
$filler = [];
|
||||||
foreach (Config::get('csv.roles') as $role) {
|
foreach (Config::get('csv.roles') as $role) {
|
||||||
$fieldName = $role['field'];
|
if (isset($role['field'])) {
|
||||||
$filler[$fieldName] = null;
|
$fieldName = $role['field'];
|
||||||
|
$filler[$fieldName] = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// some extra's:
|
// some extra's:
|
||||||
$filler['bill-id'] = null;
|
$filler['bill-id'] = null;
|
||||||
$filler['opposing-account-object'] = null;
|
$filler['opposing-account-object'] = null;
|
||||||
|
$filler['amount-modifier'] = '1';
|
||||||
|
|
||||||
return $filler;
|
return $filler;
|
||||||
|
|
||||||
@ -175,18 +178,10 @@ class Importer
|
|||||||
$data['description'] = trim($data['description']);
|
$data['description'] = trim($data['description']);
|
||||||
$data['amount'] = bcmul($data['amount'], $data['amount-modifier']);
|
$data['amount'] = bcmul($data['amount'], $data['amount-modifier']);
|
||||||
|
|
||||||
|
|
||||||
// get opposing account, which is quite complex:
|
// get opposing account, which is quite complex:
|
||||||
$data['opposing-account-object'] = $this->processOpposingAccount($data);
|
$data['opposing-account-object'] = $this->processOpposingAccount($data);
|
||||||
|
|
||||||
// opposing account type:
|
|
||||||
if ($data['amount'] < 0) {
|
|
||||||
// create expense account:
|
|
||||||
$accountType = AccountType::where('type', 'Expense account')->first();
|
|
||||||
} else {
|
|
||||||
// create revenue account:
|
|
||||||
$accountType = AccountType::where('type', 'Revenue account')->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strlen($data['description']) == 0) {
|
if (strlen($data['description']) == 0) {
|
||||||
$data['description'] = trans('firefly.csv_empty_description');
|
$data['description'] = trans('firefly.csv_empty_description');
|
||||||
}
|
}
|
||||||
@ -205,36 +200,101 @@ class Importer
|
|||||||
$specifix->setData($data);
|
$specifix->setData($data);
|
||||||
$specifix->setRow($row);
|
$specifix->setRow($row);
|
||||||
//$specifix->fix($data, $row); // TODO
|
//$specifix->fix($data, $row); // TODO
|
||||||
|
|
||||||
// get data back:
|
// get data back:
|
||||||
//$data = $specifix->getData(); // TODO
|
//$data = $specifix->getData(); // TODO
|
||||||
|
|
||||||
$data['opposing-account-object'] = Account::firstOrCreateEncrypted(
|
|
||||||
[
|
|
||||||
'user_id' => Auth::user()->id,
|
|
||||||
'name' => ucwords($data['opposing-account']),
|
|
||||||
'account_type_id' => $accountType->id,
|
|
||||||
'active' => 1,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*/
|
*/
|
||||||
public function processOpposingAccount(array $data)
|
protected function processOpposingAccount(array $data)
|
||||||
{
|
{
|
||||||
// first priority. try to find the account based on ID,
|
// first priority. try to find the account based on ID,
|
||||||
// if any.
|
// if any.
|
||||||
|
if ($data['opposing-account-id'] instanceof Account) {
|
||||||
|
return $data['opposing-account-id'];
|
||||||
|
}
|
||||||
|
|
||||||
// second: try to find the account based on IBAN, if any.
|
// second: try to find the account based on IBAN, if any.
|
||||||
|
if ($data['opposing-account-iban'] instanceof Account) {
|
||||||
|
return $data['opposing-account-iban'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$accountType = $this->getAccountType($data['amount']);
|
||||||
|
|
||||||
|
if (is_string($data['opposing-account-iban'])) {
|
||||||
|
$accounts = Auth::user()->accounts()->where('account_type_id', $accountType->id)->get();
|
||||||
|
foreach ($accounts as $entry) {
|
||||||
|
if ($entry->iban == $data['opposing-account-iban']) {
|
||||||
|
|
||||||
|
//return $entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// create if not exists:
|
||||||
|
$account = Account::firstOrCreateEncrypted(
|
||||||
|
[
|
||||||
|
'user_id' => Auth::user()->id,
|
||||||
|
'account_type_id' => $accountType->id,
|
||||||
|
'name' => $data['opposing-account-iban'],
|
||||||
|
'iban' => $data['opposing-account-iban'],
|
||||||
|
'active' => true,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return $account;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// third: try to find account based on name, if any.
|
// third: try to find account based on name, if any.
|
||||||
|
if ($data['opposing-account-name'] instanceof Account) {
|
||||||
|
return $data['opposing-account-name'];
|
||||||
|
}
|
||||||
|
|
||||||
// if nothing, create expense/revenue, never asset.
|
if (is_string($data['opposing-account-name'])) {
|
||||||
|
$accounts = Auth::user()->accounts()->where('account_type_id', $accountType->id)->get();
|
||||||
|
foreach ($accounts as $entry) {
|
||||||
|
if ($entry->name == $data['opposing-account-name']) {
|
||||||
|
return $entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// create if not exists:
|
||||||
|
$account = Account::firstOrCreateEncrypted(
|
||||||
|
[
|
||||||
|
'user_id' => Auth::user()->id,
|
||||||
|
'account_type_id' => $accountType->id,
|
||||||
|
'name' => $data['opposing-account-name'],
|
||||||
|
'iban' => '',
|
||||||
|
'active' => true,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return $account;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// if nothing, create expense/revenue, never asset. TODO
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $amount
|
||||||
|
*
|
||||||
|
* @return AccountType
|
||||||
|
*/
|
||||||
|
protected function getAccountType($amount)
|
||||||
|
{
|
||||||
|
// opposing account type:
|
||||||
|
if ($amount < 0) {
|
||||||
|
// create expense account:
|
||||||
|
|
||||||
|
return AccountType::where('type', 'Expense account')->first();
|
||||||
|
} else {
|
||||||
|
// create revenue account:
|
||||||
|
|
||||||
|
return AccountType::where('type', 'Revenue account')->first();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -247,9 +307,6 @@ class Importer
|
|||||||
if (is_null($data['date']) && is_null($data['date-rent'])) {
|
if (is_null($data['date']) && is_null($data['date-rent'])) {
|
||||||
return 'No date value for this row.';
|
return 'No date value for this row.';
|
||||||
}
|
}
|
||||||
if (strlen($data['description']) == 0) {
|
|
||||||
return 'No valid description';
|
|
||||||
}
|
|
||||||
if (is_null($data['opposing-account-object'])) {
|
if (is_null($data['opposing-account-object'])) {
|
||||||
return 'Opposing account is null';
|
return 'Opposing account is null';
|
||||||
}
|
}
|
||||||
|
@ -122,19 +122,22 @@ return [
|
|||||||
'mapper' => 'AssetAccount'
|
'mapper' => 'AssetAccount'
|
||||||
],
|
],
|
||||||
'opposing-id' => [
|
'opposing-id' => [
|
||||||
'name' => 'Opposing account account ID (matching Firefly)',
|
'name' => 'Opposing account account ID (matching Firefly)',
|
||||||
'mappable' => true,
|
'mappable' => true,
|
||||||
'field' => 'opposing-account-id',
|
'field' => 'opposing-account-id',
|
||||||
|
'converter' => 'OpposingAccountId'
|
||||||
],
|
],
|
||||||
'opposing-name' => [
|
'opposing-name' => [
|
||||||
'name' => 'Opposing account name',
|
'name' => 'Opposing account name',
|
||||||
'mappable' => true,
|
'mappable' => true,
|
||||||
'field' => 'opposing-account-name',
|
'field' => 'opposing-account-name',
|
||||||
|
'converter' => 'OpposingAccountName'
|
||||||
],
|
],
|
||||||
'opposing-iban' => [
|
'opposing-iban' => [
|
||||||
'name' => 'Opposing account IBAN',
|
'name' => 'Opposing account IBAN',
|
||||||
'mappable' => true,
|
'mappable' => true,
|
||||||
'field' => 'opposing-account-iban',
|
'field' => 'opposing-account-iban',
|
||||||
|
'converter' => 'OpposingAccountIban'
|
||||||
],
|
],
|
||||||
'amount' => [
|
'amount' => [
|
||||||
'name' => 'Amount',
|
'name' => 'Amount',
|
||||||
|
Loading…
Reference in New Issue
Block a user