diff --git a/app/Console/Commands/Library/helper.php b/app/Console/Commands/Library/helper.php index fe3054f2b..2c3aad4fd 100644 --- a/app/Console/Commands/Library/helper.php +++ b/app/Console/Commands/Library/helper.php @@ -2,9 +2,11 @@ namespace Commands\Library; +//Internal Libraries use DB; use Carbon\Carbon; +//Models use App\Models\ScheduledTask\ScheduleJob; class CommandHelper { @@ -38,6 +40,9 @@ class CommandHelper { ]); } + public function CleanJobStatusTable() { + DB::table('schedule_jobs')->where('system_time', '<', Carbon::now()->subMonths(6))->delete(); + } } ?> \ No newline at end of file diff --git a/app/Console/Commands/calculatemarkettax.php b/app/Console/Commands/calculatemarkettax.php index 87845fb01..c42d5bcfe 100644 --- a/app/Console/Commands/calculatemarkettax.php +++ b/app/Console/Commands/calculatemarkettax.php @@ -6,21 +6,21 @@ use Illuminate\Console\Command; use Carbon\Carbon; use DB; -use App\Jobs\SendEveMail; +//Jobs +use App\Jobs\SendEveMailJob; + +//Libraries use Commands\Library\CommandHelper; use App\Library\Finances\Helper\FinanceHelper; use App\Library\Structures\StructureTaxHelper; use App\Library\Esi\Esi; -use App\Library\Esi\Mail; +//Models use App\Models\Market\MonthlyMarketTax; use App\Models\ScheduledTask\ScheduleJob; -use App\Models\Finances\CorpMarketJournal; use App\Models\Corporation\CorpStructure; use App\Models\User\UserToCorporation; -use App\Models\Mail\EveMail; -use App\Models\Esi\EsiScope; -use App\Models\Esi\EsiToken; +use App\Models\Jobs\JobSendEveMail; class CalculateMarketTaxCommand extends Command { @@ -73,29 +73,31 @@ class CalculateMarketTaxCommand extends Command $corps = CorpStructure::select('corporation_id')->groupBy('corporation_id')->get(); $this->line('Got all of the corps with markets.' . sizeof($corps)); foreach($corps as $corp) { - $finalTaxes = $sHelper->GetTaxes($corp->corporation_id, 'Market', $start, $end); - if($finalTaxes < 0.00) { - $finalTaxes = 0.00; + if($corp->corporation_id != 98287666) { + $finalTaxes = $sHelper->GetTaxes($corp->corporation_id, 'Market', $start, $end); + if($finalTaxes < 0.00) { + $finalTaxes = 0.00; + } + + //Get the info about the structures from the database + $info = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first(); + + $character = UserToCorporation::where(['character_id' => $info->character_id])->first(); + + $mail = new JobSendEveMail; + $mail->sender = 93738489; + $mail->subject = 'Market Taxes Owed'; + $mail->body = 'Year ' . $start->year . ' ' . + 'Month: ' . + $start->month . + '
Market Taxes Owed: ' . + number_format($finalTaxes, 2, '.', ',') . + '
Please remit to Spatial Forces'; + $mail->recipient = (int)$info->character_id; + $mail->recipient_type = 'character'; + SendEveMailJob::dispatch($mail); } - - //Get the info about the structures from the database - $info = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first(); - - $character = UserToCorporation::where(['character_id' => $info->character_id])->first(); - - $mail = new EveMail; - $mail->sender = 93738489; - $mail->subject = 'Market Taxes Owed'; - $mail->body = 'Year ' . $start->year . ' ' . - 'Month: ' . - $start->month . - '
Market Taxes Owed: ' . - number_format($finalTaxes, 2, '.', ',') . - '
Please remit to Spatial Forces'; - $mail->recipient = (int)$info->character_id; - $mail->recipient_type = 'character'; - //$mail->save(); - SendEveMail::dispatch($mail); + } //Mark the job as finished diff --git a/app/Console/Commands/corpJournal.php b/app/Console/Commands/corpJournal.php index 7e255904d..0b3113af0 100644 --- a/app/Console/Commands/corpJournal.php +++ b/app/Console/Commands/corpJournal.php @@ -15,6 +15,7 @@ use App\Jobs\ProcessWalletJournalJob; //Models use App\Models\Corporation\CorpStructure; +use App\Models\Jobs\JobProcessWalletJournal; class CorpJournalCommand extends Command { @@ -51,23 +52,46 @@ class CorpJournalCommand extends Command { //Create the command helper container $task = new CommandHelper('CorpJournal'); + //Add the entry into the jobs table saying the job is starting $task->SetStartStatus(); + //Setup the Finances Container $finance = new FinanceHelper(); + //Setup an array to store corporations which have been logged so we don't keep calling the same ones. We need //this step in order to save time during the cronjob. $finishedCorps = array(); $corpCompleted = false; + //Get the corps with structures logged in the database $corps = CorpStructure::select('corporation_id')->groupBy('corporation_id')->get(); - + + //For all of the corporations, go through each structure and get wallet data foreach($corps as $corp) { - if($charId->character_id != 93738489) { + //If the corporation isn't the holding corporation, then process the data. + //We process holding corporation data elsewhere. + if($corp->corporation_id != 98287666) { + $charId = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first(); + $pages = $finance->GetJournalPageCount(1, $charId); + for($i = 1; $i <= $pages; $i++) { + $job = new JobProcessWalletJournal; + $job->division = 1; + $job->charId = $charId; + $job->page = $i; + ProcessWalletJournalJob::dispatch($job); + } + } + } + + /* + foreach($corps as $corp) { + if($corp->corporation_id != 98287666) { $charId = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first(); $finance->GetWalletJournal(1, $charId->character_id); } } + */ //Mark the job as finished $task->SetStopStatus(); diff --git a/app/Console/Commands/holdingfinances.php b/app/Console/Commands/holdingfinances.php index 8f7366ab9..b83f2dec1 100644 --- a/app/Console/Commands/holdingfinances.php +++ b/app/Console/Commands/holdingfinances.php @@ -48,16 +48,17 @@ class HoldingFinancesCommand extends Command { //Create the command helper container $task = new CommandHelper('HoldingFinances'); + //Add the entry into the jobs table saying the job is starting $task->SetStartStatus(); //Setup the Finances container $finance = new FinanceHelper(); - //$finance->GetHoldingWalletJournal(1, 93738489); - //$finance->GetWalletJournal(1, 93738489); - + //Get the total pages for the journal for the holding corporation $pages = $finance->GetJournalPageCount(1, 93738489); + + //Dispatch a single job for each page to process for($i = 1; $i <= $pages; $i++) { $job = new JobProcessWalletJournal; $job->division = 1; diff --git a/app/Console/Commands/moonmailer.php b/app/Console/Commands/moonmailer.php index 3e8cfd9c4..911b8f911 100644 --- a/app/Console/Commands/moonmailer.php +++ b/app/Console/Commands/moonmailer.php @@ -3,19 +3,22 @@ namespace App\Console\Commands; use Illuminate\Console\Command; - -use App\Jobs\SendEveMail; use Carbon\Carbon; +use DB; +//Jobs +use App\Jobs\SendEveMailJob; + +//Library use Commands\Library\CommandHelper; use App\Library\Moons\MoonMailer; use App\Library\Moons\MoonCalc; -use DB; +//Models use App\Models\Moon\Moon; use App\Models\Moon\MoonRent; -use App\Models\Mail\EveMail; use App\Models\Mail\SentMail; +use App\Models\Jobs\JobSendEveMail; class MoonMailerCommand extends Command { @@ -99,16 +102,15 @@ class MoonMailerCommand extends Command } //Send a mail to the contact listing for the moon - $mail = new EveMail; + $mail = new JobSendEveMail; $mail->sender = 93738489; $mail->subject = "Moon Rental"; $mail->body = $body; $mail->recipient = (int)$rental->Contact; $mail->recipient_type = 'character'; - //$mail->save(); //Dispatch the job and cycle to the next moon rental - SendEveMail::dispatch($mail); + SendEveMailJob::dispatch($mail); //After the mail is dispatched, saved the sent mail record, $sentmail = new SentMail; diff --git a/app/Console/Commands/pitransactions.php b/app/Console/Commands/pitransactions.php index 83154350e..d024c1973 100644 --- a/app/Console/Commands/pitransactions.php +++ b/app/Console/Commands/pitransactions.php @@ -4,9 +4,16 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +//Library use Commands\Library\CommandHelper; use App\Library\Finances\Helper\FinanceHelper; +//Jobs +use App\Jobs\ProcessWalletJournalJob; + +//Models +use App\Models\Jobs\JobProcessWalletJournal; + class PiTransactionsCommand extends Command { /** @@ -42,13 +49,24 @@ class PiTransactionsCommand extends Command { //Create the command helper container $task = new CommandHelper('PiTransactions'); + //Add the entry into the jobs table saying the job is starting $task->SetStartStatus(); //Setup the Finances container $finance = new FinanceHelper(); - $finance->GetWalletTransaction(3, 94415555); + //Get the total pages for the transactions + $pages = $finance->GetTransactionPageCount(3, 94415555); + + //Dispatch a single job for each page to process + for($i = 1; $i <= $pages; $i++) { + $job = new JobProcessWalletTransaction; + $job->division = 3; + $job->charId = 94415555; + $job->page = $i; + ProcessWalletTransactionJob::dispatch($job); + } //Mark the job as finished $task->SetStopStatus(); diff --git a/app/Jobs/ProcessWallettransactionJob.php b/app/Jobs/ProcessWallettransactionJob.php new file mode 100644 index 000000000..09d4b318d --- /dev/null +++ b/app/Jobs/ProcessWallettransactionJob.php @@ -0,0 +1,76 @@ +division = $pwt->division; + $this->charId = $pwt->charId; + $this->page = $pwt->page; + + $this->delay = 10; + $this->connection = 'database'; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + //Declare the class variables + $finance = new FinanceHelper(); + + $finance->GetWalletTransactionPage($this->division, $this->charId, $this->page); + + //After the job is completed, delete the job + $this->delete(); + } + + /** + * The job failed to process + * + * @param Exception $exception + * @return void + */ + public function failed($exception) { + // Send user notification of the failure, etc. + dd($exception); + } +} diff --git a/app/Jobs/SendEveMailJob.php b/app/Jobs/SendEveMailJob.php new file mode 100644 index 000000000..1804901d3 --- /dev/null +++ b/app/Jobs/SendEveMailJob.php @@ -0,0 +1,113 @@ +body = $mail->body; + $this->recipient = $mail->recipient; + $this->recipient_type = $mail->recipient_type; + $this->subject = $mail->subject; + + $this->connection = 'database'; + } + + /** + * Execute the job. + * Utilized by using SendEveMail::dispatch($mail); + * The model is passed into the dispatch function, then added to the queue + * for processing. + * + * @return void + */ + public function handle() + { + //Retrieve the token for main character to send mails from + $token = EsiToken::where(['character_id'=> 93738489])->get(); + + //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, + ]); + + //Setup the Eseye class + $esi = new Eseye($authentication); + + //Attemp to send the mail + try { + $esi->setBody([ + 'approved_cost' => 0, + 'body' => $this->body, + 'recipients' => [[ + 'recipient_id' => (int)$this->recipient, + 'recipient_type' => $this->recipient_type, + ]], + 'subject' => $this->subject, + ])->invoke('post', '/characters/{character_id}/mail/', [ + 'character_id'=> 93738489, + ]); + } catch(RequestFailedException $e) { + return null; + } + + $this->eveMail->delete(); + } + + /** + * The job failed to process. + * + * @param Exception $exception + * @return void + */ + public function failed($exception) + { + // Send user notification of failure, etc... + dd($exception); + } +} diff --git a/app/Library/Finances/Helper/FinanceHelper.php b/app/Library/Finances/Helper/FinanceHelper.php index 94b9633fb..431e7efad 100644 --- a/app/Library/Finances/Helper/FinanceHelper.php +++ b/app/Library/Finances/Helper/FinanceHelper.php @@ -31,101 +31,127 @@ use Seat\Eseye\Exceptions\RequestFailedException; class FinanceHelper { + public function GetTransactionPageCount($division, $charId) { + //Declare the class variables + $lookups = new LookupHelper; + + //Get the ESI refresh token for the data + $tokenData = $this->TokenInfo($charId); + $token = $tokenData['token']; + $scope = $tokenData['scope']; + + //If the token is not found, send the user an eve mail, and just exit out of the function + if($this->TokenNotFound($token, $scope, $charId)) { + printr("Token not found\n"); + 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); + + //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}/transactions/', [ + 'corporation_id' => 98251577, + 'division' => 3, + ]); + } catch(RequestFailedException $e) { + return $e->getEsiResponse(); + } + + //Return the number of pages needed to be handled + return $journals->pages; + } + public function GetWalletTransaction($division, $charId) { //Declare the lookup class helper $lookups = new LookupHelper; //Setup array for PI items - $pi_items = [ - //R0 Materials - '2073', - '2667', - '2268', - '2270', - '2272', - '2286', - '2287', - '2288', - '2305', - '2306', - '2307', - '2308', - '2309', - '2310', - '2311', - //P1 Materials - '2389', - '2390', - '2392', - '2393', - '2395', - '2396', - '2397', - '2398', - '2399', - '2400', - '2401', - '3645', - '3683', - '3779', - '9828', - //P2 Materials - '44', - '2312', - '2317', - '2319', - '2321', - '2327', - '2328', - '2329', - '2463', - '3689', - '3691', - '3693', - '3695', - '3697', - '3725', - '3775', - '3828', - '9830', - '9832', - '9836', - '9838', - '9840', - '9842', - '15317', - //P3 Materials - '2344', - '2345', - '2346', - '2348', - '2349', - '2351', - '2352', - '2354', - '2358', - '2360', - '2361', - '2366', - '2367', - '9834', - '9846', - '9848', - '12836', - '17136', - '17392', - '17898', - '28974', - //P4 Materials - '2867', - '2868', - '2869', - '2870', - '2871', - '2872', - '2875', - '2876', - ]; + $pi_items = $this->GetPIMaterialsArray(); + + //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']; + + //If the token is not found, send the user an eve mail, and just exit out of the function + if($this->TokenNotFound($token, $scope, $charId)) { + printr("Token not found\n"); + 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); + + //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}/transactions/', [ + 'corporation_id' => 98251577, + 'division' => 3, + ]); + } catch(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($division == 3 && $charId == 94415555) { + if(in_array($entry['type_id'], $pi_items, false)) { + $pi = new PISale(); + $pi->InsertPISale($entry, 98287666); + } + } + } + + //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); + } + + public function GetWalletTransactionPage($division, $charId, $page = 1) { + //Declare the lookup class helper + $lookups = new LookupHelper; + + //Setup array for PI items + $pi_items = $this->GetPIMaterialsArray(); //Get the ESI refresh token for the corporation to add new wallet journals into the database $tokenData = $this->TokenInfo($charId); @@ -423,6 +449,102 @@ class FinanceHelper { return false; } + private function GetPIMaterialsArray() { + //Setup array for PI items + $pi_items = [ + //R0 Materials + '2073', + '2667', + '2268', + '2270', + '2272', + '2286', + '2287', + '2288', + '2305', + '2306', + '2307', + '2308', + '2309', + '2310', + '2311', + //P1 Materials + '2389', + '2390', + '2392', + '2393', + '2395', + '2396', + '2397', + '2398', + '2399', + '2400', + '2401', + '3645', + '3683', + '3779', + '9828', + //P2 Materials + '44', + '2312', + '2317', + '2319', + '2321', + '2327', + '2328', + '2329', + '2463', + '3689', + '3691', + '3693', + '3695', + '3697', + '3725', + '3775', + '3828', + '9830', + '9832', + '9836', + '9838', + '9840', + '9842', + '15317', + //P3 Materials + '2344', + '2345', + '2346', + '2348', + '2349', + '2351', + '2352', + '2354', + '2358', + '2360', + '2361', + '2366', + '2367', + '9834', + '9846', + '9848', + '12836', + '17136', + '17392', + '17898', + '28974', + //P4 Materials + '2867', + '2868', + '2869', + '2870', + '2871', + '2872', + '2875', + '2876', + ]; + + return $pi_items; + } + } ?> diff --git a/app/Models/Jobs/JobProcessWalletJournal.php b/app/Models/Jobs/JobProcessWalletJournal.php index 8a35a79dd..7e82abc94 100644 --- a/app/Models/Jobs/JobProcessWalletJournal.php +++ b/app/Models/Jobs/JobProcessWalletJournal.php @@ -10,7 +10,7 @@ class JobProcessWalletJournal extends Model //public $table = 'job_process_wallet_journal'; //Timestamps - public $timestamps = true; + public $timestamps = false; /** * The attributes that are mass assignable @@ -24,4 +24,4 @@ class JobProcessWalletJournal extends Model ]; } -?> \ No newline at end of file +?> diff --git a/app/Models/Jobs/JobProcessWalletTransaction.php b/app/Models/Jobs/JobProcessWalletTransaction.php new file mode 100644 index 000000000..bd0e8a42d --- /dev/null +++ b/app/Models/Jobs/JobProcessWalletTransaction.php @@ -0,0 +1,24 @@ + diff --git a/app/Models/Jobs/JobSendEveMail.php b/app/Models/Jobs/JobSendEveMail.php new file mode 100644 index 000000000..cee715b2f --- /dev/null +++ b/app/Models/Jobs/JobSendEveMail.php @@ -0,0 +1,24 @@ +