From 19730208a63963a7e98a5ba341ef6ae8e8d5173d Mon Sep 17 00:00:00 2001 From: drkthunder02 Date: Thu, 11 Feb 2021 12:41:58 +0900 Subject: [PATCH] mining taxes view controller start --- .../Dashboard/DashboardController.php | 30 ++++- .../MiningTaxes/MiningTaxesController.php | 109 +++++++++++++++--- .../MiningTaxes/CalculateMiningTaxesJob.php | 12 +- .../FetchMiningTaxesLedgersJob.php | 9 ++ app/Library/Structures/StructureHelper.php | 4 - app/Models/MiningTax/Ledger.php | 1 + config/esi.php | 1 + 7 files changed, 140 insertions(+), 26 deletions(-) diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index c64dfe07e..8652d069c 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -17,6 +17,8 @@ use App\Models\User\UserPermission; use App\Models\User\UserRole; use App\Models\SRP\SRPShip; use App\Models\User\UserAlt; +use App\Models\MiningTax\Invoice; +use App\Models\MiningTax\Ledger; class DashboardController extends Controller { @@ -42,9 +44,13 @@ class DashboardController extends Controller $open = array(); $approved = array(); $denied = array(); + $ores = array(); $altCount = null; $alts = null; + /** + * Alt Counts + */ //Get the number of the user's alt which are registered so we can process the alt's on the main dashboard page $altCount = UserAlt::where([ 'main_id' => auth()->user()->character_id, @@ -57,6 +63,9 @@ class DashboardController extends Controller ])->get(); } + /** + * SRP Items + */ //See if we can get all of the open SRP requests $openCount = SRPShip::where([ 'character_id' => auth()->user()->character_id, @@ -184,13 +193,32 @@ class DashboardController extends Controller ], ]); + /** + * Mining Tax Items + */ + $invoice = Ledger::where([ + 'character_id' => auth()->user()->getId(), + 'invoiced' => 'No', + ])->sum('amount'); + + $rows = Ledger::where([ + 'character_id' => auth()->user()->getId(), + 'invoiced' => 'No', + ])->get(); + + foreach($rows as $row) { + $ores[$row->ore_name] = $ores[$row->ore_name] + $row->quantity; + } + return view('dashboard')->with('openCount', $openCount) ->with('approvedCount', $approvedCount) ->with('deniedCount', $deniedCount) ->with('open', $open) ->with('approved', $approved) ->with('denied', $denied) - ->with('lava', $lava); + ->with('lava', $lava) + ->with('invoice', $invoice) + ->with('ores', $ores); } /** diff --git a/app/Http/Controllers/MiningTaxes/MiningTaxesController.php b/app/Http/Controllers/MiningTaxes/MiningTaxesController.php index 4bf77c0c6..955b8c388 100644 --- a/app/Http/Controllers/MiningTaxes/MiningTaxesController.php +++ b/app/Http/Controllers/MiningTaxes/MiningTaxesController.php @@ -13,6 +13,8 @@ use Auth; //Library Helpers use App\Library\Lookups\LookupHelper; use App\Library\Structures\StructureHelper; +use Seat\Eseye\Exceptions\RequestFailedException; +use App\Library\Esi\Esi; //Models use App\Models\Moon\ItemComposition; @@ -20,37 +22,118 @@ use App\Models\Moon\MineralPrice; use App\Models\MiningTax\Ledger; use App\Models\MiningTax\Observer; use App\Models\MiningTax\Invoice; +use App\Models\Esi\EsiToken; +use App\Models\Esi\EsiScope; class MiningTaxesController extends Controller { + /** + * Construct to deal with middleware and other items + */ public function __construct() { $this->middleware('auth'); $this->middleware('role:User'); } + /** + * Display all of the upcoming extractions + */ public function DisplayUpcomingExtractions() { //Declare variables $structures = array(); $sHelper = new StructureHelper; - //Get all the current observers from the database + //Get the esi data for extractions + try { + $extractions = $this->esi->invoke('get', '/corporation/{corporation_id}/mining/extractions/', [ + 'corporation_id' => $config['corporation'], + ]); + } catch(RequestFailedException $e) { + Log::warning('Could not retrieve extractions from ESI in MiningTaxesController.php'); + $extractions = null; + } + + //Basically get the structure info and attach it to the variable set + foreach($extractions as $ex) { + $sName = $sHelper->GetStructureInfo($ex->structure_id); + $ex->structure_name = $sName; + + } + + //Return the view with the extractions variable for html processing + return view('miningtax.display.upcoming')->with('extractions', $extractions); + } + + /** + * Display the ledger for the moons. + */ + public function DisplayMoonLedgers() { + //Declare variables + $structures = array(); + $tempLedgers = array(); + $ledgers = array(); + $esiHelper = new Esi; + $lookup = new LookupHelper; + $sHelper = new StructureHelper; + $config = config(); + + //Check for the esi scope + if(!$esiHelper->HaveEsiScope($config['primary'], 'esi-industry.read_corporation_mining.v1')) { + return redirect('/dashboard')->with('error', 'Tell the nub Minerva to register the ESI for the holding corp.'); + } else { + if(!$esiHelper->HaveEsiScope($config['primary'], 'esi-universe.read_structures.v1')) { + return redirect('/dashboard')->with('error', 'Tell the nub Minerva to register the ESI for the holding corp.'); + } + } + + //Get the refresh token if scope checks have passed + $refreshToken = $esiHelper->GetRefreshToken($config['primary']); + + //Setup the esi container + $esi = $esiHelper->SetupEsiAuthentication($refreshToken); + + //Get the character data from the lookup table if possible or esi + $character = $lookup->GetCharacterInfo($config['primary']); + + //Get the observers from the database $observers = Observer::all(); + //Get the ledgers for each structure one at a time foreach($observers as $obs) { - $extraction = $sHelper->GetExtractions(); + /** + * Get the ledger from each observer. + * We don't care about observer type as it can only be an Athanor or Tatara + */ + $ledgers = Ledger::where([ + 'observer_id' => $obs->structure_id, + ])->where('last_updated', '>=', Carbon::now()->subDays(30))->get(); + + if($ledger != null) { + foreach($ledgers as $ledger) { + $tempArray = array(); + + //Get the character information from the character id + $charInfo = $lookup->GetCharacterInfo($ledger->character_id); + //Get the corp ticker + $corpInfo = $lookup->GetCorporationInfo($charInfo->corporation_id); + //Get the structure name from the database + $structure = $sHelper->GetStructureInfo($obs->observer_id); + + array_push($miningLedgers, [ + 'structure' => $structure->name, + 'character' => $charInfo->name, + 'corpTicker' => $corpInfo->ticker, + 'ore' => $ore, + 'quantity' => $ledger->quantity, + 'updated' => $ledger->last_updated, + ]); + } + } } - } - - public function DisplayMiningMoons() { - - } - - public function DisplayAccruedTaxes() { - - } - - public function DisplayLedgers() { + //Return the view + return view('miningtax.display.ledger')->with('miningLedgers', $miningLedgers) + ->with('structures', $structures); } } diff --git a/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php b/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php index 32d5f352d..dcb249091 100644 --- a/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php +++ b/app/Jobs/Commands/MiningTaxes/CalculateMiningTaxesJob.php @@ -58,20 +58,16 @@ class CalculateMiningTaxesJob implements ShouldQueue $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; } - //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; + //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 diff --git a/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php b/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php index baad3cbf2..f1b48c653 100644 --- a/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php +++ b/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php @@ -14,10 +14,13 @@ use Carbon\Carbon; use Seat\Eseye\Exceptions\RequestionFailedException; use App\Library\Esi\Esi; use App\Library\Lookups\LookupHelper; +use App\Library\Moons\MoonCalc; //App Models use App\Models\MiningTax\Observer; use App\Models\MiningTax\Ledger; +use App\Models\Moon\MineralPrice; +use App\Models\Moon\ItemComposition; class FetchMiningTaxesLedgersJob implements ShouldQueue { @@ -81,6 +84,7 @@ class FetchMiningTaxesLedgersJob implements ShouldQueue { //Declare variables $lookup = new LookupHelper; + $mHelper = new MoonCalc; $final = array(); $items = array(); $notSorted = array(); @@ -103,6 +107,9 @@ class FetchMiningTaxesLedgersJob implements ShouldQueue $typeName = $lookup->ItemIdToName($ledger->ore); $updated = $this->esi->DecodeDate($ledger->last_updated); + $price = $mHelper->CalculateOrePrice($ledger->type_id); + $amount = $price * $ledger->quantity; + //Insert or update the entry in the database $item = Ledger::updateOrCreate([ 'character_id' => $ledger->character_id, @@ -111,6 +118,7 @@ class FetchMiningTaxesLedgersJob implements ShouldQueue 'type_id' => $ledger->type_id, 'ore_name' => $typeName, 'quantity' => $ledger->quantity, + 'price' => $amount, ], [ 'character_id' => $ledger->character_id, 'character_name' => $charName, @@ -118,6 +126,7 @@ class FetchMiningTaxesLedgersJob implements ShouldQueue 'type_id' => $ledger->type_id, 'ore_name' => $typeName, 'quantity' => $ledger->quantity, + 'price' => $amount, ]); } diff --git a/app/Library/Structures/StructureHelper.php b/app/Library/Structures/StructureHelper.php index 0bcfede35..aff7973d2 100644 --- a/app/Library/Structures/StructureHelper.php +++ b/app/Library/Structures/StructureHelper.php @@ -43,10 +43,6 @@ class StructureHelper { } } - public function GetExtractions() { - //Try to get the ESI data - } - public function GetStructuresByPage($page) { //Try to get the ESI data diff --git a/app/Models/MiningTax/Ledger.php b/app/Models/MiningTax/Ledger.php index f52a44074..917b77896 100644 --- a/app/Models/MiningTax/Ledger.php +++ b/app/Models/MiningTax/Ledger.php @@ -23,6 +23,7 @@ class Ledger extends Model 'type_id', 'ore_name', 'quantity', + 'amount', 'invoiced', ]; } diff --git a/config/esi.php b/config/esi.php index 38e3c7406..2b80c3d90 100644 --- a/config/esi.php +++ b/config/esi.php @@ -6,5 +6,6 @@ 'callback' => env('ESI_CALLBACK_URI'), 'primary' => env('ESI_PRIMARY_CHAR', 93738489), 'alliance' => env('ESI_ALLIANCE', 99004116), + 'corporation' => env('ESI_CORPORATION', 98287666) ]; ?> \ No newline at end of file