From d4954bc7bfc880bb3c64d647ec47c82ee831811f Mon Sep 17 00:00:00 2001 From: drkthunder02 Date: Thu, 4 Feb 2021 23:34:53 +0900 Subject: [PATCH] CalculateMiningTaxesJob --- .../MiningTaxes/CalculateMiningTaxesJob.php | 51 ++++++++++- .../CreateMiningTaxesInvoiceJob.php | 11 ++- app/Library/Lookups/LookupHelper.php | 24 ++++++ app/Library/Moons/MoonCalc.php | 85 +++++++++++++++++++ app/Models/MiningTax/Ledger.php | 1 + ..._01_10_164334_create_mining_tax_tables.php | 4 + 6 files changed, 172 insertions(+), 4 deletions(-) diff --git a/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php b/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php index 78fa3bc1b..1684d49ea 100644 --- a/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php +++ b/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php @@ -2,16 +2,30 @@ namespace App\Jobs\Commands\MiningTaxes; +//Internal Library use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +//Internal Library +use App\Library\Lookups\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; + class CalculateMiningTaxesJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + //Private variables + private $mHelper; + /** * Create a new job instance. * @@ -19,7 +33,8 @@ class CalculateMiningTaxesJob implements ShouldQueue */ public function __construct() { - // + //Declare variables for use in the handler + $this->mHelper = new MoonCalc; } /** @@ -29,6 +44,38 @@ class CalculateMiningTaxesJob implements ShouldQueue */ public function handle() { - // + //Get the characters for each non-invoiced ledger entry + $chars = Ledger::distinct('character_id')->pluck('character_id'); + + //Foreach character tally up the mining ledger totals to create an invoice + foreach($chars as $char) { + //Declare some variables we need for each 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', + ]); + + //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; + } + + //From the item composition for each of the totaled ores, let's get the components and find the price + foreach($ores as $itemId => $quantity) { + //Get the price from the helper function for each unit of ore + $price = $this->mHelper->CalculateOrePrice($itemId); + + //Add up the total and keep a running total + $totalPrice += $price * $quantity; + } + + //Create the invoice job + CreateMiningTaxesInvoiceJob::dispatch($ores, $totalPrice, $char->character_id); + } } } diff --git a/app/Jobs/Commands/MiningTaxes/CreateMiningTaxesInvoiceJob.php b/app/Jobs/Commands/MiningTaxes/CreateMiningTaxesInvoiceJob.php index ddf3ee958..8e6b2c042 100644 --- a/app/Jobs/Commands/MiningTaxes/CreateMiningTaxesInvoiceJob.php +++ b/app/Jobs/Commands/MiningTaxes/CreateMiningTaxesInvoiceJob.php @@ -12,14 +12,21 @@ class CreateMiningTaxesInvoiceJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + //Private Variables + private $ores; + private $totalPrices; + private $charId; + /** * Create a new job instance. * * @return void */ - public function __construct() + public function __construct($ores, $totalPrice, $charId) { - // + $this->ores = $ores; + $this->totalPrice = $totalPrice; + $this->charId = $charId; } /** diff --git a/app/Library/Lookups/LookupHelper.php b/app/Library/Lookups/LookupHelper.php index 2fa1d7e3b..d143609d3 100644 --- a/app/Library/Lookups/LookupHelper.php +++ b/app/Library/Lookups/LookupHelper.php @@ -33,6 +33,30 @@ class LookupHelper { $this->esi = $esiHelper->SetupEsiAuthentication(); } + public function ItemNameToId($itemName) { + $item = ItemLookup::where([ + 'name' => $itemName, + ])->first(); + + if($item != null) { + return $item->type_id; + } else { + try { + $response = $this->esi->setBody(array( + $itemName, + ))->invoke('post', '/universe/ids/'); + } catch(RequestFailedException $e) { + Log::warning('Failed to get item information from /universe/'); + } + + if(isset($response->inventory_types)) { + return $response->inventory_types[0]->id; + } else { + return null; + } + } + } + public function ItemIdToName($itemId) { //Check if the item is stored in our own database first $item = $this->LookupItem($itemId); diff --git a/app/Library/Moons/MoonCalc.php b/app/Library/Moons/MoonCalc.php index 80c9ebe24..7166596ea 100644 --- a/app/Library/Moons/MoonCalc.php +++ b/app/Library/Moons/MoonCalc.php @@ -14,6 +14,9 @@ use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Client; use Log; +//Library +use App\Library\Lookups\LookupHelper; + //Models use App\Models\Moon\Config; use App\Models\Moon\ItemComposition; @@ -203,6 +206,88 @@ class MoonCalc { return $units; } + /** + * Calculate the per item price of a unit of ore + */ + public function CalculateOrePrice($oreId) { + //Declare variables + $lookupHelper = new LookupHelper; + $finalName = ''; + + //Get the price of the moongoo + $atmosphericGasesPrice = MineralPrice::where(['ItemId' => 16634])->where('Time', '>', $pastTime)->avg('Price'); + $evaporiteDepositsPirce = MineralPrice::where(['ItemId' => 16635])->where('Time', '>', $pastTime)->avg('Price'); + $hydrocarbonsPrice = MineralPrice::where(['ItemId' => 16633])->where('Time', '>', $pastTime)->avg('Price'); + $silicatesPrice = MineralPrice::where(['ItemId' => 16636])->where('Time', '>', $pastTime)->avg('Price'); + $cobaltPrice = MineralPrice::where(['ItemId' => 16640])->where('Time', '>', $pastTime)->avg('Price'); + $scandiumPrice = MineralPrice::where(['ItemId' => 16639])->where('Time', '>', $pastTime)->avg('Price'); + $titaniumPrice = MineralPrice::where(['ItemId' => 16638])->where('Time', '>', $pastTime)->avg('Price'); + $tungstenPrice = MineralPrice::where(['ItemId' => 16637])->where('Time', '>', $pastTime)->avg('Price'); + $cadmiumPrice = MineralPrice::where(['ItemId' => 16643])->where('Time', '>', $pastTime)->avg('Price'); + $platinumPrice = MineralPrice::where(['ItemId' => 16644])->where('Time', '>', $pastTime)->avg('Price'); + $vanadiumPrice = MineralPrice::where(['ItemId' => 16642])->where('Time', '>', $pastTime)->avg('Price'); + $chromiumPrice = MineralPrice::where(['ItemId' => 16641])->where('Time', '>', $pastTime)->avg('Price'); + $technetiumPrice = MineralPrice::where(['ItemId' => 16649])->where('Time', '>', $pastTime)->avg('Price'); + $hafniumPrice = MineralPrice::where(['ItemId' => 16648])->where('Time', '>', $pastTime)->avg('Price'); + $caesiumPrice = MineralPrice::where(['ItemId' => 16647])->where('Time', '>', $pastTime)->avg('Price'); + $mercuryPrice = MineralPrice::where(['ItemId' => 16646])->where('Time', '>', $pastTime)->avg('Price'); + $dysprosiumPrice = MineralPrice::where(['ItemId' => 16650])->where('Time', '>', $pastTime)->avg('Price'); + $neodymiumPrice = MineralPrice::where(['ItemId' => 16651])->where('Time', '>', $pastTime)->avg('Price'); + $promethiumPrice = MineralPrice::where(['ItemId' => 16652])->where('Time', '>', $pastTime)->avg('Price'); + $thuliumPrice = MineralPrice::where(['ItemId' => 16653])->where('Time', '>', $pastTime)->avg('Price'); + + //Get the name through the lookup table + $oreName = $lookupHelper->ItemIdToName($oreId); + + //Strip the prefix from the ore name if it has one. + //Then change the ore id if necessary + $tempName = str_split($oreName); + if(sizeof($tempName) == 1) { + $finalName = $tempName[0]; + } else { + $finalName = $tempName[sizeof($tempName) - 1]; + $oreId = $lookupHelper->ItemNameToId($finalName); + } + + //Get the item composition for the ore + $composition = ItemComposition::where('ItemId', $oreId)->first(); + + //Calculate the Batch Price + $batchPrice = ( ($composition->Tritanium * $tritaniumPrice) + + ($composition->Pyerite * $pyeritePrice) + + ($composition->Mexallon * $mexallonPrice) + + ($composition->Isogen * $isogenPrice) + + ($composition->Nocxium * $nocxiumPrice) + + ($composition->Zydrine * $zydrinePrice) + + ($composition->Megacyte * $megacytePrice) + + ($composition->AtmosphericGases * $atmosphericGasesPrice) + + ($composition->EvaporiteDeposits * $evaporiteDepositsPirce) + + ($composition->Hydrocarbons * $hydrocarbonsPrice) + + ($composition->Silicates * $silicatesPrice) + + ($composition->Cobalt * $cobaltPrice) + + ($composition->Scandium * $scandiumPrice) + + ($composition->Titanium * $titaniumPrice) + + ($composition->Tungsten * $tungstenPrice) + + ($composition->Cadmium * $cadmiumPrice) + + ($composition->Platinum * $platinumPrice) + + ($composition->Vanadium * $vanadiumPrice) + + ($composition->Chromium * $chromiumPrice)+ + ($composition->Technetium * $technetiumPrice) + + ($composition->Hafnium * $hafniumPrice) + + ($composition->Caesium * $caesiumPrice) + + ($composition->Mercury * $mercuryPrice) + + ($composition->Dysprosium * $dysprosiumPrice) + + ($composition->Neodymium * $neodymiumPrice) + + ($composition->Promethium * $promethiumPrice) + + ($composition->Thulium * $thuliumPrice)); + + //Take the batch price, and divide by batch size to get unit price + $price = $batchPrice / $composition->BatchSize; + + //Return the price to the calling function + return $price; + } + /** * Update item pricing after new prices were pulled */ diff --git a/app/Models/MiningTax/Ledger.php b/app/Models/MiningTax/Ledger.php index 9d3e108ee..f52a44074 100644 --- a/app/Models/MiningTax/Ledger.php +++ b/app/Models/MiningTax/Ledger.php @@ -23,5 +23,6 @@ class Ledger extends Model 'type_id', 'ore_name', 'quantity', + 'invoiced', ]; } diff --git a/database/migrations/2021_01_10_164334_create_mining_tax_tables.php b/database/migrations/2021_01_10_164334_create_mining_tax_tables.php index 059029951..06bf1b13a 100644 --- a/database/migrations/2021_01_10_164334_create_mining_tax_tables.php +++ b/database/migrations/2021_01_10_164334_create_mining_tax_tables.php @@ -47,6 +47,10 @@ class CreateMiningTaxTables extends Migration $table->unsignedBigInteger('type_id'); $table->string('ore_name'); $table->unsignedBigInteger('quantity'); + $table->enum('invoiced', [ + 'No', + 'Yes', + ])->default('No'); $table->timestamps(); });