mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-29 02:11:12 -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 = [];
|
||||
foreach (Config::get('csv.roles') as $role) {
|
||||
$fieldName = $role['field'];
|
||||
$filler[$fieldName] = null;
|
||||
if (isset($role['field'])) {
|
||||
$fieldName = $role['field'];
|
||||
$filler[$fieldName] = null;
|
||||
}
|
||||
}
|
||||
// some extra's:
|
||||
$filler['bill-id'] = null;
|
||||
$filler['opposing-account-object'] = null;
|
||||
$filler['amount-modifier'] = '1';
|
||||
|
||||
return $filler;
|
||||
|
||||
@ -175,18 +178,10 @@ class Importer
|
||||
$data['description'] = trim($data['description']);
|
||||
$data['amount'] = bcmul($data['amount'], $data['amount-modifier']);
|
||||
|
||||
|
||||
// get opposing account, which is quite complex:
|
||||
$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) {
|
||||
$data['description'] = trans('firefly.csv_empty_description');
|
||||
}
|
||||
@ -205,36 +200,101 @@ class Importer
|
||||
$specifix->setData($data);
|
||||
$specifix->setRow($row);
|
||||
//$specifix->fix($data, $row); // TODO
|
||||
|
||||
// get data back:
|
||||
//$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
public function processOpposingAccount(array $data)
|
||||
protected function processOpposingAccount(array $data)
|
||||
{
|
||||
// first priority. try to find the account based on ID,
|
||||
// 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.
|
||||
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.
|
||||
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'])) {
|
||||
return 'No date value for this row.';
|
||||
}
|
||||
if (strlen($data['description']) == 0) {
|
||||
return 'No valid description';
|
||||
}
|
||||
if (is_null($data['opposing-account-object'])) {
|
||||
return 'Opposing account is null';
|
||||
}
|
||||
|
@ -122,19 +122,22 @@ return [
|
||||
'mapper' => 'AssetAccount'
|
||||
],
|
||||
'opposing-id' => [
|
||||
'name' => 'Opposing account account ID (matching Firefly)',
|
||||
'mappable' => true,
|
||||
'field' => 'opposing-account-id',
|
||||
'name' => 'Opposing account account ID (matching Firefly)',
|
||||
'mappable' => true,
|
||||
'field' => 'opposing-account-id',
|
||||
'converter' => 'OpposingAccountId'
|
||||
],
|
||||
'opposing-name' => [
|
||||
'name' => 'Opposing account name',
|
||||
'mappable' => true,
|
||||
'field' => 'opposing-account-name',
|
||||
'converter' => 'OpposingAccountName'
|
||||
],
|
||||
'opposing-iban' => [
|
||||
'name' => 'Opposing account IBAN',
|
||||
'mappable' => true,
|
||||
'field' => 'opposing-account-iban',
|
||||
'converter' => 'OpposingAccountIban'
|
||||
],
|
||||
'amount' => [
|
||||
'name' => 'Amount',
|
||||
|
Loading…
Reference in New Issue
Block a user