2022-08-12 05:41:55 +02:00
< ? php
/**
* Credit Ninja ( https :// invoiceninja . com ) .
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
* @ copyright Copyright ( c ) 2022. Credit Ninja LLC ( https :// invoiceninja . com )
*
* @ license https :// www . elastic . co / licensing / elastic - license
*/
namespace App\Jobs\Bank ;
use App\Helpers\Bank\Yodlee\Yodlee ;
2022-08-12 07:25:18 +02:00
use App\Libraries\MultiDB ;
2022-08-12 05:41:55 +02:00
use App\Models\BankIntegration ;
use App\Models\BankTransaction ;
use App\Services\Bank\BankService ;
use Illuminate\Bus\Queueable ;
use Illuminate\Contracts\Queue\ShouldQueue ;
use Illuminate\Foundation\Bus\Dispatchable ;
use Illuminate\Queue\InteractsWithQueue ;
use Illuminate\Queue\SerializesModels ;
2022-08-17 08:37:05 +02:00
use Illuminate\Support\Carbon ;
2022-08-12 05:41:55 +02:00
class ProcessBankTransactions implements ShouldQueue
{
use Dispatchable , InteractsWithQueue , Queueable , SerializesModels ;
private string $bank_integration_account_id ;
private BankIntegration $bank_integration ;
2022-09-14 07:35:51 +02:00
private ? string $from_date ;
2022-08-12 05:41:55 +02:00
2022-08-17 08:37:05 +02:00
private bool $stop_loop = true ;
2022-09-14 11:07:12 +02:00
private int $skip = 0 ;
2022-08-12 05:41:55 +02:00
/**
* Create a new job instance .
*/
2022-09-14 00:54:59 +02:00
public function __construct ( string $bank_integration_account_id , BankIntegration $bank_integration )
2022-08-12 05:41:55 +02:00
{
$this -> bank_integration_account_id = $bank_integration_account_id ;
$this -> bank_integration = $bank_integration ;
2022-09-14 00:59:04 +02:00
$this -> from_date = $bank_integration -> from_date ;
2022-08-12 05:41:55 +02:00
}
/**
* Execute the job .
*
*
* @ return void
*/
public function handle ()
{
2022-09-14 01:33:49 +02:00
set_time_limit ( 0 );
2022-08-17 08:37:05 +02:00
//Loop through everything until we are up to date
2022-08-12 05:41:55 +02:00
2022-09-14 11:07:12 +02:00
$this -> from_date = $this -> from_date ? : '2020-01-01' ;
2022-09-14 00:54:59 +02:00
2022-08-17 08:37:05 +02:00
do {
2022-09-14 01:33:49 +02:00
2022-08-17 08:37:05 +02:00
$this -> processTransactions ();
2022-09-14 01:33:49 +02:00
2022-08-17 08:37:05 +02:00
}
while ( $this -> stop_loop );
}
private function processTransactions ()
{
2022-09-14 09:35:13 +02:00
2022-08-12 05:41:55 +02:00
$yodlee = new Yodlee ( $this -> bank_integration_account_id );
$data = [
'top' => 500 ,
2022-09-14 11:07:12 +02:00
'fromDate' => $this -> from_date ,
2022-08-12 05:41:55 +02:00
'accountId' => $this -> bank_integration -> bank_account_id ,
2022-09-14 11:07:12 +02:00
'skip' => $this -> skip ,
2022-08-12 05:41:55 +02:00
];
2022-09-14 08:57:47 +02:00
//Get transaction count object
2022-08-17 08:37:05 +02:00
$transaction_count = $yodlee -> getTransactionCount ( $data );
2022-09-14 00:54:59 +02:00
2022-09-14 08:57:47 +02:00
//Get int count
2022-08-17 08:37:05 +02:00
$count = $transaction_count -> transaction -> TOTAL -> count ;
2022-09-14 10:13:46 +02:00
nlog ( " Number of transactions = { $count } - bank integration id = { $this -> bank_integration -> id } - bank account id = { $this -> bank_integration -> bank_account_id } - from { $this -> from_date } " );
2022-09-14 09:35:13 +02:00
2022-09-14 08:57:47 +02:00
//get transactions array
2022-08-12 05:41:55 +02:00
$transactions = $yodlee -> getTransactions ( $data );
2022-09-14 08:57:47 +02:00
//if no transactions, update the from_date and move on
if ( count ( $transactions ) == 0 ){
$this -> bank_integration -> from_date = now ();
$this -> bank_integration -> save ();
2022-09-14 10:25:30 +02:00
nlog ( " the bank integration from_date being set to: { $this -> bank_integration -> from_date } " );
2022-09-14 09:53:38 +02:00
$this -> stop_loop = false ;
2022-09-14 07:52:54 +02:00
return ;
2022-09-14 08:57:47 +02:00
}
2022-09-14 07:52:54 +02:00
2022-09-14 08:57:47 +02:00
//Harvest the company
2022-08-12 05:41:55 +02:00
$company = $this -> bank_integration -> company ;
2022-09-14 01:33:49 +02:00
MultiDB :: setDb ( $company -> db );
2022-09-14 08:57:47 +02:00
/*Get the user */
2022-08-12 05:41:55 +02:00
$user_id = $company -> owner () -> id ;
2022-09-14 08:57:47 +02:00
/* Unguard the model to perform batch inserts */
2022-08-17 03:52:16 +02:00
BankTransaction :: unguard ();
2022-09-14 08:57:47 +02:00
$now = now ();
2022-08-12 05:41:55 +02:00
foreach ( $transactions as $transaction )
{
if ( BankTransaction :: where ( 'transaction_id' , $transaction [ 'transaction_id' ]) -> where ( 'company_id' , $company -> id ) -> withTrashed () -> exists ())
continue ;
2022-08-17 05:43:16 +02:00
//this should be much faster to insert than using ::create()
$bt = \DB :: table ( 'bank_transactions' ) -> insert (
2022-08-17 03:52:16 +02:00
array_merge ( $transaction ,[
'company_id' => $company -> id ,
'user_id' => $user_id ,
'bank_integration_id' => $this -> bank_integration -> id ,
2022-09-14 08:57:47 +02:00
'created_at' => $now ,
'updated_at' => $now ,
2022-08-17 03:52:16 +02:00
])
2022-08-17 05:43:16 +02:00
);
2022-08-12 05:41:55 +02:00
}
BankService :: dispatch ( $company -> id , $company -> db );
2022-09-14 01:40:08 +02:00
$last_transaction = reset ( $transactions );
2022-09-14 01:33:49 +02:00
2022-08-16 02:45:51 +02:00
$this -> bank_integration -> from_date = isset ( $last_transaction [ 'date' ]) ? \Carbon\Carbon :: parse ( $last_transaction [ 'date' ]) : now ();
2022-08-17 08:37:05 +02:00
2022-09-14 10:23:52 +02:00
nlog ( " the bank integration from_date being set to: { $this -> bank_integration -> from_date } " );
2022-09-14 10:25:30 +02:00
2022-09-14 11:07:12 +02:00
$this -> skip = $this -> skip + 500 ;
2022-09-14 01:33:49 +02:00
2022-09-14 09:35:13 +02:00
if ( $count < 500 ){
2022-08-17 08:37:05 +02:00
$this -> stop_loop = false ;
2022-09-14 11:07:12 +02:00
$this -> from_date = now ();
$this -> bank_integration -> save ();
2022-09-14 09:35:13 +02:00
nlog ( " stopping while loop " );
}
2022-09-14 01:40:08 +02:00
2022-08-12 05:41:55 +02:00
}
}