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

82 lines
2.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\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]);
}
}