diff --git a/app/Http/Controllers/DocumentsController.php b/app/Http/Controllers/DocumentsController.php index e4ce51c..d845c30 100644 --- a/app/Http/Controllers/DocumentsController.php +++ b/app/Http/Controllers/DocumentsController.php @@ -6,51 +6,79 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Log; +use App\Models\GeneralDocument; +use App\Models\StaffMember; +use App\Models; + class DocumentsController extends Controller { + public function index(Request $request) + { + $query = GeneralDocument::query(); + + // Optional filtering by category + if ($request->filled('category')) { + $query->where('category', $request->category); + } + + // Search by name or description + if ($request->filled('search')) { + $search = $request->search; + $query->where(function($q) use ($search) { + $q->where('name', 'like', "%{$search}%") + ->orWhere('description', 'like', "%{$search}%"); + }); + } + + $documents = $query->orderBy('created_at', 'desc')->paginate(12); + //todo remove this after we migrate from old GUI + $categories = GeneralDocument::select('category')->distinct()->whereNotNull('category')->pluck('category'); + $document_types = Models\DocumentType::orderBy('name', 'asc')->pluck('name'); + return view('documents.index', compact('documents', 'categories', 'document_types')); + } + public function store(Request $request) { - // 1. Validate the incoming request $request->validate([ - 'file' => 'required|file|max:10240', // 10MB - 'type' => 'required|string', + 'name' => 'required|string|max:191', + 'category' => 'nullable|string|max:45', + 'description' => 'nullable|string', + 'file' => 'required|file|mimes:pdf,jpg,jpeg,png,doc,docx,xls,xlsx|max:10240', // 10MB limit ]); - // 2. Store locally on your ERP server $file = $request->file('file'); - $originalName = $file->getClientOriginalName(); - $localPath = $file->storeAs('documents/' . date('Y/m'), $originalName, 'local'); + $filename = time() . '_' . preg_replace('/\s+/', '_', $file->getClientOriginalName()); + + // Store files in a 'documents' folder inside storage/app/public + $filePath = $file->storeAs('documents', $filename, 'public'); - // 3. Check if it needs to be pushed to Paperless-Ngx - // You can base this on the checkbox OR force it based on type (e.g., $request->type === 'contract') - if ($request->boolean('push_to_paperless')) { - $this->pushToPaperless($localPath, $originalName, $request->all()); - } + // Get current staff ID if applicable + $staff = StaffMember::where('email', auth()->user()->email)->first(); - return response()->json(['message' => 'Document saved successfully.']); + GeneralDocument::create([ + 'name' => $request->name, + 'description' => $request->description, + 'category' => $request->category, + 'filename' => $filePath, + 'file_extension' => $file->getClientOriginalExtension(), + 'uploaded_by' => $staff ? $staff->id : null, + ]); + + return response()->json(['success' => true, 'message' => 'Document uploaded successfully!']); } - public function storeTwo(Request $request) + public function destroy($id) { - // ... validation and local storage logic ... - $localPath = $file->storeAs('documents/' . date('Y/m'), $originalName, 'local'); + $document = GeneralDocument::findOrFail($id); - // Retrieve the actual Eloquent models based on the form input - $entity = MnoPartner::find($request->input('entity_id')); - $docType = DocumentType::where('slug', $request->input('type'))->first(); - - if ($request->boolean('push_to_paperless')) { - // Dispatch the job to the queue - PushDocumentToPaperless::dispatch( - $localPath, - $originalName, - $entity, - $docType, - $request->input('notes') - ); + // Delete actual physical file from storage + if ($document->filename && Storage::disk('public')->exists($document->filename)) { + Storage::disk('public')->delete($document->filename); } - return response()->json(['message' => 'Document saved and queued for archival.']); + $document->delete(); + + return response()->json(['success' => true, 'message' => 'Document deleted successfully!']); } private function pushToPaperless(string $localPath, string $fileName, array $metadata) @@ -72,7 +100,7 @@ class DocumentsController extends Controller ->post("{$paperlessUrl}/api/documents/post_document/", [ // Optional Paperless metadata fields 'title' => $metadata['notes'] ?? $fileName, - // You can map ERP tags to Paperless Tag IDs here + // mapping ERP tags to Paperless Tag IDs // 'tags' => [1, 4], // 'correspondent' => 2 ]); diff --git a/app/Http/Controllers/StaffMembersController.php b/app/Http/Controllers/StaffMembersController.php index 36c802e..c59f3d0 100644 --- a/app/Http/Controllers/StaffMembersController.php +++ b/app/Http/Controllers/StaffMembersController.php @@ -148,4 +148,14 @@ class StaffMembersController extends Controller return $upcomingStaff; } + public function showDetails($id) + { + $staff = StaffMember::with('dependents')->findOrFail($id); + + return response()->json([ + 'success' => true, + 'staff' => $staff, + 'dependents' => $staff->dependents + ]); + } } \ No newline at end of file diff --git a/app/Models/DocumentType.php b/app/Models/DocumentType.php new file mode 100644 index 0000000..ad05ef9 --- /dev/null +++ b/app/Models/DocumentType.php @@ -0,0 +1,11 @@ +belongsTo(StaffMember::class, 'uploaded_by', 'id'); + } } diff --git a/resources/views/documents/index.blade.php b/resources/views/documents/index.blade.php index 30de7d5..67c6acf 100644 --- a/resources/views/documents/index.blade.php +++ b/resources/views/documents/index.blade.php @@ -1,283 +1,259 @@ - - -
- - - -