Added import validation functionality and block_reference column and Updated sample data
This commit is contained in:
@@ -2,10 +2,19 @@
|
|||||||
|
|
||||||
namespace App\Filament\Imports;
|
namespace App\Filament\Imports;
|
||||||
|
|
||||||
|
use App\Models\Block;
|
||||||
|
use App\Models\Line;
|
||||||
|
use App\Models\LineStop;
|
||||||
|
use App\Models\Plant;
|
||||||
use App\Models\ProductionLineStop;
|
use App\Models\ProductionLineStop;
|
||||||
|
use App\Models\Shift;
|
||||||
|
use App\Models\User;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||||
use Filament\Actions\Imports\ImportColumn;
|
use Filament\Actions\Imports\ImportColumn;
|
||||||
use Filament\Actions\Imports\Importer;
|
use Filament\Actions\Imports\Importer;
|
||||||
use Filament\Actions\Imports\Models\Import;
|
use Filament\Actions\Imports\Models\Import;
|
||||||
|
use Filament\Facades\Filament;
|
||||||
|
|
||||||
class ProductionLineStopImporter extends Importer
|
class ProductionLineStopImporter extends Importer
|
||||||
{
|
{
|
||||||
@@ -17,61 +26,67 @@ class ProductionLineStopImporter extends Importer
|
|||||||
ImportColumn::make('from_datetime')
|
ImportColumn::make('from_datetime')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('From DateTime')
|
->exampleHeader('From DateTime')
|
||||||
->example('06-02-2025 00:00:00')
|
->example(['01-01-2025 00:00:00', '01-01-2025 20:00:00'])
|
||||||
->label('From DateTime')
|
->label('From DateTime')
|
||||||
->rules(['required']),
|
->rules(['required']), //, 'date_format:"d-m-Y H:i:s"'
|
||||||
ImportColumn::make('to_datetime')
|
ImportColumn::make('to_datetime')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('To DateTime')
|
->exampleHeader('To DateTime')
|
||||||
->example('06-02-2025 01:25:00')
|
->example(['01-01-2025 01:25:00', '01-01-2025 21:00:00'])
|
||||||
->label('To DateTime')
|
->label('To DateTime')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('stop_hour')
|
ImportColumn::make('stop_hour')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Stop Hour')
|
->exampleHeader('Stop Hour')
|
||||||
->example('1')
|
->example(['1', '1'])
|
||||||
->label('Stop Hour')
|
->label('Stop Hour')
|
||||||
->numeric()
|
->numeric()
|
||||||
->rules(['required', 'integer']),
|
->rules(['required', 'integer']),
|
||||||
ImportColumn::make('stop_min')
|
ImportColumn::make('stop_min')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Stop Min')
|
->exampleHeader('Stop Min')
|
||||||
->example('25')
|
->example(['25','0'])
|
||||||
->label('Stop Min')
|
->label('Stop Min')
|
||||||
->numeric()
|
->numeric()
|
||||||
->rules(['required', 'integer']),
|
->rules(['required', 'integer']),
|
||||||
ImportColumn::make('linestop')
|
ImportColumn::make('linestop')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Line Stop Code')
|
->exampleHeader('Line Stop Code')
|
||||||
->example('A7R')
|
->example(['A7R', 'A1R'])
|
||||||
->label('Line Stop Code')
|
->label('Line Stop Code')
|
||||||
->relationship(resolveUsing:'code')
|
->relationship(resolveUsing:'code')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('line')
|
ImportColumn::make('line')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Line Name')
|
->exampleHeader('Line Name')
|
||||||
->example('4 inch pump line')
|
->example(['4 inch pump line', '4 inch pump line'])
|
||||||
->label('Line Name')
|
->label('Line Name')
|
||||||
->relationship(resolveUsing:'name')
|
->relationship(resolveUsing:'name')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
|
ImportColumn::make('block_reference')
|
||||||
|
->requiredMapping() // Or optionalMapping() if not always present
|
||||||
|
->exampleHeader('Block Name')
|
||||||
|
->example(['Block A', 'Block A'])
|
||||||
|
->label('Block Name')
|
||||||
|
->rules(['required']), // Or remove if not required
|
||||||
ImportColumn::make('shift')
|
ImportColumn::make('shift')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Shift Name')
|
->exampleHeader('Shift Name')
|
||||||
->example('Day')
|
->example(['Day', 'Night'])
|
||||||
->label('Shift Name')
|
->label('Shift Name')
|
||||||
->relationship(resolveUsing: 'name')
|
->relationship(resolveUsing: 'name')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('plant')
|
ImportColumn::make('plant')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Plant Name')
|
->exampleHeader('Plant Name')
|
||||||
->example('Ransar Industries-I')
|
->example(['Ransar Industries-I', 'Ransar Industries-I'])
|
||||||
->label('Plant Name')
|
->label('Plant Name')
|
||||||
->relationship(resolveUsing:'name')
|
->relationship(resolveUsing:'name')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('operator_id')
|
ImportColumn::make('operator_id')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Operator ID')
|
->exampleHeader('Operator ID')
|
||||||
->example('admin')
|
->example([Filament::auth()->user()->name, Filament::auth()->user()->name])
|
||||||
->label('Operator ID')
|
->label('Operator ID')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
];
|
];
|
||||||
@@ -79,12 +94,138 @@ class ProductionLineStopImporter extends Importer
|
|||||||
|
|
||||||
public function resolveRecord(): ?ProductionLineStop
|
public function resolveRecord(): ?ProductionLineStop
|
||||||
{
|
{
|
||||||
|
$warnMsg = [];
|
||||||
|
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||||
|
$line = null;
|
||||||
|
$block = null;
|
||||||
|
if (!$plant) {
|
||||||
|
$warnMsg[] = "Plant not found";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first();
|
||||||
|
//block_reference
|
||||||
|
$block = Block::where('name', $this->data['block_reference'])->where('plant_id', $plant->id)->first();
|
||||||
|
}
|
||||||
|
if (!$line) {
|
||||||
|
$warnMsg[] = "Line not found";
|
||||||
|
}
|
||||||
|
$shift = null;
|
||||||
|
if (!$block) {
|
||||||
|
$warnMsg[] = "Block not found";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$shift = Shift::where('name', $this->data['shift'])->where('plant_id', $plant->id)->where('block_id', $block->id)->first();
|
||||||
|
}
|
||||||
|
//$shift = Shift::where('id', $this->data['shift'])->where('plant_id', $plant->id)->first();
|
||||||
|
if (!$shift) {
|
||||||
|
$warnMsg[] = "Shift not found";
|
||||||
|
}
|
||||||
|
$linestop = LineStop::where('code', $this->data['linestop'])->first();
|
||||||
|
if (!$linestop) {
|
||||||
|
$warnMsg[] = "Line stop code not found";
|
||||||
|
}
|
||||||
|
$stophour = is_numeric($this->data['stop_hour']) && $this->data['stop_hour'] >= 0;
|
||||||
|
$stopmin = is_numeric($this->data['stop_min']) && $this->data['stop_min'] >= 0 && $this->data['stop_min'] <= 60;
|
||||||
|
if (!$stophour && !$stopmin) {
|
||||||
|
$warnMsg[] = "Invalid stop hour/minute found";
|
||||||
|
}
|
||||||
|
else if (!$stophour) {
|
||||||
|
$warnMsg[] = "Invalid stop hour found";
|
||||||
|
}
|
||||||
|
else if (!$stopmin) {
|
||||||
|
$warnMsg[] = "Invalid stop min found";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$stophour = (int)$this->data['stop_hour'];
|
||||||
|
$stopmin = (int)$this->data['stop_min'];
|
||||||
|
if ($stophour == 0 && $stopmin == 0) {
|
||||||
|
$warnMsg[] = "Invalid stop hour/minute found";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$validHourMin = ($stophour == 0 && $stopmin == 0) ? false : true;
|
||||||
|
if (!$validHourMin) {
|
||||||
|
$warnMsg[] = "Invalid stop hour/minute found";
|
||||||
|
}
|
||||||
|
$fromDate = $this->data['from_datetime'];
|
||||||
|
$toDate = $this->data['to_datetime'];
|
||||||
|
|
||||||
|
$formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; //'07-05-2025 08:00' or '07-05-2025 08:00:00'
|
||||||
|
|
||||||
|
$fdateTime = null;
|
||||||
|
$tdateTime = null;
|
||||||
|
// Try parsing with multiple formats
|
||||||
|
foreach ($formats as $format) {
|
||||||
|
try {
|
||||||
|
$fdateTime = Carbon::createFromFormat($format, $fromDate);
|
||||||
|
break;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Optionally collect warning messages
|
||||||
|
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($formats as $format) {
|
||||||
|
try {
|
||||||
|
$tdateTime = Carbon::createFromFormat($format, $toDate);
|
||||||
|
break;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Optionally collect warning messages
|
||||||
|
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// $fDateOnly = '';
|
||||||
|
if (!isset($fdateTime)) {
|
||||||
|
// throw new \Exception('Invalid date time format');
|
||||||
|
$warnMsg[] = "Invalid 'From DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
}
|
||||||
|
// else {
|
||||||
|
// $fDateOnly = $fdateTime->toDateString();
|
||||||
|
// }
|
||||||
|
if (!isset($tdateTime)) {
|
||||||
|
$warnMsg[] = "Invalid 'To DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($fdateTime) && isset($tdateTime)) {
|
||||||
|
if ($fdateTime->greaterThan($tdateTime)) {
|
||||||
|
$warnMsg[] = "'From DataTime' is greater than 'To DateTime'.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (!$fromDate) { // || $fromDate->format('d-m-Y H:i') !== $from)
|
||||||
|
// $warnMsg[] = "Invalid 'From DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
// }
|
||||||
|
// else if (!$toDate) { // || $toDate->format('d-m-Y H:i') !== $to
|
||||||
|
// $warnMsg[] = "Invalid 'To DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
// }
|
||||||
|
|
||||||
|
$user = User::where('name', $this->data['operator_id'])->first();
|
||||||
|
if (!$user) {
|
||||||
|
$warnMsg[] = "Operator ID not found";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($warnMsg)) {
|
||||||
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
ProductionLineStop::updateOrCreate([
|
||||||
|
'plant_id' => $plant->id,
|
||||||
|
'line_id' => $line->id,
|
||||||
|
'shift_id' => $shift->id,
|
||||||
|
'linestop_id' => $linestop->id,
|
||||||
|
'from_datetime' => $fdateTime->format('Y-m-d H:i:s'),//$this->data['from_datetime'],
|
||||||
|
'to_datetime' => $tdateTime->format('Y-m-d H:i:s'),//$this->data['to_datetime'],
|
||||||
|
'stop_hour' => (int)$this->data['stop_hour'],
|
||||||
|
'stop_min' => (int)$this->data['stop_min'],
|
||||||
|
'operator_id' => $this->data['operator_id'],
|
||||||
|
]);
|
||||||
|
return null;
|
||||||
// return ProductionLineStop::firstOrNew([
|
// return ProductionLineStop::firstOrNew([
|
||||||
// // Update existing records, matching them by `$this->data['column_name']`
|
// // Update existing records, matching them by `$this->data['column_name']`
|
||||||
// 'email' => $this->data['email'],
|
// 'email' => $this->data['email'],
|
||||||
// ]);
|
// ]);
|
||||||
|
|
||||||
return new ProductionLineStop();
|
// return new ProductionLineStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getCompletedNotificationBody(Import $import): string
|
public static function getCompletedNotificationBody(Import $import): string
|
||||||
|
|||||||
Reference in New Issue
Block a user