connection = 'redis'; $this->onQueue('miningtaxes'); } /** * Execute the job. * * @return void */ public function handle() { //Declare variables $lookup = new LookupHelper; $config = config('esi'); $mainAlts = array(); $mailDelay = 15; $mainIds = new Collection; //Get all of the users in the database $users = User::all(); //Get a list of the alts for each character, then process the ledgers and combine them to send one mail out //in this first part foreach($users as $char) { //Gather up all of the ledgers from the character and its alts. $ledgers = $this->LedgersWithAlts($char->character_id); //If the size of the ledgers array is greater than 0, then we need to invoice the ledgers if(sizeof($ledgers) > 0) { //Create an invoice from the ledger rows $this->CreateInvoice($char->character_id, $ledgers, $mailDelay); } } //Get the ledgers characters which haven't had an invoice created yet. $charIds = Ledger::where([ 'invoiced' => 'No', ])->distinct('character_id') ->pluck('character_id'); if($charIds == null) { return 0; } $this->CreateOtherInvoices($charIds, $mailDelay); return 0; } private function CreateOtherInvoices($charIds, $mailDelay) { foreach($charIds as $charId) { $invoice = array(); $ores = array(); $totalPrice = 0.00; $body = null; $lookup = new LookupHelper; $config = config('esi'); //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(sizeof($rows) > 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, 'invoiced' => 'No', ])->update([ 'invoiced' => 'Yes', 'invoice_id' => $invoiceId, ]); //update the delay $mailDelay = $mailDelay + 20; } } } private function CreateInvoice($charId, $ledgers, &$mailDelay) { $invoice = array(); $ores = array(); $characters = array(); $characterIds = array(); $totalPrice = 0.00; $body = null; $lookup = new LookupHelper; $config = config('esi'); if(sizeof($ledgers) > 0) { foreach($ledgers as $ledger) { //Create the ores array indexes, then totalize the ores from each ledger entry if(!isset($ores[$ledger['type_id']])) { $ores[$ledger['type_id']] = 0; } $ores[$ledger['type_id']] = $ores[$ledger['type_id']] + $ledger['quantity']; $totalPrice = $totalPrice + $ledger['amount']; //Create a list of character names for the mail body if(!isset($characters[$ledger['character_name']])) { $characters[$ledger['character_name']] = $ledger['character_name']; } //Create a list of character id's to update invoices before the end of the function if(!isset($characterIds[$ledger['character_id']])) { $characterIds[$ledger['character_id']] = $ledger['character_id']; } } $invoiceAmount = round(($totalPrice * $config['mining_tax']), 2); $charName = $lookup->CharacterIdToName($charId); $invoiceId = "M" . uniqid(); $dateDue = Carbon::now()->addDays(7); $invoiceDate = Carbon::now(); $numberMiningTax = number_format(($config['mining_tax'] * 100.00), 2, ".", ","); //Create the mail body $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)); //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(); //Mark the invoices as paid foreach($characterIds as $charId) { Ledger::where([ 'character_id' => $charId, 'invoiced' => 'No', ])->update([ 'invoiced' => 'Yes', 'invoice_id' => $invoiceId, ]); } //Increase the delay for the next mail $mailDelay += 20; } } private function LedgersWithAlts($charId) { $ledgers = array(); $alts = UserAlt::where([ 'main_id' => $charId, ])->get(); $altCount = UserAlt::where([ 'main_id' => $charId, ])->count(); $rows = Ledger::where([ 'character_id' => $charId, 'invoiced' => 'No', ])->get(); $mainLedgerCount = Ledger::where([ 'character_id' => $charId, 'invoiced' => 'No', ])->count(); if($mainLedgerCount > 0) { foreach($rows as $row) { array_push($ledgers, [ 'character_id' => $row->character_id, 'character_name' => $row->character_name, 'observer_id' => $row->observer_id, 'last_updated' => $row->last_updated, 'type_id' => $row->type_id, 'ore_name' => $row->ore_name, 'quantity' => $row->quantity, 'amount' => $row->amount, 'invoiced' => $row->invoiced, 'invoice_id' => $row->invoice_id, ]); } } //If the alt count is greater than zero, let's add the alt's ledgers to the ledger array if($altCount > 0) { //Run through all of the alts and add the individual ledger rows to the ledger array foreach($alts as $alt) { //Check to make sure we aren't adding the main character twice if($alt->character_id != $charId){ //Get the count of the ledgers to make sure something is actually there to process $rowCount = Ledger::where([ 'character_id' => $alt->character_id, 'invoiced' => 'No', ])->count(); //If there are rows to process, get the rows, and add to the array if($rowCount > 0) { //Get all of the rows $rows = Ledger::where([ 'character_id' => $alt->character_id, 'invoiced' => 'No', ])->get(); //Add all of the rows to the ledger array foreach($rows as $row) { array_push($ledgers, [ 'character_id' => $row->character_id, 'character_name' => $row->character_name, 'observer_id' => $row->observer_id, 'last_updated' => $row->last_updated, 'type_id' => $row->type_id, 'ore_name' => $row->ore_name, 'quantity' => $row->quantity, 'amount' => $row->amount, 'invoiced' => $row->invoiced, 'invoice_id' => $row->invoice_id, ]); } } } } } //Return the ledgers return $ledgers; } /** * Set the tags for Horzion * * @var array */ public function tags() { return ['MiningTaxes', 'SendMiningTaxesInvoices', 'Invoices']; } }