From aa7e170aaf37853f9e47b2c47bedf2784b5e9729 Mon Sep 17 00:00:00 2001 From: drkthunder02 Date: Sat, 20 Mar 2021 18:02:25 +0900 Subject: [PATCH] row counts for mining tax invoices --- .../MiningTaxes/MiningTaxesInvoices.php | 177 ++++++++++-------- 1 file changed, 94 insertions(+), 83 deletions(-) diff --git a/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php b/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php index 1328ce185..d1a4253b0 100644 --- a/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php +++ b/app/Console/Commands/MiningTaxes/MiningTaxesInvoices.php @@ -14,6 +14,8 @@ use App\Library\Helpers\LookupHelper; //Models use App\Models\MiningTax\Invoice; use App\Models\MiningTax\Ledger; +use App\Models\User\UserAlt; +use App\Models\User\User; //Jobs use App\Jobs\Commands\Eve\ProcessSendEveMailJob; @@ -80,105 +82,114 @@ class MiningTaxesInvoices extends Command $totalPrice = 0.00; $body = null; - //Get the rows from the database for each character and the requirement of not been invoiced yet + //Get the row count to reduce overhead processing if we can. + $rowCount = Ledger::where([ + 'character_id' => $charId, + 'invoiced' => 'No', + ])->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 - foreach($rows as $row) { - if(!isset($ores[$row['type_id']])) { - $ores[$row['type_id']] = 0; + 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 = 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, ".", ",") . "
"; } - $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 = 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 MMT: " . $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 MMT: " . $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(sizeof($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 MMT: " . $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 MMT: " . $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(sizeof($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 MMT: " . $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 + ProcessSendEveMailJob::dispatch($body, $recipient, $recipientType, $subject, $sender)->onQueue('mail')->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; } - //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 - ProcessSendEveMailJob::dispatch($body, $recipient, $recipientType, $subject, $sender)->onQueue('mail')->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; } //Set the task as stopped