Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CRM/Admin/Form/Setting/BankingSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
class CRM_Admin_Form_Setting_BankingSettings extends CRM_Core_Form {

public function buildQuickForm() {
$importerOptions = [
'standard' => E::ts('Standard'),
'quick' => E::ts('Quick'),
// 'both' => E::ts('Both'),

Check warning on line 32 in CRM/Admin/Form/Setting/BankingSettings.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

This comment is 64% valid code; is this commented out code?
];
$this->add(
'select',
'banking_importer',
E::ts('Importer'),
$importerOptions,
// is not required
FALSE
);

// add new UI (#200) options
$ui_options = [
'1' => E::ts('Simplified user interface'),
Expand Down Expand Up @@ -181,6 +195,7 @@
*/
public function setDefaultValues() {
$defaults = [];
$defaults['banking_importer'] = Civi::settings()->get('banking_importer');
$defaults['new_ui'] = Civi::settings()->get('new_ui');
$defaults['menu_position'] = Civi::settings()->get('menu_position');
$defaults['json_editor_mode'] = Civi::settings()->get('json_editor_mode');
Expand Down Expand Up @@ -250,6 +265,8 @@
$logger = CRM_Banking_Helpers_Logger::getLogger();
$logger->logDebug("Log level changed to '{$values['banking_log_level']}', file is: {$values['banking_log_file']}");

Civi::settings()->set('banking_importer', $values['banking_importer']);

parent::postProcess();
}

Expand Down
77 changes: 77 additions & 0 deletions Civi/Api4/Action/BankTransactionBatch/Import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

Check failure on line 1 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

Missing required strict_types declaration

namespace Civi\Api4\Action\BankTransactionBatch;

use Civi\Api4\Generic\Result;

use CRM_Banking_ExtensionUtil as E;

/**
* Import a statement
*/
class Import extends ImportBase {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API action classes should be suffixed with Action.


protected function process(array $statement, array $transactions, Result $result): void {

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $transactions with no value type specified in iterable type array.

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $statement with no value type specified in iterable type array.

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $transactions with no value type specified in iterable type array.

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $statement with no value type specified in iterable type array.

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $transactions with no value type specified in iterable type array.

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $statement with no value type specified in iterable type array.

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $transactions with no value type specified in iterable type array.

Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

Method Civi\Api4\Action\BankTransactionBatch\Import::process() has parameter $statement with no value type specified in iterable type array.
// create a new batch / statement to add created transactions to
try {
$batch = \Civi\Api4\BankTransactionBatch::create(FALSE)
->setValues($statement)
->execute()
->single();
}
catch (\Throwable $e) {

Check failure on line 22 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

Thrown exceptions in a catch block must bundle the previous exception (see throw statement line 23).

Check failure on line 22 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

Thrown exceptions in a catch block must bundle the previous exception (see throw statement line 23).

Check failure on line 22 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

Thrown exceptions in a catch block must bundle the previous exception (see throw statement line 23).

Check failure on line 22 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

Thrown exceptions in a catch block must bundle the previous exception (see throw statement line 23).
throw new \CRM_Core_Exception('Error creating new statement - does a matching statement already exist?');
}

$batchId = $batch['id'];

foreach ($transactions as $tx) {
try {
\Civi\Api4\BankTransaction::create(FALSE)
->addValue('bank_reference', $tx['bank_reference'])
->addValue('booking_date', $tx['booking_date'])
->addValue('value_date', $tx['booking_date'])
->addValue('amount', $tx['amount'])
->addValue('data_parsed', \json_encode($tx['data_parsed']))
->addValue('data_raw', $tx['data_raw'])
->addValue('tx_batch_id', $batchId)
->addValue('status_id:name', 'new')
// TOOD: is this field used? other importers seem to set fixed value 0
->addValue('type_id', 0)
->execute()
->single();
}
catch (\Throwable $e) {

Check failure on line 44 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

In method "Civi\Api4\Action\BankTransactionBatch\Import::process", caught "Throwable" must be rethrown. Either catch a more specific exception, add a "throw" clause in the "catch" block to propagate the exception or add a "// @IgnoreException" comment.

Check failure on line 44 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

In method "Civi\Api4\Action\BankTransactionBatch\Import::process", caught "Throwable" must be rethrown. Either catch a more specific exception, add a "throw" clause in the "catch" block to propagate the exception or add a "// @IgnoreException" comment.

Check failure on line 44 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

In method "Civi\Api4\Action\BankTransactionBatch\Import::process", caught "Throwable" must be rethrown. Either catch a more specific exception, add a "throw" clause in the "catch" block to propagate the exception or add a "// @IgnoreException" comment.

Check failure on line 44 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

In method "Civi\Api4\Action\BankTransactionBatch\Import::process", caught "Throwable" must be rethrown. Either catch a more specific exception, add a "throw" clause in the "catch" block to propagate the exception or add a "// @IgnoreException" comment.
// row level error
// common case the transaction already exists
$tx['error'] = $e->getMessage();
$result['skipped']['error'][] = $tx;

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

Cannot access offset 'error' on mixed.

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

Cannot access an offset on mixed.

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

Cannot access offset 'error' on mixed.

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

Cannot access an offset on mixed.

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

Cannot access offset 'error' on mixed.

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

Cannot access an offset on mixed.

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

Cannot access offset 'error' on mixed.

Check failure on line 48 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

Cannot access an offset on mixed.
continue;
}
}

$successfulTransactions = (array) \Civi\Api4\BankTransaction::get(FALSE)
->addWhere('tx_batch_id', '=', $batchId)
->addSelect('id', 'booking_date', 'amount', 'bank_reference', 'data_parsed')
->execute();

if (!$successfulTransactions) {

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

Only booleans are allowed in a negated boolean, array given.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

If_: Use a function e.g. `count($foo) === 0` instead of `!$foo`.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-stable

BooleanNot: Use a function e.g. `count($foo) === 0` instead of `!$foo`.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

Only booleans are allowed in a negated boolean, array given.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

If_: Use a function e.g. `count($foo) === 0` instead of `!$foo`.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.1 prefer-lowest

BooleanNot: Use a function e.g. `count($foo) === 0` instead of `!$foo`.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

Only booleans are allowed in a negated boolean, array given.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

If_: Use a function e.g. `count($foo) === 0` instead of `!$foo`.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-stable

BooleanNot: Use a function e.g. `count($foo) === 0` instead of `!$foo`.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

Only booleans are allowed in a negated boolean, array given.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

If_: Use a function e.g. `count($foo) === 0` instead of `!$foo`.

Check failure on line 58 in Civi/Api4/Action/BankTransactionBatch/Import.php

View workflow job for this annotation

GitHub Actions / PHPStan with PHP 8.4 prefer-lowest

BooleanNot: Use a function e.g. `count($foo) === 0` instead of `!$foo`.
// every line failed. lets cleanup the empty batch and throw an error
\Civi\Api4\BankTransactionBatch::delete(FALSE)
->addWhere('id', '=', $batchId)
->execute();

throw new \CRM_Core_Exception("Failed to import any transactions:\n\n" . \json_encode($result['skipped'], \JSON_PRETTY_PRINT));
}

// decode data parsed for clientside
$successfulTransactions = array_map(function ($tx) {
$tx['data_parsed'] = \json_decode($tx['data_parsed'], TRUE);
return $tx;
}, $successfulTransactions);

$result['statement'] = $batch;
$result['transactions'] = $successfulTransactions;
}

}
Loading
Loading