// database/migrations/<timestamp>_create_cycles_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCyclesTable extends Migration
{
public function up()
{
Schema::create('cycles', function (Blueprint $table) {
$table->id();
$table->date('start_date');
$table->date('end_date');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('cycles');
}
}
// App/Models/Medrep.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Medrep extends Model
{
protected $fillable = ['name'];
public function visits()
{
return $this->hasMany(Visit::class);
}
public function getVisitsWithinCycle($cycleId)
{
$cycle = Cycle::findOrFail($cycleId);
$startDate = $cycle->start_date;
$endDate = $cycle->end_date;
return $this->visits()
->whereBetween('visit_date', [$startDate, $endDate])
->count();
}
}
// App/Models/Medrep.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Medrep extends Model
{
protected $fillable = ['name'];
public function visits()
{
return $this->hasMany(Visit::class);
}
public function updateKPI($cycleId, $minimumVisits, $maximumVisits)
{
$visits = $this->getVisitsWithinCycle($cycleId);
$kpi = '';
if ($visits < $minimumVisits) {
$kpi = 'Below Target';
} elseif ($visits >= $minimumVisits && $visits <= $maximumVisits) {
$kpi = 'On Target';
} else {
$kpi = 'Exceeded Target';
}
$this->update(['kpi' => $kpi]);
}
}
<!-- resources/views/medreps.blade.php -->
@foreach($medreps as $medrep)
<div>
<h3>{{ $medrep->name }}</h3>
<p>KPI: {{ $medrep->kpi }}</p>
</div>
@endforeach
// app/Console/Kernel.php
use App\Models\Medrep;
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
// Get the current cycle or create a new cycle
$currentCycle = Cycle::whereDate('end_date', '>=', now())->first();
if (!$currentCycle) {
$currentCycle = Cycle::create([
'start_date' => now(),
'end_date' => now()->addDays(15), // Assuming each cycle is 15 days
]);
}
// Calculate and update the KPI for each medrep within the current cycle
$medreps = Medrep::all();
foreach ($medreps as $medrep) {
$medrep->updateKPI($currentCycle->id, 5, 10); // Assuming minimum visits = 5 and maximum visits = 10
}
})->dailyAt('00:00');
}
// app/Console/Kernel.php
use App\Models\Medrep;
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$today = now();
$dayOfMonth = $today->day;
// Calculate the start and end dates for the current cycle
$startOfMonth = $today->startOfMonth();
$endOfMonth = $today->endOfMonth();
$startCycle = $dayOfMonth <= 15 ? $startOfMonth : $startOfMonth->addDays(15);
$endCycle = $dayOfMonth <= 15 ? $startCycle->copy()->addDays(14) : $endOfMonth;
// Create the cycle if it doesn't exist
$currentCycle = Cycle::where('start_date', $startCycle)
->where('end_date', $endCycle)
->first();
if (!$currentCycle) {
$currentCycle = Cycle::create([
'start_date' => $startCycle,
'end_date' => $endCycle,
]);
}
// Calculate and update the KPI for each medrep within the current cycle
$medreps = Medrep::all();
foreach ($medreps as $medrep) {
$medrep->updateKPI($currentCycle->id, 5, 10); // Assuming minimum visits = 5 and maximum visits = 10
}
})->dailyAt('00:00');
}