created simplified mining tax invoice

This commit is contained in:
2021-05-25 00:22:42 +09:00
parent af7c7344b4
commit 2cf1f7ff72
2 changed files with 293 additions and 171 deletions

View File

@@ -73,60 +73,85 @@ class TestController extends Controller
} }
public function DebugMiningObservers() { public function DebugMiningObservers() {
$ledgers = array(); //Declare variables
$lookup = new LookupHelper; $mailDelay = 15;
$config = config('esi');
$esiHelper = new Esi;
$time_limit = ini_get('max_execution_time'); //Get the users from the database
$memory_limit = ini_get('memory_limit'); $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']); //Count the ledgers for the main
$esi = $esiHelper->SetupEsiAuthentication($refreshToken); $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; //Cycle through the entries, and add them to the ledger to send with the invoice
$totalPages = 1; foreach($mainLedgers as $row) {
$ledgers->push([
foreach($observers as $observer) { 'character_id' => $row->character_id,
do { 'character_name' => $row->character_name,
if($esiHelper->TokenExpired($refreshToken)) { 'type_id' => $row->type_id,
$refreshToken = $esiHelper->GetRefreshToken($config['primary']); 'ore_name' => $row->ore_name,
$esi = $esiHelper->SetupEsiAuthentication($refreshToken); 'quantity' => $row->quantity,
} 'amount' => $row->amount,
$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'],
]); ]);
} }
} while($currentPage <= $totalPages);
} }
ini_set('memory_limit', $memory_limit); //Get the alt count for the main character
ini_set('max_execution_time', $time_limit); $altCount = $main->altCount();
//If more than 0 alts, grab all the alts.
if($altCount > 0) {
$alts = UserAlt::where([
'main_id' => $main->character_id,
])->get();
return view('test.miningtax.observers')->with('ledgers', $ledgers) //Cycle through the alts, and get the ledgers, and push onto the stack
->with('observers', $observers); 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,
]);
}
}
}
}
}
dd($ledgers);
} }
} }

View File

