bug fixes, daily reports, holidays
This commit is contained in:
81
app/Http/Controllers/PublicHolidaysController.php
Normal file
81
app/Http/Controllers/PublicHolidaysController.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\OfficeLocation;
|
||||
use App\Models\PublicHoliday;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class PublicHolidaysController extends Controller
|
||||
{
|
||||
public function index($officeId)
|
||||
{
|
||||
$holidays = PublicHoliday::where('office_location_id', $officeId)
|
||||
->orderBy('holiday_date', 'asc')
|
||||
->get();
|
||||
|
||||
return response()->json($holidays);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'office_location_id' => 'required|exists:office_locations,id',
|
||||
'name' => 'required|string|max:255',
|
||||
'holiday_date' => 'required|date',
|
||||
'is_recurring' => 'boolean',
|
||||
]);
|
||||
|
||||
$validated['is_recurring'] = $request->has('is_recurring');
|
||||
|
||||
PublicHoliday::create($validated);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
public function destroy(PublicHoliday $holiday)
|
||||
{
|
||||
$holiday->delete();
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
public function importFromApi(Request $request, $officeId)
|
||||
{
|
||||
$location = OfficeLocation::findOrFail($officeId);
|
||||
$year = $request->input('year', now()->year);
|
||||
$countryCode = $request->input('country_code');
|
||||
|
||||
if (!$countryCode) {
|
||||
return response()->json(['success' => false, 'message' => 'Country code missing.'], 400);
|
||||
}
|
||||
|
||||
$response = Http::get("https://date.nager.at/api/v3/PublicHolidays/{$year}/{$countryCode}");
|
||||
|
||||
if ($response->successful()) {
|
||||
$apiHolidays = $response->json();
|
||||
$count = 0;
|
||||
|
||||
foreach ($apiHolidays as $holiday) {
|
||||
PublicHoliday::updateOrCreate(
|
||||
[
|
||||
'office_location_id' => $location->id,
|
||||
'holiday_date' => $holiday['date'],
|
||||
],
|
||||
[
|
||||
'name' => $holiday['name'],
|
||||
'is_recurring' => $holiday['fixed'] ?? false,
|
||||
]
|
||||
);
|
||||
$count++;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => "Successfully imported {$count} holidays."
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json(['success' => false, 'message' => 'Failed to fetch from API.'], 500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user