CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Models\HolidayInfo;
use App\Models\Holidays;
use Livewire\Component;
use Livewire\WithPagination;
use Carbon\Carbon;
class HolidayInfoLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $holiday_type_id,$holiday_name,$start_date,$end_date,$day,$status,$data;
public $modelid;
public $showmodal=false;
public $successmsg="The Holiday Type has been saved successfully.";
public $updatemsg="The Holiday Type has been updated successfully.";
public $deletemsg="The Holiday Type data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Holiday Type name has already exists in our database. Please Try Another Name";
public $errormsg="Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules=[
'holiday_type_id'=>'required|max:25',
'holiday_name'=>'required|max:25',
'start_date'=>'required',
'end_date'=>'required',
'day'=>'required',
];
public $updatedrules=[
'holiday_type_id'=>'required|max:25',
'holiday_name'=>'required|max:25',
'start_date'=>'required',
'end_date'=>'required',
'day'=>'required',
'status'=>'required',
];
public $messages=[
'holiday_type_id.required'=>'This field is required',
'holiday_name.required'=>'This field is required',
'start_date.required'=>'This field is required',
'end_date.required'=>'This field is required',
'day.required'=>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->resetValidation();
$this->showmodal=true;
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
}
public function updateclick($id){
$this->modelid=$id;
$this->loadData($this->modelid);
$this->showmodal=true;
}
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->modelid=null;
$this->holiday_type_id=null;
$this->holiday_name=null;
$this->start_date=null;
$this->end_date=null;
$this->day=null;
$this->status=null;
$this->showmodal=false;
}
public function loadData($id){
$response=HolidayInfo::find($id);
if($response){
$this->holiday_type_id=$response['holiday_type_id'];
$this->holiday_name=$response['holiday_name'];
$this->start_date=$response['start_date'];
$this->end_date=$response['end_date'];
$this->day=$response['day'];
$this->status=$response['status'];
}
}
public function updatedEndDate($value){
//dd($value);
if($this->start_date){
$formatted_st_Date=Carbon::parse($this->start_date);
$formatted_end_Date=Carbon::parse($value);
$this->day=$formatted_st_Date->diffInDays($formatted_end_Date)+1;
}
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=HolidayInfo::where('holiday_type_id',$this->holiday_name)->latest()->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else{
$response=HolidayInfo::create([
'holiday_type_id'=>$this->holiday_type_id,
'holiday_name'=>$this->holiday_name,
'start_date'=>$this->start_date,
'end_date'=>$this->end_date,
'day'=>$this->day,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->successmsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function update(){
$this->validate($this->updatedrules,$this->messages);
$response=HolidayInfo::find($this->modelid);
if($response){
$response->update([
'holiday_type_id'=>$this->holiday_type_id,
'holiday_name'=>$this->holiday_name,
'start_date'=>$this->start_date,
'end_date'=>$this->end_date,
'day'=>$this->day,
'status'=>$this->status,
]);
//dd($response);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=HolidayInfo::find($this->modelid);
if($response){
if($response->delete()){
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->deletemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function read(){
$holiday_infos=HolidayInfo::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $holiday_infos;
}
public function render()
{
$datatable=[];
$datatable=$this->read();
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$holiday = Holidays::where('id',$data->holiday_type_id)->first();
//dd($holiday);
//$datatable[$key]['holiday_type'] = $holiday ? $holiday->holiday_type:'N/A';
if($holiday){
$datatable[$key]['holiday_type'] =$holiday->holiday_type;
}
else{
$datatable[$key]['holiday_type'] ="NA";
}
}
}
// $holidaystype=Holidays::select('id', 'holiday_type')->get();
$holidaystype=Holidays::where('status',1)->get();
// dd($datatable);
return view('livewire.hrms.holiday-info-livewire',compact('datatable','holidaystype'));
}
}