Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 17s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 15s
Laravel Pint / pint (pull_request) Successful in 6m59s
Laravel Larastan / larastan (pull_request) Failing after 9m30s
986 lines
47 KiB
PHP
986 lines
47 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Exports\ProductionOrderExporter;
|
|
use App\Filament\Imports\ProductionOrderImporter;
|
|
use App\Filament\Resources\ProductionOrderResource\Pages;
|
|
use App\Models\Item;
|
|
use App\Models\Line;
|
|
use App\Models\Plant;
|
|
use App\Models\ProductionOrder;
|
|
use Carbon\Carbon;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\Section;
|
|
use Filament\Forms\Form;
|
|
use Filament\Forms\Get;
|
|
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 Filament\Forms\Components\DateTimePicker;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Tables\Filters\Filter;
|
|
use Str;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
|
|
class ProductionOrderResource extends Resource
|
|
{
|
|
protected static ?string $model = ProductionOrder::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
protected static ?string $navigationGroup = 'Production Order';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make('')
|
|
->schema([
|
|
Forms\Components\Select::make('plant_id')
|
|
->label('Plant Name')
|
|
->relationship('plant', 'name')
|
|
->searchable()
|
|
->options(function (callable $get) {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
|
|
})
|
|
->disabled(fn (string $context): bool => $context == 'edit')
|
|
->dehydrated(true)
|
|
->default(function () {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
return ($userHas && strlen($userHas) > 0) ? $userHas : optional(ProductionOrder::latest()->first())->plant_id;
|
|
})
|
|
->reactive()
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('poPlantError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('poPlantError') ? $get('poPlantError') : null)
|
|
->hintColor('danger')
|
|
->required()
|
|
->afterStateUpdated(function ($state, callable $set) {
|
|
$set('item_id', null);
|
|
$set('quantity', null);
|
|
$set('show_extra_fields', false);
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$set('production_order', null);
|
|
$set('from_serial_number', null);
|
|
$set('to_serial_number', null);
|
|
}),
|
|
Forms\Components\Select::make('item_id')
|
|
->label('Item Code')
|
|
->searchable()
|
|
->reactive()
|
|
->options(function (callable $get) {
|
|
$plantId = $get('plant_id');
|
|
if (empty($plantId)) {
|
|
return [];
|
|
}
|
|
|
|
return Item::where('plant_id', $plantId)->pluck('code', 'id');
|
|
})
|
|
->disabled(fn (Get $get) => ! empty($get('id')))
|
|
->default(function () {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
return ($userHas && strlen($userHas) > 0) ? optional(ProductionOrder::where('plant_id', $userHas)->latest()->first())->item_id : optional(ProductionOrder::latest()->first())->item_id;
|
|
})
|
|
->required()
|
|
->afterStateUpdated(function ($state, callable $set, $get) {
|
|
$set('quantity', null);
|
|
$set('show_extra_fields', false);
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$set('production_order', null);
|
|
$set('from_serial_number', null);
|
|
$set('to_serial_number', null);
|
|
}),
|
|
Forms\Components\TextInput::make('quantity')
|
|
->label('Quantity')
|
|
->reactive()
|
|
->integer()
|
|
->required()
|
|
->readOnly(fn ($get) => ($get('plant_id') == null || $get('item_id') == null))
|
|
->disabled(fn (string $context): bool => $context === 'edit')
|
|
->afterStateUpdated(function ($state, callable $set, $get) {
|
|
if (! empty($state) && $state > 0) {
|
|
$set('show_extra_fields', true);
|
|
$now = Carbon::now();
|
|
$plantId = $get('plant_id');
|
|
$quantity = $get('quantity');
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$plantCode = Plant::find($plantId)->code;
|
|
|
|
$year = $now->format('y'); // Year (last 2 digits)
|
|
$month = $now->format('m');
|
|
|
|
$monthText = strtoupper($now->format('M')); // APR
|
|
$monthNumber = '';
|
|
|
|
foreach (str_split($monthText) as $char) {
|
|
$curSeq = ord($char) - 64;
|
|
$monthNumber .= str_pad($curSeq, 2, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
$prefix = $year.$monthNumber;
|
|
|
|
$last = ProductionOrder::where('production_order', 'like', "{$prefix}%")->where(function ($query) {
|
|
$query->whereNull('outer_status')
|
|
->orWhere('outer_status', '')
|
|
->orWhere('outer_status', '0');
|
|
})->orderByDesc('production_order')->first();
|
|
// ProductionOrder::where('production_order', 'like', $prefix.'%')->orderBy('production_order', 'desc')->first();
|
|
|
|
if ($last) {
|
|
$lastSeq = substr($last->production_order, -4);
|
|
$nextSeq = str_pad(((int) $lastSeq + 1), 4, '0', STR_PAD_LEFT);
|
|
} else {
|
|
$nextSeq = '0001';
|
|
}
|
|
|
|
$productionOrder = $prefix.$nextSeq;
|
|
|
|
$set('production_order', $productionOrder);
|
|
|
|
$prefixSerial = $plantCode.$year.$month;
|
|
|
|
// $last = ProductionOrder::where('to_serial_number', 'like', "{$prefixSerial}%")->orderByDesc('to_serial_number')->first();
|
|
|
|
$last = ProductionOrder::where('to_serial_number', 'like', "{$prefixSerial}%")->where(function ($query) {
|
|
$query->whereNull('outer_status')
|
|
->orWhere('outer_status', '')
|
|
->orWhere('outer_status', '0');
|
|
})->orderByDesc('to_serial_number')->first();
|
|
|
|
// where('outer_status', '0')->
|
|
if ($last) {
|
|
$lastSeq = (int) substr($last->to_serial_number, -6);
|
|
$startSeq = $lastSeq + 1;
|
|
} else {
|
|
$startSeq = 1;
|
|
}
|
|
|
|
$endSeq = $startSeq + $quantity - 1;
|
|
|
|
$fromSerial = $prefixSerial.str_pad($startSeq, 6, '0', STR_PAD_LEFT);
|
|
$toSerial = $prefixSerial.str_pad($endSeq, 6, '0', STR_PAD_LEFT);
|
|
|
|
$set('from_serial_number', $fromSerial);
|
|
$set('to_serial_number', $toSerial);
|
|
|
|
} else {
|
|
$set('show_extra_fields', false);
|
|
|
|
$set('production_order', null);
|
|
$set('start_date', null);
|
|
$set('end_date', null);
|
|
$set('from_serial_number', null);
|
|
$set('to_serial_number', null);
|
|
}
|
|
}),
|
|
Forms\Components\Hidden::make('show_extra_fields')
|
|
->default(false),
|
|
Forms\Components\DateTimePicker::make('start_date')
|
|
->label('Start Date')
|
|
->reactive()
|
|
// ->minDate(function () {
|
|
// return Carbon::now()->addMinute();
|
|
// })
|
|
->afterStateUpdated(function ($state, callable $set) {
|
|
$set('end_date', null);
|
|
}),
|
|
Forms\Components\DateTimePicker::make('end_date')
|
|
->label('End Date')
|
|
->reactive(),
|
|
// ->rule('after:start_date')
|
|
// ->minDate(function (callable $get) {
|
|
// $startDate = $get('start_date');
|
|
// if ($startDate) {
|
|
// return Carbon::parse($startDate)->addMinute();
|
|
// }
|
|
|
|
// return Carbon::now()->addMinute();
|
|
// }),
|
|
Forms\Components\TextInput::make('production_order')
|
|
->label('Production Order')
|
|
->readOnly()
|
|
->visible(fn ($get) => $get('quantity') > 0),
|
|
Forms\Components\TextInput::make('from_serial_number')
|
|
->label('From Serial Number')
|
|
->readOnly()
|
|
->visible(fn ($get) => $get('quantity') > 0),
|
|
Forms\Components\TextInput::make('to_serial_number')
|
|
->label('To Serial Number')
|
|
->readOnly()
|
|
->visible(fn ($get) => $get('quantity') > 0),
|
|
Forms\Components\Hidden::make('created_by')
|
|
->label('Created By')
|
|
->default(Filament::auth()->user()?->name),
|
|
Forms\Components\Hidden::make('updated_by')
|
|
->label('Updated By')
|
|
->default(Filament::auth()->user()?->name),
|
|
Forms\Components\View::make('forms.components.save-production-order-button'),
|
|
])
|
|
->columns(5),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('No.')
|
|
->label('No.')
|
|
->alignCenter()
|
|
->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
|
|
$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')
|
|
->label('Plant Name')
|
|
->alignCenter()
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('item.code')
|
|
->label('Item Code')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('item.description')
|
|
->label('Item Description')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('item.uom')
|
|
->label('Unit of Measure')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('quantity')
|
|
->label('Quantity')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('start_date')
|
|
->label('Start Date')
|
|
->searchable()
|
|
->alignCenter()
|
|
->dateTime()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('end_date')
|
|
->label('End Date')
|
|
->searchable()
|
|
->alignCenter()
|
|
->dateTime()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('production_order')
|
|
->label('Production Order')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('from_serial_number')
|
|
->label('From Serial Number')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('to_serial_number')
|
|
->label('To Serial Number')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('print_status')
|
|
->label(' Print Status')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('outer_status')
|
|
->label('External Serial Number')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('created_at')
|
|
->label('Created At')
|
|
->dateTime()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('created_by')
|
|
->label('Created By')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('updated_at')
|
|
->label('Updated At')
|
|
->dateTime()
|
|
->alignCenter()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('updated_by')
|
|
->label('Updated By')
|
|
->searchable()
|
|
->alignCenter()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
Tables\Columns\TextColumn::make('deleted_at')
|
|
->label('Deleted At')
|
|
->dateTime()
|
|
->alignCenter()
|
|
->sortable()
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
])
|
|
->filters([
|
|
Tables\Filters\TrashedFilter::make(),
|
|
Filter::make('advanced_filters')
|
|
->label('Advanced Filters')
|
|
->form([
|
|
Select::make('Plant')
|
|
->label('Search by Plant Name')
|
|
->nullable()
|
|
->searchable()
|
|
->reactive()
|
|
->options(function (callable $get) {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
if ($userHas && strlen($userHas) > 0) {
|
|
return Plant::where('id', $userHas)->pluck('name', 'id')->toArray();
|
|
} else {
|
|
return Plant::whereHas('productionOrder', function ($query) {
|
|
$query->whereNotNull('id');
|
|
})->orderBy('code')->pluck('name', 'id');
|
|
}
|
|
|
|
// return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
|
|
})
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$set('Item', null);
|
|
$set('Line', null);
|
|
}),
|
|
Select::make('Item')
|
|
->label('Search by Item Code')
|
|
->nullable()
|
|
->searchable()
|
|
->reactive()
|
|
->options(function (callable $get) {
|
|
$plantId = $get('Plant');
|
|
|
|
if (empty($plantId)) {
|
|
return [];
|
|
}
|
|
|
|
return Item::whereHas('productionOrders', function ($query) use ($plantId) {
|
|
if ($plantId) {
|
|
$query->where('plant_id', $plantId);
|
|
}
|
|
})->pluck('code', 'id');
|
|
})
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$set('production_order', null);
|
|
}),
|
|
TextInput::make('production_order')
|
|
->label('Production Order')
|
|
->reactive()
|
|
->placeholder('Enter Production Order')
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$set('Rework', null);
|
|
}),
|
|
TextInput::make('from_serial_number')
|
|
->label('From Serial Number')
|
|
->reactive()
|
|
->placeholder('Enter From Serial Number'),
|
|
TextInput::make('to_serial_number')
|
|
->label('To Serial Number')
|
|
->reactive()
|
|
->placeholder('Enter To Serial Number'),
|
|
DateTimePicker::make('created_from')
|
|
->label('Created From')
|
|
->placeholder('Select From DateTime')
|
|
->reactive()
|
|
->native(false),
|
|
DateTimePicker::make('created_to')
|
|
->label('Created To')
|
|
->placeholder('Select To DateTime')
|
|
->reactive()
|
|
->native(false),
|
|
])
|
|
->query(function ($query, array $data) {
|
|
// Hide all records initially if no filters are applied
|
|
if (empty($data['Plant']) && empty($data['Item']) && empty($data['production_order']) && empty($data['from_serial_number']) && empty($data['to_serial_number']) && empty($data['created_from']) && empty($data['created_to'])) {
|
|
return $query->whereRaw('1 = 0');
|
|
}
|
|
|
|
if (! empty($data['Plant'])) {
|
|
$query->where('plant_id', $data['Plant']);
|
|
} else {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
if ($userHas && strlen($userHas) > 0) {
|
|
return $query->whereRaw('1 = 0');
|
|
}
|
|
}
|
|
|
|
if (! empty($data['Item'])) {
|
|
$query->where('item_id', $data['Item']);
|
|
}
|
|
|
|
if (! empty($data['production_order'])) {
|
|
$query->where('production_order', 'like', '%'.$data['production_order'].'%');
|
|
}
|
|
|
|
if (!empty($data['from_serial_number']) && !empty($data['to_serial_number'])) {
|
|
|
|
$from = $data['from_serial_number'];
|
|
$to = $data['to_serial_number'];
|
|
|
|
$query->where(function ($q) use ($from, $to) {
|
|
$q->whereBetween('from_serial_number', [$from, $to])
|
|
->orWhereBetween('to_serial_number', [$from, $to]);
|
|
});
|
|
}
|
|
|
|
if (! empty($data['created_from'])) {
|
|
$query->where('created_at', '>=', $data['created_from']);
|
|
}
|
|
|
|
if (! empty($data['created_to'])) {
|
|
$query->where('created_at', '<=', $data['created_to']);
|
|
}
|
|
// $query->orderBy('created_at', 'asc');
|
|
})
|
|
->indicateUsing(function (array $data) {
|
|
$indicators = [];
|
|
|
|
if (! empty($data['Plant'])) {
|
|
$indicators[] = 'Plant Name: '.Plant::where('id', $data['Plant'])->value('name');
|
|
} else {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
if ($userHas && strlen($userHas) > 0) {
|
|
return 'Plant Name: Choose plant to filter records.';
|
|
}
|
|
}
|
|
|
|
if (! empty($data['Item'])) {
|
|
$indicators[] = 'Item Code: '.Item::where('id', $data['Item'])->value('code');
|
|
}
|
|
|
|
if (! empty($data['production_order'])) {
|
|
$indicators[] = 'Production Order: '.$data['production_order'];
|
|
}
|
|
|
|
if (! empty($data['from_serial_number'])) {
|
|
$indicators[] = 'From Serial Number: '.$data['from_serial_number'];
|
|
}
|
|
|
|
if (! empty($data['to_serial_number'])) {
|
|
$indicators[] = 'To Serial Number: '.$data['to_serial_number'];
|
|
}
|
|
|
|
if (! empty($data['created_from'])) {
|
|
$indicators[] = 'From: '.$data['created_from'];
|
|
}
|
|
|
|
if (! empty($data['created_to'])) {
|
|
$indicators[] = 'To: '.$data['created_to'];
|
|
}
|
|
|
|
return $indicators;
|
|
}),
|
|
])
|
|
->filtersFormMaxHeight('280px')
|
|
->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(),
|
|
]),
|
|
])
|
|
->headerActions([
|
|
Tables\Actions\Action::make('Import External Serial Number')
|
|
->label('Import External Serial Number')
|
|
->form([
|
|
Select::make('plant_id')
|
|
->options(function (callable $get) {
|
|
$userHas = Filament::auth()->user()->plant_id;
|
|
|
|
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
|
|
})
|
|
->searchable()
|
|
->label('Search by Plant Name')
|
|
->required()
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$set('external_serial_number', null);
|
|
$plantId = $get('plant_id');
|
|
if (! $plantId) {
|
|
$set('external_serial_number', null);
|
|
|
|
return;
|
|
}
|
|
$plantCode = Plant::find($plantId)?->code ?? null;
|
|
|
|
$directory = "uploads/ExternalSerialNo";
|
|
if ($plantId && ! Storage::disk('local')->exists($directory)) {
|
|
Storage::disk('local')->makeDirectory($directory);
|
|
}
|
|
})
|
|
->reactive(),
|
|
|
|
FileUpload::make('external_serial_number')
|
|
->label('Choose External Serial Number')
|
|
->required()
|
|
->acceptedFileTypes([
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
])
|
|
->rules(['mimes:xlsx', 'max:50'])
|
|
->maxSize(50)
|
|
->preserveFilenames()
|
|
->reactive()
|
|
->storeFiles(false)
|
|
->disk('local')
|
|
->visible(fn (Get $get) => ! empty($get('plant_id')))
|
|
->directory(function (callable $get) {
|
|
$plant = Plant::find($get('plant_id'));
|
|
$plantCode = $plant?->code ?? null;
|
|
|
|
return "uploads/ExternalSerialNo";
|
|
})
|
|
->uploadingMessage('Uploading...')
|
|
->helperText('Only .xlsx files are allowed (Excel files).'),
|
|
])
|
|
->action(function (array $data) {
|
|
|
|
$uploadedFile = $data['external_serial_number'];
|
|
|
|
$disk = Storage::disk('local');
|
|
|
|
$plantId = $data['plant_id'];
|
|
|
|
$plant = Plant::findOrFail($plantId);
|
|
|
|
$plantCode = (string) $plant->code;
|
|
|
|
$originalName = $uploadedFile->getClientOriginalName();
|
|
|
|
if (strtolower(pathinfo($originalName, PATHINFO_EXTENSION)) != 'xlsx') {
|
|
throw new \Exception('Only XLSX files are allowed.');
|
|
}
|
|
|
|
$path = $uploadedFile->storeAs("uploads/ExternalSerialNo", '.xlsx','local');
|
|
|
|
try {
|
|
|
|
$fullPath = $disk->path($path);
|
|
|
|
$rows = Excel::toArray(null, $fullPath)[0] ?? [];
|
|
|
|
if (count($rows) <= 1) {
|
|
|
|
Notification::make()
|
|
->title('Records Not Found')
|
|
->body('Uploaded Excel file does not contain any records.')
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
$productionOrders = [];
|
|
$invalidPlantCodes = [];
|
|
$invalidMatCodes = [];
|
|
$invalidPOrder = [];
|
|
$invalidQuan = [];
|
|
$invalidFromSerial = [];
|
|
$invalidToSerial = [];
|
|
$invalidToGreaterSerial = [];
|
|
$invalidSerialCountQuan = [];
|
|
$missingSerials = [];
|
|
$duplicateSerials = [];
|
|
$seenSerialNumbers = [];
|
|
$validRowsFound = false;
|
|
$excelSerialRanges = [];
|
|
$duplicateExcelSerials = [];
|
|
|
|
$userName = Filament::auth()->user()?->name;
|
|
|
|
foreach ($rows as $index => $row) {
|
|
|
|
if ($index === 0) {
|
|
continue;
|
|
}
|
|
|
|
$excelPlantCode = trim((string) ($row[0] ?? ''));
|
|
$itemCode = trim((string) ($row[1] ?? ''));
|
|
$quantity = trim((string) ($row[2] ?? ''));
|
|
$productionOrder = trim((string) ($row[3] ?? ''));
|
|
$fromSerial = trim((string) ($row[4] ?? ''));
|
|
$toSerial = trim((string) ($row[5] ?? ''));
|
|
|
|
if ($excelPlantCode == '' && $itemCode == '' && $quantity == '' && $productionOrder == '' && $fromSerial == '' && $toSerial == ''
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
$plant = Plant::where('code', $excelPlantCode)->first();
|
|
|
|
if(!$plant){
|
|
$invalidPlantCodes[] = $excelPlantCode;
|
|
}
|
|
else{
|
|
$plantId = $plant->id;
|
|
$item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first();
|
|
|
|
if(!$item){
|
|
$invalidMatCodes[] = $itemCode;
|
|
}
|
|
else{
|
|
$itemId = $item->id;
|
|
}
|
|
}
|
|
|
|
if($productionOrder == '' || $productionOrder == null){
|
|
$invalidPOrder[] = $productionOrder;
|
|
}
|
|
elseif ($productionOrder && Str::length($productionOrder) < 7) {
|
|
$invalidPOrder[] = $productionOrder;
|
|
}
|
|
|
|
if (!ctype_digit($quantity) || (int) $quantity <= 0) {
|
|
$invalidQuan[] = $quantity;
|
|
}
|
|
|
|
if (Str::length($fromSerial) < 9 || ! ctype_alnum($fromSerial)) {
|
|
$invalidFromSerial[] = $fromSerial;
|
|
}
|
|
if (Str::length($toSerial) < 9 || ! ctype_alnum($toSerial)) {
|
|
$invalidToSerial[] = $toSerial;
|
|
}
|
|
|
|
$fromNumber = (int) $fromSerial;
|
|
$toNumber = (int) $toSerial;
|
|
|
|
if ($fromNumber > $toNumber) {
|
|
$invalidToGreaterSerial = $fromNumber;
|
|
}
|
|
|
|
$serialCount = ($toNumber - $fromNumber) + 1;
|
|
|
|
if ($serialCount != (int) $quantity) {
|
|
|
|
$invalidSerialCountQuan [] = $fromNumber;
|
|
}
|
|
|
|
$existingOrders = ProductionOrder::where('plant_id', $plantId)
|
|
->where('outer_status', '1')
|
|
->whereRaw('CAST(from_serial_number AS NUMERIC) <= CAST(? AS NUMERIC)', [$toSerial])
|
|
->whereRaw('CAST(to_serial_number AS NUMERIC) >= CAST(? AS NUMERIC)', [$fromSerial])
|
|
->get(['from_serial_number', 'to_serial_number']);
|
|
|
|
$duplicateSerialRanges = [];
|
|
|
|
foreach ($existingOrders as $order) {
|
|
|
|
$start = bccomp($fromSerial, $order->from_serial_number) > 0
|
|
? $fromSerial
|
|
: $order->from_serial_number;
|
|
|
|
$end = bccomp($toSerial, $order->to_serial_number) < 0
|
|
? $toSerial
|
|
: $order->to_serial_number;
|
|
|
|
for ($serial = $start; bccomp($serial, $end) <= 0; $serial = bcadd($serial, '1')) {
|
|
$duplicateSerialRanges[] = $serial;
|
|
}
|
|
}
|
|
|
|
if (ctype_digit($fromSerial) && ctype_digit($toSerial) && strlen($fromSerial) >= 9 && strlen($toSerial) >= 9 && bccomp($fromSerial, $toSerial) <= 0)
|
|
{
|
|
foreach ($excelSerialRanges as $range) {
|
|
|
|
// Check duplicates only for the same plant
|
|
if (
|
|
$range['plant_code'] == $excelPlantCode &&
|
|
bccomp($fromSerial, $range['to']) <= 0 &&
|
|
bccomp($toSerial, $range['from']) >= 0
|
|
) {
|
|
$start = bccomp($fromSerial, $range['from']) > 0
|
|
? $fromSerial
|
|
: $range['from'];
|
|
|
|
$end = bccomp($toSerial, $range['to']) < 0
|
|
? $toSerial
|
|
: $range['to'];
|
|
|
|
for (
|
|
$serial = $start;
|
|
bccomp($serial, $end) <= 0;
|
|
$serial = bcadd($serial, '1')
|
|
) {
|
|
$duplicateExcelSerials[] = $serial;
|
|
}
|
|
}
|
|
}
|
|
|
|
$excelSerialRanges[] = [
|
|
'plant_code' => $excelPlantCode,
|
|
'from' => $fromSerial,
|
|
'to' => $toSerial,
|
|
];
|
|
}
|
|
|
|
$productionOrders[] = [
|
|
'plant_id' => $plantId,
|
|
'item_id' => $itemId,
|
|
'quantity' => (int) $quantity,
|
|
'production_order' => $productionOrder,
|
|
'from_serial_number' => $fromSerial,
|
|
'to_serial_number' => $toSerial,
|
|
'outer_status' => '1',
|
|
'created_at' => now(),
|
|
'created_by' => $userName,
|
|
'updated_at' => now(),
|
|
'updated_by' => $userName,
|
|
];
|
|
}
|
|
|
|
$uniquePlantCodes = array_unique($invalidPlantCodes);
|
|
$uniqueInvalidCodes = array_unique($invalidMatCodes);
|
|
$uniquePOrders = array_unique($invalidPOrder);
|
|
$uniqueQuantities = array_unique($invalidQuan);
|
|
$uniqueFromSerials = array_unique($invalidFromSerial);
|
|
$uniqueToSerials = array_unique($invalidToSerial);
|
|
$uniqueGreaterSerials = array_unique($invalidToGreaterSerial);
|
|
$uniqueSerialCountErrors = array_unique($invalidSerialCountQuan);
|
|
$duplicateSerialRanges = array_unique($duplicateSerialRanges);
|
|
|
|
|
|
$duplicateExcelSerials = array_unique($duplicateExcelSerials);
|
|
|
|
if (!empty($duplicateExcelSerials)) {
|
|
|
|
Notification::make()
|
|
->title('Duplicate Serial Numbers')
|
|
->body('Duplicate serial numbers found within the excel sheet same plant: ' .
|
|
implode(', ', $duplicateExcelSerials))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (!empty($duplicateSerialRanges)) {
|
|
Notification::make()
|
|
->title('Duplicate Serial Number Ranges')
|
|
->body('Duplicate serial numbers: ' . implode(', ', $duplicateSerialRanges))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
return;
|
|
}
|
|
else if (!empty($uniquePlantCodes)) {
|
|
|
|
Notification::make()
|
|
->title('Invalid Plant Codes')
|
|
->body('The following plant codes are not found:<br>' . implode(', ', $uniquePlantCodes))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
} elseif (!empty($uniqueInvalidCodes)) {
|
|
|
|
Notification::make()
|
|
->title('Invalid Item Codes')
|
|
->body('The following item codes are not found:<br>' . implode(', ', $uniqueInvalidCodes))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
} elseif (!empty($uniquePOrders)) {
|
|
|
|
Notification::make()
|
|
->title('Invalid Production Orders')
|
|
->body('The following production orders must contain at least 7 digits:<br>' . implode(', ', $uniquePOrders))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
} elseif (!empty($uniqueQuantities)) {
|
|
|
|
Notification::make()
|
|
->title('Invalid Quantity')
|
|
->body('The following quantities must be positive whole numbers:<br>' . implode(', ', $uniqueQuantities))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
} elseif (!empty($uniqueFromSerials)) {
|
|
|
|
Notification::make()
|
|
->title('Invalid From Serial Number')
|
|
->body('The following From Serial Numbers must contain at least 9 alphanumeric characters:<br>' . implode(', ', $uniqueFromSerials))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
} elseif (!empty($uniqueToSerials)) {
|
|
|
|
Notification::make()
|
|
->title('Invalid To Serial Number')
|
|
->body('The following To Serial Numbers must contain at least 9 alphanumeric characters:<br>' . implode(', ', $uniqueToSerials))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
} elseif (!empty($uniqueGreaterSerials)) {
|
|
|
|
Notification::make()
|
|
->title('Invalid Serial Range')
|
|
->body('From Serial Number must be less than or equal to To Serial Number:<br>' . implode(', ', $uniqueGreaterSerials))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
} elseif (!empty($uniqueSerialCountErrors)) {
|
|
|
|
Notification::make()
|
|
->title('Serial Count Mismatch')
|
|
->body('The serial number range does not match the specified quantity:<br>' . implode(', ', $uniqueSerialCountErrors))
|
|
->danger()
|
|
->send();
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DB::transaction(function () use ($productionOrders) {
|
|
|
|
DB::table('production_orders')->insert($productionOrders);
|
|
|
|
});
|
|
|
|
Notification::make()
|
|
->title('Production Orders Imported Successfully')
|
|
->body("External Serial Number production orders imported successfully.")
|
|
->success()
|
|
->persistent()
|
|
->send();
|
|
|
|
} finally {
|
|
|
|
if ($disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
}
|
|
})
|
|
->visible(function () {
|
|
return Filament::auth()->user()->can('view import external serial number');
|
|
}),
|
|
ImportAction::make()
|
|
->label('Import Production Orders')
|
|
->color('warning')
|
|
->importer(ProductionOrderImporter::class)
|
|
->visible(function () {
|
|
return Filament::auth()->user()->can('view import production orders');
|
|
}),
|
|
ExportAction::make()
|
|
->label('Export Production Orders')
|
|
->color('warning')
|
|
->exporter(ProductionOrderExporter::class)
|
|
->visible(function () {
|
|
return Filament::auth()->user()->can('view export production orders');
|
|
}),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListProductionOrders::route('/'),
|
|
'create' => Pages\CreateProductionOrder::route('/create'),
|
|
'view' => Pages\ViewProductionOrder::route('/{record}'),
|
|
'edit' => Pages\EditProductionOrder::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()
|
|
->withoutGlobalScopes([
|
|
SoftDeletingScope::class,
|
|
]);
|
|
}
|
|
}
|