1
0
forked from poc/pds

updated motor and pump scanning logic

This commit is contained in:
dhanabalan
2025-04-10 20:24:04 +05:30
parent 57c3a876bd
commit 2ccad83e67
6 changed files with 177 additions and 40 deletions

View File

@@ -20,6 +20,7 @@ use Filament\Notifications\Notification;
use Filament\Tables\Actions\Action;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Livewire\Livewire; // Ensure this is imported
class InvoiceValidationResource extends Resource
{
@@ -29,6 +30,9 @@ class InvoiceValidationResource extends Resource
protected static ?string $navigationGroup = 'Invoice';
public $invoiceNumber;
public static function form(Form $form): Form
{
@@ -55,8 +59,13 @@ class InvoiceValidationResource extends Resource
'x-on:keydown.enter.prevent' => '$wire.processInvoice(value)',
]),
Forms\Components\TextInput::make('serial_number')
->reactive()
Forms\Components\TextInput::make('serial_number')
->extraAttributes([
'x-data' => '{ value: "" }',
'x-model' => 'value',
'wire:keydown.enter.prevent' => 'processSerialNumber(value)', // Using wire:keydown
])
->columnSpan(1),
Forms\Components\TextInput::make('total_quantity')
@@ -68,12 +77,9 @@ class InvoiceValidationResource extends Resource
])
->columns(5),
]);
}
public static function table(Table $table): Table
{
return $table

View File

@@ -6,6 +6,7 @@ use App\Filament\Resources\InvoiceValidationResource;
use App\Livewire\InvoiceDataTable;
use App\Models\InvoiceValidation;
use App\Models\StickerMaster;
use Filament\Facades\Filament;
use Illuminate\Contracts\View\View;
use Filament\Resources\Pages\CreateRecord;
use Filament\Notifications\Notification;
@@ -28,7 +29,8 @@ class CreateInvoiceValidation extends CreateRecord
public $total_quantity;
public string $invoiceNumber;
public $invoiceNumber;
public bool $hasSearched = false;
public $excel_file;
@@ -40,7 +42,10 @@ class CreateInvoiceValidation extends CreateRecord
public function processInvoice($invoiceNumber)
{
// Get plant_id selected in the form
$user = Filament::auth()->user();
$operatorName = $user->name;
$plantId = $this->form->getState()['plant_id'];
$filePath = session('uploaded_invoice_path');
@@ -142,6 +147,7 @@ class CreateInvoiceValidation extends CreateRecord
'serial_number' => $serialNumber,
'plant_id' => $plantId,
'invoice_number' => $invoiceNumber,
'operator_id'=> $operatorName,
]);
$inserted++;
}
@@ -154,7 +160,7 @@ class CreateInvoiceValidation extends CreateRecord
->body("$inserted records were inserted.")
->success()
->send();
// Dispatch the event to refresh the Livewire component
$this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber);
}
@@ -170,6 +176,83 @@ class CreateInvoiceValidation extends CreateRecord
}
}
protected function refreshInvoiceTable()
{
if (empty($this->invoiceNumber)) {
$this->invoiceNumber = $this->form->getState()['invoice_number'] ?? '';
}
if (!empty($this->invoiceNumber)) {
$this->dispatch('refreshInvoiceData', invoiceNumber: $this->invoiceNumber);
}
}
public function processSerialNumber($serialNumber)
{
if (!preg_match('/^([a-zA-Z0-9]{6,})\|([a-zA-Z0-9]{8,})(?:\/[MmPpCc])?$/', $serialNumber, $matches)) {
Notification::make()
->danger()
->title('Invalid format')
->body('Please enter serial in correct format: ITEM123|123456789/M')
->send();
return;
}
if (preg_match('/^([a-zA-Z0-9]+)\|([a-zA-Z0-9]+(?:\/[MmPpCc]?)?)$/', $serialNumber, $matches))
{
$itemCode = $matches[1];
$serialNumber = $matches[2];
// Check if it ends with /M, /P, /C etc.
$isMarkM = preg_match('/\/[Mm]$/', $serialNumber);
$isMarkP = preg_match('/\/[Pp]$/', $serialNumber);
$serialNumber = preg_replace('/\/[MmPpCc]$/', '', $serialNumber);
$record = InvoiceValidation::where('serial_number', $serialNumber)
->whereHas('stickerMasterRelation.item', function ($query) use ($itemCode) {
$query->where('code', $itemCode);
})
->first();
if (!$record) {
Notification::make()
->danger()
->title('Serial not found')
->body("Item code '$itemCode' with serial '$serialNumber' not found.")
->send();
return;
}
if ($isMarkM) {
$record->motor_scanned_status = 1;
$record->save();
Notification::make()
->success()
->title('Updated')
->body("Motor scanned status marked as updated.")
->send();
$this->refreshInvoiceTable();
}
else if ($isMarkP) {
$record->pump_scanned_status = 1;
$record->save();
Notification::make()
->success()
->title('Updated')
->body("Pump scanned status marked as updated.")
->send();
$this->refreshInvoiceTable();
}
}
}
public function getHeading(): string
{
return 'Scan Invoice Validation';

View File

@@ -14,40 +14,45 @@ class InvoiceDataTable extends Component
public bool $hasSearched = false;
protected $listeners = ['refreshInvoiceData' => 'loadData'];
protected $listeners = ['refreshInvoiceData' => 'loadData',];
public function loadData($invoiceNumber)
{
$this->invoiceNumber = $invoiceNumber;
$this->hasSearched = true;
$this->invoiceNumber = $invoiceNumber;
$this->hasSearched = true;
$this->invoiceData = InvoiceValidation::where('invoice_number', $this->invoiceNumber)
->get()
->map(function ($record) {
return [
'sticker_master_id' => $record->sticker_master_id,
'serial_number' => $record->serial_number,
'motor_scanned_status' => $record->motor_scanned_status,
'pump_scanned_status' => $record->pump_scanned_status,
'capacitor_scanned_status' => $record->capacitor_scanned_status,
'scanned_status_set' => $record->scanned_status_set,
'panel_box_supplier' => $record->panel_box_supplier,
'panel_box_serial_number' => $record->panel_box_serial_number,
'scanned_status' => $record->scanned_status,
];
})
$this->invoiceData = InvoiceValidation::where('invoice_number', $this->invoiceNumber)
->get()
->map(function ($record) {
return [
'sticker_master_id' => $record->sticker_master_id,
'serial_number' => $record->serial_number,
'motor_scanned_status' => $record->motor_scanned_status,
'pump_scanned_status' => $record->pump_scanned_status,
'capacitor_scanned_status' => $record->capacitor_scanned_status,
'scanned_status_set' => $record->scanned_status_set,
'panel_box_supplier' => $record->panel_box_supplier,
'panel_box_serial_number' => $record->panel_box_serial_number,
'scanned_status' => $record->scanned_status,
'created_at' => $record->created_at,
'operator_id' => $record->operator_id,
];
})
->toArray();
->toArray();
//Loop through and replace 'code' using related StickerMaster > Item > code
foreach ($this->invoiceData as &$row) {
$stickerMaster = \App\Models\StickerMaster::with('item')->find($row['sticker_master_id'] ?? null);
$row['code'] = $stickerMaster?->item?->code ?? 'N/A';
}
}
public function render()
{
return view('livewire.invoice-data-table');
}
}

