Add some routes for transfers.

This commit is contained in:
James Cole
2021-03-07 15:26:42 +01:00
parent 414c99489c
commit 91394553c3
15 changed files with 682 additions and 157 deletions

View File

@@ -311,4 +311,70 @@ class OperationsRepository implements OperationsRepositoryInterface
}
return $array;
}
/**
* @inheritDoc
*/
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null, ?TransactionCurrency $currency = null): array
{
$start->startOfDay();
$end->endOfDay();
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::TRANSFER]);
if (null !== $accounts) {
$collector->setAccounts($accounts);
}
if (null !== $currency) {
$collector->setCurrency($currency);
}
$journals = $collector->getExtractedJournals();
// same but for foreign currencies:
if (null !== $currency) {
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])
->setForeignCurrency($currency);
if (null !== $accounts) {
$collector->setAccounts($accounts);
}
$result = $collector->getExtractedJournals();
// do not use array_merge because you want keys to overwrite (otherwise you get double results):
$journals = $result + $journals;
}
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
// also do foreign amount:
$foreignId = (int)$journal['foreign_currency_id'];
if (0 !== $foreignId) {
$array[$foreignId] = $array[$foreignId] ?? [
'sum' => '0',
'currency_id' => $foreignId,
'currency_name' => $journal['foreign_currency_name'],
'currency_symbol' => $journal['foreign_currency_symbol'],
'currency_code' => $journal['foreign_currency_code'],
'currency_decimal_places' => $journal['foreign_currency_decimal_places'],
];
$array[$foreignId]['sum'] = bcadd($array[$foreignId]['sum'], app('steam')->positive($journal['foreign_amount']));
}
}
return $array;
}
}

View File

@@ -89,4 +89,16 @@ interface OperationsRepositoryInterface
* @return array
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $revenue = null, ?TransactionCurrency $currency = null): array;
/**
* Sum of transfers in period for a set of accounts, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param TransactionCurrency|null $currency
*
* @return array
*/
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null, ?TransactionCurrency $currency = null): array;
}

View File

@@ -29,7 +29,6 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
*
@@ -37,8 +36,7 @@ use Log;
*/
class NoCategoryRepository implements NoCategoryRepositoryInterface
{
/** @var User */
private $user;
private User $user;
/**
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
@@ -229,4 +227,35 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
return $array;
}
/**
* @inheritDoc
*/
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionType::TRANSFER])->withoutCategory();
if (null !== $accounts && $accounts->count() > 0) {
$collector->setAccounts($accounts);
}
$journals = $collector->getExtractedJournals();
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
}
return $array;
}
}

View File

@@ -87,5 +87,16 @@ interface NoCategoryRepositoryInterface
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
/**
* Sum of transfers in period without a category, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
*
* @return array
*/
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null): array;
}

View File

@@ -294,6 +294,49 @@ class OperationsRepository implements OperationsRepositoryInterface
return $array;
}
/**
* Sum of income journals in period for a set of categories, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $categories = null): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user)->setRange($start, $end)
->setTypes([TransactionType::TRANSFER]);
if (null !== $accounts && $accounts->count() > 0) {
$collector->setAccounts($accounts);
}
if (null === $categories || (null !== $categories && 0 === $categories->count())) {
$categories = $this->getCategories();
}
$collector->setCategories($categories);
$journals = $collector->getExtractedJournals();
$array = [];
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$array[$currencyId] = $array[$currencyId] ?? [
'sum' => '0',
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($journal['amount']));
}
return $array;
}
/**
* Returns a list of all the categories belonging to a user.
*

View File

@@ -89,4 +89,16 @@ interface OperationsRepositoryInterface
* @return array
*/
public function sumIncome(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $categories = null): array;
/**
* Sum of transfers in period for a set of categories, grouped per currency. Amounts are always positive.
*
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $categories
*
* @return array
*/
public function sumTransfers(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $categories = null): array;
}

View File

@@ -104,7 +104,7 @@ class WebhookRepository implements WebhookRepositoryInterface
$webhook->delivery = $data['delivery'] ?? $webhook->delivery;
$webhook->url = $data['url'] ?? $webhook->url;
if(true === $data['secret']) {
if (true === $data['secret']) {
$secret = $random = Str::random(24);
$webhook->secret = $secret;
}
@@ -144,21 +144,23 @@ class WebhookRepository implements WebhookRepositoryInterface
public function getReadyMessages(Webhook $webhook): Collection
{
return $webhook->webhookMessages()
->where('webhook_messages.sent', 0)
->where('webhook_messages.errored', 0)
->get(['webhook_messages.*'])
->filter(
function (WebhookMessage $message) {
return $message->webhookAttempts()->count() <= 2;
}
)->splice(0, 3);
->where('webhook_messages.sent', 0)
->where('webhook_messages.errored', 0)
->get(['webhook_messages.*'])
->filter(
function (WebhookMessage $message) {
return $message->webhookAttempts()->count() <= 2;
}
)->splice(0, 3);
}
/**
* @inheritDoc
*/
public function getMessages(Webhook $webhook): Collection
{
return $webhook->webhookMessages()
->orderBy('created_at', 'DESC')
->get(['webhook_messages.*']);
}
@@ -167,6 +169,6 @@ class WebhookRepository implements WebhookRepositoryInterface
*/
public function getAttempts(WebhookMessage $webhookMessage): Collection
{
return $webhookMessage->webhookAttempts;
return $webhookMessage->webhookAttempts()->orderBy('created_at', 'DESC')->get(['webhook_attempts.*']);
}
}