mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
chore: reformat code.
This commit is contained in:
@@ -43,7 +43,7 @@ abstract class AbstractTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ParameterBag $parameters
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
final public function setParameters(ParameterBag $parameters): void
|
||||
{
|
||||
|
||||
@@ -52,7 +52,7 @@ class AccountTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the account.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param Account $account
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -133,16 +133,16 @@ class AccountTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/accounts/'.$account->id,
|
||||
'uri' => '/accounts/' . $account->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Account $account
|
||||
*
|
||||
* @param string $accountType
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
@@ -157,9 +157,47 @@ class AccountTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string|null $accountRole
|
||||
* @param string $accountType
|
||||
* TODO duplicated in the V2 transformer.
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getDate(): Carbon
|
||||
{
|
||||
$date = today(config('app.timezone'));
|
||||
if (null !== $this->parameters->get('date')) {
|
||||
$date = $this->parameters->get('date');
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
*/
|
||||
private function getCurrency(Account $account): array
|
||||
{
|
||||
$currency = $this->repository->getAccountCurrency($account);
|
||||
|
||||
// only grab default when result is null:
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
$currencyId = (string)$currency->id;
|
||||
$currencyCode = $currency->code;
|
||||
$decimalPlaces = $currency->decimal_places;
|
||||
$currencySymbol = $currency->symbol;
|
||||
|
||||
return [$currencyId, $currencyCode, $currencySymbol, $decimalPlaces];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string|null $accountRole
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -185,63 +223,8 @@ class AccountTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
*/
|
||||
private function getCurrency(Account $account): array
|
||||
{
|
||||
$currency = $this->repository->getAccountCurrency($account);
|
||||
|
||||
// only grab default when result is null:
|
||||
if (null === $currency) {
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
$currencyId = (string)$currency->id;
|
||||
$currencyCode = $currency->code;
|
||||
$decimalPlaces = $currency->decimal_places;
|
||||
$currencySymbol = $currency->symbol;
|
||||
|
||||
return [$currencyId, $currencyCode, $currencySymbol, $decimalPlaces];
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO duplicated in the V2 transformer.
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getDate(): Carbon
|
||||
{
|
||||
$date = today(config('app.timezone'));
|
||||
if (null !== $this->parameters->get('date')) {
|
||||
$date = $this->parameters->get('date');
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getInterest(Account $account, string $accountType): array
|
||||
{
|
||||
$interest = null;
|
||||
$interestPeriod = null;
|
||||
if ('liabilities' === $accountType) {
|
||||
$interest = $this->repository->getMetaValue($account, 'interest');
|
||||
$interestPeriod = $this->repository->getMetaValue($account, 'interest_period');
|
||||
}
|
||||
|
||||
return [$interest, $interestPeriod];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $accountType
|
||||
* @param Account $account
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
@@ -262,4 +245,22 @@ class AccountTransformer extends AbstractTransformer
|
||||
|
||||
return [$openingBalance, $openingBalanceDate];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getInterest(Account $account, string $accountType): array
|
||||
{
|
||||
$interest = null;
|
||||
$interestPeriod = null;
|
||||
if ('liabilities' === $accountType) {
|
||||
$interest = $this->repository->getMetaValue($account, 'interest');
|
||||
$interestPeriod = $this->repository->getMetaValue($account, 'interest_period');
|
||||
}
|
||||
|
||||
return [$interest, $interestPeriod];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class AttachmentTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform attachment.
|
||||
*
|
||||
* @param Attachment $attachment
|
||||
* @param Attachment $attachment
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -71,7 +71,7 @@ class AttachmentTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/attachment/'.$attachment->id,
|
||||
'uri' => '/attachment/' . $attachment->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -52,7 +52,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the note.
|
||||
*
|
||||
* @param AvailableBudget $availableBudget
|
||||
* @param AvailableBudget $availableBudget
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -77,7 +77,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/available_budgets/'.$availableBudget->id,
|
||||
'uri' => '/available_budgets/' . $availableBudget->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -52,7 +52,7 @@ class BillTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the bill.
|
||||
*
|
||||
* @param Bill $bill
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -137,63 +137,16 @@ class BillTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/bills/'.$bill->id,
|
||||
'uri' => '/bills/' . $bill->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the latest date in the set, or start when set is empty.
|
||||
*
|
||||
* @param Collection $dates
|
||||
* @param Carbon $default
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function lastPaidDate(Collection $dates, Carbon $default): Carbon
|
||||
{
|
||||
if (0 === $dates->count()) {
|
||||
return $default;
|
||||
}
|
||||
$latest = $dates->first()->date;
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($dates as $journal) {
|
||||
if ($journal->date->gte($latest)) {
|
||||
$latest = $journal->date;
|
||||
}
|
||||
}
|
||||
|
||||
return $latest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a bill and a date, this method will tell you at which moment this bill expects its next
|
||||
* transaction. Whether or not it is there already, is not relevant.
|
||||
*
|
||||
* @param Bill $bill
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function nextDateMatch(Bill $bill, Carbon $date): Carbon
|
||||
{
|
||||
//Log::debug(sprintf('Now in nextDateMatch(%d, %s)', $bill->id, $date->format('Y-m-d')));
|
||||
$start = clone $bill->date;
|
||||
//Log::debug(sprintf('Bill start date is %s', $start->format('Y-m-d')));
|
||||
while ($start < $date) {
|
||||
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
|
||||
}
|
||||
|
||||
//Log::debug(sprintf('End of loop, bill start date is now %s', $start->format('Y-m-d')));
|
||||
|
||||
return $start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data the bill was paid and predict the next expected match.
|
||||
*
|
||||
* @param Bill $bill
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -265,7 +218,31 @@ class BillTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
* Returns the latest date in the set, or start when set is empty.
|
||||
*
|
||||
* @param Collection $dates
|
||||
* @param Carbon $default
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function lastPaidDate(Collection $dates, Carbon $default): Carbon
|
||||
{
|
||||
if (0 === $dates->count()) {
|
||||
return $default;
|
||||
}
|
||||
$latest = $dates->first()->date;
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($dates as $journal) {
|
||||
if ($journal->date->gte($latest)) {
|
||||
$latest = $journal->date;
|
||||
}
|
||||
}
|
||||
|
||||
return $latest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -303,4 +280,27 @@ class BillTransformer extends AbstractTransformer
|
||||
|
||||
return $simple->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a bill and a date, this method will tell you at which moment this bill expects its next
|
||||
* transaction. Whether or not it is there already, is not relevant.
|
||||
*
|
||||
* @param Bill $bill
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function nextDateMatch(Bill $bill, Carbon $date): Carbon
|
||||
{
|
||||
//Log::debug(sprintf('Now in nextDateMatch(%d, %s)', $bill->id, $date->format('Y-m-d')));
|
||||
$start = clone $bill->date;
|
||||
//Log::debug(sprintf('Bill start date is %s', $start->format('Y-m-d')));
|
||||
while ($start < $date) {
|
||||
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
|
||||
}
|
||||
|
||||
//Log::debug(sprintf('End of loop, bill start date is now %s', $start->format('Y-m-d')));
|
||||
|
||||
return $start;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Include Budget
|
||||
*
|
||||
* @param BudgetLimit $limit
|
||||
* @param BudgetLimit $limit
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
@@ -54,7 +54,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the note.
|
||||
*
|
||||
* @param BudgetLimit $budgetLimit
|
||||
* @param BudgetLimit $budgetLimit
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -104,7 +104,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/budgets/limits/'.$budgetLimit->id,
|
||||
'uri' => '/budgets/limits/' . $budgetLimit->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -53,7 +53,7 @@ class BudgetTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform a budget.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -106,14 +106,14 @@ class BudgetTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/budgets/'.$budget->id,
|
||||
'uri' => '/budgets/' . $budget->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -50,7 +50,7 @@ class CategoryTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Convert category.
|
||||
*
|
||||
* @param Category $category
|
||||
* @param Category $category
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -80,14 +80,14 @@ class CategoryTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/categories/'.$category->id,
|
||||
'uri' => '/categories/' . $category->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -33,7 +33,7 @@ class CurrencyTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the currency.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -58,7 +58,7 @@ class CurrencyTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/currencies/'.$currency->id,
|
||||
'uri' => '/currencies/' . $currency->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -34,7 +34,7 @@ class LinkTypeTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the currency.
|
||||
*
|
||||
* @param LinkType $linkType
|
||||
* @param LinkType $linkType
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -51,7 +51,7 @@ class LinkTypeTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/link_types/'.$linkType->id,
|
||||
'uri' => '/link_types/' . $linkType->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -47,7 +47,7 @@ class ObjectGroupTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the account.
|
||||
*
|
||||
* @param ObjectGroup $objectGroup
|
||||
* @param ObjectGroup $objectGroup
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -64,7 +64,7 @@ class ObjectGroupTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/object_groups/'.$objectGroup->id,
|
||||
'uri' => '/object_groups/' . $objectGroup->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -54,7 +54,7 @@ class PiggyBankEventTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Convert piggy bank event.
|
||||
*
|
||||
* @param PiggyBankEvent $event
|
||||
* @param PiggyBankEvent $event
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -95,7 +95,7 @@ class PiggyBankEventTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/piggy_bank_events/'.$event->id,
|
||||
'uri' => '/piggy_bank_events/' . $event->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -55,7 +55,7 @@ class PiggyBankTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the piggy bank.
|
||||
*
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -133,7 +133,7 @@ class PiggyBankTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/piggy_banks/'.$piggyBank->id,
|
||||
'uri' => '/piggy_banks/' . $piggyBank->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -33,7 +33,7 @@ class PreferenceTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the preference
|
||||
*
|
||||
* @param Preference $preference
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -65,7 +65,7 @@ class RecurrenceTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -104,14 +104,14 @@ class RecurrenceTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/recurring/'.$recurrence->id,
|
||||
'uri' => '/recurring/' . $recurrence->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -150,76 +150,7 @@ class RecurrenceTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getTransactionMeta(RecurrenceTransaction $transaction, array $array): array
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
$array['tags'] = [];
|
||||
$array['category_id'] = null;
|
||||
$array['category_name'] = null;
|
||||
$array['budget_id'] = null;
|
||||
$array['budget_name'] = null;
|
||||
$array['piggy_bank_id'] = null;
|
||||
$array['piggy_bank_name'] = null;
|
||||
$array['bill_id'] = null;
|
||||
$array['bill_name'] = null;
|
||||
|
||||
/** @var RecurrenceTransactionMeta $transactionMeta */
|
||||
foreach ($transaction->recurrenceTransactionMeta as $transactionMeta) {
|
||||
switch ($transactionMeta->name) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('Recurrence transformer cant handle field "%s"', $transactionMeta->name));
|
||||
case 'bill_id':
|
||||
$bill = $this->billRepos->find((int)$transactionMeta->value);
|
||||
if (null !== $bill) {
|
||||
$array['bill_id'] = (string)$bill->id;
|
||||
$array['bill_name'] = $bill->name;
|
||||
}
|
||||
break;
|
||||
case 'tags':
|
||||
$array['tags'] = json_decode($transactionMeta->value);
|
||||
break;
|
||||
case 'piggy_bank_id':
|
||||
$piggy = $this->piggyRepos->find((int)$transactionMeta->value);
|
||||
if (null !== $piggy) {
|
||||
$array['piggy_bank_id'] = (string)$piggy->id;
|
||||
$array['piggy_bank_name'] = $piggy->name;
|
||||
}
|
||||
break;
|
||||
case 'category_id':
|
||||
$category = $this->factory->findOrCreate((int)$transactionMeta->value, null);
|
||||
if (null !== $category) {
|
||||
$array['category_id'] = (string)$category->id;
|
||||
$array['category_name'] = $category->name;
|
||||
}
|
||||
break;
|
||||
case 'category_name':
|
||||
$category = $this->factory->findOrCreate(null, $transactionMeta->value);
|
||||
if (null !== $category) {
|
||||
$array['category_id'] = (string)$category->id;
|
||||
$array['category_name'] = $category->name;
|
||||
}
|
||||
break;
|
||||
case 'budget_id':
|
||||
$budget = $this->budgetRepos->find((int)$transactionMeta->value);
|
||||
if (null !== $budget) {
|
||||
$array['budget_id'] = (string)$budget->id;
|
||||
$array['budget_name'] = $budget->name;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -305,4 +236,73 @@ class RecurrenceTransformer extends AbstractTransformer
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getTransactionMeta(RecurrenceTransaction $transaction, array $array): array
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
$array['tags'] = [];
|
||||
$array['category_id'] = null;
|
||||
$array['category_name'] = null;
|
||||
$array['budget_id'] = null;
|
||||
$array['budget_name'] = null;
|
||||
$array['piggy_bank_id'] = null;
|
||||
$array['piggy_bank_name'] = null;
|
||||
$array['bill_id'] = null;
|
||||
$array['bill_name'] = null;
|
||||
|
||||
/** @var RecurrenceTransactionMeta $transactionMeta */
|
||||
foreach ($transaction->recurrenceTransactionMeta as $transactionMeta) {
|
||||
switch ($transactionMeta->name) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('Recurrence transformer cant handle field "%s"', $transactionMeta->name));
|
||||
case 'bill_id':
|
||||
$bill = $this->billRepos->find((int)$transactionMeta->value);
|
||||
if (null !== $bill) {
|
||||
$array['bill_id'] = (string)$bill->id;
|
||||
$array['bill_name'] = $bill->name;
|
||||
}
|
||||
break;
|
||||
case 'tags':
|
||||
$array['tags'] = json_decode($transactionMeta->value);
|
||||
break;
|
||||
case 'piggy_bank_id':
|
||||
$piggy = $this->piggyRepos->find((int)$transactionMeta->value);
|
||||
if (null !== $piggy) {
|
||||
$array['piggy_bank_id'] = (string)$piggy->id;
|
||||
$array['piggy_bank_name'] = $piggy->name;
|
||||
}
|
||||
break;
|
||||
case 'category_id':
|
||||
$category = $this->factory->findOrCreate((int)$transactionMeta->value, null);
|
||||
if (null !== $category) {
|
||||
$array['category_id'] = (string)$category->id;
|
||||
$array['category_name'] = $category->name;
|
||||
}
|
||||
break;
|
||||
case 'category_name':
|
||||
$category = $this->factory->findOrCreate(null, $transactionMeta->value);
|
||||
if (null !== $category) {
|
||||
$array['category_id'] = (string)$category->id;
|
||||
$array['category_name'] = $category->name;
|
||||
}
|
||||
break;
|
||||
case 'budget_id':
|
||||
$budget = $this->budgetRepos->find((int)$transactionMeta->value);
|
||||
if (null !== $budget) {
|
||||
$array['budget_id'] = (string)$budget->id;
|
||||
$array['budget_name'] = $budget->name;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class RuleGroupTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the rule group
|
||||
*
|
||||
* @param RuleGroup $ruleGroup
|
||||
* @param RuleGroup $ruleGroup
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -50,7 +50,7 @@ class RuleGroupTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/rule_groups/'.$ruleGroup->id,
|
||||
'uri' => '/rule_groups/' . $ruleGroup->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -50,7 +50,7 @@ class RuleTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the rule.
|
||||
*
|
||||
* @param Rule $rule
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -77,40 +77,14 @@ class RuleTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/rules/'.$rule->id,
|
||||
'uri' => '/rules/' . $rule->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function actions(Rule $rule): array
|
||||
{
|
||||
$result = [];
|
||||
$actions = $this->ruleRepository->getRuleActions($rule);
|
||||
/** @var RuleAction $ruleAction */
|
||||
foreach ($actions as $ruleAction) {
|
||||
$result[] = [
|
||||
'id' => (string)$ruleAction->id,
|
||||
'created_at' => $ruleAction->created_at->toAtomString(),
|
||||
'updated_at' => $ruleAction->updated_at->toAtomString(),
|
||||
'type' => $ruleAction->action_type,
|
||||
'value' => $ruleAction->action_value,
|
||||
'order' => $ruleAction->order,
|
||||
'active' => $ruleAction->active,
|
||||
'stop_processing' => $ruleAction->stop_processing,
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
@@ -133,7 +107,7 @@ class RuleTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -160,4 +134,30 @@ class RuleTransformer extends AbstractTransformer
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function actions(Rule $rule): array
|
||||
{
|
||||
$result = [];
|
||||
$actions = $this->ruleRepository->getRuleActions($rule);
|
||||
/** @var RuleAction $ruleAction */
|
||||
foreach ($actions as $ruleAction) {
|
||||
$result[] = [
|
||||
'id' => (string)$ruleAction->id,
|
||||
'created_at' => $ruleAction->created_at->toAtomString(),
|
||||
'updated_at' => $ruleAction->updated_at->toAtomString(),
|
||||
'type' => $ruleAction->action_type,
|
||||
'value' => $ruleAction->action_value,
|
||||
'order' => $ruleAction->order,
|
||||
'active' => $ruleAction->active,
|
||||
'stop_processing' => $ruleAction->stop_processing,
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ class TagTransformer extends AbstractTransformer
|
||||
*
|
||||
* TODO add spent, earned, transferred, etc.
|
||||
*
|
||||
* @param Tag $tag
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -67,7 +67,7 @@ class TagTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/tags/'.$tag->id,
|
||||
'uri' => '/tags/' . $tag->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -78,7 +78,7 @@ class TransactionGroupTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $group
|
||||
* @param array $group
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -97,441 +97,30 @@ class TransactionGroupTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transactions/'.$first['transaction_group_id'],
|
||||
'uri' => '/transactions/' . $first['transaction_group_id'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionGroup $group
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function transformObject(TransactionGroup $group): array
|
||||
{
|
||||
try {
|
||||
$result = [
|
||||
'id' => (int)$group->id,
|
||||
'created_at' => $group->created_at->toAtomString(),
|
||||
'updated_at' => $group->updated_at->toAtomString(),
|
||||
'user' => (int)$group->user_id,
|
||||
'group_title' => $group->title,
|
||||
'transactions' => $this->transformJournals($group->transactionJournals),
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transactions/'.$group->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException(sprintf('Transaction group #%d is broken. Please check out your log files.', $group->id), 0, $e);
|
||||
}
|
||||
|
||||
// do something else.
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $object
|
||||
* @param string $key
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function dateFromArray(NullArrayObject $object, string $key): ?string
|
||||
{
|
||||
if (null === $object[$key]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $object[$key]->toAtomString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getAmount(string $type, string $amount): string
|
||||
{
|
||||
return app('steam')->positive($amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill|null $bill
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getBill(?Bill $bill): array
|
||||
private function transformTransactions(NullArrayObject $data): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'name' => null,
|
||||
];
|
||||
if (null === $bill) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (string)$bill->id;
|
||||
$array['name'] = $bill->name;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget|null $budget
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getBudget(?Budget $budget): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'name' => null,
|
||||
];
|
||||
if (null === $budget) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (int)$budget->id;
|
||||
$array['name'] = $budget->name;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Category|null $category
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getCategory(?Category $category): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'name' => null,
|
||||
];
|
||||
if (null === $category) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (int)$category->id;
|
||||
$array['name'] = $category->name;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $dates
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getDates(NullArrayObject $dates): array
|
||||
{
|
||||
$fields = [
|
||||
'interest_date',
|
||||
'book_date',
|
||||
'process_date',
|
||||
'due_date',
|
||||
'payment_date',
|
||||
'invoice_date',
|
||||
];
|
||||
$return = [];
|
||||
foreach ($fields as $field) {
|
||||
$return[$field] = null;
|
||||
if (null !== $dates[$field]) {
|
||||
$return[$field] = $dates[$field]->toAtomString();
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getDestinationTransaction(TransactionJournal $journal): Transaction
|
||||
{
|
||||
$result = $journal->transactions->first(
|
||||
static function (Transaction $transaction) {
|
||||
return (float)$transaction->amount > 0; // lame but it works
|
||||
}
|
||||
);
|
||||
if (null === $result) {
|
||||
throw new FireflyException(sprintf('Journal #%d unexpectedly has no destination transaction.', $journal->id));
|
||||
$result = [];
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$result[] = $this->transformTransaction($transaction);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string|null $foreignAmount
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getForeignAmount(string $type, ?string $foreignAmount): ?string
|
||||
{
|
||||
$result = null;
|
||||
if (null !== $foreignAmount && '' !== $foreignAmount && bccomp('0', $foreignAmount) !== 0) {
|
||||
$result = app('steam')->positive($foreignAmount);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency|null $currency
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getForeignCurrency(?TransactionCurrency $currency): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'code' => null,
|
||||
'symbol' => null,
|
||||
'decimal_places' => null,
|
||||
];
|
||||
if (null === $currency) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (int)$currency->id;
|
||||
$array['code'] = $currency->code;
|
||||
$array['symbol'] = $currency->symbol;
|
||||
$array['decimal_places'] = (int)$currency->decimal_places;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Location|null
|
||||
*/
|
||||
private function getLocation(TransactionJournal $journal): ?Location
|
||||
{
|
||||
return $journal->locations()->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return Location|null
|
||||
*/
|
||||
private function getLocationById(int $journalId): ?Location
|
||||
{
|
||||
return $this->groupRepos->getLocation($journalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceTransaction(TransactionJournal $journal): Transaction
|
||||
{
|
||||
$result = $journal->transactions->first(
|
||||
static function (Transaction $transaction) {
|
||||
return (float)$transaction->amount < 0; // lame but it works.
|
||||
}
|
||||
);
|
||||
if (null === $result) {
|
||||
throw new FireflyException(sprintf('Journal #%d unexpectedly has no source transaction.', $journal->id));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasAttachments(int $journalId): bool
|
||||
{
|
||||
return $this->groupRepos->countAttachments($journalId) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param string $key
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
private function integerFromArray(array $array, string $key): ?int
|
||||
{
|
||||
if (array_key_exists($key, $array)) {
|
||||
return (int)$array[$key];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param string $key
|
||||
* @param string|null $default
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function stringFromArray(array $array, string $key, ?string $default): ?string
|
||||
{
|
||||
if (array_key_exists($key, $array) && null === $array[$key]) {
|
||||
return null;
|
||||
}
|
||||
if (array_key_exists($key, $array) && null !== $array[$key]) {
|
||||
if (0 === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
if ('0' === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
return (string)$array[$key];
|
||||
}
|
||||
|
||||
if (null !== $default) {
|
||||
return (string)$default;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function transformJournal(TransactionJournal $journal): array
|
||||
{
|
||||
$source = $this->getSourceTransaction($journal);
|
||||
$destination = $this->getDestinationTransaction($journal);
|
||||
$type = $journal->transactionType->type;
|
||||
$currency = $source->transactionCurrency;
|
||||
$amount = app('steam')->bcround($this->getAmount($type, (string)$source->amount), $currency->decimal_places ?? 0);
|
||||
$foreignAmount = $this->getForeignAmount($type, null === $source->foreign_amount ? null : (string)$source->foreign_amount);
|
||||
$metaFieldData = $this->groupRepos->getMetaFields($journal->id, $this->metaFields);
|
||||
$metaDates = $this->getDates($this->groupRepos->getMetaDateFields($journal->id, $this->metaDateFields));
|
||||
$foreignCurrency = $this->getForeignCurrency($source->foreignCurrency);
|
||||
$budget = $this->getBudget($journal->budgets->first());
|
||||
$category = $this->getCategory($journal->categories->first());
|
||||
$bill = $this->getBill($journal->bill);
|
||||
|
||||
if (null !== $foreignAmount && null !== $source->foreignCurrency) {
|
||||
$foreignAmount = app('steam')->bcround($foreignAmount, $foreignCurrency['decimal_places'] ?? 0);
|
||||
}
|
||||
|
||||
$longitude = null;
|
||||
$latitude = null;
|
||||
$zoomLevel = null;
|
||||
$location = $this->getLocation($journal);
|
||||
if (null !== $location) {
|
||||
$longitude = $location->longitude;
|
||||
$latitude = $location->latitude;
|
||||
$zoomLevel = $location->zoom_level;
|
||||
}
|
||||
|
||||
return [
|
||||
'user' => (int)$journal->user_id,
|
||||
'transaction_journal_id' => (int)$journal->id,
|
||||
'type' => strtolower($type),
|
||||
'date' => $journal->date->toAtomString(),
|
||||
'order' => $journal->order,
|
||||
|
||||
'currency_id' => (int)$currency->id,
|
||||
'currency_code' => $currency->code,
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'currency_decimal_places' => (int)$currency->decimal_places,
|
||||
|
||||
'foreign_currency_id' => $foreignCurrency['id'],
|
||||
'foreign_currency_code' => $foreignCurrency['code'],
|
||||
'foreign_currency_symbol' => $foreignCurrency['symbol'],
|
||||
'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'],
|
||||
|
||||
'amount' => app('steam')->bcround($amount, $currency->decimal_places),
|
||||
'foreign_amount' => $foreignAmount,
|
||||
|
||||
'description' => $journal->description,
|
||||
|
||||
'source_id' => (int)$source->account_id,
|
||||
'source_name' => $source->account->name,
|
||||
'source_iban' => $source->account->iban,
|
||||
'source_type' => $source->account->accountType->type,
|
||||
|
||||
'destination_id' => (int)$destination->account_id,
|
||||
'destination_name' => $destination->account->name,
|
||||
'destination_iban' => $destination->account->iban,
|
||||
'destination_type' => $destination->account->accountType->type,
|
||||
|
||||
'budget_id' => $budget['id'],
|
||||
'budget_name' => $budget['name'],
|
||||
|
||||
'category_id' => $category['id'],
|
||||
'category_name' => $category['name'],
|
||||
|
||||
'bill_id' => $bill['id'],
|
||||
'bill_name' => $bill['name'],
|
||||
|
||||
'reconciled' => $source->reconciled,
|
||||
'notes' => $this->groupRepos->getNoteText($journal->id),
|
||||
'tags' => $this->groupRepos->getTags($journal->id),
|
||||
|
||||
'internal_reference' => $metaFieldData['internal_reference'],
|
||||
'external_id' => $metaFieldData['external_id'],
|
||||
'original_source' => $metaFieldData['original_source'],
|
||||
'recurrence_id' => $metaFieldData['recurrence_id'],
|
||||
'bunq_payment_id' => $metaFieldData['bunq_payment_id'],
|
||||
'import_hash_v2' => $metaFieldData['import_hash_v2'],
|
||||
|
||||
'sepa_cc' => $metaFieldData['sepa_cc'],
|
||||
'sepa_ct_op' => $metaFieldData['sepa_ct_op'],
|
||||
'sepa_ct_id' => $metaFieldData['sepa_ct_id'],
|
||||
'sepa_db' => $metaFieldData['sepa_db'],
|
||||
'sepa_country' => $metaFieldData['sepa_country'],
|
||||
'sepa_ep' => $metaFieldData['sepa_ep'],
|
||||
'sepa_ci' => $metaFieldData['sepa_ci'],
|
||||
'sepa_batch_id' => $metaFieldData['sepa_batch_id'],
|
||||
|
||||
'interest_date' => $metaDates['interest_date'],
|
||||
'book_date' => $metaDates['book_date'],
|
||||
'process_date' => $metaDates['process_date'],
|
||||
'due_date' => $metaDates['due_date'],
|
||||
'payment_date' => $metaDates['payment_date'],
|
||||
'invoice_date' => $metaDates['invoice_date'],
|
||||
|
||||
// location data
|
||||
'longitude' => $longitude,
|
||||
'latitude' => $latitude,
|
||||
'zoom_level' => $zoomLevel,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $transactionJournals
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function transformJournals(Collection $transactionJournals): array
|
||||
{
|
||||
$result = [];
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($transactionJournals as $journal) {
|
||||
$result[] = $this->transformJournal($journal);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $transaction
|
||||
* @param array $transaction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -643,18 +232,429 @@ class TransactionGroupTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $data
|
||||
* @param array $array
|
||||
* @param string $key
|
||||
* @param string|null $default
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function stringFromArray(array $array, string $key, ?string $default): ?string
|
||||
{
|
||||
if (array_key_exists($key, $array) && null === $array[$key]) {
|
||||
return null;
|
||||
}
|
||||
if (array_key_exists($key, $array) && null !== $array[$key]) {
|
||||
if (0 === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
if ('0' === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
return (string)$array[$key];
|
||||
}
|
||||
|
||||
if (null !== $default) {
|
||||
return (string)$default;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return Location|null
|
||||
*/
|
||||
private function getLocationById(int $journalId): ?Location
|
||||
{
|
||||
return $this->groupRepos->getLocation($journalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Location|null
|
||||
*/
|
||||
private function getLocation(TransactionJournal $journal): ?Location
|
||||
{
|
||||
return $journal->locations()->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param string $key
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
private function integerFromArray(array $array, string $key): ?int
|
||||
{
|
||||
if (array_key_exists($key, $array)) {
|
||||
return (int)$array[$key];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $object
|
||||
* @param string $key
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function dateFromArray(NullArrayObject $object, string $key): ?string
|
||||
{
|
||||
if (null === $object[$key]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $object[$key]->toAtomString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasAttachments(int $journalId): bool
|
||||
{
|
||||
return $this->groupRepos->countAttachments($journalId) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionGroup $group
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function transformTransactions(NullArrayObject $data): array
|
||||
public function transformObject(TransactionGroup $group): array
|
||||
{
|
||||
$result = [];
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$result[] = $this->transformTransaction($transaction);
|
||||
try {
|
||||
$result = [
|
||||
'id' => (int)$group->id,
|
||||
'created_at' => $group->created_at->toAtomString(),
|
||||
'updated_at' => $group->updated_at->toAtomString(),
|
||||
'user' => (int)$group->user_id,
|
||||
'group_title' => $group->title,
|
||||
'transactions' => $this->transformJournals($group->transactionJournals),
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transactions/' . $group->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException(sprintf('Transaction group #%d is broken. Please check out your log files.', $group->id), 0, $e);
|
||||
}
|
||||
|
||||
// do something else.
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $transactionJournals
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function transformJournals(Collection $transactionJournals): array
|
||||
{
|
||||
$result = [];
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($transactionJournals as $journal) {
|
||||
$result[] = $this->transformJournal($journal);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function transformJournal(TransactionJournal $journal): array
|
||||
{
|
||||
$source = $this->getSourceTransaction($journal);
|
||||
$destination = $this->getDestinationTransaction($journal);
|
||||
$type = $journal->transactionType->type;
|
||||
$currency = $source->transactionCurrency;
|
||||
$amount = app('steam')->bcround($this->getAmount($type, (string)$source->amount), $currency->decimal_places ?? 0);
|
||||
$foreignAmount = $this->getForeignAmount($type, null === $source->foreign_amount ? null : (string)$source->foreign_amount);
|
||||
$metaFieldData = $this->groupRepos->getMetaFields($journal->id, $this->metaFields);
|
||||
$metaDates = $this->getDates($this->groupRepos->getMetaDateFields($journal->id, $this->metaDateFields));
|
||||
$foreignCurrency = $this->getForeignCurrency($source->foreignCurrency);
|
||||
$budget = $this->getBudget($journal->budgets->first());
|
||||
$category = $this->getCategory($journal->categories->first());
|
||||
$bill = $this->getBill($journal->bill);
|
||||
|
||||
if (null !== $foreignAmount && null !== $source->foreignCurrency) {
|
||||
$foreignAmount = app('steam')->bcround($foreignAmount, $foreignCurrency['decimal_places'] ?? 0);
|
||||
}
|
||||
|
||||
$longitude = null;
|
||||
$latitude = null;
|
||||
$zoomLevel = null;
|
||||
$location = $this->getLocation($journal);
|
||||
if (null !== $location) {
|
||||
$longitude = $location->longitude;
|
||||
$latitude = $location->latitude;
|
||||
$zoomLevel = $location->zoom_level;
|
||||
}
|
||||
|
||||
return [
|
||||
'user' => (int)$journal->user_id,
|
||||
'transaction_journal_id' => (int)$journal->id,
|
||||
'type' => strtolower($type),
|
||||
'date' => $journal->date->toAtomString(),
|
||||
'order' => $journal->order,
|
||||
|
||||
'currency_id' => (int)$currency->id,
|
||||
'currency_code' => $currency->code,
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'currency_decimal_places' => (int)$currency->decimal_places,
|
||||
|
||||
'foreign_currency_id' => $foreignCurrency['id'],
|
||||
'foreign_currency_code' => $foreignCurrency['code'],
|
||||
'foreign_currency_symbol' => $foreignCurrency['symbol'],
|
||||
'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'],
|
||||
|
||||
'amount' => app('steam')->bcround($amount, $currency->decimal_places),
|
||||
'foreign_amount' => $foreignAmount,
|
||||
|
||||
'description' => $journal->description,
|
||||
|
||||
'source_id' => (int)$source->account_id,
|
||||
'source_name' => $source->account->name,
|
||||
'source_iban' => $source->account->iban,
|
||||
'source_type' => $source->account->accountType->type,
|
||||
|
||||
'destination_id' => (int)$destination->account_id,
|
||||
'destination_name' => $destination->account->name,
|
||||
'destination_iban' => $destination->account->iban,
|
||||
'destination_type' => $destination->account->accountType->type,
|
||||
|
||||
'budget_id' => $budget['id'],
|
||||
'budget_name' => $budget['name'],
|
||||
|
||||
'category_id' => $category['id'],
|
||||
'category_name' => $category['name'],
|
||||
|
||||
'bill_id' => $bill['id'],
|
||||
'bill_name' => $bill['name'],
|
||||
|
||||
'reconciled' => $source->reconciled,
|
||||
'notes' => $this->groupRepos->getNoteText($journal->id),
|
||||
'tags' => $this->groupRepos->getTags($journal->id),
|
||||
|
||||
'internal_reference' => $metaFieldData['internal_reference'],
|
||||
'external_id' => $metaFieldData['external_id'],
|
||||
'original_source' => $metaFieldData['original_source'],
|
||||
'recurrence_id' => $metaFieldData['recurrence_id'],
|
||||
'bunq_payment_id' => $metaFieldData['bunq_payment_id'],
|
||||
'import_hash_v2' => $metaFieldData['import_hash_v2'],
|
||||
|
||||
'sepa_cc' => $metaFieldData['sepa_cc'],
|
||||
'sepa_ct_op' => $metaFieldData['sepa_ct_op'],
|
||||
'sepa_ct_id' => $metaFieldData['sepa_ct_id'],
|
||||
'sepa_db' => $metaFieldData['sepa_db'],
|
||||
'sepa_country' => $metaFieldData['sepa_country'],
|
||||
'sepa_ep' => $metaFieldData['sepa_ep'],
|
||||
'sepa_ci' => $metaFieldData['sepa_ci'],
|
||||
'sepa_batch_id' => $metaFieldData['sepa_batch_id'],
|
||||
|
||||
'interest_date' => $metaDates['interest_date'],
|
||||
'book_date' => $metaDates['book_date'],
|
||||
'process_date' => $metaDates['process_date'],
|
||||
'due_date' => $metaDates['due_date'],
|
||||
'payment_date' => $metaDates['payment_date'],
|
||||
'invoice_date' => $metaDates['invoice_date'],
|
||||
|
||||
// location data
|
||||
'longitude' => $longitude,
|
||||
'latitude' => $latitude,
|
||||
'zoom_level' => $zoomLevel,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceTransaction(TransactionJournal $journal): Transaction
|
||||
{
|
||||
$result = $journal->transactions->first(
|
||||
static function (Transaction $transaction) {
|
||||
return (float)$transaction->amount < 0; // lame but it works.
|
||||
}
|
||||
);
|
||||
if (null === $result) {
|
||||
throw new FireflyException(sprintf('Journal #%d unexpectedly has no source transaction.', $journal->id));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getDestinationTransaction(TransactionJournal $journal): Transaction
|
||||
{
|
||||
$result = $journal->transactions->first(
|
||||
static function (Transaction $transaction) {
|
||||
return (float)$transaction->amount > 0; // lame but it works
|
||||
}
|
||||
);
|
||||
if (null === $result) {
|
||||
throw new FireflyException(sprintf('Journal #%d unexpectedly has no destination transaction.', $journal->id));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getAmount(string $type, string $amount): string
|
||||
{
|
||||
return app('steam')->positive($amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string|null $foreignAmount
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getForeignAmount(string $type, ?string $foreignAmount): ?string
|
||||
{
|
||||
$result = null;
|
||||
if (null !== $foreignAmount && '' !== $foreignAmount && bccomp('0', $foreignAmount) !== 0) {
|
||||
$result = app('steam')->positive($foreignAmount);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $dates
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getDates(NullArrayObject $dates): array
|
||||
{
|
||||
$fields = [
|
||||
'interest_date',
|
||||
'book_date',
|
||||
'process_date',
|
||||
'due_date',
|
||||
'payment_date',
|
||||
'invoice_date',
|
||||
];
|
||||
$return = [];
|
||||
foreach ($fields as $field) {
|
||||
$return[$field] = null;
|
||||
if (null !== $dates[$field]) {
|
||||
$return[$field] = $dates[$field]->toAtomString();
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency|null $currency
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getForeignCurrency(?TransactionCurrency $currency): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'code' => null,
|
||||
'symbol' => null,
|
||||
'decimal_places' => null,
|
||||
];
|
||||
if (null === $currency) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (int)$currency->id;
|
||||
$array['code'] = $currency->code;
|
||||
$array['symbol'] = $currency->symbol;
|
||||
$array['decimal_places'] = (int)$currency->decimal_places;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget|null $budget
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getBudget(?Budget $budget): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'name' => null,
|
||||
];
|
||||
if (null === $budget) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (int)$budget->id;
|
||||
$array['name'] = $budget->name;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Category|null $category
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getCategory(?Category $category): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'name' => null,
|
||||
];
|
||||
if (null === $category) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (int)$category->id;
|
||||
$array['name'] = $category->name;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill|null $bill
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getBill(?Bill $bill): array
|
||||
{
|
||||
$array = [
|
||||
'id' => null,
|
||||
'name' => null,
|
||||
];
|
||||
if (null === $bill) {
|
||||
return $array;
|
||||
}
|
||||
$array['id'] = (string)$bill->id;
|
||||
$array['name'] = $bill->name;
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class TransactionLinkTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournalLink $link
|
||||
* @param TransactionJournalLink $link
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -65,7 +65,7 @@ class TransactionLinkTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transaction_links/'.$link->id,
|
||||
'uri' => '/transaction_links/' . $link->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -38,7 +38,7 @@ class UserTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform user.
|
||||
*
|
||||
* @param User $user
|
||||
* @param User $user
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
@@ -59,7 +59,7 @@ class UserTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/users/'.$user->id,
|
||||
'uri' => '/users/' . $user->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -36,7 +36,7 @@ abstract class AbstractTransformer extends TransformerAbstract
|
||||
protected ParameterBag $parameters;
|
||||
|
||||
/**
|
||||
* @param Collection $objects
|
||||
* @param Collection $objects
|
||||
* @return void
|
||||
*/
|
||||
abstract public function collectMetaData(Collection $objects): void;
|
||||
@@ -51,7 +51,7 @@ abstract class AbstractTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ParameterBag $parameters
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
final public function setParameters(ParameterBag $parameters): void
|
||||
{
|
||||
|
||||
@@ -73,10 +73,23 @@ class AccountTransformer extends AbstractTransformer
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getDate(): Carbon
|
||||
{
|
||||
$date = today(config('app.timezone'));
|
||||
if (null !== $this->parameters->get('date')) {
|
||||
$date = $this->parameters->get('date');
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the account.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param Account $account
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -128,22 +141,9 @@ class AccountTransformer extends AbstractTransformer
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/accounts/'.$account->id,
|
||||
'uri' => '/accounts/' . $account->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Carbon
|
||||
*/
|
||||
private function getDate(): Carbon
|
||||
{
|
||||
$date = today(config('app.timezone'));
|
||||
if (null !== $this->parameters->get('date')) {
|
||||
$date = $this->parameters->get('date');
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Include Budget
|
||||
*
|
||||
* @param BudgetLimit $limit
|
||||
* @param BudgetLimit $limit
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
@@ -61,7 +61,7 @@ class BudgetLimitTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the note.
|
||||
*
|
||||
* @param BudgetLimit $budgetLimit
|
||||
* @param BudgetLimit $budgetLimit
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -58,7 +58,7 @@ class BudgetTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform a budget.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -117,7 +117,7 @@ class BudgetTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -42,7 +42,7 @@ class PreferenceTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the preference
|
||||
*
|
||||
* @param Preference $preference
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -80,7 +80,7 @@ class TransactionGroupTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $group
|
||||
* @param array $group
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -104,52 +104,17 @@ class TransactionGroupTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $string
|
||||
* @return Carbon|null
|
||||
* @param array $transactions
|
||||
* @return array
|
||||
*/
|
||||
private function date(?string $string): ?Carbon
|
||||
private function transformTransactions(array $transactions): array
|
||||
{
|
||||
if (null === $string) {
|
||||
return null;
|
||||
$return = [];
|
||||
/** @var array $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$return[] = $this->transformTransaction($transaction);
|
||||
}
|
||||
Log::debug(sprintf('Now in date("%s")', $string));
|
||||
if (10 === strlen($string)) {
|
||||
return Carbon::createFromFormat('Y-m-d', $string, config('app.timezone'));
|
||||
}
|
||||
// 2022-01-01 01:01:01
|
||||
return Carbon::createFromFormat('Y-m-d H:i:s', substr($string, 0, 19), config('app.timezone'));
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO also in the old transformer.
|
||||
*
|
||||
* @param NullArrayObject $array
|
||||
* @param string $key
|
||||
* @param string|null $default
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function stringFromArray(NullArrayObject $array, string $key, ?string $default): ?string
|
||||
{
|
||||
//Log::debug(sprintf('%s: %s', $key, var_export($array[$key], true)));
|
||||
if (null === $array[$key] && null === $default) {
|
||||
return null;
|
||||
}
|
||||
if (0 === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
if ('0' === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
if (null !== $array[$key]) {
|
||||
return (string)$array[$key];
|
||||
}
|
||||
|
||||
if (null !== $default) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return null;
|
||||
return $return;
|
||||
}
|
||||
|
||||
private function transformTransaction(array $transaction): array
|
||||
@@ -264,16 +229,51 @@ class TransactionGroupTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $transactions
|
||||
* @return array
|
||||
* TODO also in the old transformer.
|
||||
*
|
||||
* @param NullArrayObject $array
|
||||
* @param string $key
|
||||
* @param string|null $default
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function transformTransactions(array $transactions): array
|
||||
private function stringFromArray(NullArrayObject $array, string $key, ?string $default): ?string
|
||||
{
|
||||
$return = [];
|
||||
/** @var array $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$return[] = $this->transformTransaction($transaction);
|
||||
//Log::debug(sprintf('%s: %s', $key, var_export($array[$key], true)));
|
||||
if (null === $array[$key] && null === $default) {
|
||||
return null;
|
||||
}
|
||||
return $return;
|
||||
if (0 === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
if ('0' === $array[$key]) {
|
||||
return $default;
|
||||
}
|
||||
if (null !== $array[$key]) {
|
||||
return (string)$array[$key];
|
||||
}
|
||||
|
||||
if (null !== $default) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $string
|
||||
* @return Carbon|null
|
||||
*/
|
||||
private function date(?string $string): ?Carbon
|
||||
{
|
||||
if (null === $string) {
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('Now in date("%s")', $string));
|
||||
if (10 === strlen($string)) {
|
||||
return Carbon::createFromFormat('Y-m-d', $string, config('app.timezone'));
|
||||
}
|
||||
// 2022-01-01 01:01:01
|
||||
return Carbon::createFromFormat('Y-m-d H:i:s', substr($string, 0, 19), config('app.timezone'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class WebhookAttemptTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the preference
|
||||
*
|
||||
* @param WebhookAttempt $attempt
|
||||
* @param WebhookAttempt $attempt
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -35,7 +35,7 @@ class WebhookMessageTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform the preference
|
||||
*
|
||||
* @param WebhookMessage $message
|
||||
* @param WebhookMessage $message
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -46,7 +46,7 @@ class WebhookTransformer extends AbstractTransformer
|
||||
/**
|
||||
* Transform webhook.
|
||||
*
|
||||
* @param Webhook $webhook
|
||||
* @param Webhook $webhook
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -73,8 +73,8 @@ class WebhookTransformer extends AbstractTransformer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param int $value
|
||||
* @param string $type
|
||||
* @param int $value
|
||||
* @return string
|
||||
*/
|
||||
private function getEnum(string $type, int $value): string
|
||||
|
||||
Reference in New Issue
Block a user