Files
pds/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php
2025-04-23 20:51:21 +05:30

230 lines
7.6 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 Filament\Support\Enums\MaxWidth;
use Livewire\Livewire;
use Log;
use Route;
class CreateProductionQuantity extends CreateRecord
{
protected static string $resource = ProductionQuantityResource::class;
// public function getTitle(): string
// {
// return 'Create'; // This should display in the breadcrumb as "Production Quantities > Create"
// }
// Hide only the big H1 heading on the page
public function getHeading(): string
{
return '';
}
public function boot(): void
{
// dd('Current Route: ' . Route::currentRouteName());
Filament::registerRenderHook(
'panels::body.start',
function () {
if (str_contains(Route::currentRouteName(), 'filament.admin.resources.production-quantities.create')) {
echo <<<HTML
<style>
/* Hide sidebar and topbar */
.fi-sidebar,
.fi-topbar {
display: none !important;
}
/* Expand main container to full screen */
.fi-main {
margin: 0 !important;
padding: 0 !important;
/* height: 20%; */
max-width: 100% !important;
overflow-y: auto; Allow vertical scrolling
}
/* Expand page area fully */
.fi-main > .fi-page {
padding: 0 !important;
max-width: 100% !important;
}
/* Allow scroll on body again */
body {
overflow: auto;
}
</style>
<!-- <script>
window.addEventListener('keydown', function(event) {
if (event.key === 'F11') {
toggleFullScreen();
}
});
</script> -->
HTML;
}
}
);
}
// public function getMaxContentWidth(): MaxWidth
// {
// return MaxWidth::Full;
// }
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, $prodOrder;
// protected static bool $canCreateAnother = false;
public function getFormActions(): array
{
// return parent::getFormActions(); //return [];
// return [
// $this->getCancelFormAction(),
// ];
return [];
}
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(); //->name
$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->prodOrder = $formData['production_order'] ?? null;
$this->recQr = $formData['recent_qr'] ?? null;
} catch (\Exception $e) {
//dd('Error parsing form data:', $e->getMessage(), $formValues);
Notification::make()
->title("Error Parsing Form Data") // {$operatorName}
->body($e->getMessage())
->danger()
// ->persistent()
->send();
return;
}
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,
'production_order'=> null,
'operator_id'=> $operatorName,
'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,
'production_order'=> $this->prodOrder,
'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,
'production_order'=> $this->prodOrder,
'operator_id'=> $operatorName,
'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);
}
}