-
Notifications
You must be signed in to change notification settings - Fork 44
add alternative "quick" importer #530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| namespace Civi\Api4\Action\BankTransactionBatch; | ||
|
|
||
| use Civi\Api4\Generic\Result; | ||
|
|
||
| use CRM_Banking_ExtensionUtil as E; | ||
|
|
||
| /** | ||
| * Import a statement | ||
| */ | ||
| class Import extends ImportBase { | ||
|
|
||
| protected function process(array $statement, array $transactions, Result $result): void { | ||
|
Check failure on line 14 in Civi/Api4/Action/BankTransactionBatch/Import.php
|
||
| // 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
|
||
| 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
|
||
| // 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
|
||
| 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
|
||
| // 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; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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.