Fix various code

This commit is contained in:
James Cole 2023-12-21 06:06:23 +01:00
parent 337d14e71e
commit 8a4985261a
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
10 changed files with 111 additions and 125 deletions

View File

@ -41,6 +41,8 @@ class CreateDatabase extends Command
/**
* Execute the console command.
*
* @suppressWarnings(PHPMD.MissingImport)
*/
public function handle(): int
{

View File

@ -34,8 +34,6 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* This command will take split transactions and migrate them to "transaction groups".
@ -59,9 +57,6 @@ class MigrateToGroups extends Command
/**
* Execute the console command.
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): int
{
@ -105,10 +100,6 @@ class MigrateToGroups extends Command
$this->cliRepository = app(JournalCLIRepositoryInterface::class);
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function isMigrated(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
@ -142,7 +133,7 @@ class MigrateToGroups extends Command
{
// double check transaction count.
if ($journal->transactions->count() <= 2) {
app('log')->debug(sprintf('Will not try to convert journal #%d because it has 2 or less transactions.', $journal->id));
app('log')->debug(sprintf('Will not try to convert journal #%d because it has 2 or fewer transactions.', $journal->id));
return;
}
@ -158,95 +149,12 @@ class MigrateToGroups extends Command
'transactions' => [],
];
$destTransactions = $this->getDestinationTransactions($journal);
$budgetId = $this->cliRepository->getJournalBudgetId($journal);
$categoryId = $this->cliRepository->getJournalCategoryId($journal);
$notes = $this->cliRepository->getNoteText($journal);
$tags = $this->cliRepository->getTags($journal);
$internalRef = $this->cliRepository->getMetaField($journal, 'internal-reference');
$sepaCC = $this->cliRepository->getMetaField($journal, 'sepa_cc');
$sepaCtOp = $this->cliRepository->getMetaField($journal, 'sepa_ct_op');
$sepaCtId = $this->cliRepository->getMetaField($journal, 'sepa_ct_id');
$sepaDb = $this->cliRepository->getMetaField($journal, 'sepa_db');
$sepaCountry = $this->cliRepository->getMetaField($journal, 'sepa_country');
$sepaEp = $this->cliRepository->getMetaField($journal, 'sepa_ep');
$sepaCi = $this->cliRepository->getMetaField($journal, 'sepa_ci');
$sepaBatchId = $this->cliRepository->getMetaField($journal, 'sepa_batch_id');
$externalId = $this->cliRepository->getMetaField($journal, 'external-id');
$originalSource = $this->cliRepository->getMetaField($journal, 'original-source');
$recurrenceId = $this->cliRepository->getMetaField($journal, 'recurrence_id');
$bunq = $this->cliRepository->getMetaField($journal, 'bunq_payment_id');
$hash = $this->cliRepository->getMetaField($journal, 'import_hash');
$hashTwo = $this->cliRepository->getMetaField($journal, 'import_hash_v2');
$interestDate = $this->cliRepository->getMetaDate($journal, 'interest_date');
$bookDate = $this->cliRepository->getMetaDate($journal, 'book_date');
$processDate = $this->cliRepository->getMetaDate($journal, 'process_date');
$dueDate = $this->cliRepository->getMetaDate($journal, 'due_date');
$paymentDate = $this->cliRepository->getMetaDate($journal, 'payment_date');
$invoiceDate = $this->cliRepository->getMetaDate($journal, 'invoice_date');
app('log')->debug(sprintf('Will use %d positive transactions to create a new group.', $destTransactions->count()));
/** @var Transaction $transaction */
foreach ($destTransactions as $transaction) {
app('log')->debug(sprintf('Now going to add transaction #%d to the array.', $transaction->id));
$opposingTr = $this->findOpposingTransaction($journal, $transaction);
if (null === $opposingTr) {
$this->friendlyError(
sprintf(
'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.',
$journal->id,
$transaction->id
)
);
continue;
}
// overrule journal category with transaction category.
$budgetId = $this->getTransactionBudget($transaction, $opposingTr) ?? $budgetId;
$categoryId = $this->getTransactionCategory($transaction, $opposingTr) ?? $categoryId;
$tArray = [
'type' => strtolower($journal->transactionType->type),
'date' => $journal->date,
'user' => $journal->user_id,
'currency_id' => $transaction->transaction_currency_id,
'foreign_currency_id' => $transaction->foreign_currency_id,
'amount' => $transaction->amount,
'foreign_amount' => $transaction->foreign_amount,
'description' => $transaction->description ?? $journal->description,
'source_id' => $opposingTr->account_id,
'destination_id' => $transaction->account_id,
'budget_id' => $budgetId,
'category_id' => $categoryId,
'bill_id' => $journal->bill_id,
'notes' => $notes,
'tags' => $tags,
'internal_reference' => $internalRef,
'sepa_cc' => $sepaCC,
'sepa_ct_op' => $sepaCtOp,
'sepa_ct_id' => $sepaCtId,
'sepa_db' => $sepaDb,
'sepa_country' => $sepaCountry,
'sepa_ep' => $sepaEp,
'sepa_ci' => $sepaCi,
'sepa_batch_id' => $sepaBatchId,
'external_id' => $externalId,
'original-source' => $originalSource,
'recurrence_id' => $recurrenceId,
'bunq_payment_id' => $bunq,
'import_hash' => $hash,
'import_hash_v2' => $hashTwo,
'interest_date' => $interestDate,
'book_date' => $bookDate,
'process_date' => $processDate,
'due_date' => $dueDate,
'payment_date' => $paymentDate,
'invoice_date' => $invoiceDate,
];
$data['transactions'][] = $tArray;
$data['transactions'][] = $this->generateTransaction($journal, $transaction);
}
app('log')->debug(sprintf('Now calling transaction journal factory (%d transactions in array)', count($data['transactions'])));
$group = $this->groupFactory->create($data);
@ -301,6 +209,93 @@ class MigrateToGroups extends Command
return $set->first();
}
private function generateTransaction(TransactionJournal $journal, Transaction $transaction): array
{
app('log')->debug(sprintf('Now going to add transaction #%d to the array.', $transaction->id));
$opposingTr = $this->findOpposingTransaction($journal, $transaction);
if (null === $opposingTr) {
$this->friendlyError(
sprintf(
'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.',
$journal->id,
$transaction->id
)
);
return [];
}
$budgetId = $this->cliRepository->getJournalBudgetId($journal);
$categoryId = $this->cliRepository->getJournalCategoryId($journal);
$notes = $this->cliRepository->getNoteText($journal);
$tags = $this->cliRepository->getTags($journal);
$internalRef = $this->cliRepository->getMetaField($journal, 'internal-reference');
$sepaCC = $this->cliRepository->getMetaField($journal, 'sepa_cc');
$sepaCtOp = $this->cliRepository->getMetaField($journal, 'sepa_ct_op');
$sepaCtId = $this->cliRepository->getMetaField($journal, 'sepa_ct_id');
$sepaDb = $this->cliRepository->getMetaField($journal, 'sepa_db');
$sepaCountry = $this->cliRepository->getMetaField($journal, 'sepa_country');
$sepaEp = $this->cliRepository->getMetaField($journal, 'sepa_ep');
$sepaCi = $this->cliRepository->getMetaField($journal, 'sepa_ci');
$sepaBatchId = $this->cliRepository->getMetaField($journal, 'sepa_batch_id');
$externalId = $this->cliRepository->getMetaField($journal, 'external-id');
$originalSource = $this->cliRepository->getMetaField($journal, 'original-source');
$recurrenceId = $this->cliRepository->getMetaField($journal, 'recurrence_id');
$bunq = $this->cliRepository->getMetaField($journal, 'bunq_payment_id');
$hash = $this->cliRepository->getMetaField($journal, 'import_hash');
$hashTwo = $this->cliRepository->getMetaField($journal, 'import_hash_v2');
$interestDate = $this->cliRepository->getMetaDate($journal, 'interest_date');
$bookDate = $this->cliRepository->getMetaDate($journal, 'book_date');
$processDate = $this->cliRepository->getMetaDate($journal, 'process_date');
$dueDate = $this->cliRepository->getMetaDate($journal, 'due_date');
$paymentDate = $this->cliRepository->getMetaDate($journal, 'payment_date');
$invoiceDate = $this->cliRepository->getMetaDate($journal, 'invoice_date');
// overrule journal category with transaction category.
$budgetId = $this->getTransactionBudget($transaction, $opposingTr) ?? $budgetId;
$categoryId = $this->getTransactionCategory($transaction, $opposingTr) ?? $categoryId;
return [
'type' => strtolower($journal->transactionType->type),
'date' => $journal->date,
'user' => $journal->user_id,
'currency_id' => $transaction->transaction_currency_id,
'foreign_currency_id' => $transaction->foreign_currency_id,
'amount' => $transaction->amount,
'foreign_amount' => $transaction->foreign_amount,
'description' => $transaction->description ?? $journal->description,
'source_id' => $opposingTr->account_id,
'destination_id' => $transaction->account_id,
'budget_id' => $budgetId,
'category_id' => $categoryId,
'bill_id' => $journal->bill_id,
'notes' => $notes,
'tags' => $tags,
'internal_reference' => $internalRef,
'sepa_cc' => $sepaCC,
'sepa_ct_op' => $sepaCtOp,
'sepa_ct_id' => $sepaCtId,
'sepa_db' => $sepaDb,
'sepa_country' => $sepaCountry,
'sepa_ep' => $sepaEp,
'sepa_ci' => $sepaCi,
'sepa_batch_id' => $sepaBatchId,
'external_id' => $externalId,
'original-source' => $originalSource,
'recurrence_id' => $recurrenceId,
'bunq_payment_id' => $bunq,
'import_hash' => $hash,
'import_hash_v2' => $hashTwo,
'interest_date' => $interestDate,
'book_date' => $bookDate,
'process_date' => $processDate,
'due_date' => $dueDate,
'payment_date' => $paymentDate,
'invoice_date' => $invoiceDate,
];
}
private function getTransactionBudget(Transaction $left, Transaction $right): ?int
{
app('log')->debug('Now in getTransactionBudget()');

View File

@ -92,6 +92,8 @@ class FireflyServiceProvider extends ServiceProvider
/**
* Register stuff.
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function register(): void
{

View File

@ -34,6 +34,9 @@ class IsDuplicateTransaction implements ValidationRule
{
private string $value;
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$this->value = $value;

View File

@ -50,6 +50,8 @@ class Amount
* as a currency, given two things: the currency required and the current locale.
*
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.MissingImport)
*/
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string
{
@ -61,7 +63,7 @@ class Amount
$fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
$result = (string)$fmt->format((float)$rounded); // intentional float
$result = (string) $fmt->format((float) $rounded); // intentional float
if (true === $coloured) {
if (1 === bccomp($rounded, '0')) {
@ -234,6 +236,8 @@ class Amount
/**
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.MissingImport)
*/
private function getLocaleInfo(): array
{

View File

@ -34,7 +34,7 @@ class Calculator
{
public const int DEFAULT_INTERVAL = 1;
private static ?\SplObjectStorage $intervalMap = null;
private static array $intervals = [];
private static array $intervals = [];
/**
* @throws IntervalException
@ -62,6 +62,9 @@ class Calculator
return self::loadIntervalMap()->contains($periodicity);
}
/**
* @SuppressWarnings(PHPMD.MissingImport)
*/
private static function loadIntervalMap(): \SplObjectStorage
{
if (null !== self::$intervalMap) {

View File

@ -179,35 +179,7 @@ class FireflyValidator extends Validator
// replace characters outside of ASCI range.
$value = (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
$replace = [
'',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23',
'24',
'25',
'26',
'27',
'28',
'29',
'30',
'31',
'32',
'33',
'34',
'35',
];
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'];
// take
$first = substr($value, 0, 4);

View File

@ -522,6 +522,9 @@ class CreateMainTables extends Migration
}
}
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function createTransactionTables(): void
{
if (!Schema::hasTable('transaction_journals')) {

View File

@ -49,6 +49,7 @@ class ChangesForV475 extends Migration
* Run the migrations.
*
* @SuppressWarnings(PHPMD.ShortMethodName)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function up(): void
{

View File

@ -124,6 +124,7 @@ class ChangesForV550 extends Migration
* Run the migrations.
*
* @SuppressWarnings(PHPMD.ShortMethodName)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function up(): void
{