mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
clean company average spending code
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?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]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Companies;
|
||||
|
||||
use App\Classes\Modules\Companies\ControllersLogic\FetchCompanyAverageSpending;
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Http\Controllers\Controller;
|
||||
@@ -14,25 +15,10 @@ use Illuminate\Http\Request;
|
||||
|
||||
class QualityRating extends Controller
|
||||
{
|
||||
public function getAverageSpending($id)
|
||||
|
||||
public function getAverageSpending(Request $request, FetchCompanyAverageSpending $logic)
|
||||
{
|
||||
$company = Company::find($id);
|
||||
$bookings = $company->bookings()->get();
|
||||
$total = 0;
|
||||
|
||||
foreach ($bookings as $booking) {
|
||||
$transaction = $booking->transactions()->where('type', TransactionType::PAYMENT)->count();
|
||||
$total = $total + $transaction;
|
||||
}
|
||||
|
||||
// dd($total);
|
||||
|
||||
$start_date = $company->created_at;
|
||||
$date_age = date_diff($start_date,date_create('now'))->format("%a");
|
||||
|
||||
$average = $total / $date_age;
|
||||
dd($average);
|
||||
|
||||
return $logic->execute($request);
|
||||
}
|
||||
|
||||
public function getBookingAverage($id)
|
||||
@@ -43,7 +29,7 @@ class QualityRating extends Controller
|
||||
$total = 0;
|
||||
|
||||
// dd($bookings->pluck("fix_amount"));
|
||||
foreach ($bookings as $booking)
|
||||
foreach ($company->bookings as $booking)
|
||||
{
|
||||
$fix_amount = $booking->fix_amount;
|
||||
array_push($amount_arr,$booking->fix_amount);
|
||||
|
||||
Reference in New Issue
Block a user