Files
pds/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php
2025-04-19 12:47:53 +05:30

146 lines
4.7 KiB
PHP

<?php
namespace App\Filament\Resources\ProductionQuantityResource\Pages;
use App\Filament\Resources\ProductionQuantityResource;
use App\Filament\Widgets\ItemOverview;
use App\Models\ProductionQuantity;
use App\Models\Shift;
use Filament\Actions;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Livewire\Livewire;
class CreateProductionQuantity extends CreateRecord
{
protected static string $resource = ProductionQuantityResource::class;
protected function getFooterWidgets(): array
{
return [
ItemOverview::make(),
];
}
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('create'); // Stay on Create Page after saving
}
public $qrData, $pId, $bId, $sId, $lId, $iId, $succId, $sNoId, $succStat, $recQr;
// protected static bool $canCreateAnother = false;
public function getFormActions(): array
{
// return parent::getFormActions(); //return [];
return [
$this->getCancelFormAction(),
];
}
public function processAllValues($formData)
{
//$formValues = [];
$formValues = request()->all(); // This will get all form data from the request
//dd($formValues);
$this->validateAndProcessForm($formValues); // Process the form values
}
public function validateAndProcessForm($formValues)
{
$user = Filament::auth()->user();
$operatorName = $user->name;
// $this->validate();
try {
// Access the nested form data
$componentData = json_decode($formValues['components'][0]['snapshot'], true);
$formData = $componentData['data']['data'][0]; // Access first item in data array
// Extract specific values
$this->qrData = $formData['item_code'] ?? null;
$this->pId = $formData['plant_id'] ?? null;
$this->bId = $formData['block_name'] ?? null;
$this->sId = $formData['shift_id'] ?? null;
$this->lId = $formData['line_id'] ?? null;
$this->iId = $formData['item_id'] ?? null;
$this->succId = $formData['success_msg'] ?? null;
// $this->sNoId = $formData['serial_number'] ?? null;
$this->recQr = $formData['recent_qr'] ?? null;
} catch (\Exception $e) {
dd('Error parsing form data:', $e->getMessage(), $formValues);
}
if ($this->succId === null) {
$this->form->fill([
'plant_id'=> $this->pId,
'block_name'=> $this->bId,
'shift_id'=> $this->sId,
'line_id'=> $this->lId,
'item_id'=> null,
'serial_number'=> null,
'success_msg'=> null,
'recent_qr' => $this->recQr,
]);
Notification::make()
->title("Invalid QR Found") // {$operatorName}
->body("Please, scan the valid QR code.")
->danger()
// ->persistent()
->send();
return;
}
else
{
// // Perform any additional processing or database operations
// $this->saveFormData($formValues);
$parts = explode('|', $this->qrData);
$itemCode = trim($parts[0]);
$this->sNoId = isset($parts[1]) ? trim($parts[1]) : null;
ProductionQuantity::create([
'plant_id'=> $this->pId,
'shift_id'=> $this->sId,
'line_id' => $this->lId,
'item_id'=> $this->iId,
'serial_number' => $this->sNoId,
// 'operator_id'=> $operatorName,
]);
// after success insertion
$this->form->fill([
'plant_id'=> $this->pId,
'block_name'=> $this->bId,
'shift_id'=> $this->sId,
'line_id'=> $this->lId,
'item_id'=> null,
'serial_number'=> null,
'success_msg'=> null,
'recent_qr' => $itemCode.' | '.$this->sNoId,
]);
Notification::make()
->title("Valid QR Found") // {$operatorName}
->body("Valid QR code scanned: {$this->qrData}.")
->success()
// ->persistent()
->send();
}
}
protected function saveFormData($formValues)
{
// Save the form data to the database or perform other operations
// For example:
$model = ProductionQuantity::create($formValues);
// // Optionally, you can emit an event or perform a redirect after saving
// $this->emit('formSaved', $model->id);
}
}