reworked structure controller

This commit is contained in:
2018-12-10 01:38:38 -06:00
parent e918376bf4
commit 98a7347d26
3 changed files with 114 additions and 111 deletions

View File

@@ -14,7 +14,6 @@ use App\Models\User\UserPermission;
use App\Models\Corporation\CorpJournal;
use App\Models\Corporation\CorpStructure;
use App\Library\FinanceHelper;
use App\Library\Esi;
class StructureController extends Controller
@@ -27,64 +26,23 @@ class StructureController extends Controller
public function displayTaxes() {
//Make the helper esi class
$helper = new Esi();
//Make the helper class for finances
$hFinances = new FinanceHelper();
//Set the carbon date for the first day of this month, last day of this month, and the previous month's first and last days.
$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;
//Get the character's corporation from esi
//$corporation = $helper->FindCorporationName(Auth::user()->character_id);
$corpId = $helper->FindCorporationId(Auth::user()->character_id);
//Get the number of structures registered to a corporation
$citadelCount = CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => 'Citadel'])->count();
//Get the dates we are working with
$dates = $this->GetTimeFrame();
//Get the market taxes for this month from the database
$tempMonthTaxesMarket = CorpJournal::where(['ref_type' => 'brokers_fee', 'corporation_id' => $corpId])
->whereBetween('date', [$start, $end])
->sum('amount');
//Get the market taxes from last month from the database
$tempLastTaxesMarket = CorpJournal::where(['ref_type' => 'brokers_fee', 'corporation_id' => $corpId])
->whereBetween('date', [$startLast, $endLast])
->sum('amount');
/**
* In this next section we are removing the cost of fuel blocks from one structure
*/
$marketFuelCost = $hFinances->CalculateFuelBlockCost('market');
/**
* Calculate the final taxes and send to display
*/
$mTax = CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => 'Citadel'])->avg('tax');
$monthTaxesMarket = $tempMonthTaxesMarket - ($marketFuelCost * $citadelCount);
$monthTaxesMarket = $hFinances->CalculateTax($monthTaxesMarket, $mTax, 'market');
if($monthTaxesMarket < 0.00) {
$monthTaxesMarket = 0.00;
}
$lastTaxesMarket = $tempLastTaxesMarket - $marketFuelCost;
$lastTaxesMarket = $hFinances->CalculateTax($lastTaxesMarket, $mTax, 'market');
if($lastTaxesMarket < 0.00) {
$lastTaxesMarket = 0.00;
}
//Create the array to pass to the blade view
$totalTaxes = [
'thisMonthMarket' => number_format($monthTaxesMarket, 2, '.', ','),
'lastMonthMarket' => number_format($lastTaxesMarket, 2, '.', ','),
'thisMoMarketGeneration' => number_format($tempMonthTaxesMarket, 2, '.', ','),
'lastMoMarketGeneration' => number_format($tempLastTaxesMarket, 2, '.', ','),
'thisMonthMarket' => $this->GetTaxes($corpId, 'Market', $dates['ThisMonthStart'], $dates['ThisMonthEnd']),
'thisMonthRefinery' => $this->GetTaxes($corpId, 'Refinery', $dates['ThisMonthStart'], $dates['ThisMonthEnd']),
'lastMonthMarket' => $this->GetTaxes($corpId, 'Market', $dates['LastMonthStart'], $dates['LastMonthEnd']),
'lastMonthRefinery' => $this->GetTaxes($corpId, 'Refinery', $dates['LastMonthStart'], $dates['LastMonthEnd']),
'thisMonthRevMarket' => $this->GetRevenue($corpId, 'Market', $dates['ThisMonthStart'], $dates['ThisMonthEnd']),
'thisMonthRevRefinery' => $this->GetRevenue($corpId, 'Refinery', $dates['ThisMonthStart'], $dates['ThisMonthEnd']),
'lastMonthRevMarket' => $this->GetRevenue($corpId, 'Market', $dates['LastMonthStart'], $dates['LastMonthEnd']),
'lastMonthRevRefinery' => $this->GetRevenue($corpId, 'Refinery', $dates['LastMonthStart'], $dates['LastMonthEnd']),
];
return view('structures.taxes')->with('totalTaxes', $totalTaxes);
@@ -162,4 +120,106 @@ class StructureController extends Controller
return $dates;
}
private function GetTaxes($corpId, $refType, $start, $end) {
//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);
//Get the total taxes produced by the structure(s) over a given set of dates
$revenue = $this->GetRevenue($corpId, $refType, $start, $end);
//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) {
if($refType == 'Market') {
return CorpJournal::where(['ref_type' => 'brokers_fee', 'corporation_id' => $corpId])
->whereBetween('date', [$start, $end])
->sum('amount');
} else if ($refType == 'Refinery'){
return CorpJournal::where(['ref_type' => 'reprocessing_fee', 'corporation_id' => $corpId])
->whereBetween('date', [$start, $end])
->sum('amount');
} else {
return 0.00;
}
}
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 {
$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) {
return CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => $structureType])->avg('tax');
}
private function GetStructureCount($corpId, $structureType) {
return CorpStructure::where(['corporation_id' => $corpId, 'structure_type' => $structureType])->count();
}
}