CalculateMiningTaxesJob

This commit is contained in:
2021-02-04 23:34:53 +09:00
parent 416f4abd03
commit d4954bc7bf
6 changed files with 172 additions and 4 deletions

View File

@@ -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);
}
}
}

View File

@@ -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;
}
/**

View File

@@ -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);

View File

@@ -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
*/

View File

@@ -23,5 +23,6 @@ class Ledger extends Model
'type_id',
'ore_name',
'quantity',
'invoiced',
];
}

View File

@@ -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();
});