Update MotorTestingMasterExporter and MotorTestingMasterImporter to use plant code and improve validation logic

This commit is contained in:
dhanabalan
2025-09-10 10:24:07 +05:30
parent aaa162a3f2
commit 1bd14d5f73
2 changed files with 257 additions and 8 deletions

View File

@@ -22,7 +22,7 @@ class MotorTestingMasterExporter extends Exporter
// Increment and return the row number // Increment and return the row number
return ++$rowNumber; return ++$rowNumber;
}), }),
ExportColumn::make('plant.name') ExportColumn::make('plant.code')
->label('PLANT'), ->label('PLANT'),
ExportColumn::make('item.category') ExportColumn::make('item.category')
->label('CATEGORY'), ->label('CATEGORY'),

View File

@@ -2,11 +2,17 @@
namespace App\Filament\Imports; namespace App\Filament\Imports;
use App\Models\Item;
use App\Models\MotorTestingMaster; use App\Models\MotorTestingMaster;
use App\Models\Plant;
use App\Models\User;
use DateTime;
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; use Filament\Facades\Filament;
use Str;
class MotorTestingMasterImporter extends Importer class MotorTestingMasterImporter extends Importer
{ {
@@ -206,9 +212,9 @@ class MotorTestingMasterImporter extends Importer
ImportColumn::make('plant') ImportColumn::make('plant')
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant') ->exampleHeader('Plant')
->example(['Ransar Industries-I','Ransar Industries-I','Ransar Industries-I']) ->example(['1000','1010','1020'])
->label('Plant') ->label('Plant')
->relationship(resolveUsing: 'name') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
ImportColumn::make('created_by') ImportColumn::make('created_by')
->requiredMapping() ->requiredMapping()
@@ -227,12 +233,245 @@ class MotorTestingMasterImporter extends Importer
public function resolveRecord(): ?MotorTestingMaster public function resolveRecord(): ?MotorTestingMaster
{ {
// return MotorTestingMaster::firstOrNew([ $warnMsg = [];
// // Update existing records, matching them by `$this->data['column_name']` $plantCod = trim($this->data['plant']);
// 'email' => $this->data['email'], $iCode = trim($this->data['item']);
// ]); $sCode = trim($this->data['subassembly_code']);
$isiModel = (trim($this->data['isi_model']) == "1") ? true : false;
$phase = trim($this->data['phase']);
$kw = trim($this->data['kw']);
$hp = trim($this->data['hp']);
$volt = trim($this->data['volt']);
$current = trim($this->data['current']);
$rpm = trim($this->data['rpm']);
$torque = trim($this->data['torque']);
$frequency = trim($this->data['frequency']);
$connection = trim($this->data['connection']);
$insResType = trim($this->data['ins_res_type']);
$insResLimit = trim($this->data['ins_res_limit']);
$routineTestTime = trim($this->data['routine_test_time']);
$resRyLl = trim($this->data['res_ry_ll']);
$resRyUl = trim($this->data['res_ry_ul']);
$resYbLl = trim($this->data['res_yb_ll']);
$resYbUl = trim($this->data['res_yb_ul']);
$resBrLl = trim($this->data['res_br_ll']);
$resBrUl = trim($this->data['res_br_ul']);
$lockVoltLimit = trim($this->data['lock_volt_limit']);
$leakCurLimit = trim($this->data['leak_cur_limit']);
$lockCurLl = trim($this->data['lock_cur_ll']);
$lockCurUl = trim($this->data['lock_cur_ul']);
$noloadCurLl = trim($this->data['noload_cur_ll']);
$noloadCurUl = trim($this->data['noload_cur_ul']);
$noloadPowLl = trim($this->data['noload_pow_ll']);
$noloadPowUl = trim($this->data['noload_pow_ul']);
$noloadSpdLl = trim($this->data['noload_spd_ll']);
$noloadSpdUl = trim($this->data['noload_spd_ul']);
$createdBy = trim($this->data['created_by']);
$updatedBy = trim($this->data['updated_by']);
return new MotorTestingMaster(); $plant = null;
if (Str::length($plantCod) < 4 || !is_numeric($plantCod) || !preg_match('/^[1-9]\d{3,}$/', $plantCod))
{
$warnMsg[] = "Invalid plant code found";
}
else if (Str::length($iCode) < 6 || !ctype_alnum($iCode))
{
$warnMsg[] = "Invalid item code found";
}
else if (Str::length($sCode) < 6 || !ctype_alnum($sCode))
{
$warnMsg[] = "Invalid sub-assembly code found";
}
else
{
$plant = Plant::where('code', $plantCod)->first();
$codeExist = Item::where('code', $iCode)->first();
if ($plant)
{
$iCode = Item::where('code', $iCode)->where('plant_id', $plant->id)->first();
}
if (!$plant)
{
$warnMsg[] = "Plant not found";
}
else if (!$codeExist)
{
$warnMsg[] = "Item code not found";
}
else if (!$iCode)
{
$warnMsg[] = "Item code not found for choosed plant";
}
else
{
// if (Str::length($isiModel) <= 0 || $isiModel == "0" || $isiModel == "1") {
// $warnMsg[] = "Invalid ISI Model found";
// }
// else
// {
// $isiModel = ($isiModel == "1");
// }
if (Str::length($phase) <= 0 || ($phase != "Single" && $phase != "Three")) {//!is_string($phase) ||
$warnMsg[] = "Invalid phase found";
}
if (Str::length($hp) <= 0 || !is_numeric($hp)) {
$warnMsg[] = "Invalid HP found";
}
if (Str::length($kw) <= 0 || !is_numeric($kw)) {
$warnMsg[] = "Invalid KW found";
}
if (Str::length($volt) <= 0 || !is_numeric($volt)) {
$warnMsg[] = "Invalid volt found";
}
if (Str::length($current) <= 0 || !is_numeric($current)) {
$warnMsg[] = "Invalid current found";
}
if (Str::length($rpm) <= 0 || !is_numeric($rpm)) {
$warnMsg[] = "Invalid RPM found";
}
if (Str::length($torque) <= 0 || !is_numeric($torque)) {
$warnMsg[] = "Invalid torque found";
}
if (Str::length($frequency) <= 0 || !is_numeric($frequency)) {
$warnMsg[] = "Invalid frequency found";
}
if (Str::length($connection) <= 0 || ($connection != "Star-Delta" && $connection != "Star" && $connection != "Delta")) {
$warnMsg[] = "Invalid connection found";
}
if (Str::length($insResLimit) <= 0 || !is_numeric($insResLimit)) {
$warnMsg[] = "Invalid insulation resistance limit found";
}
if (Str::length($insResType) <= 0 || ($insResType != "O" && $insResType != "M" && $insResType != "G")) {
$warnMsg[] = "Invalid insulation resistance type found";
}
if (Str::length($routineTestTime) <= 0 || !isValidTimeFormat($routineTestTime)) {
$warnMsg[] = "Invalid routine test time found";
}
if (Str::length($resRyLl) <= 0 || !is_numeric($resRyLl)) {
$warnMsg[] = "Invalid resistance RY lower limit found";
}
if (Str::length($resRyUl) <= 0 || !is_numeric($resRyUl)) {
$warnMsg[] = "Invalid resistance RY upper limit found";
}
if (Str::length($resYbLl) <= 0 || !is_numeric($resYbLl)) {
$warnMsg[] = "Invalid resistance YB lower limit found";
}
if (Str::length($resYbUl) <= 0 || !is_numeric($resYbUl)) {
$warnMsg[] = "Invalid resistance YB upper limit found";
}
if (Str::length($resBrLl) <= 0 || !is_numeric($resBrLl)) {
$warnMsg[] = "Invalid resistance BR lower limit found";
}
if (Str::length($resBrUl) <= 0 || !is_numeric($resBrUl)) {
$warnMsg[] = "Invalid resistance BR upper limit found";
}
if (Str::length($lockVoltLimit) <= 0 || !is_numeric($lockVoltLimit)) {
$warnMsg[] = "Invalid locked volt limit found";
}
if (Str::length($leakCurLimit) <= 0 || !is_numeric($leakCurLimit)) {
$warnMsg[] = "Invalid leakage current limit found";
}
if (Str::length($lockCurLl) <= 0 || !is_numeric($lockCurLl)) {
$warnMsg[] = "Invalid locked current lower limit found";
}
if (Str::length($lockCurUl) <= 0 || !is_numeric($lockCurUl)) {
$warnMsg[] = "Invalid locked current upper limit found";
}
if (Str::length($noloadCurLl) <= 0 || !is_numeric($noloadCurLl)) {
$warnMsg[] = "Invalid no load current lower limit found";
}
if (Str::length($noloadCurUl) <= 0 || !is_numeric($noloadCurUl)) {
$warnMsg[] = "Invalid no load current upper limit found";
}
if (Str::length($noloadPowLl) <= 0 || !is_numeric($noloadPowLl)) {
$warnMsg[] = "Invalid no load power lower limit found";
}
if (Str::length($noloadPowUl) <= 0 || !is_numeric($noloadPowUl)) {
$warnMsg[] = "Invalid no load power upper limit found";
}
if (Str::length($noloadSpdLl) <= 0 || !is_numeric($noloadSpdLl)) {
$warnMsg[] = "Invalid no load speed lower limit found";
}
if (Str::length($noloadSpdUl) <= 0 || !is_numeric($noloadSpdUl)) {
$warnMsg[] = "Invalid no load speed upper limit found";
}
$oldCode = MotorTestingMaster::where('item_id', $iCode->id)->where('plant_id', $plant->id)->first();
if ($oldCode) {
$created = $oldCode->created_by ? User::where('name', $oldCode->created_by)->first() : null;
if (!$created) {
$warnMsg[] = "Created by not found on update";
}
$updated = User::where('name', $updatedBy)->first();
if (!$updated) {
$warnMsg[] = "Updated by not found on update";
}
}
else
{
$created = User::where('name', $createdBy)->first();
if (!$created) {
$warnMsg[] = "Created by not found";
}
$updated = User::where('name', $updatedBy)->first();
if (!$updated) {
$warnMsg[] = "Updated by not found";
}
}
}
}
if (!empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
MotorTestingMaster::updateOrCreate([
'plant_id' => $plant->id,
'item_id' => $iCode->id
],
[
'subassembly_code' => $sCode,
'isi_model' => $isiModel,
'phase' => $phase,
'kw' => $kw,
'hp' => $hp,
'volt' => $volt,
'current' => $current,
'rpm' => $rpm,
'torque' => $torque,
'frequency' => $frequency,
'connection' => $connection,
'ins_res_limit' => $insResLimit,
'ins_res_type' => $insResType,
'routine_test_time' => $routineTestTime,
'res_ry_ll' => $resRyLl,
'res_ry_ul' => $resRyUl,
'res_yb_ll' => $resYbLl,
'res_yb_ul' => $resYbUl,
'res_br_ll' => $resBrLl,
'res_br_ul' => $resBrUl,
'lock_volt_limit' => $lockVoltLimit,
'leak_cur_limit' => $leakCurLimit,
'lock_cur_ll' => $lockCurLl,
'lock_cur_ul' => $lockCurUl,
'noload_cur_ll' => $noloadCurLl,
'noload_cur_ul' => $noloadCurUl,
'noload_pow_ll' => $noloadPowLl,
'noload_pow_ul' => $noloadPowUl,
'noload_spd_ll' => $noloadSpdLl,
'noload_spd_ul' => $noloadSpdUl,
'created_by' => $created->name,
'updated_by' => $updated->name,
]
);
return null;
// // return MotorTestingMaster::firstOrNew([
// // // Update existing records, matching them by `$this->data['column_name']`
// // 'email' => $this->data['email'],
// // ]);
// return new MotorTestingMaster();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
@@ -246,3 +485,13 @@ class MotorTestingMasterImporter extends Importer
return $body; return $body;
} }
} }
function isValidTimeFormat($time) {
// If time starts with a single digit hour without leading zero, pad it
if (preg_match('/^\d(:\d{2}:\d{2})$/', $time, $matches)) {
$time = '0' . $time;
}
$dateTime = DateTime::createFromFormat('H:i:s', $time);
return $dateTime && $dateTime->format('H:i:s') === $time;
}