From 716d2e48d7bc89056c0843620388bd46494e667d Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 25 Apr 2025 13:01:17 +0530 Subject: [PATCH] Added Item code exist validation against plant on create invoice and scan qr --- .../Pages/CreateInvoiceValidation.php | 156 +++++++++++++++++- 1 file changed, 148 insertions(+), 8 deletions(-) diff --git a/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php b/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php index c76dd696c..e1ab0ec3b 100644 --- a/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php +++ b/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php @@ -277,6 +277,7 @@ class CreateInvoiceValidation extends CreateRecord $uniqueCodes = array_unique($materialCodes); + //itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -294,7 +295,36 @@ class CreateInvoiceValidation extends CreateRecord $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); Notification::make() - ->title('Unknown Item Codes') + ->title('Unknown: Item Codes') + ->body($message) + ->danger() + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + //plantNotFound + $matchedItems = StickerMaster::with('item') + ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + }) + ->get(); + + $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + if (!empty($missingCodes)) + { + $missingCount = count($missingCodes); + + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + Notification::make() + ->title('Unknown: Item Codes') ->body($message) ->danger() ->send(); @@ -737,6 +767,7 @@ class CreateInvoiceValidation extends CreateRecord $uniqueCodes = array_unique($materialCodes); + //itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -751,10 +782,10 @@ class CreateInvoiceValidation extends CreateRecord { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); //->where('plant_id', $plantId) Notification::make() - ->title('Unknown Item Codes') + ->title('Unknown: Item Codes') ->body($message) ->danger() ->send(); @@ -765,6 +796,36 @@ class CreateInvoiceValidation extends CreateRecord return; } + //plantNotFound + $matchedItems = StickerMaster::with('item') + ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + }) + ->get(); + + $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + if (!empty($missingCodes)) + { + $missingCount = count($missingCodes); + + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + Notification::make() + ->title('Unknown: Item Codes') + ->body($message) + ->danger() + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + $invalidCodes = $matchedItems ->filter(fn ($sticker) => !empty($sticker->material_type)) //filter invalid ->pluck('item.code') @@ -1217,6 +1278,7 @@ class CreateInvoiceValidation extends CreateRecord $uniqueCodes = array_unique($materialCodes); + //itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -1234,7 +1296,36 @@ class CreateInvoiceValidation extends CreateRecord $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); Notification::make() - ->title('Unknown Item Codes') + ->title('Unknown: Item Codes') + ->body($message) + ->danger() + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + //plantNotFound + $matchedItems = StickerMaster::with('item') + ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + }) + ->get(); + + $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + if (!empty($missingCodes)) + { + $missingCount = count($missingCodes); + + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + Notification::make() + ->title('Unknown: Item Codes') ->body($message) ->danger() ->send(); @@ -1250,7 +1341,6 @@ class CreateInvoiceValidation extends CreateRecord ->pluck('item.code') ->toArray(); - if (!empty($invalidCodes)) { $missingCount = count($invalidCodes); @@ -1551,6 +1641,7 @@ class CreateInvoiceValidation extends CreateRecord $uniqueCodes = array_unique($materialCodes); + //itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -1578,7 +1669,36 @@ class CreateInvoiceValidation extends CreateRecord $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); Notification::make() - ->title('Unknown Item Codes') + ->title('Unknown: Item Codes') + ->body($message) + ->danger() + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + //plantNotFound + $matchedItems = StickerMaster::with('item') + ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + }) + ->get(); + + $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + if (!empty($missingCodes)) + { + $missingCount = count($missingCodes); + + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + Notification::make() + ->title('Unknown: Item Codes') ->body($message) ->danger() ->send(); @@ -1594,7 +1714,6 @@ class CreateInvoiceValidation extends CreateRecord ->pluck('item.code') ->toArray(); - if (!empty($invalidCodes)) { $missingCount = count($invalidCodes); @@ -1995,7 +2114,7 @@ class CreateInvoiceValidation extends CreateRecord } // $record = StickerMaster::where('plant_id', $plantId)->with('item')->where('item.code', $itemCode); - $record = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) { + $record = StickerMaster::whereHas('item', function ($query) { $query->where('code', $this->currentItemCode); }); if ($record->count() <= 0) @@ -2016,6 +2135,27 @@ class CreateInvoiceValidation extends CreateRecord return; } + $record = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) { + $query->where('code', $this->currentItemCode); + }); + if ($record->count() <= 0) + { + Notification::make() + ->title('Unknown: Item Code') + ->body("Item code '$itemCode' not found in database for choosed plant.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + $record = StickerMaster::where('plant_id', $plantId)->where('item_id', Item::where('plant_id', $plantId)->where('code', $itemCode)->first()->id)->first(); if (empty($record->material_type) || !$record->material_type)