View File

@@ -26,6 +26,7 @@ class InvoiceValidation extends Model
'upload_status',
'batch_number',
'quantity',
'operator_id',
];
public function plant(): BelongsTo
@@ -37,6 +38,10 @@ class InvoiceValidation extends Model
{
return $this->belongsTo(StickerMaster::class);
}
public function stickerMasterRelation()
{
return $this->belongsTo(StickerMaster::class, 'sticker_master_id');
}
}

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$sql = <<<'SQL'
ALTER TABLE invoice_validations
ADD operator_id TEXT DEFAULT NULL;
SQL;
DB::statement($sql);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
}
};

View File

@@ -127,23 +127,26 @@
</div>
</div>
@if($hasSearched)
<div class="overflow-x-auto overflow-y-visible">
<table class="min-w-[1500px] text-sm text-center border border-gray-300">
{{-- <table class="min-w-[1500px] text-sm text-center border border-gray-300"> --}}
{{-- <table class="table-fixed min-w-[1500px] text-sm text-center border border-gray-300"> --}}
<table class="min-w-full text-sm text-center border border-gray-300">
<thead class="bg-gray-100 font-bold">
<tr>
<th class="border px-4 py-2 min-w-[100px]">No</th>
<th class="border px-4 py-2 min-w-[200px]">Material Code</th>
<th class="border px-4 py-2 min-w-[250px]">Serial Number</th>
<th class="border px-4 py-2 min-w-[200px]">Motor Scanned Status</th>
<th class="border px-4 py-2 min-w-[200px]">Pump Scanned Status</th>
<th class="border px-4 py-2 min-w-[250px]">Capacitor Scanned Status</th>
<th class="border px-4 py-2 min-w-[200px]">Scanned Status Set</th>
<th class="border px-4 py-2 min-w-[250px]">Panel Box Supplier</th>
<th class="border px-4 py-2 min-w-[250px]">Panel Box Serial Number</th>
<th class="border px-4 py-2 min-w-[200px]">Scanned Status</th>
<th class="border px-4 py-2">No</th>
<th class="border px-4 py-2">Material Code</th>
<th class="border px-4 py-2">Serial Number</th>
<th class="border px-4 py-2">Motor Scanned Status</th>
<th class="border px-4 py-2">Pump Scanned Status</th>
<th class="border px-4 py-2">Capacitor Scanned Status</th>
<th class="border px-4 py-2">Scanned Status Set</th>
<th class="border px-4 py-2">Scanned Status</th>
<th class="border px-4 py-2 w-[300px] whitespace-nowrap">Time Stamp</th>
<th class="border px-4 py-2">Operator ID</th>
<th class="border px-4 py-2">Panel Box Supplier</th>
<th class="border px-4 py-2">Panel Box Serial Number</th>
</tr>
</thead>
<tbody>
@@ -156,9 +159,11 @@
<td class="border px-4 py-2">{{ $row['pump_scanned_status'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['capacitor_scanned_status'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['scanned_status_set'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['scanned_status'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['created_at'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['operator_id'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['panel_box_supplier'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['panel_box_serial_number'] ?? 'N/A' }}</td>
<td class="border px-4 py-2">{{ $row['scanned_status'] ?? 'N/A' }}</td>
</tr>
@empty
<tr>
@@ -173,3 +178,5 @@
@endif
</div>