diff --git a/app/Http/Controllers/StructureController.php b/app/Http/Controllers/StructureController.php index b33654c18..89ded0649 100644 --- a/app/Http/Controllers/StructureController.php +++ b/app/Http/Controllers/StructureController.php @@ -7,12 +7,11 @@ use Illuminate\Http\Request; use Auth; use DB; use Carbon\Carbon; +use App\Library\Structures\StructureHelper; use App\User; use App\Models\User\UserRole; use App\Models\User\UserPermission; -use App\Models\Corporation\CorpJournal; -use App\Models\Corporation\CorpStructure; use App\Library\Esi; @@ -26,145 +25,29 @@ class StructureController extends Controller public function displayTaxes() { //Make the helper esi class $helper = new Esi(); + //Get the character's corporation from esi $corpId = $helper->FindCorporationId(Auth::user()->character_id); //Get the dates we are working with - $dates = $this->GetTimeFrame(); + $dates = $sHelper->GetTimeFrame(); + $sHelper = new StructureTaxHelper(); //Get the market taxes for this month from the database $totalTaxes = [ - 'thisMonthMarket' => number_format($this->GetTaxes($corpId, 'Market', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), - 'thisMonthRefinery' => number_format($this->GetTaxes($corpId, 'Refinery', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), - 'lastMonthMarket' => number_format($this->GetTaxes($corpId, 'Market', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), - 'lastMonthRefinery' => number_format($this->GetTaxes($corpId, 'Refinery', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), - 'thisMonthRevMarket' => number_format($this->GetRevenue($corpId, 'Market', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), - 'thisMonthRevRefinery' => number_format($this->GetRevenue($corpId, 'Refinery', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), - 'lastMonthRevMarket' => number_format($this->GetRevenue($corpId, 'Market', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), - 'lastMonthRevRefinery' => number_format($this->GetRevenue($corpId, 'Refinery', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), + 'thisMonthMarket' => number_format($sHelper->GetTaxes($corpId, 'Market', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), + 'thisMonthRefinery' => number_format($sHelper->GetTaxes($corpId, 'Refinery', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), + 'lastMonthMarket' => number_format($sHelper->GetTaxes($corpId, 'Market', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), + 'lastMonthRefinery' => number_format($sHelper->GetTaxes($corpId, 'Refinery', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), + 'thisMonthRevMarket' => number_format($sHelper->GetRevenue($corpId, 'Market', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), + 'thisMonthRevRefinery' => number_format($sHelper->GetRevenue($corpId, 'Refinery', $dates['ThisMonthStart'], $dates['ThisMonthEnd']), 2, '.', ','), + 'lastMonthRevMarket' => number_format($sHelper->GetRevenue($corpId, 'Market', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), + 'lastMonthRevRefinery' => number_format($sHelper->GetRevenue($corpId, 'Refinery', $dates['LastMonthStart'], $dates['LastMonthEnd']), 2, '.', ','), 'thisMonthStart' => $dates['ThisMonthStart']->toFormattedDateString(), 'lastMonthStart' => $dates['LastMonthStart']->toFormattedDateString(), ]; return view('structures.taxes')->with('totalTaxes', $totalTaxes); } - - private function GetTaxes($corpId, $refType, $start, $end) { - $taxOwed = 0.00; - //Get the number of structures of a certain type - $count = $this->GetStructureCount($corpId, $refType); - - //Calculate the fuel cost for one type of structure - $fuelCost = $this->CalculateFuelBlockCost($refType); - - //Calculate the average tax for a given structure type - $tax = $this->GetStructureTax($corpId, $refType); - - //Calculate the tax ratio to later be divided against the tax to find the - //actual tax owed to the alliance. Revenue will be a separate function - $ratio = $this->CalculateTaxRatio($tax, $refType, $start, $end); - if($ratio == 0 || $ratio == null) { - $ratio = 1.0; - } - //Get the total taxes produced by the structure(s) over a given set of dates - $revenue = $this->GetRevenue($corpId, $refType, $start, $end); - - $revenue = $revenue - ($fuelCost * $count); - - //Calculate the tax owed which is revenue divided by ratio previously calculated - $taxOwed = $revenue / $ratio; - //Check for negative number, and if negative, zero it out. - if($taxOwed < 0.00){ - $taxOwed = 0.00; - } - - //Return the amount - return $taxOwed; - } - - private function GetRevenue($corpId, $refType, $start, $end) { - $revenue = 0.00; - if($refType == 'Market') { - $revenue = CorpJournal::where(['ref_type' => 'brokers_fee', 'corporation_id' => $corpId]) - ->whereBetween('date', [$start, $end]) - ->sum('amount'); - } else if($refType == 'Refinery'){ - $revenue = CorpJournal::where(['ref_type' => 'reprocessing_tax', 'corporation_id' => $corpId]) - ->whereBetween('date', [$start, $end]) - ->sum('amount'); - } else { - $revenue = 0.00; - } - - return $revenue; - } - - private function CalculateTaxRatio($overallTax, $type) { - //The alliance will get a ratio of the tax. - //We need to calculate the correct ratio based on structure tax, - //Then figure out what is owed to the alliance - if($type == 'Market') { - $ratioType = 2.0; - } else if($type == 'Refinery') { - $ratioType = 1.0; - } else { - $ratioType = 1.0; - } - //Calculate the ratio since we have the base percentage the alliance takes - $taxRatio = $overallTax / $ratioType; - - //Return what is owed to the alliance - return $taxRatio; - } - - private function CalculateFuelBlockCost($type) { - //Calculate how many fuel blocks are used in a month by a structure type - if($type === 'Market') { - $fuelBlocks = 24*30*32; - } else if ($type === 'Refinery') { - $fuelBlocks = 24*30*8; - } else { - $fuelBlocks = 0; - } - - //Multiply the amount of fuel blocks used by the structure by 20,000. - $cost = $fuelBlocks * 20000.00; - //Return to the calling function - return $cost; - } - - private function GetTimeFrame() { - $start = Carbon::now()->startOfMonth(); - $end = Carbon::now()->endOfMonth(); - $end->hour = 23; - $end->minute = 59; - $end->second = 59; - $startLast = new Carbon('first day of last month'); - $endLast = new Carbon('last day of last month'); - $endLast->hour = 23; - $endLast->minute = 59; - $endLast->second = 59; - - $dates = [ - 'ThisMonthStart' => $start, - 'ThisMonthEnd' => $end, - 'LastMonthStart' => $startLast, - 'LastMonthEnd' => $endLast, - ]; - - return $dates; - } - - private function GetStructureTax($corpId, $structureType) { - $tax = CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => $structureType])->avg('tax'); - - return (float) $tax; - } - - private function GetStructureCount($corpId, $structureType) { - $count = CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => $structureType])->count(); - - return (float) $count; - } } diff --git a/app/Library/Structures/StructureHelper.php b/app/Library/Structures/StructureHelper.php new file mode 100644 index 000000000..84908320c --- /dev/null +++ b/app/Library/Structures/StructureHelper.php @@ -0,0 +1,139 @@ +GetStructureCount($corpId, $refType); + + //Calculate the fuel cost for one type of structure + $fuelCost = $this->CalculateFuelBlockCost($refType); + + //Calculate the average tax for a given structure type + $tax = $this->GetStructureTax($corpId, $refType); + + //Calculate the tax ratio to later be divided against the tax to find the + //actual tax owed to the alliance. Revenue will be a separate function + $ratio = $this->CalculateTaxRatio($tax, $refType, $start, $end); + if($ratio == 0 || $ratio == null) { + $ratio = 1.0; + } + //Get the total taxes produced by the structure(s) over a given set of dates + $revenue = $this->GetRevenue($corpId, $refType, $start, $end); + + $revenue = $revenue - ($fuelCost * $count); + + //Calculate the tax owed which is revenue divided by ratio previously calculated + $taxOwed = $revenue / $ratio; + //Check for negative number, and if negative, zero it out. + if($taxOwed < 0.00){ + $taxOwed = 0.00; + } + + //Return the amount + return $taxOwed; + } + + public function GetRevenue($corpId, $refType, $start, $end) { + $revenue = 0.00; + if($refType == 'Market') { + $revenue = CorpJournal::where(['ref_type' => 'brokers_fee', 'corporation_id' => $corpId]) + ->whereBetween('date', [$start, $end]) + ->sum('amount'); + } else if($refType == 'Refinery'){ + $revenue = CorpJournal::where(['ref_type' => 'reprocessing_tax', 'corporation_id' => $corpId]) + ->whereBetween('date', [$start, $end]) + ->sum('amount'); + } else { + $revenue = 0.00; + } + + return $revenue; + } + + private function CalculateTaxRatio($overallTax, $type) { + //The alliance will get a ratio of the tax. + //We need to calculate the correct ratio based on structure tax, + //Then figure out what is owed to the alliance + if($type == 'Market') { + $ratioType = 2.0; + } else if($type == 'Refinery') { + $ratioType = 1.0; + } else { + $ratioType = 1.0; + } + //Calculate the ratio since we have the base percentage the alliance takes + $taxRatio = $overallTax / $ratioType; + + //Return what is owed to the alliance + return $taxRatio; + } + + private function CalculateFuelBlockCost($type) { + //Calculate how many fuel blocks are used in a month by a structure type + if($type === 'Market') { + $fuelBlocks = 24*30*32; + } else if ($type === 'Refinery') { + $fuelBlocks = 24*30*8; + } else { + $fuelBlocks = 0; + } + + //Multiply the amount of fuel blocks used by the structure by 20,000. + $cost = $fuelBlocks * 20000.00; + //Return to the calling function + return $cost; + } + + private function GetTimeFrame() { + $start = Carbon::now()->startOfMonth(); + $end = Carbon::now()->endOfMonth(); + $end->hour = 23; + $end->minute = 59; + $end->second = 59; + $startLast = new Carbon('first day of last month'); + $endLast = new Carbon('last day of last month'); + $endLast->hour = 23; + $endLast->minute = 59; + $endLast->second = 59; + + $dates = [ + 'ThisMonthStart' => $start, + 'ThisMonthEnd' => $end, + 'LastMonthStart' => $startLast, + 'LastMonthEnd' => $endLast, + ]; + + return $dates; + } + + private function GetStructureTax($corpId, $structureType) { + $tax = CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => $structureType])->avg('tax'); + + return (float) $tax; + } + + private function GetStructureCount($corpId, $structureType) { + $count = CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => $structureType])->count(); + + return (float) $count; + } +} + +?> \ No newline at end of file