1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 06:02:39 +01:00
invoiceninja/app/Import/Transformers/Bank/BankTransformer.php

81 lines
3.2 KiB
PHP
Raw Normal View History

2022-10-25 01:25:04 +02:00
<?php
/**
* client Ninja (https://clientninja.com).
*
* @link https://github.com/clientninja/clientninja source repository
*
* @copyright Copyright (c) 2022. client Ninja LLC (https://clientninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Import\Transformers\Bank;
use App\Import\ImportException;
2022-11-03 07:01:58 +01:00
use App\Import\Transformer\BaseTransformer;
2022-10-25 01:25:04 +02:00
use App\Models\BankTransaction;
use App\Utils\Number;
/**
* Class BankTransformer.
*/
class BankTransformer extends BaseTransformer
{
/**
* @param $line_items_data
*
* @return bool|array
*/
public function transform($transaction)
{
$now = now();
$transformed = [
2022-11-15 13:04:14 +01:00
'bank_integration_id' => $transaction['transaction.bank_integration_id'],
'transaction_id' => $this->getNumber($transaction,'transaction.transaction_id'),
'amount' => abs($this->getFloat($transaction, 'transaction.amount')),
'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'])
2022-10-25 01:25:04 +02:00
: now()->format('Y-m-d'),
2022-11-15 13:04:14 +01:00
'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'] : '',
2022-10-25 01:25:04 +02:00
'base_type' => $this->calculateType($transaction),
'created_at' => $now,
'updated_at' => $now,
'company_id' => $this->company->id,
'user_id' => $this->company->owner()->id,
];
return $transformed;
}
private function calculateType($transaction)
{
2022-11-15 13:04:14 +01:00
if(array_key_exists('transaction.base_type', $transaction) && (($transaction['transaction.base_type'] == 'CREDIT') || strtolower($transaction['transaction.base_type']) == 'deposit'))
2022-10-25 01:25:04 +02:00
return 'CREDIT';
2022-11-15 13:04:14 +01:00
if(array_key_exists('transaction.base_type', $transaction) && (($transaction['transaction.base_type'] == 'DEBIT') || strtolower($transaction['transaction.bank_type']) == 'withdrawal'))
2022-10-25 01:25:04 +02:00
return 'DEBIT';
2022-11-15 13:04:14 +01:00
if(array_key_exists('transaction.category_id', $transaction))
2022-10-25 01:25:04 +02:00
return 'DEBIT';
2022-11-15 13:04:14 +01:00
if(array_key_exists('transaction.category_type', $transaction) && $transaction['transaction.category_type'] == 'Income')
2022-10-25 01:25:04 +02:00
return 'CREDIT';
2022-11-15 13:04:14 +01:00
if(array_key_exists('transaction.category_type', $transaction))
2022-10-25 01:25:04 +02:00
return 'DEBIT';
2022-11-15 13:04:14 +01:00
if(array_key_exists('transaction.amount', $transaction) && is_numeric($transaction['transaction.amount']) && $transaction['transaction.amount'] > 0)
2022-10-25 01:25:04 +02:00
return 'CREDIT';
return 'DEBIT';
}
}