added migration, model, resource , grid table
This commit is contained in:
155
app/Filament/Resources/InvoiceValidationResource.php
Normal file
155
app/Filament/Resources/InvoiceValidationResource.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||
use App\Models\InvoiceValidation;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Filament\Forms\Components\View;
|
||||
|
||||
|
||||
|
||||
class InvoiceValidationResource extends Resource
|
||||
{
|
||||
protected static ?string $model = InvoiceValidation::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
||||
|
||||
protected static ?string $navigationGroup = 'Invoice';
|
||||
|
||||
public $enter_pressed = false;
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\Hidden::make('sticker_master_id')
|
||||
//->relationship('stickerMaster', 'id')
|
||||
->required(),
|
||||
|
||||
Section::make('')
|
||||
->schema([
|
||||
|
||||
// FileUpload::make('excel_file')
|
||||
// ->label('Choose Excel File')
|
||||
// ->disk('local')
|
||||
// ->columnSpan(1),
|
||||
|
||||
Forms\Components\Select::make('plant_id')
|
||||
->relationship('plant', 'name')
|
||||
->required(),
|
||||
|
||||
Forms\Components\TextInput::make('invoice_number')
|
||||
->required()
|
||||
->label('Invoice Number')
|
||||
->columnSpan(1)
|
||||
->extraAttributes([
|
||||
'x-data' => '{ value: "" }',
|
||||
'x-model' => 'value',
|
||||
'x-on:keydown.enter.prevent' => '$wire.processInvoice(value)',
|
||||
]),
|
||||
|
||||
Forms\Components\TextInput::make('serial_number')
|
||||
//->required()
|
||||
->reactive()
|
||||
->columnSpan(1),
|
||||
|
||||
Forms\Components\TextInput::make('total_quantity')
|
||||
->label('Total Quantity')
|
||||
->columnSpan(1),
|
||||
Forms\Components\TextInput::make('scanned_quantity')
|
||||
->label('Scanned Quantity')
|
||||
->columnSpan(1),
|
||||
|
||||
])
|
||||
->columns(5),
|
||||
|
||||
// View::make('livewire.invoice-data-table')
|
||||
// ->label('Invoice Details')
|
||||
// ->viewData([
|
||||
// 'invoiceData' => fn ($get) => $get('invoice_data') ?? [], // Changed from invoiceData to invoice_data
|
||||
// ])
|
||||
// ->columnSpan('full'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('id')
|
||||
->label('ID')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('stickerMaster.id')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('plant.name')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('load_rate')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('created_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('updated_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('deleted_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\TrashedFilter::make(),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\ViewAction::make(),
|
||||
Tables\Actions\EditAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
Tables\Actions\ForceDeleteBulkAction::make(),
|
||||
Tables\Actions\RestoreBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListInvoiceValidations::route('/'),
|
||||
'create' => Pages\CreateInvoiceValidation::route('/create'),
|
||||
'view' => Pages\ViewInvoiceValidation::route('/{record}'),
|
||||
'edit' => Pages\EditInvoiceValidation::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getEloquentQuery(): Builder
|
||||
{
|
||||
return parent::getEloquentQuery()
|
||||
->withoutGlobalScopes([
|
||||
SoftDeletingScope::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||
|
||||
use App\Filament\Resources\InvoiceValidationResource;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Filament\Notifications\Notification;
|
||||
|
||||
|
||||
class CreateInvoiceValidation extends CreateRecord
|
||||
{
|
||||
protected static string $resource = InvoiceValidationResource::class;
|
||||
|
||||
protected static string $view = 'filament.resources.invoice-validation-resource.pages.create-invoice-validation';
|
||||
|
||||
public $invoice_number;
|
||||
|
||||
public $invoice_data;
|
||||
|
||||
public $scanned_quantity;
|
||||
|
||||
public $total_quantity;
|
||||
|
||||
|
||||
public $excel_file;
|
||||
|
||||
public function processInvoice($invoiceNumber)
|
||||
{
|
||||
|
||||
$this->invoice_number = $invoiceNumber;
|
||||
|
||||
// Check if the file is uploaded
|
||||
if (!$this->excel_file) {
|
||||
Notification::make()
|
||||
->title('No File Uploaded')
|
||||
->body("Please upload an Excel file to proceed with invoice: {$invoiceNumber}.")
|
||||
->danger()
|
||||
->persistent()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
$localPath = $this->excel_file->getPath();
|
||||
|
||||
// Check if file exists
|
||||
if (!file_exists($localPath)) {
|
||||
Notification::make()
|
||||
->title('File Not Found')
|
||||
->body("No Excel file found for invoice: {$invoiceNumber}. Please ensure the file exists in your Downloads folder.")
|
||||
->danger()
|
||||
->persistent()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$rows = \Maatwebsite\Excel\Facades\Excel::toArray([], $localPath)[0] ?? [];
|
||||
array_shift($rows);
|
||||
|
||||
if (empty($rows)) {
|
||||
Notification::make()
|
||||
->title('Empty File')
|
||||
->body('The Excel file contains no data rows')
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
$validRecords = [];
|
||||
$invalidMaterials = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$materialCode = $row[0] ?? null;
|
||||
$serialNumber = $row[1] ?? null;
|
||||
|
||||
if (!\App\Models\StickerMaster::where('item_id', $materialCode)->exists()) {
|
||||
$invalidMaterials[] = $materialCode;
|
||||
continue;
|
||||
}
|
||||
|
||||
$validRecords[] = [
|
||||
'material_code' => $materialCode,
|
||||
'serial_number' => $serialNumber,
|
||||
'invoice_number' => $invoiceNumber,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($invalidMaterials)) {
|
||||
Notification::make()
|
||||
->title('Invalid Materials')
|
||||
->body('These codes not found: ' . implode(', ', array_unique($invalidMaterials)))
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
\DB::table('invoice_validations')->insert($validRecords);
|
||||
|
||||
$this->invoice_data = $validRecords;
|
||||
$this->scanned_quantity = count($validRecords);
|
||||
$this->total_quantity = count($validRecords);
|
||||
|
||||
Notification::make()
|
||||
->title('Success')
|
||||
->body(count($validRecords) . ' records inserted successfully')
|
||||
->success()
|
||||
->send();
|
||||
} catch (\Exception $e) {
|
||||
Notification::make()
|
||||
->title('Error')
|
||||
->body($e->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
|
||||
public function getHeading(): string
|
||||
{
|
||||
return 'Scan Invoice Validation';
|
||||
}
|
||||
|
||||
// public function render(): View
|
||||
// {
|
||||
// return view('filament.resources.invoice-validation-resource.pages.create-invoice-validation');
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||
|
||||
use App\Filament\Resources\InvoiceValidationResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditInvoiceValidation extends EditRecord
|
||||
{
|
||||
protected static string $resource = InvoiceValidationResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\ViewAction::make(),
|
||||
Actions\DeleteAction::make(),
|
||||
Actions\ForceDeleteAction::make(),
|
||||
Actions\RestoreAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||
|
||||
use App\Filament\Resources\InvoiceValidationResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListInvoiceValidations extends ListRecords
|
||||
{
|
||||
protected static string $resource = InvoiceValidationResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||
|
||||
use App\Filament\Resources\InvoiceValidationResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewInvoiceValidation extends ViewRecord
|
||||
{
|
||||
protected static string $resource = InvoiceValidationResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
40
app/Livewire/InvoiceDataTable.php
Normal file
40
app/Livewire/InvoiceDataTable.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\InvoiceValidation;
|
||||
use Livewire\Component;
|
||||
|
||||
class InvoiceDataTable extends Component
|
||||
{
|
||||
|
||||
public $invoiceData = [];
|
||||
|
||||
protected $listeners = ['refreshInvoiceData' => 'loadData'];
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->loadData();
|
||||
|
||||
}
|
||||
|
||||
public function loadData()
|
||||
{
|
||||
$this->invoiceData = InvoiceValidation::all()->toArray();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
// Always fetch fresh data when Livewire re-renders (like via polling)
|
||||
$invoiceData = InvoiceValidation::latest()->get();
|
||||
|
||||
return view('livewire.invoice-data-table', [
|
||||
'invoiceData' => $invoiceData,
|
||||
]);
|
||||
// return view('livewire.invoice-data-table', [
|
||||
// 'invoiceData' => $this->invoiceData, // <--- this is important
|
||||
// ]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
42
app/Models/InvoiceValidation.php
Normal file
42
app/Models/InvoiceValidation.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class InvoiceValidation extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'sticker_master_id',
|
||||
'plant_id',
|
||||
'invoice_number',
|
||||
'serial_number',
|
||||
'motor_scanned_status',
|
||||
'pump_scanned_status',
|
||||
'capacitor_scanned_status',
|
||||
'scanned_status_set',
|
||||
'scanned_status',
|
||||
'panel_box_supplier',
|
||||
'panel_box_serial_number',
|
||||
'load_rate',
|
||||
'upload_status',
|
||||
'batch_number',
|
||||
'quantity',
|
||||
];
|
||||
|
||||
public function plant(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Plant::class);
|
||||
}
|
||||
|
||||
public function stickerMaster(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(StickerMaster::class);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user