129 lines
5.0 KiB
PHP
129 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\OfficeLocation;
|
|
use App\Models\StaffMember;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\PublicHoliday;
|
|
|
|
|
|
class OfficeLocationController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$locations = OfficeLocation::with('manager', 'documents')
|
|
->orderBy('created_at', 'desc')
|
|
->paginate(9);
|
|
|
|
$staffMembers = StaffMember::where('status', 'ACTIVE')->orderBy('name')->get();
|
|
|
|
$countries = DB::table('countries')->orderBy('en_short_name', 'asc')->get();
|
|
|
|
return view('offices.index', compact('locations', 'staffMembers', 'countries'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'country' => 'required|string|max:191',
|
|
'city' => 'required|string|max:191',
|
|
'physical_address' => 'nullable|string',
|
|
'postal_address' => 'nullable|string|max:191',
|
|
'zip_code' => 'nullable|string|max:15',
|
|
'office_phone' => 'nullable|string|max:20',
|
|
'country_manager_id' => 'nullable|integer',
|
|
]);
|
|
|
|
OfficeLocation::create($validated);
|
|
|
|
return response()->json(['success' => true, 'message' => 'Office location added successfully!']);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$location = OfficeLocation::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'country' => 'required|string|max:191',
|
|
'city' => 'required|string|max:191',
|
|
'physical_address' => 'nullable|string',
|
|
'postal_address' => 'nullable|string|max:191',
|
|
'zip_code' => 'nullable|string|max:15',
|
|
'office_phone' => 'nullable|string|max:20',
|
|
'country_manager_id' => 'nullable|integer',
|
|
]);
|
|
|
|
$location->update($validated);
|
|
|
|
return response()->json(['success' => true, 'message' => 'Office location updated successfully!']);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
OfficeLocation::findOrFail($id)->delete();
|
|
return response()->json(['success' => true, 'message' => 'Office location removed.']);
|
|
}
|
|
|
|
// --- ADD THESE METHODS TO OfficeLocationController.php ---
|
|
|
|
public function showDocuments($id)
|
|
{
|
|
$location = OfficeLocation::with('documents')->findOrFail($id);
|
|
return view('offices.documents', compact('location'));
|
|
}
|
|
|
|
public function storeDocument(Request $request, $id)
|
|
{
|
|
$location = OfficeLocation::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:191',
|
|
'file' => 'required|file|mimes:pdf,jpg,jpeg,png,doc,docx|max:10240',
|
|
'validity_period' => 'required|string',
|
|
// Require a date UNLESS the period is 'On Demand'
|
|
'next_renewal_date' => 'nullable|required_unless:validity_period,On Demand|date'
|
|
]);
|
|
|
|
$file = $request->file('file');
|
|
$filename = time() . '_' . preg_replace('/\s+/', '_', $file->getClientOriginalName());
|
|
$filePath = $file->storeAs("office_documents/{$location->id}", $filename, 'public');
|
|
|
|
// Get current staff ID
|
|
$staff = \App\Models\StaffMember::where('email', auth()->user()->email)->first();
|
|
|
|
\App\Models\OfficeLocationDocument::create([
|
|
'office_location_id' => $location->id,
|
|
'name' => $request->name,
|
|
'file_path' => $filePath,
|
|
'file_extension' => $file->getClientOriginalExtension(),
|
|
'validity_period' => $request->validity_period,
|
|
'next_renewal_date' => $request->validity_period === 'On Demand' ? null : $request->next_renewal_date,
|
|
'uploaded_by' => $staff ? $staff->id : null,
|
|
]);
|
|
|
|
return response()->json(['success' => true, 'message' => 'Document archived successfully!']);
|
|
}
|
|
|
|
public function destroyDocument($docId)
|
|
{
|
|
$document = \App\Models\OfficeLocationDocument::findOrFail($docId);
|
|
|
|
if ($document->file_path && \Illuminate\Support\Facades\Storage::disk('public')->exists($document->file_path)) {
|
|
\Illuminate\Support\Facades\Storage::disk('public')->delete($document->file_path);
|
|
}
|
|
|
|
$document->delete();
|
|
return response()->json(['success' => true, 'message' => 'Document deleted.']);
|
|
}
|
|
|
|
}
|
|
|
|
// Swift track malawi
|
|
// eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3MTYiLCJvaWQiOjcxNiwidWlkIjoiNDU3OTIzYzgtYmNkMS00ZWJhLWI3NzYtMGYyOTJmNmYzNWM4IiwiYXBpZCI6NTcxLCJpYXQiOjE3ODg1MzYwMzgsImV4cCI6MjEyODUzNjAzOH0.xbcNFzMNcryMACwYjOQMfF4Q06S80ttH4gYrcbduekTRXQcE4LptFEmir7iyeshghXy-C7Jcqno4UCKRIVnNig
|
|
|
|
|
|
|
|
// klick
|
|
// eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI2OSIsIm9pZCI6NjksInVpZCI6IjEyMTdiMmE4LWNlYTYtNGE3ZC1hODlmLTFiODBhNDQxMzRjYSIsImFwaWQiOjQwMCwiaWF0IjoxNzI2NDc2MTc4LCJleHAiOjIwNjY0NzYxNzh9.7Yv2dyT3wEGz28ddFEMdEN0EsmD18hLSaHfNxIPCAskPjHFQ8819u5cLwfzNeZy5Xa3Xaz1ZNc66j0Fd8xSWDw
|