Files
exchange-2.0/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyAverageSpending.php
T
2021-07-14 17:39:18 +08:00

63 lines
1.6 KiB
PHP

<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
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 FetchCompanyAverageSpending extends AbstractControllerLogic
{
/**
* FetchCompanyAverageSpending constructor.
* @param FetchesCompany $fetchesCompany
*/
public function __construct(FetchesCompany $fetchesCompany)
{
$this->fetchesCompany = $fetchesCompany;
}
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Fetch Company Average Spending',
'message' => 'You have successfully fetched company average spending'
];
}
/** @var FetchesCompany */
private $fetchesCompany;
/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function logic(Request $request): JsonResponse {
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
$totalPaymentsAmount = 0;
foreach ($company->bookings as $booking) {
$transaction = $booking->transactions()->where('type', TransactionType::PAYMENT)->sum('amount');
$totalPaymentsAmount += $transaction;
}
$date_age = date_diff($company->created_at, date_create('now'))->format("%a");
$average = $totalPaymentsAmount / $date_age;
return $this->response(['average_spending' => $average]);
}
}