Skip to content

Commit

Permalink
upcoming demand (#24)
Browse files Browse the repository at this point in the history
also tweak demand and availability formats
  • Loading branch information
baradhili authored Nov 25, 2024
1 parent 5dbb867 commit e7bf1f9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/Widgets/ResourceAvailability.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function run()
}

}
Log::info("resourceAvailability: " . json_encode($resourceAvailability));

// Initialize the array with all year-months and 1.00 as default availability
$yearMonthSums = [];

Expand All @@ -131,7 +131,7 @@ public function run()
$yearMonthSums[$yearMonthShortName] += $availability;
}
}
Log::info("Year-Month: " . json_encode($yearMonthSums));


return view('widgets.resource_availability', [
'config' => $this->config,
Expand Down
81 changes: 80 additions & 1 deletion app/Widgets/UpcomingDemand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace App\Widgets;

use Arrilot\Widgets\AbstractWidget;
use Carbon\Carbon;
use App\Models\Demand;
use App\Models\Project;
use Illuminate\Support\Facades\Log;

class UpcomingDemand extends AbstractWidget
{
Expand All @@ -19,10 +23,85 @@ class UpcomingDemand extends AbstractWidget
*/
public function run()
{
//
// work out availability
$nextThreeMonths = [];

for ($i = 0; $i < 3; $i++) {
$date = Carbon::now()->addMonthsNoOverflow($i);
$nextThreeMonths[] = [
'year' => $date->year,
'month' => $date->month,
'monthName' => $date->format('F'),
];
}

// Start and end dates for the period
$startDate = Carbon::now()->startOfMonth();
$endDate = Carbon::now()->addMonths(3)->startOfMonth();

// Collect the projects_id from demands in our window
$demandIDs = Demand::whereBetween('demand_date', [$startDate, $endDate])
->pluck('projects_id')
->unique()
->values()
->all();

// Eager load the projects with their names
$projects = Project::whereIn('id', $demandIDs)
->with('demands') // Eager load the demands relationship
->paginate();

$demandArray = [];
// For each project - find the allocations for the period

foreach ($projects as $project) {

$demandArray[$project->id] = [];

foreach ($nextThreeMonths as $month) {

$monthStartDate = Carbon::create($month['year'], $month['month'], 1);
$totalAllocation = Demand::where('demand_date', '=', $monthStartDate)
->where('projects_id', '=', $project->id)
->pluck('fte')
->first();
$key = $month['year'] . '-' . str_pad($month['month'], 2, '0', STR_PAD_LEFT);

// Add the calculated base availability to the resource availability array - only if not zero
if ($totalAllocation > 0) {
$demandArray[$project->id]['demand'][$key] = $totalAllocation;
}
}
}

// Initialize the array with all year-months and 1.00 as default availability
$yearMonthSums = [];

foreach ($demandArray as $projectId => $projectInfo) {

foreach ($projectInfo['demand'] ?? [] as $yearMonth => $demand) {
// $date = Carbon::createFromFormat('Y-m', $yearMonth);
// $yearMonthShortName = $date->format('M');

if (!isset($yearMonthSums[$yearMonth])) {
$yearMonthSums[$yearMonth] = 0;
}
$yearMonthSums[$yearMonth] += (float) $demand;
}
}

ksort($yearMonthSums);
foreach ($yearMonthSums as $yearMonth => $sum) {
$date = Carbon::createFromFormat('Y-m', $yearMonth);
$yearMonthShortName = $date->format('M');

$yearMonthSums[$yearMonthShortName] = $sum;
unset($yearMonthSums[$yearMonth]);
}

return view('widgets.upcoming_demand', [
'config' => $this->config,
'yearMonthSums' => $yearMonthSums,
]);
}
}
14 changes: 7 additions & 7 deletions resources/views/widgets/resource_availability.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@
</div>
</div>
<div class="row">
@foreach($yearMonthSums as $month => $sum)
@foreach ($yearMonthSums as $month => $sum)
<div class="col">
<div style="margin-bottom: 0; padding-bottom: 0" class="row justify-content-center align-items-end">
<div class="col text-center">
<h1 style="margin-bottom: 0; padding-bottom: 0">{{ number_format(round($sum, 1), 1) }}</h1>
</div>
</div>
<div class="row justify-content-center align-items-start">
<div class="col text-center">
{{ $month }}
<div class="row justify-content-center align-items-start" style="margin-top: 0; padding-top: 0">
<div class="col text-center" style="margin-top: 0; padding-top: 0">
<span style="font-size: smaller; margin-top: 0; padding-top: 0;">{{ $month }}</span>
</div>
</div>
</div>
@endforeach
</div>

<div class="mb-0">
<span class="text-danger">{{ number_format((($yearMonthSums[array_key_last($yearMonthSums)] / $yearMonthSums[array_key_first($yearMonthSums)] - 1) * 100), 2) }}%</span>
<span class="text-muted">next month</span>
<div class="mb-0" style="margin-bottom: 0; padding-bottom: 0">
<span class="text-danger" style="font-size: smaller; margin-top: 0; padding-top: 0;">{{ number_format(($yearMonthSums[array_key_last($yearMonthSums)] / $yearMonthSums[array_key_first($yearMonthSums)] - 1) * 100, 2) }}%</span>
<span class="text-muted" style="font-size: smaller; margin-top: 0; padding-top: 0;">next month</span>
</div>
</div>
28 changes: 22 additions & 6 deletions resources/views/widgets/upcoming_demand.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="bg-secondary">
<div >
<div class="row">
<div class="col mt-0">
<h5 class="card-title">Upcoming Demands</h5>
<h5 class="card-title">Upcoming Unresolved Demands</h5>
</div>

<div class="col-auto">
Expand All @@ -10,9 +10,25 @@
</div>
</div>
</div>
<h1 class="mt-1 mb-3">2.382 FTE</h1>
<div class="mb-0">
<span class="text-danger">-3.65%</span>
<span class="text-muted">Since last month</span>
<div class="row">
@foreach ($yearMonthSums as $month => $sum)
<div class="col">
<div style="margin-bottom: 0; padding-bottom: 0" class="row justify-content-center align-items-end">
<div class="col text-center">
<h1 style="margin-bottom: 0; padding-bottom: 0">{{ number_format(round($sum, 1), 1) }}</h1>
</div>
</div>
<div class="row justify-content-center align-items-start" style="margin-top: 0; padding-top: 0">
<div class="col text-center" style="margin-top: 0; padding-top: 0">
<span style="font-size: smaller; margin-top: 0; padding-top: 0;">{{ $month }}</span>
</div>
</div>
</div>
@endforeach
</div>

<div class="mb-0" style="margin-bottom: 0; padding-bottom: 0">
<span class="text-danger" style="font-size: smaller; margin-top: 0; padding-top: 0;">{{ number_format(($yearMonthSums[array_key_last($yearMonthSums)] / $yearMonthSums[array_key_first($yearMonthSums)] - 1) * 100, 2) }}%</span>
<span class="text-muted" style="font-size: smaller; margin-top: 0; padding-top: 0;">next month</span>
</div>
</div>

0 comments on commit e7bf1f9

Please sign in to comment.