1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Import/Transformer/Bank/BankTransformer.php

106 lines
4.3 KiB
PHP
Raw Normal View History

<?php
/**
2023-05-01 00:07:41 +02:00
* Invoice Ninja (https://invoiceninja.com).
*
2023-05-01 00:07:41 +02:00
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-05-01 00:07:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Import\Transformer\Bank;
use App\Import\Transformer\BaseTransformer;
/**
* Class BankTransformer.
*/
class BankTransformer extends BaseTransformer
{
/**
* @param $line_items_data
*
* @return bool|array
*/
public function transform($transaction)
{
2023-02-16 02:36:09 +01:00
$now = now();
2023-02-16 02:36:09 +01:00
$transformed = [
'bank_integration_id' => $transaction['transaction.bank_integration_id'],
2023-02-16 02:36:09 +01:00
'transaction_id' => $this->getNumber($transaction, 'transaction.transaction_id'),
2023-06-25 05:15:15 +02:00
'amount' => $this->calculateAmount($transaction),
'currency_id' => $this->getCurrencyByCode($transaction, 'transaction.currency'),
'account_type' => strlen($this->getString($transaction, 'transaction.account_type')) > 1 ? $this->getString($transaction, 'transaction.account_type') : 'bank',
'category_id' => $this->getNumber($transaction, 'transaction.category_id') > 0 ? $this->getNumber($transaction, 'transaction.category_id') : null,
'category_type' => $this->getString($transaction, 'transaction.category_type'),
'date' => array_key_exists('transaction.date', $transaction) ? $this->parseDate($transaction['transaction.date'])
: now()->format('Y-m-d'),
'bank_account_id' => array_key_exists('transaction.bank_account_id', $transaction) ? $transaction['transaction.bank_account_id'] : 0,
'description' => array_key_exists('transaction.description', $transaction) ? $transaction['transaction.description'] : '',
'base_type' => $this->calculateType($transaction),
'created_at' => $now,
'updated_at' => $now,
'company_id' => $this->company->id,
'user_id' => $this->company->owner()->id,
2024-01-13 08:35:34 +01:00
'participant' => $this->getString($transaction, 'transaction.participant'),
'participant_name' => $this->getString($transaction, 'transaction.participant_name'),
];
return $transformed;
}
2024-01-14 05:05:00 +01:00
private function calculateAmount(array $transaction): float
2023-06-25 05:15:15 +02:00
{
if (array_key_exists('transaction.amount', $transaction) && is_numeric($transaction['transaction.amount'])) {
return abs($this->getFloat($transaction, 'transaction.amount'));
}
if (array_key_exists('transaction.payment_type_Credit', $transaction) && is_numeric($transaction['transaction.payment_type_Credit'])) {
return abs($this->getFloat($transaction, 'transaction.payment_type_Credit'));
}
if (array_key_exists('transaction.payment_type_Debit', $transaction) && is_numeric($transaction['transaction.payment_type_Debit'])) {
return abs($this->getFloat($transaction, 'transaction.payment_type_Debit'));
}
return 0;
}
private function calculateType($transaction)
{
2023-06-25 05:15:15 +02:00
if (array_key_exists('transaction.payment_type_Credit', $transaction) && is_numeric($transaction['transaction.payment_type_Credit'])) {
2023-02-16 02:36:09 +01:00
return 'CREDIT';
}
2023-06-25 05:15:15 +02:00
if (array_key_exists('transaction.transaction.payment_type_Debit', $transaction) && is_numeric($transaction['transaction.payment_type_Debit'])) {
return 'DEBIT';
}
2023-02-16 02:36:09 +01:00
if (array_key_exists('transaction.base_type', $transaction) && (($transaction['transaction.base_type'] == 'DEBIT') || strtolower($transaction['transaction.base_type']) == 'withdrawal')) {
return 'DEBIT';
}
2023-02-16 02:36:09 +01:00
if (array_key_exists('transaction.category_id', $transaction)) {
return 'DEBIT';
}
2023-02-16 02:36:09 +01:00
if (array_key_exists('transaction.category_type', $transaction) && $transaction['transaction.category_type'] == 'Income') {
return 'CREDIT';
}
2023-02-16 02:36:09 +01:00
if (array_key_exists('transaction.category_type', $transaction)) {
return 'DEBIT';
}
2023-02-16 02:36:09 +01:00
if (array_key_exists('transaction.amount', $transaction) && is_numeric($transaction['transaction.amount']) && $transaction['transaction.amount'] > 0) {
return 'CREDIT';
}
2023-02-16 02:36:09 +01:00
return 'DEBIT';
}
2023-02-16 02:36:09 +01:00
}