CasperSecurity
<?php
namespace App\Http\Livewire\Projects;
use App\Models\DailyManpowerUtilisation;
use App\Models\EmployeeRegistration;
use App\Models\ManageManpower;
use App\Models\ManageProject;
use App\Models\Role;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class DailyManpowerUtilisationLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_name = "Daily Manpower";
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $project_id, $employee_id, $work_date, $no_of_hours, $status;
public $modelId, $employeeCheck, $workDate, $projectId;
public $employees = [];
public $selectedEmployee = [];
public $showmodal = false;
public $successmsg = "The manpower utilisation data has been saved successfully.";
public $updatemsg = "The manpower utilisation data has been updated successfully.";
public $deletemsg = "The manpower utilisation data has been deleted successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete', 'update'];
public $rules = [
'project_id' => 'required',
'employee_id' => 'required',
'no_of_hours' => 'required',
'work_date' => 'required|date',
];
public $messages = [
'project_id.required' => 'This field is required',
'employee_id.required' => 'This field is required',
'no_of_hours.required' => 'This field is required',
'work_date.required' => 'This field is required',
'work_date.date' => 'The date must be a valid date.',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->cleanVar();
$this->resetValidation();
$this->showmodal = true;
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($workDate, $projectId)
{
$this->loadData($workDate, $projectId);
$this->modelId = true;
}
public function deleteclick($workDate, $projectId)
{
$this->workDate = $workDate;
$this->projectId = $projectId;
$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->work_date = null;
$this->no_of_hours = null;
$this->status = null;
$this->workDate = null;
$this->projectId = null;
$this->employees = [];
$this->employeeCheck = [];
$this->selectedEmployee = [];
$this->showmodal = false;
}
public function loadData($workDate, $projectId)
{
$this->employees = [];
$response = DailyManpowerUtilisation::where('project_id', $projectId)->where('work_date', $workDate)->get();
if ($response) {
$this->project_id = $projectId;
$this->work_date = $workDate;
foreach ($response as $key => $manpower) {
$this->employeeCheck[$key] = true;
$this->selectedEmployee[$key] = $manpower['employee_id'];
$this->no_of_hours[$key] = $manpower['no_of_hours'];
}
//dd($this->selectedEmployee);
if (sizeof($this->selectedEmployee) > 0) {
foreach ($this->selectedEmployee as $employeeId) {
$employee = EmployeeRegistration::select('id', 'employee_id', 'name')->find($employeeId);
if ($employee) {
$this->employees[] = $employee;
}
}
}
}
// dd($this->employees);
$this->showmodal = true;
}
public function modelData()
{
if ($this->modelId) {
$modified_by = Auth::user()->id;
$created_by = null;
} else {
$created_by = Auth::user()->id;
$modified_by = null;
}
$data = [
'project_id' => $this->project_id,
'employee_id' => $this->employee_id,
'work_date' => $this->work_date,
'no_of_hours' => $this->no_of_hours,
'created_by' => $created_by,
'modified_by' => $modified_by,
'status' => $this->modelId ? $this->status : '1',
];
return $data;
}
public function validateField()
{
$rules = [
'project_id' => 'required',
'work_date' => 'required|date',
];
$messages = [
'project_id.required' => 'This field is required',
'work_date.required' => 'This field is required',
'work_date.date' => 'The date must be a valid date.',
];
if (!empty($this->selectedEmployee)) {
foreach ($this->selectedEmployee as $key => $hours) {
$rules["no_of_hours.$key"] = 'required';
$messages["no_of_hours.$key.required"] = 'This field is required';
}
}
$this->validate($rules, $messages);
}
public function save()
{
$this->validateField();
$existingData = DailyManpowerUtilisation::where('project_id', $this->project_id)->where('work_date', $this->work_date)->latest()->first();
if ($existingData) {
$this->dispatchBrowserEvent('existingUtilisation', [
'message' => 'Data exists ! Are you sure want to update this data ?',
'funcname' => 'update'
]);
} else {
if ($this->selectedEmployee && sizeof($this->selectedEmployee) > 0) {
$created_by = Auth::user()->id;
$manpowerUtilisation = [];
foreach ($this->selectedEmployee as $key => $employee) {
$manpowerUtilisation[] = [
'project_id' => $this->project_id,
'employee_id' => $this->selectedEmployee[$key],
'no_of_hours' => $this->no_of_hours[$key],
'work_date' => $this->work_date,
'created_by' => $created_by,
];
}
$response = DailyManpowerUtilisation::insert($manpowerUtilisation);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Select atleast one employee.'
]);
}
}
}
public function update()
{
if ($this->selectedEmployee && sizeof($this->selectedEmployee) > 0) {
DailyManpowerUtilisation::where('project_id', $this->project_id)
->where('work_date', $this->work_date)
->delete();
$manpowerUtilisation = [];
foreach ($this->selectedEmployee as $key => $employee) {
$manpowerUtilisation[] = [
'project_id' => $this->project_id,
'employee_id' => $this->selectedEmployee[$key],
'no_of_hours' => $this->no_of_hours[$key],
'work_date' => $this->work_date,
'modified_by' => auth()->id(),
];
}
$inserted = DailyManpowerUtilisation::insert($manpowerUtilisation);
if ($inserted) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Select atleast one employee.'
]);
}
}
public function delete()
{
$deleteCount = 0;
$response = DailyManpowerUtilisation::where('work_date', $this->workDate)->where('project_id', $this->projectId)->get();
if (sizeof($response) > 0) {
foreach ($response as $res) {
$res->delete();
$deleteCount += 1;
}
$this->cleanVar();
}
if ($deleteCount > 0) {
$this->dispatchBrowserEvent('showsuccessmsg', ['type' => 'success',
'message' => $this->deletemsg]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
public function read()
{
$dailyManpower = DailyManpowerUtilisation::with('project')->select('project_id', 'work_date')
->selectRaw('COUNT(employee_id) as total_employees')
->selectRaw('SUM(no_of_hours) as total_hours')
->groupBy('work_date', 'project_id')
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $dailyManpower;
}
public function updatedProjectId($val)
{
$employeeIds = [];
$this->employees = [];
if ($val) {
$employeeIds = ManageManpower::where('project_id', $val)->where('status', 1)->pluck('employee_id')->toArray();
foreach ($employeeIds as $employeeId) {
$employee = EmployeeRegistration::select('id', 'employee_id', 'name')->find($employeeId);
if ($employee) {
$this->employees[] = $employee;
}
}
}
}
public function employeeClick($id, $keyVal)
{
if ($keyVal != "") {
if ($this->employeeCheck[$keyVal]) {
$this->selectedEmployee[$keyVal] = $id;
} else {
unset($this->selectedEmployee, $keyVal);
unset($this->no_of_hours, $keyVal);
}
}
}
public function render()
{
$dataTables = [];
$dataTables = $this->read();
$projects = ManageProject::select('id', 'project_name')->get();
return view('livewire.projects.daily-manpower-utilisation-livewire', compact('dataTables', 'projects'));
}
}