employee module fix

This commit is contained in:
Dharmaraj Shrestha 2024-04-05 17:56:16 +05:45
parent dbe6632313
commit 8a9cb39352
20 changed files with 287 additions and 254 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace Modules\Employee\Models;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
protected $table = 'employees';
protected $primaryKey = 'employee_id';
protected $guarded = [];
}

View File

@ -4,6 +4,8 @@ namespace Modules\Employee\Providers;
use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\Employee\Repositories\EmployeeRepository;
class EmployeeServiceProvider extends ServiceProvider class EmployeeServiceProvider extends ServiceProvider
{ {
@ -29,6 +31,7 @@ class EmployeeServiceProvider extends ServiceProvider
*/ */
public function register(): void public function register(): void
{ {
$this->app->bind(EmployeeInterface::class, EmployeeRepository::class);
$this->app->register(RouteServiceProvider::class); $this->app->register(RouteServiceProvider::class);
} }

View File

@ -0,0 +1,12 @@
<?php
namespace Modules\Employee\Repositories;
interface EmployeeInterface
{
public function findAll();
public function getEmployeeById($employeeId);
public function delete($employeeId);
public function create(array $EmployeeDetails);
public function update($employeeId, array $newDetails);
}

View File

@ -0,0 +1,34 @@
<?php
namespace Modules\Employee\Repositories;
use Modules\Employee\Models\Employee;
class EmployeeRepository implements EmployeeInterface
{
public function findAll()
{
return Employee::get();
}
public function getEmployeeById($employeeId)
{
return Employee::findOrFail($employeeId);
}
public function delete($employeeId)
{
Employee::destroy($employeeId);
}
public function create(array $employeeDetails)
{
return Employee::create($employeeDetails);
}
public function update($employeeId, array $newDetails)
{
return Employee::whereId($employeeId)->update($newDetails);
}
}

View File

@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Modules\Leave\Repositories\LeaveInterface; use Modules\Leave\Repositories\LeaveInterface;
use Yoeunes\Toastr\Facades\Toastr;
class LeaveController extends Controller class LeaveController extends Controller
{ {
@ -14,6 +15,12 @@ class LeaveController extends Controller
public function __construct(LeaveInterface $leaveRepository) public function __construct(LeaveInterface $leaveRepository)
{ {
$this->leaveRepository = $leaveRepository; $this->leaveRepository = $leaveRepository;
$this->middleware('role_or_permission:access leaves|create leaves|edit leaves|delete leaves', ['only' => ['index', 'show']]);
$this->middleware('role_or_permission:create leaves', ['only' => ['create', 'store']]);
$this->middleware('role_or_permission:edit leaves', ['only' => ['edit', 'update']]);
$this->middleware('role_or_permission:delete leaves', ['only' => ['destroy']]);
} }
/** /**
@ -22,8 +29,7 @@ class LeaveController extends Controller
public function index() public function index()
{ {
$data['leaves'] = $this->leaveRepository->findAll(); $data['leaves'] = $this->leaveRepository->findAll();
// dd($data['leaves']); return view('leave::index',$data);
return view('leave::index');
} }
/** /**
@ -43,7 +49,7 @@ class LeaveController extends Controller
$inputData = $request->all(); $inputData = $request->all();
try { try {
$this->leaveRepository->create($inputData); $this->leaveRepository->create($inputData);
toastr()->success('Leave Created Succesfully'); Toastr()->success('Leave Created Succesfully');
} catch (\Throwable $th) { } catch (\Throwable $th) {
toastr()->error($th->getMessage()); toastr()->error($th->getMessage());
} }
@ -63,7 +69,9 @@ class LeaveController extends Controller
*/ */
public function edit($id) public function edit($id)
{ {
return view('leave::edit'); $data['title'] = 'Edit Leave';
$data['leave'] = $this->leaveRepository->getLeaveById($id);
return view('leave::edit',$data);
} }
/** /**
@ -71,7 +79,14 @@ class LeaveController extends Controller
*/ */
public function update(Request $request, $id): RedirectResponse public function update(Request $request, $id): RedirectResponse
{ {
// $inputData = $request->all();
try {
$this->leaveRepository->update($id,$inputData);
toastr()->success('Leave Updated Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
} }
/** /**
@ -79,6 +94,7 @@ class LeaveController extends Controller
*/ */
public function destroy($id) public function destroy($id)
{ {
// $this->leaveRepository->delete($id);
toastr()->success('Leave Deleted Succesfully');
} }
} }

View File

@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model;
class Leave extends Model class Leave extends Model
{ {
protected $table = 'leaves'; protected $table = 'leaves';
protected $primaryKey = 'leave_id';
protected $guarded = []; protected $guarded = [];
} }

View File

@ -28,7 +28,7 @@ class LeaveRepository implements LeaveInterface
public function update($leaveId, array $newDetails) public function update($leaveId, array $newDetails)
{ {
return Leave::whereId($leaveId)->update($newDetails); return Leave::where('leave_id',$leaveId)->update($newDetails);
} }
} }

View File

@ -0,0 +1,47 @@
@extends('layouts.app')
@section('content')
<div class="page-content">
<div class="container-fluid">
<!-- start page title -->
<div class="row">
<div class="col-12">
<div class="page-title-box d-sm-flex align-items-center justify-content-between">
<h4 class="mb-sm-0">{{ $title }}</h4>
<div class="page-title-right">
<ol class="breadcrumb m-0">
<li class="breadcrumb-item"><a href="javascript: void(0);">Dashboards</a></li>
<li class="breadcrumb-item active">{{ $title }}</li>
</ol>
</div>
</div>
</div>
</div>
<!-- end page title -->
<div class="row">
<div class="col-lg-8">
<div class="card">
<div class="card-body">
<form action="{{ route('leave.update', $leave->leave_id) }}" class="needs-validation" novalidate
method="post">
@csrf
@method('put')
<input type="hidden" name="leave_id" value="{{ $leave->leave_id }}">
@include('leave::partials.action')
</form>
</div>
</div>
</div>
</div>
<!--end row-->
</div>
<!-- container-fluid -->
</div>
@endsection
@push('js')
<script src="{{ asset('assets/js/pages/form-validation.init.js') }}"></script>
@endpush

View File

@ -49,250 +49,97 @@
<div class="card-header align-items-center d-flex"> <div class="card-header align-items-center d-flex">
<h5 class="card-title flex-grow-1 mb-0">Leave Lists</h5> <h5 class="card-title flex-grow-1 mb-0">Leave Lists</h5>
<div class="flex-shrink-0"> <div class="flex-shrink-0">
<a href="{{ route('leave.create') }}" class="btn btn-success waves-effect waves-light"><i <a href="{{ route('leave.create') }}" class="btn btn-success waves-effect waves-light btn-sm"><i
class="ri-add-fill me-1 align-bottom"></i> Add</a> class="ri-add-fill me-1 align-bottom"></i> Add</a>
</div> </div>
</div> </div>
<div class="card-body"> <div class="card-body">
<div class="table-responsive"> <div class="table-responsive">
<table id="buttons-datatables" class="display table-bordered table" style="width:100%"> <table id="buttons-datatables" class="display table-sm table-bordered table" style="width:100%">
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>S.N</th>
<th>Position</th> <th>Employee Name</th>
<th>Office</th> <th>Start Date</th>
<th>Age</th> <th>End Date</th>
<th>Start date</th> <th>Created At</th>
<th>Salary</th> <th>Action</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@forelse ($leaves as $key => $leave)
<tr> <tr>
<td>Tiger Nixon</td> <td>{{ $key + 1 }}</td>
<td>System Architect</td> <td>{{ $leave->employee_id }}</td>
<td>Edinburgh</td> <td>{{ $leave->start_date }}</td>
<td>61</td> <td>{{ $leave->end_date }}</td>
<td>2011/04/25</td> <td>{{ $leave->created_at }}</td>
<td>$320,800</td> <td>
</tr> <div class="hstack flex-wrap gap-3">
<tr> <a href="javascript:void(0);" class="link-info fs-15 view-item-btn" data-bs-toggle="modal"
<td>Garrett Winters</td> data-bs-target="#viewModal">
<td>Accountant</td> <i class="ri-eye-line"></i>
<td>Tokyo</td> </a>
<td>63</td> <a href="{{ route('leave.edit', $leave->leave_id) }}"
<td>2011/07/25</td> class="link-success fs-15 edit-item-btn"><i class="ri-edit-2-line"></i></a>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
</tr>
<tr>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>$372,000</td>
</tr>
<tr>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>$137,500</td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>$327,900</td>
</tr>
<tr>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009/09/15</td>
<td>$205,500</td>
</tr>
<tr>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008/12/13</td>
<td>$103,600</td>
</tr>
<tr>
<td>Jena Gaines</td>
<td>Office Manager</td>
<td>London</td>
<td>30</td>
<td>2008/12/19</td>
<td>$90,560</td>
</tr>
<tr>
<td>Quinn Flynn</td>
<td>Support Lead</td>
<td>Edinburgh</td>
<td>22</td>
<td>2013/03/03</td>
<td>$342,000</td>
</tr>
<tr>
<td>Charde Marshall</td>
<td>Regional Director</td>
<td>San Francisco</td>
<td>36</td>
<td>2008/10/16</td>
<td>$470,600</td>
</tr>
<tr>
<td>Haley Kennedy</td>
<td>Senior Marketing Designer</td>
<td>London</td>
<td>43</td>
<td>2012/12/18</td>
<td>$313,500</td>
</tr>
<tr>
<td>Tatyana Fitzpatrick</td>
<td>Regional Director</td>
<td>London</td>
<td>19</td>
<td>2010/03/17</td>
<td>$385,750</td>
</tr>
<tr>
<td>Michael Silva</td>
<td>Marketing Designer</td>
<td>London</td>
<td>66</td>
<td>2012/11/27</td>
<td>$198,500</td>
</tr>
<tr>
<td>Paul Byrd</td>
<td>Chief Financial Officer (CFO)</td>
<td>New York</td>
<td>64</td>
<td>2010/06/09</td>
<td>$725,000</td>
</tr>
<tr>
<td>Gloria Little</td>
<td>Systems Administrator</td>
<td>New York</td>
<td>59</td>
<td>2009/04/10</td>
<td>$237,500</td>
</tr>
<tr>
<td>Bradley Greer</td>
<td>Software Engineer</td>
<td>London</td>
<td>41</td>
<td>2012/10/13</td>
<td>$132,000</td>
</tr>
<tr>
<td>Dai Rios</td>
<td>Personnel Lead</td>
<td>Edinburgh</td>
<td>35</td>
<td>2012/09/26</td>
<td>$217,500</td>
</tr>
<tr>
<td>Jenette Caldwell</td>
<td>Development Lead</td>
<td>New York</td>
<td>30</td>
<td>2011/09/03</td>
<td>$345,000</td>
</tr>
<tr>
<td>Yuri Berry</td>
<td>Chief Marketing Officer (CMO)</td>
<td>New York</td>
<td>40</td>
<td>2009/06/25</td>
<td>$675,000</td>
</tr>
<tr>
<td>Caesar Vance</td>
<td>Pre-Sales Support</td>
<td>New York</td>
<td>21</td>
<td>2011/12/12</td>
<td>$106,450</td>
</tr>
<tr>
<td>Doris Wilder</td>
<td>Sales Assistant</td>
<td>Sydney</td>
<td>23</td>
<td>2010/09/20</td>
<td>$85,600</td>
</tr>
<tr> <a href="{{ route('leave.destroy', $leave->leave_id) }}" data-id="{{ $leave->leave_id }}"
<td>Gavin Cortez</td> class="link-danger fs-15 remove-item-btn"><i class="ri-delete-bin-line"></i></a>
<td>Team Leader</td>
<td>San Francisco</td> </div>
<td>22</td> </td>
<td>2008/10/26</td>
<td>$235,500</td>
</tr>
<tr>
<td>Martena Mccray</td>
<td>Post-Sales support</td>
<td>Edinburgh</td>
<td>46</td>
<td>2011/03/09</td>
<td>$324,050</td>
</tr>
<tr>
<td>Unity Butler</td>
<td>Marketing Designer</td>
<td>San Francisco</td>
<td>47</td>
<td>2009/12/09</td>
<td>$85,675</td>
</tr> </tr>
@empty
@endforelse
</tbody>
</table> </table>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div><!--end row--> </div><!--end row-->
</div> </div>
<!-- container-fluid --> <!-- container-fluid -->
</div> </div>
@include('leave::partials.view')
@endsection @endsection
@push('js')
<script>
$('.remove-item-btn').on('click', function(e) {
e.preventDefault();
let url = $(this).attr('href');
let id = $(this).data('id');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: url,
type: 'DELETE',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {
id: id
},
success: function(response) {
location.reload();
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
}
});
}
});
});
</script>
@endpush

