Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions app/Http/Controllers/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,42 @@ public function Populate(Request $request, $id)
return redirect()->route('devices.index')->with('error', 'Error al actualizar biométrico');
}
}

public function deleteEmployeeRecord(Request $request)
{
$oficinas = Oficina::all();
$title = "Delete Employee Record from Device";
return view('devices.delete_employee', compact('oficinas', 'title'));
}

public function runDeleteFingerRecord(Request $request)
{
$idagente = $request->input('idagente');
$idoficina = $request->input('oficina');

$devices = Device::where('idoficina', $idoficina)->get();

if ($devices->isEmpty()) {
return redirect()->back()->with('error', 'No devices found for this office');
}

try {
$cmdIdService = resolve(CommandIdService::class);

foreach ($devices as $device) {
$nextCmdId = $cmdIdService->getNextCmdId();
$device->commands()->create([
'device_id' => $device->id,
'command' => $nextCmdId,
'data' => "C:{$nextCmdId}:DATA DELETE USERINFO PIN={$idagente}",
'executed_at' => null
]);
}

return redirect()->route('devices.index')->with('success', "Command to delete user {$idagente} sent to " . $devices->count() . " devices.");
} catch (\Exception $e) {
Log::error('Error deleting employee record', ['error' => $e->getMessage()]);
return redirect()->back()->with('error', 'Error sending delete command');
}
}
}
39 changes: 25 additions & 14 deletions app/Http/Controllers/iclockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,33 @@ public function getrequest(Request $request)
$nextCmdId = $cmdIdService->getNextCmdId();
Log::info('Get Request', ['nextCmdId' => $nextCmdId]);

$timezone = 'America/Mexico_City'; // Default fallback
if ($device && $device->oficina && $device->oficina->timezone) {
$timezone = $device->oficina->timezone;
}

$intDateTime = $this->oldEncodeTime(
Carbon::now('America/Mexico_City')->year,
Carbon::now('America/Mexico_City')->month,
Carbon::now('America/Mexico_City')->day,
Carbon::now('America/Mexico_City')->hour,
Carbon::now('America/Mexico_City')->minute,
Carbon::now('America/Mexico_City')->second
Carbon::now($timezone)->year,
Carbon::now($timezone)->month,
Carbon::now($timezone)->day,
Carbon::now($timezone)->hour,
Carbon::now($timezone)->minute,
Carbon::now($timezone)->second
);
/*
// Add a set time command to the database
$device->commands()->create([
'device_id' => $device->id,
'command' => $nextCmdId,
'data' => "C:{$nextCmdId}:SET OPTIONS DateTime=" . $intDateTime,
'executed_at' => null
]);*/

// Add a set time command to the database synchronously if clock is out of sync
// For now, mirroring the logic to always send it or send it as a regular command
// We will send it as a pending command if there's a discrepancy
if ($device->getTimezoneDiscrepancyCount() > 0) {
$device->commands()->create([
'device_id' => $device->id,
'command' => $nextCmdId,
'data' => "C:{$nextCmdId}:SET OPTIONS DateTime=" . $intDateTime,
'executed_at' => null
]);
// refresh pending commands
$commands = $device->pendingCommands();
}

Log::info('getrequest commands', ['commands' => count($commands)]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('device_options', function (Blueprint $table) {
$table->unsignedInteger('idoficina')->after('timestamp')->nullable();
$table->unsignedInteger('idempresa')->after('idoficina')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('device_options', function (Blueprint $table) {
$table->dropColumn(['idoficina', 'idempresa']);
});
}
};
32 changes: 32 additions & 0 deletions resources/views/devices/delete_employee.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@extends('layouts.app')

@section('content')
<div class="container">
<h2>{{ $title }}</h2>
<form method="post" action="{{ route('devices.runDeleteFingerRecord') }}">
@csrf
<div class="form-group mb-3">
<label for="oficina">{{ __('devices.oficina') }}</label>
<select name="oficina" class="form-control" id="oficina" required>
<option value="">Select Office</option>
@foreach ($oficinas as $oficina)
<option value="{{ $oficina->idoficina }}">{{ $oficina->ubicacion }} ({{ $oficina->idoficina }})</option>
@endforeach
</select>
<small class="form-text text-muted">The delete command will be sent to all devices in this office.</small>
</div>

<div class="form-group mb-3">
<label for="idagente">Employee PIN (ID Agente)</label>
<input type="text" name="idagente" class="form-control" id="idagente" placeholder="Enter PIN to delete" required>
</div>

<div class="alert alert-warning">
<strong>Warning!</strong> This will queue a command to remove the user from the biometric devices. This action cannot be undone from the server easily.
</div>

<button type="submit" class="btn btn-danger">Delete from Devices</button>
<a href="{{ route('devices.index') }}" class="btn btn-secondary">Cancel</a>
</form>
</div>
@endsection
3 changes: 3 additions & 0 deletions resources/views/devices/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<h2>{{ $title }}</h2>
<div class="d-flex gap-2 mb-3">
<a href="{{ route('devices.create') }}" class="btn btn-primary">{{ __('devices.create_device') }}</a>
<a href="{{ route('devices.deleteEmployeeRecord') }}" class="btn btn-danger">
<i class="fas fa-user-minus"></i> Delete User from Device
</a>
<a href="{{ route('devices.monitor') }}" class="btn btn-success">
<i class="fas fa-traffic-light"></i> {{ __('devices.monitor_status') }}
</a>
Expand Down