From c43bc208b84afc94cacf8b2f2cd0d3a1356518e0 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 13 Dec 2025 14:45:14 +0530 Subject: [PATCH] changed loadData method in invoice data tabele in livewire --- app/Livewire/InvoiceDataTable.php | 207 +++++++++++++++++++++--------- 1 file changed, 145 insertions(+), 62 deletions(-) diff --git a/app/Livewire/InvoiceDataTable.php b/app/Livewire/InvoiceDataTable.php index bd70248..99ee2f8 100644 --- a/app/Livewire/InvoiceDataTable.php +++ b/app/Livewire/InvoiceDataTable.php @@ -83,7 +83,100 @@ class InvoiceDataTable extends Component // $this->showCapacitorInput = false; } - public function loadData($invoiceNumber, $plantId, $onCapFocus) + // public function loadData($invoiceNumber, $plantId, $onCapFocus) + // { + // $this->plantId = $plantId; + // $this->invoiceNumber = $invoiceNumber; + // $this->completedInvoice = false; + // $this->isSerial = true; + // $this->onCapFocus = $onCapFocus; + // $this->emptyInvoice = false; + // $this->hasSearched = true; + // $this->materialInvoice = false; + // // $this->showCapacitorInput = false; + + // // ->where('serial_number', '!=', '') + // $this->invoiceData = InvoiceValidation::where('invoice_number', $this->invoiceNumber) + // ->where('plant_id', $plantId)->where('scanned_status', null) + // ->get() + // ->map(function ($record) { + // return [ + // 'sticker_master_id' => $record->sticker_master_id, + // 'serial_number' => $record->serial_number, + // 'motor_scanned_status' => $record->motor_scanned_status ?? '', + // 'pump_scanned_status' => $record->pump_scanned_status ?? '', + // 'capacitor_scanned_status' => $record->capacitor_scanned_status ?? '', + // 'scanned_status_set' => $record->scanned_status_set ?? '', + // 'scanned_status' => $record->scanned_status ?? '', + // 'panel_box_supplier' => $record->panel_box_supplier ?? '', + // 'panel_box_serial_number' => $record->panel_box_serial_number ?? '', + // 'created_at' => $record->created_at, + // 'operator_id' => $record->operator_id, + // ]; + // }) + // ->toArray(); + + // $this->packageCount = 0; + + // // Loop through and replace 'code' using related StickerMaster > Item > code + // foreach ($this->invoiceData as &$row) { + // $stickCount = 0; + // $scannedCount = 0; + + // // $stickerMaster = \App\Models\StickerMaster::with('item')->find($row['sticker_master_id'] ?? null); + // $row['code'] = StickerMaster::with('item')->find($row['sticker_master_id'] ?? null)?->item?->code ?? 'N/A'; + // $curStick = StickerMaster::where('id', $row['sticker_master_id'])->first(); + // if ($curStick) { + // if (Str::length($curStick->panel_box_code) > 0) { + // $stickCount++; + // } + // if ($curStick->tube_sticker_motor == 1 || $curStick->tube_sticker_pump == 1 || $curStick->tube_sticker_pumpset == 1) { + // if ($curStick->tube_sticker_motor == 1) { + // $stickCount++; + // } + // if ($curStick->tube_sticker_pump == 1 || ($curStick->tube_sticker_pumpset != 1 && $curStick->tube_sticker_pump != 1 && $curStick->pack_slip_pump == 1)) { + // $stickCount++; + // } + // if ($curStick->tube_sticker_pumpset == 1) { + // $stickCount++; + // } + // } elseif ($curStick->pack_slip_motor == 1 || $curStick->pack_slip_pump == 1 || $curStick->pack_slip_pumpset == 1) { + // if ($curStick->pack_slip_motor == 1) { + // $stickCount++; + // } + // if ($curStick->pack_slip_pump == 1) { + // $stickCount++; + // } + // if ($curStick->pack_slip_pumpset == 1) { + // $stickCount++; + // } + // } + // } + + // if ($row['motor_scanned_status'] == 1) { + // $scannedCount++; + // } + // if ($row['pump_scanned_status'] == 1) { + // $scannedCount++; + // } + // if ($row['capacitor_scanned_status'] == 1) { + // $scannedCount++; + // } + // if ($row['scanned_status_set'] == 1) { + // $scannedCount++; + // } + + // $this->packageCount += $stickCount - $scannedCount; + // } + + // if ($onCapFocus) { + // $this->dispatch('focus-capacitor-input'); + // } else { + // $this->dispatch('focus-serial-number'); + // } + // } + + public function loadData($invoiceNumber, $plantId, $onCapFocus = false) { $this->plantId = $plantId; $this->invoiceNumber = $invoiceNumber; @@ -93,80 +186,70 @@ class InvoiceDataTable extends Component $this->emptyInvoice = false; $this->hasSearched = true; $this->materialInvoice = false; - // $this->showCapacitorInput = false; - // ->where('serial_number', '!=', '') - $this->invoiceData = InvoiceValidation::where('invoice_number', $this->invoiceNumber) - ->where('plant_id', $plantId)->where('scanned_status', null) - ->get() - ->map(function ($record) { - return [ - 'sticker_master_id' => $record->sticker_master_id, - 'serial_number' => $record->serial_number, - 'motor_scanned_status' => $record->motor_scanned_status ?? '', - 'pump_scanned_status' => $record->pump_scanned_status ?? '', - 'capacitor_scanned_status' => $record->capacitor_scanned_status ?? '', - 'scanned_status_set' => $record->scanned_status_set ?? '', - 'scanned_status' => $record->scanned_status ?? '', - 'panel_box_supplier' => $record->panel_box_supplier ?? '', - 'panel_box_serial_number' => $record->panel_box_serial_number ?? '', - 'created_at' => $record->created_at, - 'operator_id' => $record->operator_id, - ]; - }) - ->toArray(); + // Eager load stickerMasterRelation and item + $invoiceRecords = InvoiceValidation::with('stickerMasterRelation.item') + ->where('invoice_number', $invoiceNumber) + ->where('plant_id', $plantId) + ->whereNull('scanned_status') + ->get(); + $this->invoiceData = []; $this->packageCount = 0; - // Loop through and replace 'code' using related StickerMaster > Item > code - foreach ($this->invoiceData as &$row) { + foreach ($invoiceRecords as $record) { + $sm = $record->stickerMasterRelation; + + // Compute code + $rowCode = $sm?->item?->code ?? 'N/A'; + $stickCount = 0; $scannedCount = 0; - // $stickerMaster = \App\Models\StickerMaster::with('item')->find($row['sticker_master_id'] ?? null); - $row['code'] = StickerMaster::with('item')->find($row['sticker_master_id'] ?? null)?->item?->code ?? 'N/A'; - $curStick = StickerMaster::where('id', $row['sticker_master_id'])->first(); - if ($curStick) { - if (Str::length($curStick->panel_box_code) > 0) { + if ($sm) { + // Panel box code + if (Str::length($sm->panel_box_code) > 0) { $stickCount++; } - if ($curStick->tube_sticker_motor == 1 || $curStick->tube_sticker_pump == 1 || $curStick->tube_sticker_pumpset == 1) { - if ($curStick->tube_sticker_motor == 1) { - $stickCount++; - } - if ($curStick->tube_sticker_pump == 1 || ($curStick->tube_sticker_pumpset != 1 && $curStick->tube_sticker_pump != 1 && $curStick->pack_slip_pump == 1)) { - $stickCount++; - } - if ($curStick->tube_sticker_pumpset == 1) { - $stickCount++; - } - } elseif ($curStick->pack_slip_motor == 1 || $curStick->pack_slip_pump == 1 || $curStick->pack_slip_pumpset == 1) { - if ($curStick->pack_slip_motor == 1) { - $stickCount++; - } - if ($curStick->pack_slip_pump == 1) { - $stickCount++; - } - if ($curStick->pack_slip_pumpset == 1) { - $stickCount++; - } + + // Tube stickers logic + if ($sm->tube_sticker_motor == 1 || $sm->tube_sticker_pump == 1 || $sm->tube_sticker_pumpset == 1) { + if ($sm->tube_sticker_motor == 1) $stickCount++; + if ($sm->tube_sticker_pump == 1 || ($sm->tube_sticker_pumpset != 1 && $sm->tube_sticker_pump != 1 && $sm->pack_slip_pump == 1)) $stickCount++; + if ($sm->tube_sticker_pumpset == 1) $stickCount++; + } + // Pack slip logic (only if tube sticker block didn't apply) + elseif ($sm->pack_slip_motor == 1 || $sm->pack_slip_pump == 1 || $sm->pack_slip_pumpset == 1) { + if ($sm->pack_slip_motor == 1) $stickCount++; + if ($sm->pack_slip_pump == 1) $stickCount++; + if ($sm->pack_slip_pumpset == 1) $stickCount++; } } - if ($row['motor_scanned_status'] == 1) { - $scannedCount++; - } - if ($row['pump_scanned_status'] == 1) { - $scannedCount++; - } - if ($row['capacitor_scanned_status'] == 1) { - $scannedCount++; - } - if ($row['scanned_status_set'] == 1) { - $scannedCount++; - } + // Count already scanned + $scannedCount += ($record->motor_scanned_status == 1) ? 1 : 0; + $scannedCount += ($record->pump_scanned_status == 1) ? 1 : 0; + $scannedCount += ($record->capacitor_scanned_status == 1) ? 1 : 0; + $scannedCount += ($record->scanned_status_set == 1) ? 1 : 0; - $this->packageCount += $stickCount - $scannedCount; + // Increment packageCount + $this->packageCount += max($stickCount - $scannedCount, 0); + + $this->invoiceData[] = [ + 'sticker_master_id' => $record->sticker_master_id, + 'serial_number' => $record->serial_number, + 'motor_scanned_status' => $record->motor_scanned_status ?? '', + 'pump_scanned_status' => $record->pump_scanned_status ?? '', + 'capacitor_scanned_status' => $record->capacitor_scanned_status ?? '', + 'scanned_status_set' => $record->scanned_status_set ?? '', + 'scanned_status' => $record->scanned_status ?? '', + 'panel_box_supplier' => $record->panel_box_supplier ?? '', + 'panel_box_serial_number' => $record->panel_box_serial_number ?? '', + 'created_at' => $record->created_at, + 'operator_id' => $record->operator_id, + 'code' => $rowCode, + 'stickCount' => $stickCount, + ]; } if ($onCapFocus) {