CasperSecurity
<?php
namespace App\Http\Livewire\Projects;
use App\Models\EmployeeRegistration;
use App\Models\ManageManpower;
use App\Models\ManageProject;
use App\Models\Role;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class ManageManpowerLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_name = "Manage Manpower";
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $employees = [], $engagedEmployee = [];
public $employeesId, $employee_id, $designation, $employee_name, $created_by;
public $project_id, $start_date, $end_date, $status;
public $modelId, $removeClick = false;
public $showmodal = false;
public $successmsg = "The manpower data has been saved successfully.";
public $updatemsg = "The manpower data has been updated successfully.";
public $deletemsg = "The manpower data has been deleted successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'project_id' => 'required',
'employeesId' => 'required',
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
];
public $updateRules = [
'project_id' => 'required',
'employee_id' => 'required',
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
];
public $messages = [
'project_id.required' => 'This field is required',
'employeesId.required' => 'This field is required',
'employee_id.required' => 'This field is required',
'start_date.required' => 'This field is required',
'start_date.date' => 'The date must be a valid date.',
'end_date.required' => 'This field is required',
'end_date.date' => 'The date must be a valid date.',
'end_date.after_or_equal' => 'The end date must be after or equal to the start date.',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->cleanVar();
$this->resetValidation();
$this->showmodal = true;
$this->dispatchBrowserEvent('cleanEmployee');
}
public function removeClick()
{
$this->cleanVar();
$this->resetValidation();
$this->showmodal = true;
$this->removeClick = true;
$this->dispatchBrowserEvent('cleanEmployee');
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($id)
{
$this->modelId = $id;
$this->loadData($this->modelId);
$this->showmodal = true;
$this->removeClick = false;
}
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->project_id = null;
$this->employee_id = null;
$this->start_date = null;
$this->end_date = null;
$this->status = null;
$this->showmodal = false;
$this->removeClick = false;
$this->engagedEmployee = [];
$this->dispatchBrowserEvent('loadEmployeeSelect');
}
public function loadData($id)
{
$response = ManageManpower::find($id);
if ($response) {
$this->project_id = $response['project_id'];
$this->start_date = $response['start_date'];
$this->employee_id = $response['employee_id'];
$this->end_date = $response['end_date'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$created_by = Auth::user()->id;
$manpowerData = [];
foreach ($this->employeesId as $employee) {
$data = explode(',', $employee);
$manpowerData[] = [
'project_id' => $this->project_id,
'employee_id' => $data[0],
'designation' => $data[1],
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'created_by' => $created_by,
];
}
$response = ManageManpower::insert($manpowerData);
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 removeAndSave()
{
if (count($this->engagedEmployee) > 0) {
$idArray = array_column($this->engagedEmployee, 'id');
ManageManpower::where('project_id', $this->project_id)
->where('start_date', $this->start_date)
->where('end_date', $this->end_date)
->whereIn('employee_id', $idArray)
->update(['status' => 1]);
ManageManpower::where('project_id', $this->project_id)
->where('start_date', $this->start_date)
->where('end_date', $this->end_date)
->whereNotIn('employee_id', $idArray)
->update(['status' => 0]);
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Project should have at least one employee'
]);
}
}
public function update()
{
$this->validate($this->updateRules, $this->messages);
$modified_by = Auth::user()->id;
$employee = EmployeeRegistration::select('designation')->find($this->employee_id);
$updateData = [
'project_id' => $this->project_id,
'employee_id' => $this->employee_id,
'designation' => $employee ? $employee->designation : null,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'modified_by' => $modified_by,
];
$response = ManageManpower::find($this->modelId);
if ($response) {
$response->update($updateData);
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 = ManageManpower::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 getEmployees()
{
$employeeIds = ManageManpower::where('status', 0)->pluck('employee_id')->toArray();
$this->employees = EmployeeRegistration::select('id', 'name', 'designation')
->whereNotIn('id', $employeeIds)
->get();
}
public function removeEmployee($key)
{
if (isset($this->engagedEmployee[$key])) {
unset($this->engagedEmployee[$key]);
}
}
public function updatedProjectId($val)
{
$this->engagedEmployee = [];
if ($val) {
$employeeData = [];
$this->project_id = $val;
$firstEmployee = ManageManpower::where('project_id', $val)->first();
if ($firstEmployee) {
$this->start_date = $firstEmployee->start_date;
$this->end_date = $firstEmployee->end_date;
$this->created_by = $firstEmployee->created_by;
$employeeIds = ManageManpower::where('project_id', $val)->where('status', 1)->pluck('employee_id');
$employeeData = EmployeeRegistration::whereIn('id', $employeeIds)->get(['id', 'name', 'designation'])->toArray();
}
$this->engagedEmployee = $employeeData;
}
}
public function read()
{
$manPower = ManageManpower::search($this->search)->where('status',1)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
foreach ($manPower as $power) {
$power->employee_name = $power->getEmployeeName->name;
}
return $manPower;
}
public function mount()
{
$this->getEmployees();
}
public function render()
{
$dataTables = [];
$dataTables = $this->read();
foreach ($dataTables as $key => $data) {
$project = ManageProject::find($data->project_id);
$dataTables[$key]['project_name'] = $project ? $project->project_name : 'N/A';
$employee = EmployeeRegistration::find($data->employee_id);
$dataTables[$key]['employee_name'] = $employee ? $employee->name : 'N/A';
}
$projects = ManageProject::select('id', 'project_name')->get();
return view('livewire.projects.manage-manpower-livewire', compact('dataTables', 'projects'));
}
}