mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-25 23:43:58 +00:00
Added quality rating controller logic
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?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 Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FetchCompanyActivityRating extends AbstractControllerLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* FetchCompanyActivityRating constructor.
|
||||
* @param FetchesCompany $fetchesCompany
|
||||
*/
|
||||
public function __construct(FetchesCompany $fetchesCompany)
|
||||
{
|
||||
$this->fetchesCompany = $fetchesCompany;
|
||||
}
|
||||
|
||||
|
||||
protected function notification(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'Fetch Company Activity Rating',
|
||||
'message' => 'You have successfully fetched company activity rating'
|
||||
];
|
||||
}
|
||||
|
||||
/** @var FetchesCompany */
|
||||
private $fetchesCompany;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
protected function logic(Request $request): JsonResponse
|
||||
{
|
||||
// TODO: Implement logic() method.
|
||||
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
|
||||
|
||||
$last_booking = $company->bookings()->oldest()->first();
|
||||
// $last_activity = date_format($last_booking->created_at, "Y/m/d H:i:s");
|
||||
$last_activity = $last_booking->created_at;
|
||||
$days_activity = date_diff($last_activity, date_create("now"))->format("%a");
|
||||
|
||||
$start_date = $company->created_at;
|
||||
$date_age = date_diff($start_date,date_create('now'))->format("%a");
|
||||
|
||||
// not sure about the calculations
|
||||
|
||||
// dd($days_activity);
|
||||
// return $this->response(['activity_rating' => $normalized]);
|
||||
}
|
||||
}
|
||||
@@ -60,4 +60,4 @@ class FetchCompanyAverageSpending extends AbstractControllerLogic
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?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]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?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\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FetchPoSubmissionRating extends AbstractControllerLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* FetchPoSubmissionRating constructor.
|
||||
* @param FetchesCompany $fetchesCompany
|
||||
*/
|
||||
public function __construct(FetchesCompany $fetchesCompany)
|
||||
{
|
||||
$this->fetchesCompany = $fetchesCompany;
|
||||
}
|
||||
|
||||
|
||||
protected function notification(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'Fetch Company Po Submission Rating',
|
||||
'message' => 'You have successfully fetched company po submission rating'
|
||||
];
|
||||
}
|
||||
|
||||
/** @var FetchesCompany */
|
||||
private $fetchesCompany;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
protected function logic(Request $request): JsonResponse
|
||||
{
|
||||
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
|
||||
|
||||
$start_date = $company->created_at;
|
||||
$date_age = date_diff($start_date,date_create('now'))->format("%a");
|
||||
|
||||
$bookings = $company->bookings()
|
||||
->whereIn('status',[3,2])
|
||||
->get();
|
||||
$total = 0;
|
||||
|
||||
foreach ($bookings as $booking) {
|
||||
|
||||
$transaction = $booking->transactions()
|
||||
->where('type', TransactionType::PURCHASE_ORDER)
|
||||
->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION,ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
||||
->first();
|
||||
|
||||
// $bookingDate = Carbon::parse($booking->created_at);
|
||||
//
|
||||
// $transaction_date = $transaction ? Carbon::parse($transaction->created_at) : Carbon::now() - Carbon::parse($booking->created_at);
|
||||
// $date_difference = $bookingDate->diffInDays($transaction_date);
|
||||
|
||||
$date_difference = Carbon::parse($booking->created_at)->diffInDays($transaction ? Carbon::parse($transaction->created_at) : Carbon::now());
|
||||
$total = $date_difference + $total;
|
||||
// echo "Booking Date : ".$booking->created_at." Po date :".($transaction ? Carbon::parse($transaction->created_at) : Carbon::now())->format('Y-m-d')." = ".$date_difference."\n\n";
|
||||
// $total = $total + $transaction;
|
||||
}
|
||||
|
||||
$average_po_delay = $total / count($bookings);
|
||||
|
||||
$rating = ((($average_po_delay / $date_age) * 100) - 100);
|
||||
// dd($rating);
|
||||
// dd($total);
|
||||
return $this->response(['po_submission_rating' => $rating]);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,10 @@
|
||||
|
||||
namespace App\Http\Controllers\Companies;
|
||||
|
||||
use App\Classes\Modules\Companies\ControllersLogic\FetchCompanyActivityRating;
|
||||
use App\Classes\Modules\Companies\ControllersLogic\FetchCompanyAverageSpending;
|
||||
use App\Classes\Modules\Companies\ControllersLogic\FetchCompanyBookingAverage;
|
||||
use App\Classes\Modules\Companies\ControllersLogic\FetchPoSubmissionRating;
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Http\Controllers\Controller;
|
||||
@@ -21,81 +24,18 @@ class QualityRating extends Controller
|
||||
return $logic->execute($request);
|
||||
}
|
||||
|
||||
public function getBookingAverage($id)
|
||||
public function getBookingAverage(Request $request, FetchCompanyBookingAverage $logic)
|
||||
{
|
||||
$company = Company::find($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);
|
||||
// dd($amount_arr);
|
||||
return $logic->execute($request);
|
||||
}
|
||||
|
||||
public function activityRating($id)
|
||||
public function activityRating(Request $request, FetchCompanyActivityRating $logic)
|
||||
{
|
||||
$company = Company::find($id);
|
||||
|
||||
$last_booking = $company->bookings()->oldest()->first();
|
||||
// $last_activity = date_format($last_booking->created_at, "Y/m/d H:i:s");
|
||||
$last_activity = $last_booking->created_at;
|
||||
$days_activity = date_diff($last_activity, date_create("now"))->format("%a");
|
||||
|
||||
$start_date = $company->created_at;
|
||||
$date_age = date_diff($start_date,date_create('now'))->format("%a");
|
||||
|
||||
// not sure about the calculations
|
||||
|
||||
dd($days_activity);
|
||||
return $logic->execute($request);
|
||||
}
|
||||
|
||||
public function poSubmissionRating($id)
|
||||
public function poSubmissionRating(Request $request, FetchPoSubmissionRating $logic)
|
||||
{
|
||||
$company = Company::find($id);
|
||||
|
||||
$start_date = $company->created_at;
|
||||
$date_age = date_diff($start_date,date_create('now'))->format("%a");
|
||||
|
||||
$bookings = $company->bookings()
|
||||
->whereIn('status',[3,2])
|
||||
->get();
|
||||
$total = 0;
|
||||
|
||||
foreach ($bookings as $booking) {
|
||||
|
||||
$transaction = $booking->transactions()
|
||||
->where('type', TransactionType::PURCHASE_ORDER)
|
||||
->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION,ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
||||
->first();
|
||||
|
||||
// $bookingDate = Carbon::parse($booking->created_at);
|
||||
//
|
||||
// $transaction_date = $transaction ? Carbon::parse($transaction->created_at) : Carbon::now() - Carbon::parse($booking->created_at);
|
||||
// $date_difference = $bookingDate->diffInDays($transaction_date);
|
||||
|
||||
$date_difference = Carbon::parse($booking->created_at)->diffInDays($transaction ? Carbon::parse($transaction->created_at) : Carbon::now());
|
||||
$total = $date_difference + $total;
|
||||
// echo "Booking Date : ".$booking->created_at." Po date :".($transaction ? Carbon::parse($transaction->created_at) : Carbon::now())->format('Y-m-d')." = ".$date_difference."\n\n";
|
||||
// $total = $total + $transaction;
|
||||
}
|
||||
|
||||
$average_po_delay = $total / count($bookings);
|
||||
|
||||
$rating = ((($average_po_delay / $date_age) * 100) - 100);
|
||||
dd($rating);
|
||||
// dd($total);
|
||||
return $logic->execute($request);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user