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() {
$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);
}
ini_set('memory_limit', $memory_limit);
ini_set('max_execution_time', $time_limit);
//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();
return view('test.miningtax.observers')->with('ledgers', $ledgers)
->with('observers', $observers);
//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,
]);
}
}
}
}
}
dd($ledgers);
}
}

View File

@@ -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,71 +61,147 @@ 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;
//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.
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 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();
//Get the row count to reduce overhead processing if we can.
$rowCount = Ledger::where([
'character_id' => $charId,
//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();
//Get the actual rows
$rows = Ledger::where([
'character_id' => $charId,
if($altLedgerCount > 0) {
$altLedgers = Ledger::where([
'character_id' => 'No',
'invoiced' => 'No',
])->get()->toArray();
])->get();
//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;
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,
]);
}
}
}
$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);
/**
* Send the collected information over to the function to send the actual mail
*/
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);
//Generate a unique invoice id
$invoiceId = "M" uniqid();
//Set the due date of the invoice
//Create the date due and the invoice date
$dateDue = Carbon::now()->addDays(7);
$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, ".", ",");
//Create the mail body
$body .= "Dear Miner,<br><br>";
//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 . ",<br><br>";
$body .= "Mining Taxes are due for the following ores mined from alliance moons: <br>";
foreach($ores as $ore => $quantity) {
$oreName = $lookup->ItemIdToName($ore);
@@ -142,15 +219,20 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue
$body .= $oreName . ": " . number_format(round($quantity * $config['mining_tax']), 0, ".", ",") . "<br>";
}
$body .= "<br>";
$body .= "Characters Processed: <br>";
foreach($characters as $character) {
$body .= $character . "<br>";
}
$body .= "<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
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 .= "<br><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 .= "<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
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->character_id = $charId;
$invoice->character_name = $charName;
@@ -178,18 +262,31 @@ class SendMiningTaxesInvoicesOld implements ShouldQueue
$invoice->mail_body = $body;
$invoice->save();
//Update the ledger entries
/**
* Mark the invoices as paid
*/
foreach($characterIds as $char) {
Ledger::where([
'character_id' => $charId,
'character_id' => $char,
'invoiced' => 'No',
])->update([
'invoice' => $invoiceId,
'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'];
}
}