diff --git a/app/Console/Commands/corpJournal.php b/app/Console/Commands/corpJournal.php index 10f7fbc40..3516d7b38 100644 --- a/app/Console/Commands/corpJournal.php +++ b/app/Console/Commands/corpJournal.php @@ -79,7 +79,6 @@ class CorpJournal extends Command } //If we didn't find the corporation was already done, then complete it. if($corpCompleted === false) { - //$this->line('Getting corp journal'); $this->GetJournal($structure->character_id); $finishedCorps[sizeof($finishedCorps)] = $structure->corporation_id; //After the corporation has been done set the variable back to false diff --git a/app/Http/Controllers/FinancesController.php b/app/Http/Controllers/FinancesController.php index d9e8dfcff..700658923 100644 --- a/app/Http/Controllers/FinancesController.php +++ b/app/Http/Controllers/FinancesController.php @@ -14,7 +14,7 @@ use App\Models\User\UserRole; use App\Models\User\UserPermission; use App\Models\Corporation\CorpJournal; -use App\Library\Finances; +use App\Library\FinanceHelper; use App\Library\Esi; use App\Library\SeatHelper; @@ -31,7 +31,7 @@ class FinancesController extends Controller } public function displayWallet() { - $helper = new Finances(); + $helper = new FinanceHelper(); $helper->GetWalletJournal(1, 92626011); dd($helper); @@ -41,7 +41,7 @@ class FinancesController extends Controller //Make the helper esi class $helper = new Esi(); //Make the helper class for finances - $hFinances = new Finances(); + $hFinances = new FinanceHelper(); //Set the carbon date for the first day of this month, last day of this month, and the previous month's first and last days. $start = Carbon::now()->startOfMonth(); $end = Carbon::now()->endOfMonth(); diff --git a/app/Library/FinanceHelper.php b/app/Library/FinanceHelper.php new file mode 100644 index 000000000..c524117d3 --- /dev/null +++ b/app/Library/FinanceHelper.php @@ -0,0 +1,220 @@ +where('character_id', $charId)->get(); + + //Reference to see if the character is in our look up table for corporations and characters + $corpId = $this->GetCharCorp($charId); + + //Disable all caching by setting the NullCache as the preferred cache handler. + $configuraiton = Configuration::getInstance(); + $configuration->cache = NullCache::class; + + //Create an ESI authentication container + $config = config('esi'); + $authentication = new EsiAuthentication([ + 'client_id' => $config['client_id'], + 'secret' => $config['secret'], + 'refresh_token' => $token[0]->refresh_token, + ]); + + //Create the esi class varialble + $esi = new Eseye($authentication); + + //Set our current page to 1 which is the one we are starting on. + $currentPage = 1; + //Set our default total pages to 1 in case our try section fails out. + $totalPages = 1; + + //If more than one page is found, decode the first set of wallet entries, then call for the next pages + do { + //Call the first page of the wallet journal, as we are always going to get at least one page. + //If we have more pages, then we will continue through the while loop. + try { + $journals = $esi->page($currentPage) + ->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [ + 'corporation_id' => $character->corporation_id, + 'division' => $division, + ]); + } catch(\Seat\Eseye\Exceptions\RequestFailedException $e) { + return $e->getEsiResponse(); + } + + //Set the total pages we need to cycle through. + $totalPages = $journals->pages; + //Decode the wallet from json into an array + $wallet = json_decode($journals->raw, true); + //For each journal entry, attempt to store it in the database. + //The PutWalletJournal function checks to see if it's already in the database. + foreach($wallet as $entry) { + if($entry['ref_type'] == 'brokers_fee' || $entry['ref_type'] == 'reprocessing_tax') { + $this->PutWalletJournal($entry, $corpId, $division); + } + } + + //Increment the current page we are on. + $currentPage++; + //Continue looping through the do while loop until the current page is greater than or equal to the total pages. + } while ($currentPage < $totalPages); + } + + /** + * Returns the corporation a character is in if found in the lookup table, otherwise, + * adds the character to the lookup table, and returns the corporation id + * + * @param charId + * @return corpId + */ + private function GetCharCorp($charId) { + //Check for the character the user_to_corporation table + $found = DB::table('user_to_corporation')->where('character_id', $charId)->get(); + //If we don't find the character in the table, then let's retrieve the information from ESI + if($found == null) { + //Get the configuration for ESI from the environmental variables + $config = config('esi'); + //Setup a new ESI container + $esi = new Eseye(); + //Try to get the character information, then the corporation information + try { + $character = $esi->invoke('get', '/characters/{character_id}/', [ + 'character_id' => $charId, + ]); + $corporation = $esi->invoke('get', '/corporations/{corporation_id}/', [ + 'corporation_id' => $character->corporation_id, + ]); + } catch(\Seat\Eseye\Exceptions\RequestFailedException $e){ + return $e->getEsiResponse(); + } + + //Save all of the data to the database + $character = new UserToCorporation; + $character->character_id = $character->character_id; + $character->character_name = $character->name; + $character->corporation_id = $character->corporation_id; + $character->corporation_name = $corporation->name; + $character->save(); + //Return the corporation_id which is what the calling function is looking for + return $character->corporation_id; + } else { + //Return the corporation_id if it was found in the database as it is what the calling function is looking for + return $found->corporation_id; + } + } + + private function PutWalletJournal($journal, $corpId, $division) { + //Create ESI Helper class + $esiHelper = new Esi; + $date = $esiHelper->DecodeDate($journal['date']); + + $check = DB::table('CorpJournals')->where('id', $journal['id'])->get(); + //if we don't find the journal entry, add the journal entry to the database + if($check->count() === 0) { + $entry = new CorpJournal; + $entry->id = $journal['id']; + $entry->corporation_id = $corpId; + $entry->division = $division; + if(isset($journal['amount'])) { + $entry->amount = $journal['amount']; + } + if(isset($journal['balance'])) { + $entry->balance = $journal['balance']; + } + if(isset($journal['context_id'])) { + $entry->context_id = $journal['context_id']; + } + if(isset($journal['context_id_type'])) { + $entry->context_id_type = $journal['context_id_type']; + } + $entry->date = $date; + $entry->description = $journal['description']; + if(isset($journal['first_party_id'])) { + $entry->first_party_id = $journal['first_party_id']; + } + if(isset($journal['reason'])) { + $entry->reason = $journal['reason']; + } + $entry->ref_type = $journal['ref_type']; + if(isset($journal['second_party_id'])) { + $entry->second_party_id = $journal['second_party_id']; + } + if(isset($journal['tax'])) { + $entry->tax = $journal['tax']; + } + if(isset($journal['tax_receiver_id'])) { + $entry->tax_receiver_id = $journal['tax_receiver_id']; + } + $entry->save(); + } + + } + +} + +?> \ No newline at end of file diff --git a/app/Library/Finances.php b/app/Library/Finances.php deleted file mode 100644 index 60d7af218..000000000 --- a/app/Library/Finances.php +++ /dev/null @@ -1,157 +0,0 @@ -where('character_id', $charId)->get(); - //Disable all caching by setting the NullCache as the preferred cache handler. - $configuration = Configuration::getInstance(); - $configuration->cache = NullCache::class; - $configuration->logfile_location = '/var/www/w4rpservices/storage/logs/eseye'; - //Create the ESI authentication container - $config = config('esi'); - $authentication = new EsiAuthentication([ - 'client_id' => $config['client_id'], - 'secret' => $config['secret'], - 'refresh_token' => $token[0]->refresh_token, - ]); - //Create the esi class variable - $esi = new Eseye($authentication); - //Try an esi call to get public data to get the character information for corp id. - try { - $character = $esi->invoke('get', '/characters/{character_id}/', [ - 'character_id' => $charId, - ]); - } catch(\Seat\Eseye\Exceptions\RequestFailedException $e){ - return $e->getEsiResponse(); - } - - //Try the ESI call to get the wallet journal - try { - $journals = $esi->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [ - 'corporation_id' => $character->corporation_id, - 'division' => $division, - ]); - } catch(\Seat\Eseye\Exceptions\RequestFailedException $e) { - return $e->getEsiResponse(); - } - //Decode the journal from json into an array for future processing - $journals = json_decode($journals->raw, true); - //For each journal array, attempt to store in the database - foreach($journals as $entry) { - if($entry['ref_type'] == 'brokers_fee' || $entry['ref_type'] == 'reprocessing_tax') { - $this->PutWalletJournal($entry, $character->corporation_id, $division); - } - } - } - - private function PutWalletJournal($journal, $corpId, $division) { - //Create ESI Helper class - $esiHelper = new Esi; - $date = $esiHelper->DecodeDate($journal['date']); - - $check = DB::table('CorpJournals')->where('id', $journal['id'])->get(); - //if we don't find the journal entry, add the journal entry to the database - if($check->count() === 0) { - $entry = new CorpJournal; - $entry->id = $journal['id']; - $entry->corporation_id = $corpId; - $entry->division = $division; - if(isset($journal['amount'])) { - $entry->amount = $journal['amount']; - } - if(isset($journal['balance'])) { - $entry->balance = $journal['balance']; - } - if(isset($journal['context_id'])) { - $entry->context_id = $journal['context_id']; - } - if(isset($journal['context_id_type'])) { - $entry->context_id_type = $journal['context_id_type']; - } - $entry->date = $date; - $entry->description = $journal['description']; - if(isset($journal['first_party_id'])) { - $entry->first_party_id = $journal['first_party_id']; - } - if(isset($journal['reason'])) { - $entry->reason = $journal['reason']; - } - $entry->ref_type = $journal['ref_type']; - if(isset($journal['second_party_id'])) { - $entry->second_party_id = $journal['second_party_id']; - } - if(isset($journal['tax'])) { - $entry->tax = $journal['tax']; - } - if(isset($journal['tax_receiver_id'])) { - $entry->tax_receiver_id = $journal['tax_receiver_id']; - } - $entry->save(); - } - - } - -} - -?> \ No newline at end of file diff --git a/app/Models/User/UserToCorporation.php b/app/Models/User/UserToCorporation.php new file mode 100644 index 000000000..899349166 --- /dev/null +++ b/app/Models/User/UserToCorporation.php @@ -0,0 +1,26 @@ +increments('id'); + $table->string('character_id'); + $table->string('character_name'); + $table->string('corporation_id'); + $table->string('corporation_name'); + }); + } + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_to_corporation'); + } +} diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 4119f6ad1..90e317068 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -14,7 +14,6 @@ return array( 'App\\Console\\Commands\\sendMail' => $baseDir . '/app/Console/Commands/sendmail.php', 'App\\Console\\Kernel' => $baseDir . '/app/Console/Kernel.php', 'App\\Exceptions\\Handler' => $baseDir . '/app/Exceptions/Handler.php', - 'App\\FleetActivity' => $baseDir . '/app/Models/Fleet/FleetActivity.php', 'App\\Http\\Controllers\\AdminController' => $baseDir . '/app/Http/Controllers/AdminController.php', 'App\\Http\\Controllers\\AjaxController' => $baseDir . '/app/Http/Controllers/AjaxController.php', 'App\\Http\\Controllers\\Auth\\ForgotPasswordController' => $baseDir . '/app/Http/Controllers/Auth/ForgotPasswordController.php', @@ -60,6 +59,7 @@ return array( 'App\\Models\\Esi\\EsiScope' => $baseDir . '/app/Models/Esi/EsiScope.php', 'App\\Models\\Esi\\EsiToken' => $baseDir . '/app/Models/Esi/EsiToken.php', 'App\\Models\\Fleet\\Fleet' => $baseDir . '/app/Models/Fleet/Fleet.php', + 'App\\Models\\Fleet\\FleetActivity' => $baseDir . '/app/Models/Fleet/FleetActivity.php', 'App\\Models\\Logistics\\Contract' => $baseDir . '/app/Models/Logistics/Contract.php', 'App\\Models\\Market\\MarketOrder' => $baseDir . '/app/Models/Market/MarketOrder.php', 'App\\Models\\Moon\\ItemComposition' => $baseDir . '/app/Models/Moon/ItemComposition.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 443813295..ee761bac2 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -409,7 +409,6 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9 'App\\Console\\Commands\\sendMail' => __DIR__ . '/../..' . '/app/Console/Commands/sendmail.php', 'App\\Console\\Kernel' => __DIR__ . '/../..' . '/app/Console/Kernel.php', 'App\\Exceptions\\Handler' => __DIR__ . '/../..' . '/app/Exceptions/Handler.php', - 'App\\FleetActivity' => __DIR__ . '/../..' . '/app/Models/Fleet/FleetActivity.php', 'App\\Http\\Controllers\\AdminController' => __DIR__ . '/../..' . '/app/Http/Controllers/AdminController.php', 'App\\Http\\Controllers\\AjaxController' => __DIR__ . '/../..' . '/app/Http/Controllers/AjaxController.php', 'App\\Http\\Controllers\\Auth\\ForgotPasswordController' => __DIR__ . '/../..' . '/app/Http/Controllers/Auth/ForgotPasswordController.php', @@ -455,6 +454,7 @@ class ComposerStaticInitc3f953f8a7291d41a76e1664339777c9 'App\\Models\\Esi\\EsiScope' => __DIR__ . '/../..' . '/app/Models/Esi/EsiScope.php', 'App\\Models\\Esi\\EsiToken' => __DIR__ . '/../..' . '/app/Models/Esi/EsiToken.php', 'App\\Models\\Fleet\\Fleet' => __DIR__ . '/../..' . '/app/Models/Fleet/Fleet.php', + 'App\\Models\\Fleet\\FleetActivity' => __DIR__ . '/../..' . '/app/Models/Fleet/FleetActivity.php', 'App\\Models\\Logistics\\Contract' => __DIR__ . '/../..' . '/app/Models/Logistics/Contract.php', 'App\\Models\\Market\\MarketOrder' => __DIR__ . '/../..' . '/app/Models/Market/MarketOrder.php', 'App\\Models\\Moon\\ItemComposition' => __DIR__ . '/../..' . '/app/Models/Moon/ItemComposition.php',