diff --git a/app/Jobs/ProcessWalletJournal.php b/app/Jobs/ProcessWalletJournalJob.php similarity index 51% rename from app/Jobs/ProcessWalletJournal.php rename to app/Jobs/ProcessWalletJournalJob.php index a2094c6c6..0a1440e8c 100644 --- a/app/Jobs/ProcessWalletJournal.php +++ b/app/Jobs/ProcessWalletJournalJob.php @@ -10,28 +10,12 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; //App Library -use App\Library\Finances\MarketTax; -use App\Library\Finances\PlayerDonation; -use App\Library\Finances\ReprocessingTax; -use App\Library\Finances\JumpBridgeTax; -use App\Library\Finances\StructureIndustryTax; -use App\Library\Finances\OfficeFee; -use App\Library\Finances\PlanetProductionTax; -use App\Library\Finances\PISale; -use App\Library\Lookups\LookupHelper; +use App\Library\Finances\Helper\FinanceHelper; //App Models -use App\Models\User\UserToCorporation; -use App\Models\Finances\CorpMarketJournal; -use App\Models\Finances\JumpBridgeJournal; -use App\Models\Finances\OfficeFeesJournal; -use App\Models\Finances\PISaleJournal; -use App\Models\Finances\PlanetProductionTaxJournal; -use App\Models\Finances\PlayerDonationJournal; -use App\Models\Finances\REprocessingTaxJournal; -use App\Models\Finances\StructureIndustryTaxJournal; +use App\Models\Jobs\ProcessWalletJournalJob as JobModel; -class ProcessWalletJournal implements ShouldQueue +class ProcessWalletJournalJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -42,18 +26,32 @@ class ProcessWalletJournal implements ShouldQueue */ public $timeout = 600; + /** + * Connection to utilize + * + * @var string + */ + public $connection = 'database'; + + /** + * Delay time for job + * + * @var int + */ + public $delay = 15; + /** * Create a new job instance. * * @return void */ - public function __construct() { - + public function __construct(JobModel $pwj) { + $this->pwj = $pwj; } /** * Execute the job. - * Utilized by using ProcessWalletJournal::dispatch() + * Utilized by using ProcessWalletJournalJob::dispatch() * The model is passed into the dispatch function, then added to the queue * for processing. * @@ -61,7 +59,13 @@ class ProcessWalletJournal implements ShouldQueue */ public function handle() { + //Declare the class variable we need + $finance = new FinanceHelper(); + $finance->GetWalletJournalPage($pwj->division, $pwj->charId, $pwj->page); + + //After the job is completed, delete the job + $this->pwj->delete(); } /** diff --git a/app/Library/Finances/Helper/FinanceHelper.php b/app/Library/Finances/Helper/FinanceHelper.php index d43a47ec9..d7e670533 100644 --- a/app/Library/Finances/Helper/FinanceHelper.php +++ b/app/Library/Finances/Helper/FinanceHelper.php @@ -279,6 +279,122 @@ class FinanceHelper { } while ($currentPage < $totalPages); } + public function GetJournalPageCount($division, $charId) { + //Declare class variables + $lookups = new LookupHelper; + + //Get the ESI refresh token for the corporation + $tokenData = $this->TokenInfo($charId); + $token = $tokenData['token']; + $scope = $tokdenData['scope']; + + if($this->TokenNotFound($token, $scope)) { + return null; + } + + //Refrence to see if the character is in our look up table for corporation and characters + $corpId = $lookups->LookupCharacter($charId); + + //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); + $esi->setVersion('v4'); + + //Call the first page so we can get the header data for the number of pages + try { + $journals = $esi->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [ + 'corporation_id' => $corpId, + 'division' => $division, + ]); + } catch(RequestFailedException $e) { + return $e->getEsiResponse(); + } + + $pages = $journals->pages; + + return $pages; + } + + public function GetWalletJournalPage($division, $charId, $page = 1) { + //Declare new class variables + $market = new MarketTax(); + $reprocessing = new ReprocessingTax(); + $jb = new JumpBridgeTax(); + $other = new PlayerDonation(); + $industry = new StructureIndustryTax(); + $office = new OfficeFee(); + + //Get the ESI refresh token for the corporation to add new wallet journals into the database + $tokenData = $this->TokenInfo($charId); + $token = $tokenData['token']; + $scope = $tokenData['scope']; + + //Declare the lookup class helper + $lookups = new LookupHelper; + + //If the token is not found, send the user an eve mail, and just exit out of the function + if($this->TokenNotFound($token, $scope)) { + return null; + } + + //Reference to see if the character is in our look up table for corporations and characters + $corpId = $lookups->LookupCharacter($charId); + + //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); + $esi->setVersion('v4'); + + //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($page) + ->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [ + 'corporation_id' => $corpId, + 'division' => $division, + ]); + } catch(RequestFailedException $e) { + return $e->getEsiResponse(); + } + + //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['amount'] > 0) { + if($entry['ref_type'] == 'brokers_fee') { + $market->InsertMarketTax($entry, $corpId, $division); + } else if($entry['ref_type'] == 'reprocessing_tax') { + $reprocessing->InsertReprocessingTax($entry, $corpId, $division); + } else if($entry['ref_type'] == 'structure_gate_jump') { + $jb->InsertJumpBridgeTax($entry, $corpId, $division); + } else if($entry['ref_type'] == 'player_donation' || + ($entry['ref_type'] == 'corporation_account_withdrawal' && $entry['second_party_id'] == 98287666)) { + $other->InsertPlayerDonation($entry, $corpId, $division); + } else if($entry['ref_type'] == 'industry_job_tax' && $entry['second_party_id'] == 98287666) { + $industry->InsertStructureIndustryTax($entry, $corpId, $division); + } else if($entry['ref_type'] == 'office_rental_fee' && $entry['second_party_id'] == 98287666) { + $office->InsertOfficeFee($entry, $corpId, $division); + } + } + } + } + private function TokenInfo($charId) { //Get the ESI refresh token for the corporation to add a new wallet jouranls into the database //send the token and scope back to the calling function diff --git a/app/Models/Jobs/ProcessWalletJournalJob.php b/app/Models/Jobs/ProcessWalletJournalJob.php new file mode 100644 index 000000000..4f59fb8a9 --- /dev/null +++ b/app/Models/Jobs/ProcessWalletJournalJob.php @@ -0,0 +1,27 @@ + \ No newline at end of file diff --git a/database/migrations/2019_05_04_00000_create_job_process_wallet_journal.php b/database/migrations/2019_05_04_00000_create_job_process_wallet_journal.php new file mode 100644 index 000000000..7f682228d --- /dev/null +++ b/database/migrations/2019_05_04_00000_create_job_process_wallet_journal.php @@ -0,0 +1,36 @@ +increments('id')->unique(); + $table->string('charId'); + $table->string('division'); + $table->integer('page'); + $table->timestamps(); + }); + } + } + + /** + * Reverse the migration + * + * @return void + */ + public function down() { + Schema::dropIfExists('job_process_wallet_journal'); + } +} + +?> \ No newline at end of file