CasperSecurity
<?php
namespace App\Http\Livewire\Documentmanagement;
use App\Models\DocumentClassification;
use App\Models\DocumentPermission;
use App\Models\DocumentType;
use App\Models\EmployeeRegistration;
use App\Models\StoreDocument;
use App\Models\Team;
use Carbon\Carbon;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
use App\Models\User;
class StoreDocumentLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $store_doc_id,$letter_no,$received_sent, $doc_date,$document_type_id,$doc_classification_id,$from_to,$doc_reference, $doc_subject,$doc_title,$doc_desc,$doc_status,$reminder_date,$doc_copy_path,$pemission_level, $department_id,$view_flag,$download_flag,$employee_id,$status, $data;
public $selectedDocument;
public $modelid;
public $showmodal = false;
public $modal = false;
public $view = false;
public $download = false;
public $successmsg = "The Document data has been saved successfully.";
public $updatemsg = "The Document data has been updated successfully.";
public $deletemsg = "The Document data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Document Classification has already exists in our database. Please Try Another Name";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'doc_date' => 'required',
'document_type_id' => 'required',
'doc_classification_id' => 'required',
'received_sent' => 'required',
'from_to' => 'required',
'doc_subject' => 'required',
'doc_title' => 'required',
'doc_desc' => 'required',
];
public $updatedrules = [
'doc_date' => 'required',
'document_type_id' => 'required',
'doc_classification_id' => 'required',
'received_sent' => 'required',
'from_to' => 'required',
'doc_subject' => 'required',
'doc_title' => 'required',
'doc_desc' => 'required',
'status' => 'required',
];
public $messages = [
'doc_date.required' => 'This field is required',
'document_type_id.required' => 'This field is required',
'doc_classification_id.required' => 'This field is required',
'received_sent.required' => 'This field is required',
'from_to.required' => 'This field is required',
'doc_subject.required' => 'This field is required',
'doc_title.required' => 'This field is required',
'doc_desc.required' => 'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function modalclick($id)
{
$this->store_doc_id=$id;
$this->resetValidation();
$this->cleanVar();
$this->modal=true;
}
public function showmodalclick()
{
$this->modelid=null;
$this->resetValidation();
$this->cleanVar();
$this->showmodal=true;
}
public function closemodalclick()
{
$this->modelid=null;
$this->resetValidation();
$this->showmodal = false;
$this->modal=false;
}
public function mount()
{
$this->datatable = StoreDocument::all(); // Fetch all documents when the component mounts
}
public function viewclick($id)
{
$this->selectedDocument = StoreDocument::find($id);
if (!$this->selectedDocument) {
session()->flash('error', 'Document not found.');
return;
}
$this->view = true;
}
public function closeview()
{
$this->view = false;
$this->selectedDocument = null;
}
public function downloadclick()
{
}
public function deleteclick($id)
{
$this->modelid = $id;
$this->dispatchBrowserEvent('confirmdelete', [
'message' => 'Are you sure want to delete this data ?',
'funcname' => 'delete'
]);
}
public function cleanVar()
{
$this->letter_no=null ;
$this->doc_date=null;
$this->document_type_id=null;
$this->doc_classification_id=null;
$this->received_sent=null;
$this->from_to=null;
$this->doc_reference=null;
$this->doc_subject=null;
$this->doc_title=null;
$this->doc_desc=null;
$this->doc_copy_path=null;
$this->reminder_date=null;
$this->status=null;
$this->showmodal=false;
}
public function save()
{
$this->validate($this->rules, $this->messages);
$data = StoreDocument::where('doc_title', $this->doc_title)->latest()->first();
if ($data) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->duplicateerrormsg
]);
} else{
$classdoc=DocumentClassification::where('id',$this->doc_classification_id)->first();
$current_year = Carbon::now()->year;
$res=StoreDocument::all();
if(sizeof($res)<10){
$add="0000".sizeof($res)+1;
}
elseif(sizeof($res)<100){
$add="000".sizeof($res)+1;
}
elseif(sizeof($res)<1000){
$add="00".sizeof($res)+1;
}
elseif(sizeof($res)<10000){
$add="0".sizeof($res)+1;
}
else{
$add=sizeof($res+1);
}
$this->letter_no= $classdoc->classification_shortname.$current_year.$add;
{
$data = StoreDocument::create([
'letter_no' => $this->letter_no,
'doc_date' => $this->doc_date,
'document_type_id' => $this->document_type_id,
'doc_classification_id' => $this->doc_classification_id,
'received_sent' => $this->received_sent,
'from_to' => $this->from_to,
'doc_reference' => $this->doc_reference,
'doc_subject' => $this->doc_subject,
'doc_title' => $this->doc_title,
'doc_desc' => $this->doc_desc,
'doc_copy_path' => $this->doc_copy_path->store('store_documents'),
'reminder_date' => $this->reminder_date,
]);
if ($data) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}}
}
public function updateclick($id)
{
$this->modelid = $id;
$store_doc = StoreDocument::find($id);
if ($store_doc != null) {
$this->letter_no = $store_doc['letter_no'];
$this->doc_date = $store_doc['doc_date'];
$this->document_type_id = $store_doc['document_type_id'];
$this->doc_classification_id = $store_doc['doc_classification_id'];
$this->received_sent = $store_doc['received_sent'];
$this->from_to = $store_doc['from_to'];
$this->doc_reference = $store_doc['doc_reference'];
$this->doc_subject = $store_doc['doc_subject'];
$this->doc_title = $store_doc['doc_title'];
$this->doc_desc = $store_doc['doc_desc'];
$this->reminder_date = $store_doc['reminder_date'];
$this->status = $store_doc['status'];
$this->showmodal = true;
}
//dd($store_doc);
}
public function submitclick()
{
$response = StoreDocument::where('id', $this->store_doc_id)->first();
if($response != null){
$response->doc_status = 2;
if($response->save()) {
$permission = DocumentPermission::create([
'store_doc_id' => $response->id,
'pemission_level' => $this->pemission_level,
'department_id' => $this->department_id,
'employee_id' => $this->employee_id,
'view_flag' => $this->view_flag,
'download_flag' => $this->download_flag,
]);
}
}
if (!$permission) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Failed to set document permissions'
]);
return;
}
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => 'Document permission has been given'
]);
}
public function update()
{
$this->validate($this->updatedrules, $this->messages);
$doc =null;
$response = StoreDocument::find($this->modelid);
if ($response) {
if($this->doc_copy_path != null){
$doc = $this->doc_copy_path->store('store_documents');
}else{
$doc = $response['doc_copy_path'];
}
$response->update([
'letter_no' => $this->letter_no,
'doc_date' => $this->doc_date,
'document_type_id' => $this->document_type_id,
'doc_classification_id' => $this->doc_classification_id,
'received_sent' => $this->received_sent,
'from_to' => $this->from_to,
'doc_reference' => $this->doc_reference,
'doc_subject' => $this->doc_subject,
'doc_title' => $this->doc_title,
'doc_desc' => $this->doc_desc,
'doc_copy_path' => $doc,
'reminder_date' => $this->reminder_date,
'status' => $this->status,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read()
{
$query = StoreDocument::select('store_documents.*', 'document_permissions.view_flag', 'document_permissions.download_flag')
->join('document_permissions', 'store_documents.id', '=', 'document_permissions.store_doc_id')
->join('employee_registrations', 'document_permissions.employee_id', '=', 'employee_registrations.employee_id')
->where('employee_registrations.user_id',auth()->user()->id)
->where('store_documents.status', '1');
if ($this->search != null) {
$query->where(function($q) {
$q->where('doc_reference', 'LIKE', '%' . $this->search . '%')
->orWhere('doc_subject', 'LIKE', '%' . $this->search . '%')
->orWhere('doc_title', 'LIKE', '%' . $this->search . '%')
->orWhere('document_type_id', 'LIKE', '%' . $this->search . '%')
->orWhere('doc_classification_id', 'LIKE', '%' . $this->search . '%')
->orWhere('received_sent', 'LIKE', '%' . $this->search . '%')
->orWhere('from_to', 'LIKE', '%' . $this->search . '%');
});
}
$store_documents = $query->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $store_documents;
}
/* if ($this->search != null) {
if ($this->filter_by == 'name') {
$salary_calculations->where(function($query) {
$query->where('employee_registrations.name', 'like', '%' . $this->search . '%');
});
} elseif ($this->filter_by == 'designation_name') {
$salary_calculations->where(function($query) {
$query->where('designations.designation_name', 'like', '%' . $this->search . '%');
});
}
}*/
public function render()
{
$datatable = [];
$datatable = $this->read();
$users = User::where('id', '>=', 1)->get();
$department = Team::where('id', '>=', 1)->get();
$employee = EmployeeRegistration::where('team_id', $this->department_id)->get();
$doctype = DocumentType::where('status',1)->get();
$classdoc = DocumentClassification::where('status',1)->get();
return view('livewire.documentmanagement.store-document-livewire',compact('datatable','doctype','classdoc','users','department','employee'));
}
}