diff --git a/app/Jobs/Commands/Finances/UpdateAllianceWalletJournal.php b/app/Jobs/Commands/Finances/UpdateAllianceWalletJournal.php index 6014700cf..f206edfb0 100644 --- a/app/Jobs/Commands/Finances/UpdateAllianceWalletJournal.php +++ b/app/Jobs/Commands/Finances/UpdateAllianceWalletJournal.php @@ -58,7 +58,7 @@ class UpdateAllianceWalletJournal implements ShouldQueue $pages = $fHelper->GetAllianceWalletJournalPages(1, $config['primary']); - for($i = 1; $i < $pages; $i++) { + for($i = 1; $i <= $pages; $i++) { UpdateAllianceWalletJournalPage::dispatch(1, $config['primary'], $page)->onQueue('finances'); } } diff --git a/app/Jobs/Commands/Finances/UpdateAllianceWalletJournalPage.php b/app/Jobs/Commands/Finances/UpdateAllianceWalletJournalPage.php index d8e068df5..8312a2554 100644 --- a/app/Jobs/Commands/Finances/UpdateAllianceWalletJournalPage.php +++ b/app/Jobs/Commands/Finances/UpdateAllianceWalletJournalPage.php @@ -1,25 +1,45 @@ division = $division; + $this->charId = $charId; + $this->page = $page; } /** @@ -29,6 +49,96 @@ class UpdateAllianceWalletJournalPage implements ShouldQueue */ public function handle() { - // + //Declare variables in the handler + $lookup = new LookupHelper; + $esiHelper = new Esi; + + //Setup the esi container. + $token = $esiHelper->GetRefreshToken($this->charId); + $esi = $esiHelper->SetupEsiAuthentication($token); + + //Check the scope + if(!$esiHelper->HaveEsiScope($this->charId, 'esi-wallet.read_corporation_wallets.v1')) { + Log::critical('Scope check failed for esi-wallet.read_corporation_wallets.v1 for character id: ' . $charId); + return null; + } + + if($esiHelper->TokenExpired($token)) { + $token = $esiHelper->GetRefreshToken($this->charId); + $esi = $esiHelper->SetupEsiAuthentication($token); + } + + //Reference the character id to the corporation id + $char = $lookup->GetCharacterInfo($this->charId); + $corpId = $char->corporation_id; + + /** + * Attempt to get the data from the esi api. If it fails, we skip the page, and go onto the next page, unless + * the failed page is the first page. + */ + try { + $journals = $esi->page($currentPage) + ->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [ + 'corporation_id' => $corpId, + 'division' => $division, + ]); + } catch(RequestFailedException $e) { + Log::warning('Failed to get wallet journal page ' . $currentPage . ' for character id: ' . $charId); + Log::warning($e); + $this->delete(); + } + + //Decode the json data, and return it as an array + $wallet = json_decode($journals->raw, true); + + //Foreach journal entry, add the journal entry to the table + foreach($wallet as $entry) { + //See if we find the entry id in the database already + $found = AllianceWalletJournal::where([ + 'id' => $entry['id'], + ])->count(); + + if($found == 0) { + $awj = new AllianceWalletJournal; + $awj->id = $entry['id']; + $awj->corporation_id = $corpId; + $awj->division = $division; + if(isset($entry['amount'])) { + $awj->amount = $entry['amount']; + } + if(isset($entry['balance'])) { + $awj->balance = $entry['balance']; + } + if(isset($entry['context_id'])) { + $awj->context_id = $entry['context_id']; + } + if(isset($entry['date'])) { + $awj->date = $esiHelper->DecodeDate($entry['date']); + } + if(isset($entry['description'])) { + $awj->description = $entry['description']; + } + if(isset($entry['first_party_id'])) { + $awj->first_party_id = $entry['first_party_id']; + } + if(isset($entry['reason'])) { + $awj->reason = $entry['reason']; + } + if(isset($entry['ref_type'])) { + $awj->ref_type = $entry['ref_type']; + } + if(isset($entry['tax'])) { + $awj->tax = $entry['tax']; + } + if(isset($entry['tax_receiver_id'])) { + $awj->tax_receiver_id = $entry['tax_receiver_id']; + } + $awj->save(); + + } + } + + //Return as completed + return 0; } } diff --git a/app/Library/Helpers/FinanceHelper.php b/app/Library/Helpers/FinanceHelper.php index 77784d3a8..98ca517c2 100644 --- a/app/Library/Helpers/FinanceHelper.php +++ b/app/Library/Helpers/FinanceHelper.php @@ -26,7 +26,6 @@ class FinanceHelper { public function GetApiWalletJournal($division, $charId) { //Declare class variables $lookup = new LookupHelper; - $finance = new FinanceHelper; $esiHelper = new Esi; //Setup the esi container. @@ -159,6 +158,47 @@ class FinanceHelper { return 0; } + /** + * Get the pages for the alliance wallet journal + */ + public function GetAllianceWalletJournalPages() { + $lookup = new LookupHelper; + $esiHelper = new Esi; + + //Setup the esi container. + $token = $esiHelper->GetRefreshToken($charId); + $esi = $esiHelper->SetupEsiAuthentication($token); + + //Check the scope + if(!$esiHelper->HaveEsiScope($charId, 'esi-wallet.read_corporation_wallets.v1')) { + Log::critical('Scope check failed for esi-wallet.read_corporation_wallets.v1 for character id: ' . $charId); + return null; + } + + //Reference the character id to the corporation id + $char = $lookup->GetCharacterInfo($charId); + $corpId = $char->corporation_id; + + /** + * Attempt to get the data from the esi api. If it fails, we skip the page, and go onto the next page, unless + * the failed page is the first page. + */ + try { + $journals = $esi->page(1) + ->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [ + 'corporation_id' => $corpId, + 'division' => $division, + ]); + } catch(RequestFailedException $e) { + Log::warning('Failed to get wallet journal pages for character id: ' . $charId); + Log::warning($e); + return 0; + } + + //Return the total pages + return $journals->pages; + } + private function GetPIMaterialsArray() { //Setup array for PI items $pi_items = [