feat: timesheet auto hm, sync excel vehicle locations, heavy equipment grouped by type, 2-party bast signature, and compact role permission ui
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\UnitSupportTimesheet;
|
||||
use App\Models\Vehicle;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class RecalculateTimesheetHoursCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'qma:recalculate-timesheet-hours';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Hitung otomatis dan perbarui finish HM serta durasi jam operasional (1 HM = 1 Jam) dari entri kronologis berikutnya';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$this->info('Memulai rekalkulasi finish HM & jam operasi timesheet unit support...');
|
||||
|
||||
$vehicles = Vehicle::whereHas('supportTimesheets')->get();
|
||||
$totalUpdated = 0;
|
||||
|
||||
foreach ($vehicles as $veh) {
|
||||
$timesheets = UnitSupportTimesheet::where('vehicle_id', $veh->id)
|
||||
->orderBy('date', 'asc')
|
||||
->orderBy('start_time', 'asc')
|
||||
->orderBy('id', 'asc')
|
||||
->get();
|
||||
|
||||
$count = $timesheets->count();
|
||||
for ($i = 0; $i < $count - 1; $i++) {
|
||||
$current = $timesheets[$i];
|
||||
$next = $timesheets[$i + 1];
|
||||
|
||||
// Jika current belum memiliki end_meter atau bernilai 0
|
||||
if ($current->end_meter === null || (float) $current->end_meter == 0) {
|
||||
if ($next->start_meter !== null && (float) $next->start_meter >= (float) $current->start_meter) {
|
||||
$diff = (float) $next->start_meter - (float) $current->start_meter;
|
||||
$current->update([
|
||||
'end_meter' => $next->start_meter,
|
||||
'operating_hours' => $diff,
|
||||
]);
|
||||
$totalUpdated++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->info("Rekalkulasi selesai. Sebanyak {$totalUpdated} entri timesheet berhasil diperbarui.");
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Vehicle;
|
||||
use App\Models\VehicleLocationHistory;
|
||||
use App\Services\SimpleXlsxImporter;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SyncExcelVehicleDataCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'qma:sync-excel-locations {--file=Berkas/DATA LOKASI TERBARU RKM KMH.xlsx}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Perbarui lokasi, status site, tipe, dan spesifikasi unit alat berat (Heavy Equipment) dari berkas Excel DATA LOKASI TERBARU RKM KMH';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$filePath = base_path($this->option('file'));
|
||||
if (!file_exists($filePath)) {
|
||||
$this->error("Berkas Excel tidak ditemukan di: {$filePath}");
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->info("Membaca berkas: {$filePath} ...");
|
||||
$rows = SimpleXlsxImporter::parse($filePath);
|
||||
$totalRows = count($rows);
|
||||
$this->info("Total baris terbaca: {$totalRows}");
|
||||
|
||||
if ($totalRows < 2) {
|
||||
$this->warn('Berkas Excel kosong atau tidak memiliki baris data.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$updatedCount = 0;
|
||||
$createdCount = 0;
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
for ($i = 1; $i < $totalRows; $i++) {
|
||||
$r = $rows[$i];
|
||||
$code1 = trim($r[4] ?? '');
|
||||
if (!$code1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cat = trim($r[2] ?? '') ?: 'SUPPORT';
|
||||
$type = trim($r[3] ?? '') ?: 'HEAVY EQUIPMENT';
|
||||
$statusRaw = strtoupper(trim($r[6] ?? ''));
|
||||
$jenis = strtoupper(trim($r[7] ?? ''));
|
||||
$lokasiSite = trim($r[8] ?? '');
|
||||
$plate = trim($r[9] ?? '');
|
||||
$assetNo = trim($r[10] ?? '');
|
||||
$brand = trim($r[11] ?? '');
|
||||
$model = trim($r[12] ?? '');
|
||||
$serial = trim($r[13] ?? '');
|
||||
$prodYear = trim($r[16] ?? '');
|
||||
$lokasiCurrent = trim($r[18] ?? '');
|
||||
$owner = trim($r[20] ?? '');
|
||||
|
||||
// 1. Status Operasional
|
||||
$status = match ($statusRaw) {
|
||||
'OPERASI', 'OPERATION' => 'Operation',
|
||||
'BREAKDOWN' => 'Breakdown',
|
||||
'STANDBY' => 'Standby',
|
||||
'MAINTENANCE' => 'Maintenance',
|
||||
default => 'Standby',
|
||||
};
|
||||
|
||||
// 2. Lokasi & Status Area (Inside / Outside Site)
|
||||
if (strtoupper($lokasiCurrent) === 'SITE' || empty($lokasiCurrent)) {
|
||||
$finalLocation = $lokasiSite ?: 'Site Kerinci';
|
||||
$siteStatus = 'inside_site';
|
||||
} else {
|
||||
$finalLocation = $lokasiCurrent;
|
||||
$siteStatus = 'outside_site';
|
||||
}
|
||||
|
||||
// 3. Timesheet Mobile Flag
|
||||
$isMobile = (str_contains($jenis, 'MOBILE') || $type === 'DUMP TRUCK' || $type === 'LIGHT TRUCK');
|
||||
|
||||
// 4. Cari unit di database (cocokkan kode asli, kode tanpa spasi, atau nomor seri)
|
||||
$cleanCode = preg_replace('/[^a-zA-Z0-9]/', '', $code1);
|
||||
$vehicle = Vehicle::where('code', $code1)
|
||||
->orWhereRaw("REPLACE(REPLACE(code, ' ', ''), '-', '') = ?", [$cleanCode])
|
||||
->first();
|
||||
|
||||
if (!$vehicle && !empty($serial) && $serial !== '?') {
|
||||
$vehicle = Vehicle::where('serial_number', $serial)->first();
|
||||
}
|
||||
|
||||
$cleanBrand = ($brand !== '?' && !empty($brand)) ? $brand : null;
|
||||
$cleanModel = ($model !== '?' && !empty($model)) ? $model : null;
|
||||
$cleanSerial = ($serial !== '?' && !empty($serial)) ? $serial : null;
|
||||
$yearInt = (is_numeric($prodYear) && (int) $prodYear > 1950 && (int) $prodYear < 2100) ? (int) $prodYear : null;
|
||||
$brandTypeCombined = trim(($cleanBrand ?? '') . ' ' . ($cleanModel ?? ''));
|
||||
|
||||
if ($vehicle) {
|
||||
// Update data unit yang sudah ada
|
||||
$updatePayload = [
|
||||
'category' => 'unit_support',
|
||||
'unit_category' => $cat,
|
||||
'unit_type' => $type,
|
||||
'location' => $finalLocation,
|
||||
'site_status' => $siteStatus,
|
||||
'status' => $status,
|
||||
'location_updated_at' => Carbon::now(),
|
||||
'location_updated_by' => 'Import Berkas Excel Lokasi',
|
||||
'location_note' => "Pembaruan dari berkas DATA LOKASI TERBARU RKM KMH ({$finalLocation})",
|
||||
];
|
||||
|
||||
if ($cleanBrand) {
|
||||
$updatePayload['brand'] = $cleanBrand;
|
||||
}
|
||||
if ($cleanModel) {
|
||||
$updatePayload['model_number'] = $cleanModel;
|
||||
}
|
||||
if (empty($vehicle->brand_type) && !empty($brandTypeCombined)) {
|
||||
$updatePayload['brand_type'] = $brandTypeCombined;
|
||||
}
|
||||
if ($cleanSerial && empty($vehicle->serial_number)) {
|
||||
$updatePayload['serial_number'] = $cleanSerial;
|
||||
}
|
||||
if ($yearInt && empty($vehicle->production_year)) {
|
||||
$updatePayload['production_year'] = $yearInt;
|
||||
}
|
||||
if (!empty($plate) && empty($vehicle->license_plate)) {
|
||||
$updatePayload['license_plate'] = $plate;
|
||||
}
|
||||
if (!empty($owner)) {
|
||||
$updatePayload['ownership'] = $owner;
|
||||
}
|
||||
if ($isMobile) {
|
||||
$updatePayload['is_timesheet_mobile'] = true;
|
||||
}
|
||||
|
||||
$vehicle->update($updatePayload);
|
||||
$updatedCount++;
|
||||
} else {
|
||||
// Buat unit baru jika belum terdaftar
|
||||
$vehicle = Vehicle::create([
|
||||
'code' => $code1,
|
||||
'category' => 'unit_support',
|
||||
'unit_category' => $cat,
|
||||
'unit_type' => $type,
|
||||
'brand' => $cleanBrand,
|
||||
'model_number' => $cleanModel,
|
||||
'brand_type' => !empty($brandTypeCombined) ? $brandTypeCombined : $type,
|
||||
'serial_number' => $cleanSerial,
|
||||
'license_plate' => $plate ?: null,
|
||||
'production_year' => $yearInt,
|
||||
'location' => $finalLocation,
|
||||
'site_status' => $siteStatus,
|
||||
'status' => $status,
|
||||
'ownership' => $owner ?: 'PT Kerinci Merangin Hidro',
|
||||
'is_timesheet_mobile' => $isMobile,
|
||||
'location_updated_at' => Carbon::now(),
|
||||
'location_updated_by' => 'Import Berkas Excel Lokasi',
|
||||
'location_note' => "Pendaftaran baru dari berkas DATA LOKASI TERBARU RKM KMH",
|
||||
]);
|
||||
$createdCount++;
|
||||
}
|
||||
|
||||
// Catat riwayat lokasi unit
|
||||
VehicleLocationHistory::create([
|
||||
'vehicle_id' => $vehicle->id,
|
||||
'site_status' => $siteStatus,
|
||||
'location' => $finalLocation,
|
||||
'note' => "Sinkronisasi Berkas Excel: Status {$status} di {$finalLocation}",
|
||||
'source' => 'import',
|
||||
'user_id' => null,
|
||||
'user_name' => 'Import Berkas Excel Lokasi',
|
||||
'recorded_at' => Carbon::now(),
|
||||
]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
$this->error("Gagal melakukan sinkronisasi: " . $e->getMessage());
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->info("Sinkronisasi Selesai!");
|
||||
$this->info("- Unit diperbarui: {$updatedCount}");
|
||||
$this->info("- Unit baru dibuat: {$createdCount}");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user