bank_integration_account_id = $bank_integration_account_id; $this->bank_integration = $bank_integration; $this->from_date = $bank_integration->from_date; } /** * Execute the job. * * * @return void */ public function handle() { set_time_limit(0); //Loop through everything until we are up to date $this->from_date = $this->from_date ?: '2021-01-01'; do{ $this->processTransactions(); } while($this->stop_loop); } private function processTransactions() { $yodlee = new Yodlee($this->bank_integration_account_id); $data = [ 'top' => 500, 'fromDate' => $this->from_date, 'toDate' => now()->format('Y-m-d'), 'accountId' => $this->bank_integration->bank_account_id, ]; $transaction_count = $yodlee->getTransactionCount($data); $count = $transaction_count->transaction->TOTAL->count; //expense transactions $transactions = $yodlee->getTransactions($data); if(count($transactions) == 0) return; $company = $this->bank_integration->company; MultiDB::setDb($company->db); $user_id = $company->owner()->id; BankTransaction::unguard(); foreach($transactions as $transaction) { if(BankTransaction::where('transaction_id', $transaction['transaction_id'])->where('company_id', $company->id)->withTrashed()->exists()) continue; //this should be much faster to insert than using ::create() $bt = \DB::table('bank_transactions')->insert( array_merge($transaction,[ 'company_id' => $company->id, 'user_id' => $user_id, 'bank_integration_id' => $this->bank_integration->id, ]) ); } BankService::dispatch($company->id, $company->db); $last_transaction = reset($transactions); $this->bank_integration->from_date = isset($last_transaction['date']) ? \Carbon\Carbon::parse($last_transaction['date']) : now(); $this->from_date = \Carbon\Carbon::parse($this->bank_integration->from_date)->format('Y-m-d'); $this->bank_integration->save(); if($count < 500) $this->stop_loop = false; } }