diff --git a/app/Http/Controllers/Test/TestController.php b/app/Http/Controllers/Test/TestController.php index fed3811b4..895f0fa0b 100644 --- a/app/Http/Controllers/Test/TestController.php +++ b/app/Http/Controllers/Test/TestController.php @@ -73,60 +73,85 @@ class TestController extends Controller } public function DebugMiningObservers() { - $ledgers = array(); - $lookup = new LookupHelper; - $config = config('esi'); - $esiHelper = new Esi; + //Declare variables + $mailDelay = 15; - $time_limit = ini_get('max_execution_time'); - $memory_limit = ini_get('memory_limit'); + //Get the users from the database + $mains = User::all(); - ini_set('memory_limit', -1); - ini_set('max_execution_time', 600); + /** + * For each of the users, let's determine if there are any ledgers, + * then determine if there are any alts and ledgers associated with the alts. + */ + foreach($mains as $main) { + //Declare some variables for each run through the for loop + $ledgers = new Collection; + $mainLedgers = new Collection; + $mainLedgerCount = 0; + $altLedgers = new Collection; + $alts = new Collection; - $refreshToken = $esiHelper->GetRefreshToken($config['primary']); - $esi = $esiHelper->SetupEsiAuthentication($refreshToken); + //Count the ledgers for the main + $mainLedgerCount = Ledger::where([ + 'character_id' => $main->character_id, + ])->count(); - $observers = [1035697195484, 1035697216662]; + //If there are ledgers for the main, then let's grab them + if($mainLedgerCount > 0) { + $mainLedgers = Ledger::where([ + 'character_id' => $main->character_id, + 'invoiced' => 'No', + ])->get(); - $currentPage = 1; - $totalPages = 1; - - foreach($observers as $observer) { - do { - if($esiHelper->TokenExpired($refreshToken)) { - $refreshToken = $esiHelper->GetRefreshToken($config['primary']); - $esi = $esiHelper->SetupEsiAuthentication($refreshToken); - } - - $response = $esi->page($currentPage) - ->invoke('get', '/corporation/{corporation_id}/mining/observers/{observer_id}', [ - 'corporation_id' => $config['corporation'], - 'observer_id' => $observer['observer_id'], - ]); - - if($currentPage == 1) { - $totalPages = $response->pages; - } - - $tempLedgers = json_decode($response->raw, true); - - foreach($tempLedgers as $ledg) { - array_push($ledgers, [ - 'observer_id' => $observer['observer_id'], - 'character_id' => $ledg['character_id'], - 'last_updated' => $ledg['last_updated'], - 'type_id' => $ledg['type_id'], - 'quantity' => $ledg['quantity'], + //Cycle through the entries, and add them to the ledger to send with the invoice + foreach($mainLedgers as $row) { + $ledgers->push([ + 'character_id' => $row->character_id, + 'character_name' => $row->character_name, + 'type_id' => $row->type_id, + 'ore_name' => $row->ore_name, + 'quantity' => $row->quantity, + 'amount' => $row->amount, ]); } - } while($currentPage <= $totalPages); + } + + //Get the alt count for the main character + $altCount = $main->altCount(); + //If more than 0 alts, grab all the alts. + if($altCount > 0) { + $alts = UserAlt::where([ + 'main_id' => $main->character_id, + ])->get(); + + //Cycle through the alts, and get the ledgers, and push onto the stack + foreach($alts as $alt) { + $altLedgerCount = Ledger::where([ + 'character_id' => 'No', + 'invoiced' => 'No', + ])->count(); + + if($altLedgerCount > 0) { + $altLedgers = Ledger::where([ + 'character_id' => 'No', + 'invoiced' => 'No', + ])->get(); + + foreach($altLedgers as $row) { + $ledgers->push([ + 'character_id' => $row->character_id, + 'character_name' => $row->character_name, + 'type_id' => $row->type_id, + 'ore_name' => $row->ore_name, + 'quantity' => $row->quantity, + 'amount' => $row->amount, + ]); + } + } + } + } } - ini_set('memory_limit', $memory_limit); - ini_set('max_execution_time', $time_limit); - - return view('test.miningtax.observers')->with('ledgers', $ledgers) - ->with('observers', $observers); + dd($ledgers); } } diff --git a/app/Jobs/Commands/MiningTaxes/SendMiningTaxesInvoicesOld.php b/app/Jobs/Commands/MiningTaxes/SendMiningTaxesInvoicesOld.php index 58b45dd04..8c169184a 100644 --- a/app/Jobs/Commands/MiningTaxes/SendMiningTaxesInvoicesOld.php +++ b/app/Jobs/Commands/MiningTaxes/SendMiningTaxesInvoicesOld.php @@ -9,6 +9,8 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Log; use Carbon\Carbon; +use Illuminate\Support\Collection; +use Illuminate\Support\Str; //Application Library use App\Library\Helpers\LookupHelper; @@ -22,7 +24,6 @@ use App\Models\User\User; //Jobs use App\Jobs\Commands\Eve\SendEveMail; - class SendMiningTaxesInvoicesOld implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -35,11 +36,11 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue public $timeout = 3600; /** - * Retries + * Number of job retries * * @var int */ - public $retries = 3; + public $tries = 3; /** * Create a new job instance. @@ -60,136 +61,232 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue public function handle() { //Declare variables - $lookup = new LookupHelper; - $config = config('esi'); $mailDelay = 15; - //Get the characters for each non-invoiced ledger entry - $charIds = Ledger::where([ - 'invoiced' => 'No', - ])->distinct('character_id') - ->pluck('character_id'); + //Get the users from the database + $mains = User::all(); - if($charIds == null) { - Log::warning("No characters found to send invoices to in MiningTaxesInvoices Command."); - return 0; - } + /** + * For each of the users, let's determine if there are any ledgers, + * then determine if there are any alts and ledgers associated with the alts. + */ + foreach($mains as $main) { + //Declare some variables for each run through the for loop + $ledgers = new Collection; + $mainLedgers = new Collection; + $mainLedgerCount = 0; + $altLedgers = new Collection; + $alts = new Collection; - //Foreach character tally up the mining ledger. - foreach($charIds as $charId) { - //Declare some variables we need for each iteration of the loop - $invoice = array(); - $ores = array(); - $totalPrice = 0.00; - $body = null; - - //Get the row count to reduce overhead processing if we can. - $rowCount = Ledger::where([ - 'character_id' => $charId, - 'invoiced' => 'No', + //Count the ledgers for the main + $mainLedgerCount = Ledger::where([ + 'character_id' => $main->character_id, ])->count(); - - //Get the actual rows - $rows = Ledger::where([ - 'character_id' => $charId, - 'invoiced' => 'No', - ])->get()->toArray(); - //Taly up the item composition from each row and multiply by the quantity - if($rowCount > 0) { - foreach($rows as $row) { - if(!isset($ores[$row['type_id']])) { - $ores[$row['type_id']] = 0; - } - $ores[$row['type_id']] = $ores[$row['type_id']] + $row['quantity']; - - //Add up the total price from the ledger rows for the report later - $totalPrice = $totalPrice + $row['amount']; - } - - //Reduce the total price by the take percentage - $invoiceAmount = $totalPrice * $config['mining_tax']; - $invoiceAmount = round($invoiceAmount, 2); - - //Get the character name from the character id - $charName = $lookup->CharacterIdToName($charId); - - //Generate a unique invoice id - $invoiceId = "M" uniqid(); - //Set the due date of the invoice - $dateDue = Carbon::now()->addDays(7); - $invoiceDate = Carbon::now(); - - //Format the mining tax into a human readable number - $numberMiningTax = number_format(($config['mining_tax'] * 100.00), 2, ".", ","); - - //Create the mail body - $body .= "Dear Miner,

"; - $body .= "Mining Taxes are due for the following ores mined from alliance moons:
"; - foreach($ores as $ore => $quantity) { - $oreName = $lookup->ItemIdToName($ore); - $body .= $oreName . ": " . number_format($quantity, 0, ".", ",") . "
"; - } - $body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK."; - $body .= "

"; - $body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "
"; - $body .= "Set the reason for transfer as " . $invoiceId . "
"; - $body .= "The mining taxes are currently set to " . $numberMiningTax . "%.
"; - $body .= "

"; - $body .= "You can also send a contract with the following ores in the contract with the reason set as: " . $invoiceId . "
"; - foreach($ores as $ore => $quantity) { - $oreName = $lookup->ItemIdToName($ore); - $body .= $oreName . ": " . number_format(round($quantity * $config['mining_tax']), 0, ".", ",") . "
"; - } - $body .= "
"; - $body .= "
Sincerely,
Warped Intentions Leadership
"; - - //Check if the mail body is greater than 2000 characters. If greater than 2,000 characters, then - if(strlen($body) > 2000) { - $body = "Dear Miner,

"; - $body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK."; - $body .= "

"; - $body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "
"; - $body .= "Set the reason for transfer as " . $invoiceId . "
"; - $body .= "The mining taxes are currently set to " . $numberMiningTax . "%.
"; - $body .= "
"; - $body .= "
Sincerely,
Warped Intentions Leadership
"; - } - - //Mail the invoice to the character if the character is in - //Warped Intentions or Legacy - $subject = 'Warped Intentions Mining Taxes'; - $sender = $config['primary']; - $recipientType = 'character'; - $recipient = $charId; - - //Send the Eve Mail Job to the queue to be dispatched - SendEveMail::dispatch($body, $recipient, $recipientType, $subject, $sender)->delay(Carbon::now()->addSeconds($mailDelay)); - - //Save the invoice model - $invoice = new Invoice; - $invoice->character_id = $charId; - $invoice->character_name = $charName; - $invoice->invoice_id = $invoiceId; - $invoice->invoice_amount = $invoiceAmount; - $invoice->date_issued = $invoiceDate; - $invoice->date_due = $dateDue; - $invoice->status = 'Pending'; - $invoice->mail_body = $body; - $invoice->save(); - - //Update the ledger entries - Ledger::where([ - 'character_id' => $charId, + //If there are ledgers for the main, then let's grab them + if($mainLedgerCount > 0) { + $mainLedgers = Ledger::where([ + 'character_id' => $main->character_id, 'invoiced' => 'No', - ])->update([ - 'invoiced' => 'Yes', - 'invoice_id' => $invoiceId, - ]); - - //update the delay - $mailDelay = $mailDelay + 20; + ])->get(); + + //Cycle through the entries, and add them to the ledger to send with the invoice + foreach($mainLedgers as $row) { + $ledgers->push([ + 'character_id' => $row->character_id, + 'character_name' => $row->character_name, + 'type_id' => $row->type_id, + 'ore_name' => $row->ore_name, + 'quantity' => $row->quantity, + 'amount' => $row->amount, + ]); + } + } + + //Get the alt count for the main character + $altCount = $main->altCount(); + //If more than 0 alts, grab all the alts. + if($altCount > 0) { + $alts = UserAlt::where([ + 'main_id' => $main->character_id, + ])->get(); + + //Cycle through the alts, and get the ledgers, and push onto the stack + foreach($alts as $alt) { + $altLedgerCount = Ledger::where([ + 'character_id' => 'No', + 'invoiced' => 'No', + ])->count(); + + if($altLedgerCount > 0) { + $altLedgers = Ledger::where([ + 'character_id' => 'No', + 'invoiced' => 'No', + ])->get(); + + foreach($altLedgers as $row) { + $ledgers->push([ + 'character_id' => $row->character_id, + 'character_name' => $row->character_name, + 'type_id' => $row->type_id, + 'ore_name' => $row->ore_name, + 'quantity' => $row->quantity, + 'amount' => $row->amount, + ]); + } + } + } + } + + /** + * Send the collected information over to the function to send the actual mail + */ + if($ledgers->count() > 0) { + $this->CreateInvoice($main->character_id, $ledgers, $mailDelay); } } } + + /** + * Create the invoice to the mail out + * + * @var charId + * @var ledgers + * @var mailDelay + */ + private function CreateInvoice($charId, Collection $ledgers, int &$mailDelay) { + $ores = array(); + $characters = array(); + $characterIds = array(); + $totalPrice = 0.00; + $body = null; + $lookup = new LookupHelper; + $config = config('esi'); + + //Create an invoice id + $invoiceId = "M" . uniqid(); + + //Get the sum of all the ledgers + $invoiceAmount = round(($ledgers->sum('amount') * $config['mining_tax']), 2); + + //Get the character name from the lookup table + $charName = $lookup->CharacterIdToName($charId); + + //Create the date due and the invoice date + $dateDue = Carbon::now()->addDays(7); + $invoiceDate = Carbon::now(); + + //Set the mining tax from the config file + $numberMiningTax = number_format(($config['mining_tax'] * 100.00), 2, ".", ","); + + //Create the list of ores to put in the mail + $temp = $ledgers->toArray(); + foreach($temp as $t) { + //If the key isn't set, set it to the default of 0 + if(!isset($ores[$t['type_id']])) { + $ores[$t['type_id']] = 0; + } + //Add the quantity into the ores array + $ores[$t['type_id']] += $t['quantity']; + + if(!isset($characters[$t['character_name']])) { + $characters[$t['character_name']] = $t['character_name']; + } + + if(!isset($characterIds[$t['character_id']])) { + $characterIds[$t['character_id']] = $t['character_id']; + } + } + + /** + * Create the mail body to send to the main character + */ + $body .= "Dear " . $charName . ",

"; + $body .= "Mining Taxes are due for the following ores mined from alliance moons:
"; + foreach($ores as $ore => $quantity) { + $oreName = $lookup->ItemIdToName($ore); + $body .= $oreName . ": " . number_format($quantity, 0, ".", ",") . "
"; + } + $body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK."; + $body .= "

"; + $body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "
"; + $body .= "Set the reason for transfer as " . $invoiceId . "
"; + $body .= "The mining taxes are currently set to " . $numberMiningTax . "%.
"; + $body .= "

"; + $body .= "You can also send a contract with the following ores in the contract with the reason set as: " . $invoiceId . "
"; + foreach($ores as $ore => $quantity) { + $oreName = $lookup->ItemIdToName($ore); + $body .= $oreName . ": " . number_format(round($quantity * $config['mining_tax']), 0, ".", ",") . "
"; + } + $body .= "
"; + $body .= "Characters Processed:
"; + foreach($characters as $character) { + $body .= $character . "
"; + } + $body .= "
"; + $body .= "
Sincerely,
Warped Intentions Leadership
"; + + //Check if the mail body is greater than 2000 characters. If greater than 2,000 characters, then + if(strlen($body) > 2000) { + $body = "Dear " . $charName . "

"; + $body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK."; + $body .= "

"; + $body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "
"; + $body .= "Set the reason for transfer as: " . $invoiceId . "
"; + $body .= "The mining taxes are currently set to " . $numberMiningTax . "%.
"; + $body .= "
"; + $body .= "
Sincerely,
Warped Intentions Leadership
"; + } + + //Mail the invoice to the character if the character is in + //Warped Intentions or Legacy + $subject = 'Warped Intentions Mining Taxes'; + $sender = $config['primary']; + $recipientType = 'character'; + $recipient = $charId; + + //Send the Eve Mail Job to the queue to be dispatched + SendEveMail::dispatch($body, $recipient, $recipientType, $subject, $sender)->delay(Carbon::now()->addSeconds($mailDelay)); + + /** + * Create a new invoice model, and save it to the database + */ + $invoice = new Invoice; + $invoice->character_id = $charId; + $invoice->character_name = $charName; + $invoice->invoice_id = $invoiceId; + $invoice->invoice_amount = $invoiceAmount; + $invoice->date_issued = $invoiceDate; + $invoice->date_due = $dateDue; + $invoice->status = 'Pending'; + $invoice->mail_body = $body; + $invoice->save(); + + /** + * Mark the invoices as paid + */ + foreach($characterIds as $char) { + Ledger::where([ + 'character_id' => $char, + 'invoiced' => 'No', + ])->update([ + 'invoice' => $invoiceId, + 'invoiced' => 'Yes', + ]); + } + + /** + * Increment the mail delay for the next cycle + */ + $mailDelay += 20; + } + + /** + * Set the tags for Horizon + * + * @var array + */ + public function tags() { + return ['MiningTaxes', 'SendMiningTaxesInvoics', 'Invoices']; + } }