Files
exchange-2.0/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyBookingAverage.php
T
2021-07-22 14:37:14 +08:00

61 lines
1.7 KiB
PHP

<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Classes\Exceptions\ErrorException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\ValueObjects\Constants\TransactionType;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class FetchCompanyBookingAverage extends AbstractControllerLogic
{
/**
* FetchCompanyAverageSpending constructor.
* @param FetchesCompany $fetchesCompany
*/
public function __construct(FetchesCompany $fetchesCompany)
{
$this->fetchesCompany = $fetchesCompany;
}
protected function notification(): array
{
return [
'title' => 'Fetch Company Average Booking',
'message' => 'You have successfully fetched company booking average'
];
}
/** @var FetchesCompany */
private $fetchesCompany;
protected function logic(Request $request): JsonResponse
{
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
$bookings = $company->bookings()->get();
$amount_arr = [];
$total = 0;
// dd($bookings->pluck("fix_amount"));
foreach ($company->bookings as $booking)
{
$fix_amount = $booking->fix_amount;
array_push($amount_arr,$booking->fix_amount);
$total = $total + $fix_amount;
}
$average = $total / count($bookings);
// dd($average);
// note: an err will occur when eq to 0
$normalized = ($average - min($amount_arr)) / (max($amount_arr) - min($amount_arr));
// dd($normalized);
return $this->response(['average_booking' => $normalized]);
}
}