diff --git a/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyActivityRating.php b/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyActivityRating.php new file mode 100644 index 00000000..721886ce --- /dev/null +++ b/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyActivityRating.php @@ -0,0 +1,59 @@ +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]); + } +} diff --git a/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyAverageSpending.php b/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyAverageSpending.php index de29ca71..8d8359ff 100644 --- a/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyAverageSpending.php +++ b/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyAverageSpending.php @@ -60,4 +60,4 @@ class FetchCompanyAverageSpending extends AbstractControllerLogic } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyBookingAverage.php b/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyBookingAverage.php new file mode 100644 index 00000000..1179f902 --- /dev/null +++ b/app/Classes/Modules/Companies/ControllersLogic/FetchCompanyBookingAverage.php @@ -0,0 +1,60 @@ +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]); + } +} diff --git a/app/Classes/Modules/Companies/ControllersLogic/FetchPoSubmissionRating.php b/app/Classes/Modules/Companies/ControllersLogic/FetchPoSubmissionRating.php new file mode 100644 index 00000000..06049a41 --- /dev/null +++ b/app/Classes/Modules/Companies/ControllersLogic/FetchPoSubmissionRating.php @@ -0,0 +1,81 @@ +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]); + } +} diff --git a/app/Http/Controllers/Companies/QualityRating.php b/app/Http/Controllers/Companies/QualityRating.php index 27cd3520..79de8a65 100644 --- a/app/Http/Controllers/Companies/QualityRating.php +++ b/app/Http/Controllers/Companies/QualityRating.php @@ -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); } }