Added combined unique and default value
This commit is contained in:
@@ -5,7 +5,9 @@ namespace App\Filament\Resources;
|
||||
use App\Filament\Imports\ProductionQuantityImporter;
|
||||
use App\Filament\Resources\ProductionQuantityResource\Pages;
|
||||
use App\Filament\Resources\ProductionQuantityResource\RelationManagers;
|
||||
use App\Models\Item;
|
||||
use App\Models\ProductionQuantity;
|
||||
use App\Models\Shift;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
@@ -40,6 +42,9 @@ class ProductionQuantityResource extends Resource
|
||||
->required()
|
||||
// ->nullable()
|
||||
->reactive()
|
||||
->default(function () {
|
||||
return optional(ProductionQuantity::latest()->first())->plant_id;
|
||||
})
|
||||
->disabled(fn (Get $get) => !empty($get('id')))
|
||||
// ->afterStateUpdated(fn ($set) => $set('block_name', null))
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
@@ -73,12 +78,16 @@ class ProductionQuantityResource extends Resource
|
||||
->toArray();
|
||||
})
|
||||
->reactive()
|
||||
->default(function () {
|
||||
$latestShiftId = optional(ProductionQuantity::latest()->first())->shift_id;
|
||||
return optional(Shift::where('id', $latestShiftId)->first())->block_id;
|
||||
})
|
||||
// ->afterStateUpdated(fn ($set) => $set('shift_id', null))
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
$getShift = ProductionQuantity::where('id', $get('id'))->first();
|
||||
$getBlock = \App\Models\Shift::where('id', $getShift->shift_id)->first();
|
||||
$getBlock = Shift::where('id', $getShift->shift_id)->first();
|
||||
if($getBlock->block_id)
|
||||
{
|
||||
$set('block_name', $getBlock->block_id);
|
||||
@@ -113,13 +122,16 @@ class ProductionQuantityResource extends Resource
|
||||
return [];
|
||||
}
|
||||
|
||||
return \App\Models\Shift::where('plant_id', $get('plant_id'))
|
||||
return Shift::where('plant_id', $get('plant_id'))
|
||||
->where('block_id', $get('block_name'))
|
||||
->pluck('name', 'id')
|
||||
->toArray();
|
||||
})
|
||||
->reactive()
|
||||
->afterStateUpdated(fn ($set) => $set('line_id', null))
|
||||
->default(function () {
|
||||
return optional(ProductionQuantity::latest()->first())->shift_id;
|
||||
})
|
||||
//->afterStateUpdated(fn ($set) => $set('line_id', null))
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
@@ -168,6 +180,9 @@ class ProductionQuantityResource extends Resource
|
||||
->toArray();
|
||||
})
|
||||
->reactive()
|
||||
->default(function () {
|
||||
return optional(ProductionQuantity::latest()->first())->line_id;
|
||||
})
|
||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||
if($get('id'))
|
||||
{
|
||||
@@ -200,15 +215,15 @@ class ProductionQuantityResource extends Resource
|
||||
->latest() // Orders by created_at DESC
|
||||
->first();
|
||||
|
||||
if($exists)
|
||||
{
|
||||
$existCode = \App\Models\Item::where('id', $exists->item_id)->first();
|
||||
$set('recent_qr', $existCode->code.'|'.$exists->serial_number);
|
||||
}
|
||||
else
|
||||
{
|
||||
$set('recent_qr', null);
|
||||
}
|
||||
// if($exists)
|
||||
// {
|
||||
// $existCode = Item::where('id', $exists->item_id)->first();
|
||||
// $set('recent_qr', $existCode->code.'|'.$exists->serial_number);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// $set('recent_qr', null);
|
||||
// }
|
||||
}
|
||||
})
|
||||
->extraAttributes(fn ($get) => [
|
||||
@@ -230,6 +245,8 @@ class ProductionQuantityResource extends Resource
|
||||
// ->required()
|
||||
->reactive()
|
||||
->autofocus(true)
|
||||
->debounce(300)
|
||||
// ->submitOnEnter()
|
||||
->afterStateUpdated(function ($state, callable $get, callable $set) {
|
||||
// **Check if input is empty before processing**
|
||||
if (empty($state)) {
|
||||
@@ -307,7 +324,7 @@ class ProductionQuantityResource extends Resource
|
||||
{
|
||||
$currentDate = date('Y-m-d');
|
||||
|
||||
$shiftId = \App\Models\Shift::where('id', $get('shift_id'))
|
||||
$shiftId = Shift::where('id', $get('shift_id'))
|
||||
->first();
|
||||
|
||||
[$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0];
|
||||
@@ -362,7 +379,7 @@ class ProductionQuantityResource extends Resource
|
||||
//$currentDate = date('Y-m-d');
|
||||
$yesterday = date('Y-m-d', strtotime('-1 days'));
|
||||
|
||||
$shiftId = \App\Models\Shift::where('id', $get('shift_id'))
|
||||
$shiftId = Shift::where('id', $get('shift_id'))
|
||||
->first();
|
||||
|
||||
[$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0];
|
||||
@@ -388,7 +405,7 @@ class ProductionQuantityResource extends Resource
|
||||
{
|
||||
$currentDate = date('Y-m-d');
|
||||
|
||||
$shiftId = \App\Models\Shift::where('id', $get('shift_id'))
|
||||
$shiftId = Shift::where('id', $get('shift_id'))
|
||||
->first();
|
||||
|
||||
[$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0];
|
||||
@@ -447,7 +464,7 @@ class ProductionQuantityResource extends Resource
|
||||
//$currentDate = date('Y-m-d');
|
||||
$yesterday = date('Y-m-d', strtotime('-1 days'));
|
||||
|
||||
$shiftId = \App\Models\Shift::where('id', $get('shift_id'))
|
||||
$shiftId = Shift::where('id', $get('shift_id'))
|
||||
->first();
|
||||
|
||||
[$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0];
|
||||
@@ -484,7 +501,7 @@ class ProductionQuantityResource extends Resource
|
||||
{
|
||||
$currentDate = date('Y-m-d');
|
||||
|
||||
$shiftId = \App\Models\Shift::where('id', $get('shift_id'))
|
||||
$shiftId = Shift::where('id', $get('shift_id'))
|
||||
->first();
|
||||
|
||||
[$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0];
|
||||
@@ -555,7 +572,7 @@ class ProductionQuantityResource extends Resource
|
||||
|
||||
Notification::make()
|
||||
->title('Invalid QR')
|
||||
->body("Scan the valid QR code. (Ex: Item_Code|Serial_Number )")
|
||||
->body("Scan the valid QR code.<br>(Ex: Item_Code|Serial_Number )")
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
@@ -623,7 +640,7 @@ class ProductionQuantityResource extends Resource
|
||||
|
||||
Notification::make()
|
||||
->title('Invalid QR')
|
||||
->body("Scan the valid QR code. (Ex: Item_Code|Serial_Number )")
|
||||
->body("Scan the valid QR code.<br>(Ex: Item_Code|Serial_Number )")
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
@@ -642,7 +659,7 @@ class ProductionQuantityResource extends Resource
|
||||
$serialNumber = isset($parts[1]) ? trim($parts[1]) : null;
|
||||
|
||||
// Fetch item using item code and plant_id
|
||||
$item = \App\Models\Item::where('code', $itemCode)
|
||||
$item = Item::where('code', $itemCode)
|
||||
->where('plant_id', $get('plant_id'))
|
||||
->first();
|
||||
|
||||
@@ -698,11 +715,27 @@ class ProductionQuantityResource extends Resource
|
||||
->required()
|
||||
->unique(ignoreRecord: true)
|
||||
->readOnly(true)
|
||||
->autocapitalize('serial_number'),
|
||||
->autocapitalize('characters'),
|
||||
//->columnSpanFull(),
|
||||
Forms\Components\TextInput::make('recent_qr') //item_description
|
||||
->label('Last scanned QR')
|
||||
->reactive()
|
||||
->default(function () {
|
||||
// Get the latest 'item_id' foreign key from 'production_quantities' table
|
||||
$latestProductionQuantity = ProductionQuantity::latest()->first();
|
||||
if (!$latestProductionQuantity) {
|
||||
return null; // Return null if no production quantities exist
|
||||
}
|
||||
|
||||
// Get the corresponding 'code' from 'items' table where 'id' matches 'item_id'
|
||||
$itemCode = optional(Item::find($latestProductionQuantity->item_id))->code;
|
||||
|
||||
// Get the latest 'serial_number' from 'production_quantities' table
|
||||
$serialNumber = $latestProductionQuantity->serial_number;
|
||||
|
||||
// Combine 'code' and 'serial_number' into the desired format
|
||||
return $itemCode && $serialNumber ? "{$itemCode} | {$serialNumber}" : null;
|
||||
})
|
||||
->readOnly(true),
|
||||
Forms\Components\TextInput::make('id')
|
||||
->hidden()
|
||||
@@ -759,7 +792,8 @@ class ProductionQuantityResource extends Resource
|
||||
])
|
||||
->headerActions([
|
||||
ImportAction::make()
|
||||
->importer(ProductionQuantityImporter::class),
|
||||
->importer(ProductionQuantityImporter::class)
|
||||
->maxRows(100000),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user