2017-06-12 12:12:07 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-17 15:49:44 -05:00
|
|
|
* ImportJournal.php
|
2017-06-12 12:12:07 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Import\Object;
|
|
|
|
|
|
|
|
|
2017-06-17 15:49:44 -05:00
|
|
|
use Carbon\Carbon;
|
2017-06-12 12:12:07 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2017-06-20 14:04:25 -05:00
|
|
|
use FireflyIII\Import\Converter\Amount;
|
|
|
|
use FireflyIII\Import\Converter\ConverterInterface;
|
2017-06-17 15:49:44 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2017-06-12 12:12:07 -05:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-06-20 14:04:25 -05:00
|
|
|
use Steam;
|
2017-06-12 12:12:07 -05:00
|
|
|
|
2017-06-17 15:49:44 -05:00
|
|
|
/**
|
|
|
|
* Class ImportJournal
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Import\Object
|
|
|
|
*/
|
|
|
|
class ImportJournal
|
2017-06-12 12:12:07 -05:00
|
|
|
{
|
2017-06-20 14:04:25 -05:00
|
|
|
/** @var ImportAccount */
|
|
|
|
public $asset;
|
|
|
|
/** @var ImportBudget */
|
|
|
|
public $budget;
|
|
|
|
/** @var string */
|
|
|
|
public $description;
|
2017-06-12 12:12:07 -05:00
|
|
|
/** @var Collection */
|
|
|
|
public $errors;
|
2017-06-20 14:04:25 -05:00
|
|
|
/** @var string */
|
|
|
|
public $hash;
|
|
|
|
/** @var ImportAccount */
|
|
|
|
public $opposing;
|
2017-06-17 15:49:44 -05:00
|
|
|
/** @var string */
|
|
|
|
private $amount = '0';
|
2017-06-14 13:13:19 -05:00
|
|
|
/** @var ImportBill */
|
|
|
|
private $bill;
|
|
|
|
/** @var ImportCategory */
|
2017-06-20 14:04:25 -05:00
|
|
|
public $category;
|
2017-06-17 15:49:44 -05:00
|
|
|
/** @var ImportCurrency */
|
|
|
|
private $currency;
|
|
|
|
/** @var string */
|
|
|
|
private $date = '';
|
|
|
|
/** @var string */
|
2017-06-14 13:13:19 -05:00
|
|
|
private $externalId = '';
|
2017-06-17 15:49:44 -05:00
|
|
|
/** @var array */
|
|
|
|
private $modifiers = [];
|
2017-06-20 14:04:25 -05:00
|
|
|
private $tags = [];
|
2017-06-17 15:49:44 -05:00
|
|
|
/** @var string */
|
|
|
|
private $transactionType = '';
|
2017-06-12 12:12:07 -05:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ImportEntry constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->errors = new Collection;
|
|
|
|
$this->asset = new ImportAccount;
|
|
|
|
$this->opposing = new ImportAccount;
|
|
|
|
$this->bill = new ImportBill;
|
|
|
|
$this->category = new ImportCategory;
|
|
|
|
$this->budget = new ImportBudget;
|
2017-06-20 14:04:25 -05:00
|
|
|
$this->currency = new ImportCurrency;
|
2017-06-17 15:49:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $modifier
|
|
|
|
*/
|
|
|
|
public function addToModifier(array $modifier)
|
|
|
|
{
|
|
|
|
$this->modifiers[] = $modifier;
|
2017-06-12 12:12:07 -05:00
|
|
|
}
|
|
|
|
|
2017-06-17 15:49:44 -05:00
|
|
|
/**
|
|
|
|
* @return TransactionJournal
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function createTransactionJournal(): TransactionJournal
|
2017-06-12 12:12:07 -05:00
|
|
|
{
|
2017-06-17 15:49:44 -05:00
|
|
|
exit('does not work yet');
|
2017-06-12 12:12:07 -05:00
|
|
|
}
|
|
|
|
|
2017-06-20 14:04:25 -05:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAmount(): string
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var ConverterInterface $amountConverter */
|
|
|
|
$amountConverter = app(Amount::class);
|
|
|
|
$this->amount = strval($amountConverter->convert($this->amount));
|
|
|
|
// modify
|
|
|
|
foreach ($this->modifiers as $modifier) {
|
|
|
|
$class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $modifier['role'])));
|
|
|
|
/** @var ConverterInterface $converter */
|
|
|
|
$converter = app($class);
|
|
|
|
if ($converter->convert($modifier['value']) === -1) {
|
|
|
|
$this->amount = Steam::negative($this->amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ImportCurrency
|
|
|
|
*/
|
|
|
|
public function getCurrency(): ImportCurrency
|
|
|
|
{
|
|
|
|
return $this->currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $format
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function getDate(string $format): Carbon
|
|
|
|
{
|
|
|
|
return Carbon::createFromFormat($format, $this->date);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $hash
|
|
|
|
*/
|
|
|
|
public function setHash(string $hash)
|
|
|
|
{
|
|
|
|
$this->hash = $hash;
|
|
|
|
}
|
|
|
|
|
2017-06-12 12:12:07 -05:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
2017-06-20 14:04:25 -05:00
|
|
|
|
2017-06-17 15:49:44 -05:00
|
|
|
// set user for related objects:
|
|
|
|
$this->asset->setUser($user);
|
|
|
|
$this->opposing->setUser($user);
|
2017-06-20 14:04:25 -05:00
|
|
|
$this->budget->setUser($user);
|
|
|
|
$this->category->setUser($user);
|
2017-06-12 12:12:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $array
|
2017-06-14 13:13:19 -05:00
|
|
|
*
|
|
|
|
* @throws FireflyException
|
2017-06-12 12:12:07 -05:00
|
|
|
*/
|
|
|
|
public function setValue(array $array)
|
|
|
|
{
|
|
|
|
switch ($array['role']) {
|
|
|
|
default:
|
2017-06-17 15:49:44 -05:00
|
|
|
throw new FireflyException(sprintf('ImportJournal cannot handle "%s" with value "%s".', $array['role'], $array['value']));
|
2017-06-12 12:12:07 -05:00
|
|
|
case 'account-id':
|
2017-06-14 13:13:19 -05:00
|
|
|
$this->asset->setAccountId($array);
|
|
|
|
break;
|
|
|
|
case 'amount':
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->amount = $array['value'];
|
2017-06-14 13:13:19 -05:00
|
|
|
break;
|
|
|
|
case 'account-iban':
|
|
|
|
$this->asset->setAccountIban($array);
|
|
|
|
break;
|
|
|
|
case 'account-name':
|
|
|
|
$this->asset->setAccountName($array);
|
|
|
|
break;
|
|
|
|
case 'account-number':
|
|
|
|
$this->asset->setAccountNumber($array);
|
|
|
|
break;
|
|
|
|
case 'bill-id':
|
|
|
|
$this->bill->setId($array);
|
|
|
|
break;
|
|
|
|
case 'bill-name':
|
|
|
|
$this->bill->setName($array);
|
|
|
|
break;
|
|
|
|
case 'budget-id':
|
|
|
|
$this->budget->setId($array);
|
|
|
|
break;
|
|
|
|
case 'budget-name':
|
|
|
|
$this->budget->setName($array);
|
|
|
|
break;
|
|
|
|
case 'category-id':
|
|
|
|
$this->category->setId($array);
|
|
|
|
break;
|
|
|
|
case 'category-name':
|
|
|
|
$this->category->setName($array);
|
|
|
|
break;
|
|
|
|
case 'currency-code':
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->currency->setCode($array);
|
2017-06-14 13:13:19 -05:00
|
|
|
break;
|
|
|
|
case 'currency-id':
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->currency->setId($array);
|
2017-06-14 13:13:19 -05:00
|
|
|
break;
|
|
|
|
case 'currency-name':
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->currency->setName($array);
|
2017-06-14 13:13:19 -05:00
|
|
|
break;
|
|
|
|
case 'currency-symbol':
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->currency->setSymbol($array);
|
2017-06-14 13:13:19 -05:00
|
|
|
break;
|
|
|
|
case 'date-transaction':
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->date = $array['value'];
|
2017-06-14 13:13:19 -05:00
|
|
|
break;
|
|
|
|
case 'description':
|
|
|
|
$this->description = $array['value'];
|
|
|
|
break;
|
|
|
|
case 'external-id':
|
|
|
|
$this->externalId = $array['value'];
|
|
|
|
break;
|
|
|
|
case '_ignore':
|
|
|
|
break;
|
|
|
|
case 'ing-debet-credit':
|
|
|
|
case 'rabo-debet-credit':
|
2017-06-17 15:49:44 -05:00
|
|
|
$this->addToModifier($array);
|
2017-06-14 13:13:19 -05:00
|
|
|
break;
|
|
|
|
case 'opposing-iban':
|
|
|
|
$this->opposing->setAccountIban($array);
|
|
|
|
break;
|
|
|
|
case 'opposing-name':
|
|
|
|
$this->opposing->setAccountName($array);
|
|
|
|
break;
|
|
|
|
case 'opposing-number':
|
|
|
|
$this->opposing->setAccountNumber($array);
|
|
|
|
break;
|
|
|
|
case 'opposing-id':
|
|
|
|
$this->opposing->setAccountId($array);
|
|
|
|
break;
|
|
|
|
case 'tags-comma':
|
|
|
|
case 'tags-space':
|
|
|
|
$this->tags[] = $array;
|
2017-06-12 12:12:07 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|