View File

@ -1,28 +1,36 @@
<div class="mb-3"> <div class="mb-3">
<label for="employeeName" class="form-label">Employee Name</label> <label for="employee_id" class="form-label">Employee Name</label>
<input type="text" class="form-control" id="employeeName" placeholder="Enter employee name" name="employeeName" required> <input type="text" class="form-control" id="employee_id" placeholder="Enter employee name" name="employee_id"
value="{{ old('end_date', $leave->employee_id ?? '') }}" required>
<div class="invalid-feedback"> <div class="invalid-feedback">
Please enter employee name. Please enter employee name.
</div> </div>
</div> </div>
<div class="mb-3"> {{-- <div class="mb-3">
<label for="employeeUrl" class="form-label">Employee Department URL</label> <label for="employeeUrl" class="form-label">Employee Department URL</label>
<input type="url" class="form-control" id="employeeUrl" placeholder="Enter emploree url" name="employeeUrl"> <input type="url" class="form-control" id="employeeUrl" placeholder="Enter emploree url" name="employeeUrl">
</div> </div> --}}
<div class="mb-3"> <div class="mb-3">
<label for="StartleaveDate" class="form-label">Start Leave Date</label> <label for="start_date" class="form-label">Start Leave Date</label>
<input type="date" class="form-control" id="StartleaveDate" name="start_date"> <input type="date" class="form-control" id="start_date" name="start_date"
value="{{ old('start_date', $leave->start_date ?? '') }}">
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="EndleaveDate" class="form-label">End Leave Date</label> <label for="end_date" class="form-label">End Leave Date</label>
<input type="date" class="form-control" id="EndleaveDate" name="end_date"> <input type="date" class="form-control" id="end_date" name="end_date"
value="{{ old('end_date', $leave->end_date ?? '') }}">
</div> </div>
{{--
<div class="mb-3"> <div class="mb-3">
<label for="VertimeassageInput" class="form-label">Message</label> <label for="VertimeassageInput" class="form-label">Message</label>
<textarea class="form-control" id="VertimeassageInput" rows="3" placeholder="Enter your message" name="remark"></textarea> <textarea class="form-control" id="VertimeassageInput" rows="3" placeholder="Enter your message" name="remark"></textarea>
</div> </div> --}}
<div class="text-end"> <div class="text-end">
<button type="submit" class="btn btn-primary">Add Leave</button> <button type="submit" class="btn btn-primary">{{ isset($leave) ? 'Update' : 'Add Leave' }}</button>
</div> </div>
@push('js') @push('js')

