101 lines
3.2 KiB
PHP
101 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\AdvancedPlanning\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Carbon;
|
|
use Modules\AdvancedPlanning\Http\Controllers\Concerns\AuthorizesAdvancedPlanning;
|
|
use Modules\AdvancedPlanning\Models\ScheduleRun;
|
|
use Modules\AdvancedPlanning\Services\FiniteCapacitySchedulerService;
|
|
use Modules\AdvancedPlanning\Services\IntegrationBridgeService;
|
|
use Modules\AdvancedPlanning\Utils\AdvancedPlanningUtil;
|
|
|
|
class ScheduleRunController extends Controller
|
|
{
|
|
use AuthorizesAdvancedPlanning;
|
|
|
|
public function __construct(
|
|
protected FiniteCapacitySchedulerService $scheduler,
|
|
protected IntegrationBridgeService $bridge,
|
|
protected AdvancedPlanningUtil $util
|
|
) {
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->authorizeAp('ap.schedule.view');
|
|
|
|
$runs = ScheduleRun::forBusiness($this->businessId())
|
|
->withCount('scheduledOperations')
|
|
->orderByDesc('id')
|
|
->paginate(20);
|
|
|
|
return view('advancedplanning::schedule_runs.index', compact('runs'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeAp('ap.schedule.run');
|
|
|
|
$defaultDays = (int) config('advancedplanning.default_horizon_days', 30);
|
|
$horizonStart = now()->startOfDay();
|
|
$horizonEnd = now()->addDays($defaultDays)->endOfDay();
|
|
|
|
return view('advancedplanning::schedule_runs.create', compact('horizonStart', 'horizonEnd', 'defaultDays'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeAp('ap.schedule.run');
|
|
|
|
$data = $request->validate([
|
|
'horizon_start' => 'required|date',
|
|
'horizon_end' => 'required|date|after_or_equal:horizon_start',
|
|
'scenario_name' => 'nullable|string|max:255',
|
|
'notes' => 'nullable|string',
|
|
'sync_resources' => 'nullable|boolean',
|
|
]);
|
|
|
|
if ($request->boolean('sync_resources')) {
|
|
$this->bridge->syncCapacityResources($this->businessId());
|
|
}
|
|
|
|
try {
|
|
$run = $this->scheduler->run(
|
|
$this->businessId(),
|
|
Carbon::parse($data['horizon_start'])->startOfDay(),
|
|
Carbon::parse($data['horizon_end'])->endOfDay(),
|
|
$data['scenario_name'] ?? null,
|
|
['notes' => $data['notes'] ?? null]
|
|
);
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
|
|
return back()->with('status', [
|
|
'success' => false,
|
|
'msg' => $e->getMessage(),
|
|
])->withInput();
|
|
}
|
|
|
|
return redirect()->action([self::class, 'show'], $run->id)->with('status', [
|
|
'success' => true,
|
|
'msg' => __('advancedplanning::lang.schedule_completed', [
|
|
'ref' => $run->ref_no,
|
|
'count' => $run->scheduledOperations->count(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function show(int $id)
|
|
{
|
|
$this->authorizeAp('ap.schedule.view');
|
|
|
|
$run = ScheduleRun::forBusiness($this->businessId())
|
|
->with(['scheduledOperations.resource', 'scheduledOperations.workOrder', 'createdBy'])
|
|
->findOrFail($id);
|
|
|
|
return view('advancedplanning::schedule_runs.show', compact('run'));
|
|
}
|
|
}
|