From 14a37e869c321b51f78a293142c7bac340687b4b Mon Sep 17 00:00:00 2001 From: drkthunder02 Date: Thu, 25 Feb 2021 21:39:56 +0900 Subject: [PATCH] mining taxes invoices --- .../MiningTaxes/MiningTaxesInvoices.php | 92 ++++++++++++++++++- .../MiningTaxes/CalculateMiningTaxesJob.php | 3 - config/esi.php | 3 +- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php b/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php index 37bd54473..53e95bec5 100644 --- a/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php +++ b/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php @@ -5,12 +5,19 @@ namespace App\Console\Commands\MiningTaxes; //Internal Library use Illuminate\Console\Command; use Log; +use Carbon\Carbon; //Application Library use Commands\Library\CommandHelper; +use App\Library\Helpers\LookupHelper; + +//Models +use App\Models\MiningTaxes\Invoice; +use App\Models\MiningTaxes\Ledger; //Jobs -use App\Jobs\Commands\MiningTaxes\CalculateMiningTaxesJob; +//use App\Jobs\Commands\MiningTaxes\CalculateMiningTaxesJob; +use App\Jobs\Commands\Eve\ProcessSendEveMailJob; class MiningTaxesInvoices extends Command { @@ -45,11 +52,92 @@ class MiningTaxesInvoices extends Command */ public function handle() { + //Declare variables + $lookup = new LookupHelper; + $config = config('esi'); + $body = null; $task = new CommandHelper('MiningTaxesInvoices'); //Set the task as started $task->SetStartStatus(); - CalculateMiningTaxesJob::dispatch()->onQueue('miningtaxes'); + //Get the characters for each non-invoiced ledger entry + $chars = Ledger::distinct('character_id')->pluck('character_id'); + + //Foreach character tally up the mining ledger. + foreach($chars as $char) { + //Declare some variables we need for each iteration of the loop + $invoice = array(); + $ores = array(); + $totalPrice = 0.00; + //Get the rows from the database for each character and the requirement of not been + //invoiced yet + $rows = Ledger::where([ + 'character_id' => $char->character_id, + 'invoiced' => 'No', + ])->get(); + + //Taly up the item composition from each row and multiply by the quantity + foreach($rows as $row) { + $ores[$row->type_id] = $ores[$row->type_id] + $row->quantity; + } + + //Add up the total price from the ledger rows + foreach($rows as $row) { + $totalPrice = $totalPrice + $row->price; + } + + //Reduce the total price by the take percentage + $totalPrice = $totalPrice * $config['mining_tax']; + + //Get the character name from the character id + $charName = $lookup->CharacterIdToName($char); + + //Generate a unique invoice id + $invoiceId = uniqid(); + + //Save the invoice model + $invoice = new Invoice; + $invoice->character_id = $char; + $invoice->character_name = $charName; + $invoice->invoice_id = $invoiceId; + $invoice->invoice_amount = $totalPrice; + $invoice->date_issued = Carbon::now(); + $invoice->date_due = Carbon::now()->addDays(7); + $invoice->status = 'Pending'; + $invoice->save(); + + //Update the ledger entries + Ledger::where([ + 'character_id' => $char, + 'invoiced' => 'No', + ])->update([ + 'invoiced' => 'Yes', + 'invoice_id' => $invoiceId, + ]); + + //Create the mail body + $body .= "Dear Miner,

"; + $body .= "Mining Taxes are due for the following ores mined from alliance moons:
"; + foreach($rows as $ore => $quantity) { + $oreName = $lookup->ItemIdToName($ore); + $body .= $oreName . ": " . number_format($quantity, 0, ".", ",") . "
"; + } + $body .= "Please remit " . number_format($totalPrice, 2, ".", ",") . " ISK to Spatial Forces by " . $invoice->date_due . "
"; + $body .= "Set the reason for transfer as MMT: " . $invoice->invoice_id . "
"; + $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 = $config['primary']; + + //Send the Eve Mail Job to the queue to be dispatched + ProcessSendEveMailJob::dispatch($body, $recipient, $recipientType, $subject, $sender)->onQueue('mail'); + } + + //CalculateMiningTaxesJob::dispatch()->onQueue('miningtaxes'); //Set the task as stopped $task->SetStopStatus(); diff --git a/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php b/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php index e3279190a..72de3eb23 100644 --- a/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php +++ b/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php @@ -11,11 +11,8 @@ use Illuminate\Queue\SerializesModels; //Internal Library use App\Library\Helpers\LookupHelper; -use App\Library\Moons\MoonCalc; //Models -use App\Models\Moon\ItemComposition; -use App\Models\Moon\MineralPrice; use App\Models\MiningTax\Ledger; use App\Models\MiningTax\Invoice; diff --git a/config/esi.php b/config/esi.php index 2b80c3d90..46b2ae3fc 100644 --- a/config/esi.php +++ b/config/esi.php @@ -6,6 +6,7 @@ 'callback' => env('ESI_CALLBACK_URI'), 'primary' => env('ESI_PRIMARY_CHAR', 93738489), 'alliance' => env('ESI_ALLIANCE', 99004116), - 'corporation' => env('ESI_CORPORATION', 98287666) + 'corporation' => env('ESI_CORPORATION', 98287666), + 'mining_tax' => 0.10, ]; ?> \ No newline at end of file