mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-20 21:13:59 +00:00
102 lines
3.4 KiB
PHP
102 lines
3.4 KiB
PHP
<?php
|
|
|
|
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;
|
|
use App\Models\Booking;
|
|
use App\Models\Company;
|
|
use App\Models\Transaction;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class QualityRating extends Controller
|
|
{
|
|
|
|
public function getAverageSpending(Request $request, FetchCompanyAverageSpending $logic)
|
|
{
|
|
return $logic->execute($request);
|
|
}
|
|
|
|
public function getBookingAverage($id)
|
|
{
|
|
$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);
|
|
}
|
|
|
|
public function activityRating($id)
|
|
{
|
|
$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);
|
|
}
|
|
|
|
public function poSubmissionRating($id)
|
|
{
|
|
$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);
|
|
}
|
|
}
|