first commit
This commit is contained in:
0
Modules/Attendance/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Attendance/app/Http/Controllers/.gitkeep
Normal file
242
Modules/Attendance/app/Http/Controllers/AttendanceController.php
Normal file
242
Modules/Attendance/app/Http/Controllers/AttendanceController.php
Normal file
@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attendance\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\Admin\Models\Holiday;
|
||||
use Modules\Attendance\Models\Attendance;
|
||||
use Modules\Attendance\Repositories\AttendanceInterface;
|
||||
use Modules\Attendance\Repositories\AttendanceRepository;
|
||||
use Modules\Employee\Repositories\EmployeeInterface;
|
||||
use Modules\Leave\Models\Leave;
|
||||
|
||||
class AttendanceController extends Controller
|
||||
{
|
||||
private $attendanceRepository;
|
||||
private $employeeRepository;
|
||||
|
||||
public function __construct(
|
||||
AttendanceInterface $attendanceRepository, EmployeeInterface $employeeRepository) {
|
||||
$this->attendanceRepository = $attendanceRepository;
|
||||
$this->employeeRepository = $employeeRepository;
|
||||
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$data['title'] = 'Attendance Lists';
|
||||
|
||||
$data['employees'] = $this->employeeRepository->findAll()
|
||||
->transform(function ($employee) use ($data) {
|
||||
$employee->status = 'Absent';
|
||||
$leave = Leave::
|
||||
where('employee_id', $employee->id)
|
||||
->whereDate('start_date', date('Y-m-d'))
|
||||
->whereStatus(2)
|
||||
->first();
|
||||
if ($leave) {
|
||||
$employee->status = 'Leave';
|
||||
}
|
||||
|
||||
$holiday = Holiday::
|
||||
whereDate('start_date', date('Y-m-d'))
|
||||
->first();
|
||||
|
||||
if ($holiday) {
|
||||
$employee->status = 'Holiday';
|
||||
}
|
||||
|
||||
$attendances = Attendance::
|
||||
where('employee_id', $employee->id)
|
||||
->whereDate('date', date('Y-m-d'))
|
||||
->first();
|
||||
|
||||
if ($attendances) {
|
||||
$employee->atd_date = $attendances->date;
|
||||
$employee->clock_in = $attendances->clock_in_time;
|
||||
$employee->clock_out = $attendances->clock_out_time;
|
||||
$employee->total_hrs = Carbon::parse($employee->clock_out)->diffInMinutes(Carbon::parse($employee->clock_in));
|
||||
$employee->status = 'Present';
|
||||
}
|
||||
|
||||
return $employee;
|
||||
});
|
||||
return view('attendance::attendances.index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = 'Create Attendance';
|
||||
$data['editable'] = false;
|
||||
$data['employeeList'] = $this->employeeRepository->pluck();
|
||||
|
||||
return view('attendance::attendances.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
$attendance = Attendance::whereDate('date', now())->where('employee_id', $request->employee_id)->first();
|
||||
if ($attendance) {
|
||||
toastr()->error("Attendance Already Exists");
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
$request->merge([
|
||||
'date' => $request->date ? $request->date : now()->format('Y-m-d'),
|
||||
// 'clock_in_time' => $request->clock_in_time ? $request->clock_in_time : now()->format('H:i:s'),
|
||||
'clock_in_ip' => request()->ip(),
|
||||
'clock_out_ip' => request()->ip(),
|
||||
'createdBy' => auth()->user()->id,
|
||||
]);
|
||||
|
||||
$this->attendanceRepository->create($request->all());
|
||||
toastr()->success('Attendance Created Successfully');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
toastr()->error($th->getMessage());
|
||||
}
|
||||
|
||||
return redirect()->route('attendance.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('attendance::attendances.show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
try {
|
||||
$data['title'] = 'Edit Attendance';
|
||||
$data['editable'] = true;
|
||||
$data['employeeList'] = $this->employeeRepository->pluck();
|
||||
$data['attendance'] = $this->attendanceRepository->getAttendanceById($id);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
toastr()->error($th->getMessage());
|
||||
}
|
||||
|
||||
return view('attendance::attendances.edit', $data);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id): RedirectResponse
|
||||
{
|
||||
try {
|
||||
|
||||
$this->attendanceRepository->update($id, $request->all());
|
||||
toastr()->success('Attendance Updated Successfully');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
toastr()->error($th->getMessage());
|
||||
}
|
||||
return redirect()->route('attendance.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
$this->attendanceRepository->delete($id);
|
||||
toastr()->success('Attendance Deleted Successfully');
|
||||
} catch (\Throwable $th) {
|
||||
toastr()->error($th->getMessage());
|
||||
}
|
||||
return redirect()->route('attendance.index');
|
||||
}
|
||||
|
||||
public function clockInOut(Request $request): RedirectResponse
|
||||
{
|
||||
$request->merge([
|
||||
'date' => now()->format('Y-m-d'),
|
||||
'employee_id' => auth()->user()->employee_id,
|
||||
'createdBy' => auth()->user()->id,
|
||||
]);
|
||||
|
||||
$attendance = Attendance::whereDate('date', now())->where('employee_id', auth()->user()->employee_id)->first();
|
||||
if ($attendance) {
|
||||
if ($request->type == 'clockin') {
|
||||
$request->merge([
|
||||
'clock_in_time' => now()->format('H:i:s'),
|
||||
'clock_in_ip' => request()->ip(),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($request->type == 'clockout') {
|
||||
$request->merge([
|
||||
'clock_out_time' => now()->format('H:i:s'),
|
||||
'clock_out_ip' => request()->ip(),
|
||||
]);
|
||||
}
|
||||
$attendance->update($request->all());
|
||||
} else {
|
||||
if ($request->type == 'clockin') {
|
||||
$request->merge([
|
||||
'clock_in_time' => now()->format('H:i:s'),
|
||||
'clock_in_ip' => request()->ip(),
|
||||
]);
|
||||
}
|
||||
$this->attendanceRepository->create($request->all());
|
||||
}
|
||||
|
||||
// dd('asd');
|
||||
// try {
|
||||
|
||||
// $attendance = Attendance::whereDate('date', now())->where('employee_id', auth()->user()->employee_id)->latest()->first();
|
||||
|
||||
// if ($attendance && $attendance->clock_out_time == null) {
|
||||
|
||||
// $request->merge([
|
||||
// 'clock_out_time' => now()->format('H:i:s'),
|
||||
// 'clock_out_ip' => request()->ip(),
|
||||
// 'updatedBy' => auth()->user()->id,
|
||||
// ]);
|
||||
|
||||
// $this->attendanceRepository->update($attendance->attendance_id, $request->except('_token'));
|
||||
|
||||
// toastr()->success('Clocked Out Successfully.');
|
||||
|
||||
// } else {
|
||||
|
||||
// $request->merge([
|
||||
// 'date' => now()->format('Y-m-d'),
|
||||
// 'clock_in_time' => now()->format('H:i:s'),
|
||||
// 'clock_in_ip' => request()->ip(),
|
||||
// 'employee_id' => auth()->user()->employee_id,
|
||||
// 'createdBy' => auth()->user()->id,
|
||||
// ]);
|
||||
|
||||
// $this->attendanceRepository->create($request->all());
|
||||
|
||||
// toastr()->success('Clocked In Successfully');
|
||||
// }
|
||||
|
||||
// } catch (\Throwable $th) {
|
||||
// toastr()->error($th->getMessage());
|
||||
// }
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
67
Modules/Attendance/app/Http/Controllers/ReportController.php
Normal file
67
Modules/Attendance/app/Http/Controllers/ReportController.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attendance\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Carbon\Carbon;
|
||||
use Modules\Attendance\Models\Attendance;
|
||||
use Modules\Attendance\Repositories\AttendanceInterface;
|
||||
use Modules\Attendance\Repositories\AttendanceRepository;
|
||||
use Modules\Employee\Repositories\EmployeeInterface;
|
||||
use Modules\Leave\Models\Leave;
|
||||
|
||||
class ReportController extends Controller
|
||||
{
|
||||
private $attendanceRepository;
|
||||
private $employeeRepository;
|
||||
|
||||
public function __construct(
|
||||
AttendanceInterface $attendanceRepository,
|
||||
EmployeeInterface $employeeRepository) {
|
||||
$this->attendanceRepository = $attendanceRepository;
|
||||
$this->employeeRepository = $employeeRepository;
|
||||
|
||||
}
|
||||
|
||||
public function monthly()
|
||||
{
|
||||
$data['title'] = 'Employee Attendance Lists';
|
||||
$firstDayOfMonth = Carbon::createFromDate(date('Y'), date('m'), 1)->startOfMonth();
|
||||
$lastDayOfMonth = Carbon::createFromDate(date('Y'), date('m'), 1)->endOfMonth();
|
||||
|
||||
for ($day = $firstDayOfMonth; $day <= $lastDayOfMonth; $day->addDay()) {
|
||||
$data['dayLists'][] = $day->format('Y-m-d');
|
||||
}
|
||||
$data['employees'] = $this->employeeRepository->findAll()
|
||||
->transform(function ($employee) use ($data) {
|
||||
$attendances = Attendance::
|
||||
where('employee_id', $employee->id)
|
||||
->whereIn('date', $data['dayLists'])
|
||||
->select('clock_in_time', 'clock_out_time', 'date')
|
||||
->get()
|
||||
// ->groupBy('date')
|
||||
->mapWithKeys(function ($atd) {
|
||||
return [$atd['date'] => 'P'];
|
||||
});
|
||||
|
||||
$leave = Leave::
|
||||
where('employee_id', $employee->id)
|
||||
->whereIn('start_date', $data['dayLists'])
|
||||
->whereStatus(2)
|
||||
->get()
|
||||
->mapWithKeys(function ($atd) {
|
||||
return [$atd['start_date'] => 'L'];
|
||||
});
|
||||
|
||||
$final = $attendances->push($leave);
|
||||
$attendanceArray = [];
|
||||
foreach ($data['dayLists'] as $date) {
|
||||
$attendanceArray[$date] = array_key_exists($date, $final->toArray()) ? $final[$date] : '';
|
||||
}
|
||||
$employee->dates = $attendanceArray;
|
||||
return $employee;
|
||||
});
|
||||
return view('attendance::report.monthly', $data);
|
||||
}
|
||||
|
||||
}
|
0
Modules/Attendance/app/Http/Requests/.gitkeep
Normal file
0
Modules/Attendance/app/Http/Requests/.gitkeep
Normal file
0
Modules/Attendance/app/Models/.gitkeep
Normal file
0
Modules/Attendance/app/Models/.gitkeep
Normal file
39
Modules/Attendance/app/Models/Attendance.php
Normal file
39
Modules/Attendance/app/Models/Attendance.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attendance\Models;
|
||||
|
||||
use App\Models\Scopes\CreatedByScope;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Attendance extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'tbl_attendances';
|
||||
protected $primaryKey = 'attendance_id';
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'employee_id',
|
||||
'clock_in_time',
|
||||
'clock_out_time',
|
||||
'clock_in_ip',
|
||||
'clock_out_ip',
|
||||
'work_from_type',
|
||||
'type',
|
||||
'date',
|
||||
'status',
|
||||
'total_hours',
|
||||
'description',
|
||||
'remarks',
|
||||
'createdBy',
|
||||
'updatedBy',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::addGlobalScope(new CreatedByScope);
|
||||
}
|
||||
}
|
0
Modules/Attendance/app/Providers/.gitkeep
Normal file
0
Modules/Attendance/app/Providers/.gitkeep
Normal file
120
Modules/Attendance/app/Providers/AttendanceServiceProvider.php
Normal file
120
Modules/Attendance/app/Providers/AttendanceServiceProvider.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attendance\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\Attendance\Repositories\AttendanceInterface;
|
||||
use Modules\Attendance\Repositories\AttendanceRepository;
|
||||
use Modules\Employee\Repositories\EmployeeInterface;
|
||||
use Modules\Employee\Repositories\EmployeeRepository;
|
||||
|
||||
class AttendanceServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Attendance';
|
||||
|
||||
protected string $moduleNameLower = 'attendance';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
$this->app->bind(AttendanceInterface::class, AttendanceRepository::class);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register commands in the format of Command::class
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
// $this->commands([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register command Schedules.
|
||||
*/
|
||||
protected function registerCommandSchedules(): void
|
||||
{
|
||||
// $this->app->booted(function () {
|
||||
// $schedule = $this->app->make(Schedule::class);
|
||||
// $schedule->command('inspire')->hourly();
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register translations.
|
||||
*/
|
||||
public function registerTranslations(): void
|
||||
{
|
||||
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
|
||||
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
|
||||
$sourcePath = module_path($this->moduleName, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
|
||||
|
||||
$componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.ltrim(config('modules.paths.generator.component-class.path'), config('modules.paths.app_folder','')));
|
||||
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function getPublishableViewPaths(): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach (config('view.paths') as $path) {
|
||||
if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->moduleNameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
49
Modules/Attendance/app/Providers/RouteServiceProvider.php
Normal file
49
Modules/Attendance/app/Providers/RouteServiceProvider.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attendance\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Called before routes are registered.
|
||||
*
|
||||
* Register any model bindings or pattern based filters.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the routes for the application.
|
||||
*/
|
||||
public function map(): void
|
||||
{
|
||||
$this->mapApiRoutes();
|
||||
|
||||
$this->mapWebRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "web" routes for the application.
|
||||
*
|
||||
* These routes all receive session state, CSRF protection, etc.
|
||||
*/
|
||||
protected function mapWebRoutes(): void
|
||||
{
|
||||
Route::middleware('web')->group(module_path('Attendance', '/routes/web.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapApiRoutes(): void
|
||||
{
|
||||
Route::middleware('api')->prefix('api')->name('api.')->group(module_path('Attendance', '/routes/api.php'));
|
||||
}
|
||||
}
|
0
Modules/Attendance/app/Repositories/.gitkeep
Normal file
0
Modules/Attendance/app/Repositories/.gitkeep
Normal file
12
Modules/Attendance/app/Repositories/AttendanceInterface.php
Normal file
12
Modules/Attendance/app/Repositories/AttendanceInterface.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attendance\Repositories;
|
||||
|
||||
interface AttendanceInterface
|
||||
{
|
||||
public function findAll();
|
||||
public function getAttendanceById($attendanceId);
|
||||
public function delete($attendanceId);
|
||||
public function create(array $attendanceDetails);
|
||||
public function update($attendanceId, array $newDetails);
|
||||
}
|
35
Modules/Attendance/app/Repositories/AttendanceRepository.php
Normal file
35
Modules/Attendance/app/Repositories/AttendanceRepository.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Attendance\Repositories;
|
||||
|
||||
use Modules\Attendance\Models\Attendance;
|
||||
|
||||
|
||||
class AttendanceRepository implements AttendanceInterface
|
||||
{
|
||||
public function findAll()
|
||||
{
|
||||
return Attendance::get();
|
||||
}
|
||||
|
||||
public function getAttendanceById($attendanceId)
|
||||
{
|
||||
return Attendance::findOrFail($attendanceId);
|
||||
}
|
||||
|
||||
public function delete($attendanceId)
|
||||
{
|
||||
Attendance::destroy($attendanceId);
|
||||
}
|
||||
|
||||
public function create(array $attendanceDetails)
|
||||
{
|
||||
return Attendance::create($attendanceDetails);
|
||||
}
|
||||
|
||||
public function update($attendanceId, array $newDetails)
|
||||
{
|
||||
return Attendance::where('attendance_id', $attendanceId)->update($newDetails);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user