Added sfg_number and machine_name column into resource file
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s

This commit is contained in:
dhanabalan
2025-11-26 19:36:56 +05:30
parent 4c93c3cdd6
commit a018b0d06f

View File

@@ -2,29 +2,28 @@
namespace App\Filament\Resources;
use App\Filament\Exports\ProcessOrderExporter;
use App\Filament\Imports\ProcessOrderImporter;
use App\Filament\Resources\ProcessOrderResource\Pages;
use App\Filament\Resources\ProcessOrderResource\RelationManagers;
use App\Models\Plant;
use App\Models\ProcessOrder;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Storage;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use Filament\Notifications\Notification;
use Filament\Forms\Components\Actions\Action;
use Filament\Facades\Filament;
use Smalot\PdfParser\Parser;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use App\Filament\Exports\ProcessOrderExporter;
use App\Filament\Imports\ProcessOrderImporter;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Storage;
class ProcessOrderResource extends Resource
{
@@ -44,6 +43,7 @@ class ProcessOrderResource extends Resource
->relationship('plant', 'name')
->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
})
->required(),
@@ -58,6 +58,7 @@ class ProcessOrderResource extends Resource
if (empty($plantId)) {
return [];
}
return \App\Models\Item::where('plant_id', $plantId)->pluck('code', 'id');
})
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
@@ -72,8 +73,8 @@ class ProcessOrderResource extends Resource
if ($itemCode) {
// Now get the item description using plant_id + code
$item = \App\Models\Item::where('plant_id', $plantId)
->where('code', $itemCode)
->first();
->where('code', $itemCode)
->first();
$set('item_description', $item?->description);
} else {
@@ -104,7 +105,7 @@ class ProcessOrderResource extends Resource
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
// ->readOnly(true),
// ->readOnly(true),
Forms\Components\TextInput::make('process_order')
->label('Process Order')
->required(),
@@ -119,6 +120,12 @@ class ProcessOrderResource extends Resource
->label('Received Quantity')
->default('0')
->required(),
Forms\Components\TextInput::make('sfg_number')
->label('SFG Number')
->required(),
Forms\Components\TextInput::make('machine_name')
->label('Machine ID')
->required(),
Forms\Components\FileUpload::make('attachment')
->label('PDF Upload')
->acceptedFileTypes(['application/pdf'])
@@ -133,21 +140,21 @@ class ProcessOrderResource extends Resource
->action(function ($get, callable $set) {
$uploadedFiles = $get('attachment');
if (is_array($uploadedFiles) && count($uploadedFiles) > 0)
{
if (is_array($uploadedFiles) && count($uploadedFiles) > 0) {
$uploaded = reset($uploadedFiles);
if ($uploaded instanceof TemporaryUploadedFile) {
$originalName = $uploaded->getClientOriginalName();
$path = 'uploads/ProcessOrder/' . $originalName;
$path = 'uploads/ProcessOrder/'.$originalName;
// Check if file already exists
// Check if file already exists
if (Storage::disk('local')->exists($path)) {
Notification::make()
->title('Duplicate File')
->body("The file '{$originalName}' already exists in uploads/ProcessOrder.")
->warning()
->send();
return; // Stop here
}
@@ -157,28 +164,25 @@ class ProcessOrderResource extends Resource
'local'
);
// $fullPath = storage_path('app/' . $storedPath);
$fullPath = storage_path('app/' . $storedPath);
// $fullPath = storage_path('app/' . $storedPath);
$fullPath = storage_path('app/'.$storedPath);
// Parse PDF using smalot/pdfparser
$parser = new Parser();
$parser = new Parser;
$pdf = $parser->parseContent(file_get_contents($uploaded->getRealPath()));
$text = $pdf->getText();
// if (preg_match('/Batch ID:\s*(\d+)\s*--/i', $text, $matches))
// {
// $batchId = $matches[1];
// }
if (preg_match('/Batch ID:\s*(\d+)(?:\s*--)?/i', $text, $matches)) {
$batchId = $matches[1];
}
else
{
} else {
$batchId = null;
}
// Get the value of process_order field
// Get the value of process_order field
$processOrder = $get('process_order');
if ($batchId != $processOrder) {
@@ -187,11 +191,11 @@ class ProcessOrderResource extends Resource
->body("Batch ID ($batchId) does not match Process Order ($processOrder)")
->danger()
->send();
return;
}
if ($batchId == $processOrder)
{
if ($batchId == $processOrder) {
// If batch matches, store the PDF permanently
$storedPath = $uploaded->storeAs(
'uploads/ProcessOrder',
@@ -204,17 +208,17 @@ class ProcessOrderResource extends Resource
->body("Batch ID matches Process Order: $batchId. PDF uploaded successfully.")
->success()
->send();
return;
return;
}
}
}
else
{
} else {
Notification::make()
->title('No file selected to upload')
->warning()
->send();
return;
return;
}
}),
@@ -223,11 +227,12 @@ class ProcessOrderResource extends Resource
->action(function ($get) {
$equipmentNumber = $get('process_order');
if (!$equipmentNumber) {
if (! $equipmentNumber) {
Notification::make()
->title('No process order entered')
->danger()
->send();
return;
}
@@ -241,59 +246,60 @@ class ProcessOrderResource extends Resource
}
}
if (!$fileToDownload) {
if (! $fileToDownload) {
Notification::make()
->title('PDF not found for this process order')
->danger()
->send();
return;
}
return response()->download(Storage::disk('local')->path($fileToDownload));
}),
// Action::make('removeAttachment')
// ->label('Remove PDF')
// ->action(function ($get) {
// $equipmentNumber = $get('process_order');
// Action::make('removeAttachment')
// ->label('Remove PDF')
// ->action(function ($get) {
// $equipmentNumber = $get('process_order');
// if (!$equipmentNumber) {
// Notification::make()
// ->title('No process order entered')
// ->danger()
// ->send();
// return;
// }
// if (!$equipmentNumber) {
// Notification::make()
// ->title('No process order entered')
// ->danger()
// ->send();
// return;
// }
// // Get all files from uploads/temp
// $files = Storage::disk('local')->files('uploads/ProcessOrder');
// // Get all files from uploads/temp
// $files = Storage::disk('local')->files('uploads/ProcessOrder');
// $fileToDelete = null;
// foreach ($files as $file) {
// if (str_contains($file, $equipmentNumber)) {
// $fileToDelete = $file;
// break;
// }
// }
// $fileToDelete = null;
// foreach ($files as $file) {
// if (str_contains($file, $equipmentNumber)) {
// $fileToDelete = $file;
// break;
// }
// }
// if (!$fileToDelete) {
// Notification::make()
// ->title('PDF not found for this process order')
// ->danger()
// ->send();
// return;
// }
// if (!$fileToDelete) {
// Notification::make()
// ->title('PDF not found for this process order')
// ->danger()
// ->send();
// return;
// }
// // Delete the matched file
// Storage::disk('local')->delete($fileToDelete);
// // Delete the matched file
// Storage::disk('local')->delete($fileToDelete);
// Notification::make()
// ->title('PDF removed successfully')
// ->body("File for process order {$equipmentNumber} has been deleted.")
// ->success()
// ->send();
// }),
]),
// Notification::make()
// ->title('PDF removed successfully')
// ->body("File for process order {$equipmentNumber} has been deleted.")
// ->success()
// ->send();
// }),
]),
Forms\Components\Hidden::make('created_by')
->label('Created By')
->default(Filament::auth()->user()?->name),
@@ -312,6 +318,7 @@ class ProcessOrderResource extends Resource
$paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}),
Tables\Columns\TextColumn::make('plant.name')
@@ -349,6 +356,16 @@ class ProcessOrderResource extends Resource
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('sfg_number')
->label('SFG Number')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('machine_name')
->label('Machine ID')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->label('Created At')
->alignCenter()
@@ -385,7 +402,7 @@ class ProcessOrderResource extends Resource
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
@@ -396,14 +413,14 @@ class ProcessOrderResource extends Resource
->label('Import Process Order')
->color('warning')
->importer(ProcessOrderImporter::class)
->visible(function() {
->visible(function () {
return Filament::auth()->user()->can('view import process order');
}),
ExportAction::make()
->label('Export Process Order')
->color('warning')
->exporter(ProcessOrderExporter::class)
->visible(function() {
->visible(function () {
return Filament::auth()->user()->can('view export process order');
}),
]);