View File

@ -0,0 +1,16 @@
<div class="modal fade" id="viewModal" tabindex="-1" aria-labelledby="viewModalLabel" aria-modal="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalgridLabel">View Leave</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="{{ route('leave.store') }}" class="needs-validation" novalidate method="post">
@csrf
@include('leave::partials.action')
</form>
</div>
</div>
</div>
</div>

View File

@ -15,7 +15,7 @@ class DatabaseSeeder extends Seeder
*/ */
public function run(): void public function run(): void
{ {
// \App\Models\User::factory(10)->create(); \App\Models\User::factory(10)->create();
$admin = \App\Models\User::factory()->create([ $admin = \App\Models\User::factory()->create([
'name' => 'Admin User', 'name' => 'Admin User',
@ -34,10 +34,10 @@ class DatabaseSeeder extends Seeder
$adminRole = Role::create(['name' => 'admin']); $adminRole = Role::create(['name' => 'admin']);
$memberRole = Role::create(['name' => 'member']); $memberRole = Role::create(['name' => 'member']);
$permission = Permission::create(['name' => 'create products']); $permission = Permission::create(['name' => 'create leaves']);
$permission = Permission::create(['name' => 'access products']); $permission = Permission::create(['name' => 'access leaves']);
$permission = Permission::create(['name' => 'edit products']); $permission = Permission::create(['name' => 'edit leaves']);
$permission = Permission::create(['name' => 'delete products']); $permission = Permission::create(['name' => 'delete leaves']);
$permission = Permission::create(['name' => 'access roles']); $permission = Permission::create(['name' => 'access roles']);
$permission = Permission::create(['name' => 'edit roles']); $permission = Permission::create(['name' => 'edit roles']);

View File

@ -0,0 +1,36 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
class PermissionSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$permission = Permission::create(['name' => 'create leaves']);
$permission = Permission::create(['name' => 'access leaves']);
$permission = Permission::create(['name' => 'edit leaves']);
$permission = Permission::create(['name' => 'delete leaves']);
$permission = Permission::create(['name' => 'access roles']);
$permission = Permission::create(['name' => 'edit roles']);
$permission = Permission::create(['name' => 'create roles']);
$permission = Permission::create(['name' => 'delete roles']);
$permission = Permission::create(['name' => 'access users']);
$permission = Permission::create(['name' => 'edit users']);
$permission = Permission::create(['name' => 'create users']);
$permission = Permission::create(['name' => 'delete users']);
$permission = Permission::create(['name' => 'access permissions']);
$permission = Permission::create(['name' => 'edit permissions']);
$permission = Permission::create(['name' => 'create permissions']);
$permission = Permission::create(['name' => 'delete permissions']);
}
}