@@ -9,6 +9,8 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Log; use Log;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
//Application Library //Application Library
use App\Library\Helpers\LookupHelper; use App\Library\Helpers\LookupHelper;
@@ -22,7 +24,6 @@ use App\Models\User\User;
//Jobs //Jobs
use App\Jobs\Commands\Eve\SendEveMail; use App\Jobs\Commands\Eve\SendEveMail;
class SendMiningTaxesInvoicesOld implements ShouldQueue class SendMiningTaxesInvoicesOld implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@@ -35,11 +36,11 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue
public $timeout = 3600; public $timeout = 3600;
/** /**
* Retries * Number of job retries
* *
* @var int * @var int
*/ */
public $retries = 3; public $tries = 3;
/** /**
* Create a new job instance. * Create a new job instance.
@@ -60,71 +61,147 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue
public function handle() public function handle()
{ {
//Declare variables //Declare variables
$lookup = new LookupHelper;
$config = config('esi');
$mailDelay = 15; $mailDelay = 15;
//Get the characters for each non-invoiced ledger entry //Get the users from the database
$charIds = Ledger::where([ $mains = User::all();
'invoiced' => 'No',
])->distinct('character_id')
->pluck('character_id');
if($charIds == null) { /**
Log::warning("No characters found to send invoices to in MiningTaxesInvoices Command."); * For each of the users, let's determine if there are any ledgers,
return 0; * 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;
//Count the ledgers for the main
$mainLedgerCount = Ledger::where([
'character_id' => $main->character_id,
])->count();
//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();
//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,
]);
}
} }
//Foreach character tally up the mining ledger. //Get the alt count for the main character
foreach($charIds as $charId) { $altCount = $main->altCount();
//Declare some variables we need for each iteration of the loop //If more than 0 alts, grab all the alts.
$invoice = array(); if($altCount > 0) {
$ores = array(); $alts = UserAlt::where([
$totalPrice = 0.00; 'main_id' => $main->character_id,
$body = null; ])->get();
//Get the row count to reduce overhead processing if we can. //Cycle through the alts, and get the ledgers, and push onto the stack
$rowCount = Ledger::where([ foreach($alts as $alt) {
'character_id' => $charId, $altLedgerCount = Ledger::where([
'character_id' => 'No',
'invoiced' => 'No', 'invoiced' => 'No',
])->count(); ])->count();
//Get the actual rows if($altLedgerCount > 0) {
$rows = Ledger::where([ $altLedgers = Ledger::where([
'character_id' => $charId, 'character_id' => 'No',
'invoiced' => 'No', 'invoiced' => 'No',
])->get()->toArray(); ])->get();
//Taly up the item composition from each row and multiply by the quantity foreach($altLedgers as $row) {
if($rowCount > 0) { $ledgers->push([
foreach($rows as $row) { 'character_id' => $row->character_id,
if(!isset($ores[$row['type_id']])) { 'character_name' => $row->character_name,
$ores[$row['type_id']] = 0; 'type_id' => $row->type_id,
'ore_name' => $row->ore_name,
'quantity' => $row->quantity,
'amount' => $row->amount,
]);
}
}
} }
$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']; * Send the collected information over to the function to send the actual mail
$invoiceAmount = round($invoiceAmount, 2); */
if($ledgers->count() > 0) {
$this->CreateInvoice($main->character_id, $ledgers, $mailDelay);
}
}
}
//Get the character name from the character id /**
* 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); $charName = $lookup->CharacterIdToName($charId);
//Generate a unique invoice id //Create the date due and the invoice date
$invoiceId = "M" uniqid();
//Set the due date of the invoice
$dateDue = Carbon::now()->addDays(7); $dateDue = Carbon::now()->addDays(7);
$invoiceDate = Carbon::now(); $invoiceDate = Carbon::now();
//Format the mining tax into a human readable number //Set the mining tax from the config file
$numberMiningTax = number_format(($config['mining_tax'] * 100.00), 2, ".", ","); $numberMiningTax = number_format(($config['mining_tax'] * 100.00), 2, ".", ",");
//Create the mail body //Create the list of ores to put in the mail
$body .= "Dear Miner,<br><br>"; $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 . ",<br><br>";
$body .= "Mining Taxes are due for the following ores mined from alliance moons: <br>"; $body .= "Mining Taxes are due for the following ores mined from alliance moons: <br>";
foreach($ores as $ore => $quantity) { foreach($ores as $ore => $quantity) {
$oreName = $lookup->ItemIdToName($ore); $oreName = $lookup->ItemIdToName($ore);
@@ -142,15 +219,20 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue
$body .= $oreName . ": " . number_format(round($quantity * $config['mining_tax']), 0, ".", ",") . "<br>"; $body .= $oreName . ": " . number_format(round($quantity * $config['mining_tax']), 0, ".", ",") . "<br>";
} }
$body .= "<br>"; $body .= "<br>";
$body .= "Characters Processed: <br>";
foreach($characters as $character) {
$body .= $character . "<br>";
}
$body .= "<br>";
$body .= "<br>Sincerely,<br>Warped Intentions Leadership<br>"; $body .= "<br>Sincerely,<br>Warped Intentions Leadership<br>";
//Check if the mail body is greater than 2000 characters. If greater than 2,000 characters, then //Check if the mail body is greater than 2000 characters. If greater than 2,000 characters, then
if(strlen($body) > 2000) { if(strlen($body) > 2000) {
$body = "Dear Miner,<br><br>"; $body = "Dear " . $charName . "<br><br>";
$body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK."; $body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK.";
$body .= "<br><br>"; $body .= "<br><br>";
$body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "<br>"; $body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "<br>";
$body .= "Set the reason for transfer as " . $invoiceId . "<br>"; $body .= "Set the reason for transfer as: " . $invoiceId . "<br>";
$body .= "The mining taxes are currently set to " . $numberMiningTax . "%.<br>"; $body .= "The mining taxes are currently set to " . $numberMiningTax . "%.<br>";
$body .= "<br>"; $body .= "<br>";
$body .= "<br>Sincerely,<br>Warped Intentions Leadership<br>"; $body .= "<br>Sincerely,<br>Warped Intentions Leadership<br>";
@@ -166,7 +248,9 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue
//Send the Eve Mail Job to the queue to be dispatched //Send the Eve Mail Job to the queue to be dispatched
SendEveMail::dispatch($body, $recipient, $recipientType, $subject, $sender)->delay(Carbon::now()->addSeconds($mailDelay)); SendEveMail::dispatch($body, $recipient, $recipientType, $subject, $sender)->delay(Carbon::now()->addSeconds($mailDelay));
//Save the invoice model /**
* Create a new invoice model, and save it to the database
*/
$invoice = new Invoice; $invoice = new Invoice;
$invoice->character_id = $charId; $invoice->character_id = $charId;
$invoice->character_name = $charName; $invoice->character_name = $charName;
@@ -178,18 +262,31 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue
$invoice->mail_body = $body; $invoice->mail_body = $body;
$invoice->save(); $invoice->save();
//Update the ledger entries /**
* Mark the invoices as paid
*/
foreach($characterIds as $char) {
Ledger::where([ Ledger::where([
'character_id' => $charId, 'character_id' => $char,
'invoiced' => 'No', 'invoiced' => 'No',
])->update([ ])->update([
'invoice' => $invoiceId,
'invoiced' => 'Yes', 'invoiced' => 'Yes',
'invoice_id' => $invoiceId,
]); ]);
}
//update the delay /**
$mailDelay = $mailDelay + 20; * Increment the mail delay for the next cycle
} */
} $mailDelay += 20;
}
/**
* Set the tags for Horizon
*
* @var array
*/
public function tags() {
return ['MiningTaxes', 'SendMiningTaxesInvoics', 'Invoices'];
} }
} }