diff --git a/app/Filament/Resources/ProductionOrderResource.php b/app/Filament/Resources/ProductionOrderResource.php index fa1d1ef..5343f34 100644 --- a/app/Filament/Resources/ProductionOrderResource.php +++ b/app/Filament/Resources/ProductionOrderResource.php @@ -45,7 +45,8 @@ class ProductionOrderResource extends Resource return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) - ->disabled(fn (Get $get) => ! empty($get('id'))) + ->disabled(fn (string $context): bool => $context == 'edit') + ->dehydrated(true) ->default(function () { $userHas = Filament::auth()->user()->plant_id; @@ -84,7 +85,7 @@ class ProductionOrderResource extends Resource ->default(function () { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? optional(ProductionOrder::where('plant_id', $userHas)->latest()->first())->plant_id : optional(ProductionOrder::latest()->first())->plant_id; + return ($userHas && strlen($userHas) > 0) ? optional(ProductionOrder::where('plant_id', $userHas)->latest()->first())->item_id : optional(ProductionOrder::latest()->first())->item_id; }) ->required() ->afterStateUpdated(function ($state, callable $set) { @@ -102,6 +103,7 @@ class ProductionOrderResource extends Resource ->integer() ->required() ->readOnly(fn ($get) => ($get('plant_id') == null || $get('item_id') == null)) + ->disabled(fn (string $context): bool => $context === 'edit') ->afterStateUpdated(function ($state, callable $set, $get) { if (! empty($state) && $state > 0) { $set('show_extra_fields', true); @@ -170,21 +172,39 @@ class ProductionOrderResource extends Resource Forms\Components\Hidden::make('show_extra_fields') ->default(false), Forms\Components\DateTimePicker::make('start_date') - ->label('Start Date'), + ->label('Start Date') + ->reactive() + // ->minDate(function () { + // return Carbon::now()->addMinute(); + // }) + ->afterStateUpdated(function ($state, callable $set) { + $set('end_date', null); + }), Forms\Components\DateTimePicker::make('end_date') - ->label('End Date'), + ->label('End Date') + ->reactive(), + // ->rule('after:start_date') + // ->minDate(function (callable $get) { + // $startDate = $get('start_date'); + // if ($startDate) { + // return Carbon::parse($startDate)->addMinute(); + // } + + // return Carbon::now()->addMinute(); + // }), Forms\Components\TextInput::make('production_order') ->label('Production Order') ->readOnly() - ->visible(fn ($get) => $get('show_extra_fields')), + ->visible(fn ($get) => $get('quantity') > 0), + // ->visible(fn ($get) => $get('show_extra_fields')), Forms\Components\TextInput::make('from_serial_number') ->label('From Serial Number') ->readOnly() - ->visible(fn ($get) => $get('show_extra_fields')), + ->visible(fn ($get) => $get('quantity') > 0), Forms\Components\TextInput::make('to_serial_number') ->label('To Serial Number') ->readOnly() - ->visible(fn ($get) => $get('show_extra_fields')), + ->visible(fn ($get) => $get('quantity') > 0), Forms\Components\Hidden::make('created_by') ->label('Created By') ->default(Filament::auth()->user()?->name), diff --git a/app/Filament/Resources/ProductionOrderResource/Pages/CreateProductionOrder.php b/app/Filament/Resources/ProductionOrderResource/Pages/CreateProductionOrder.php index fa6fc40..581535d 100644 --- a/app/Filament/Resources/ProductionOrderResource/Pages/CreateProductionOrder.php +++ b/app/Filament/Resources/ProductionOrderResource/Pages/CreateProductionOrder.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources\ProductionOrderResource\Pages; use App\Filament\Resources\ProductionOrderResource; +use App\Models\Plant; use App\Models\ProductionOrder; use Filament\Facades\Filament; use Filament\Notifications\Notification; @@ -209,6 +210,44 @@ class CreateProductionOrder extends CreateRecord } } + public function printPanel() + { + $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null; + + $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null; + + $plantCode = Plant::where('id', $plantId)->value('code'); + + if (empty($plantId)) { + Notification::make() + ->title('Plant name cannot be empty!') + ->danger() + ->send(); + + return; + } elseif (empty($pOrder)) { + Notification::make() + ->title('Production order cannot be empty!') + ->danger() + ->send(); + return; + } + + $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first(); + + if (! $pOrderExists) { + Notification::make() + ->title("Production Order '{$pOrder}' does not exist to get print!") + ->danger() + ->send(); + + return; + } else { + return redirect()->route('production-orders.printpanel', ['production_order' => $pOrder, 'plant_code' => $plantCode]); + } + + } + protected function getFormActions(): array { return []; diff --git a/app/Filament/Resources/ProductionOrderResource/Pages/EditProductionOrder.php b/app/Filament/Resources/ProductionOrderResource/Pages/EditProductionOrder.php index 7d0a85f..7fbc4ec 100644 --- a/app/Filament/Resources/ProductionOrderResource/Pages/EditProductionOrder.php +++ b/app/Filament/Resources/ProductionOrderResource/Pages/EditProductionOrder.php @@ -3,13 +3,92 @@ namespace App\Filament\Resources\ProductionOrderResource\Pages; use App\Filament\Resources\ProductionOrderResource; +use App\Models\Plant; +use App\Models\ProductionOrder; use Filament\Actions; +use Filament\Notifications\Notification; use Filament\Resources\Pages\EditRecord; class EditProductionOrder extends EditRecord { protected static string $resource = ProductionOrderResource::class; + public function printProductionOrder() + { + $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null; + + $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null; + + if (empty($plantId)) { + Notification::make() + ->title('Plant name cannot be empty!') + ->danger() + ->send(); + + return; + } elseif (empty($pOrder)) { + Notification::make() + ->title('Production order cannot be empty!') + ->danger() + ->send(); + + return; + } + + $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first(); + + if (! $pOrderExists) { + Notification::make() + ->title("Production Order '{$pOrder}' does not exist to get print!") + ->danger() + ->send(); + + return; + } else { + return redirect()->route('production-orders.print', ['production_order' => $pOrder]); + } + } + + public function printPanel() + { + $plantId = $this->form->getState()['plant_id'] ?? null; + $plantId = filled($plantId) ? (int) $plantId : null; + + $pOrder = $this->form->getState()['production_order'] ?? null; + $pOrder = filled($pOrder) ? trim($pOrder) : null; + + $plantCode = Plant::where('id', $plantId)->value('code'); + + if (empty($plantId)) { + Notification::make() + ->title('Plant name cannot be empty!') + ->danger() + ->send(); + + return; + } elseif (empty($pOrder)) { + Notification::make() + ->title('Production order cannot be empty!') + ->danger() + ->send(); + return; + } + + $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first(); + + if (! $pOrderExists) { + Notification::make() + ->title("Production Order '{$pOrder}' does not exist to get print!") + ->danger() + ->send(); + + return; + } else { + return redirect()->route('production-orders.printpanel', ['production_order' => $pOrder, 'plant_code' => $plantCode]); + } + + } + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/ProductionOrderResource/Pages/ViewProductionOrder.php b/app/Filament/Resources/ProductionOrderResource/Pages/ViewProductionOrder.php index 7366c10..2cf1577 100644 --- a/app/Filament/Resources/ProductionOrderResource/Pages/ViewProductionOrder.php +++ b/app/Filament/Resources/ProductionOrderResource/Pages/ViewProductionOrder.php @@ -3,13 +3,92 @@ namespace App\Filament\Resources\ProductionOrderResource\Pages; use App\Filament\Resources\ProductionOrderResource; +use App\Models\Plant; +use App\Models\ProductionOrder; use Filament\Actions; +use Filament\Notifications\Notification; use Filament\Resources\Pages\ViewRecord; class ViewProductionOrder extends ViewRecord { protected static string $resource = ProductionOrderResource::class; + public function printProductionOrder() + { + $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null; + + $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null; + + if (empty($plantId)) { + Notification::make() + ->title('Plant name cannot be empty!') + ->danger() + ->send(); + + return; + } elseif (empty($pOrder)) { + Notification::make() + ->title('Production order cannot be empty!') + ->danger() + ->send(); + + return; + } + + $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first(); + + if (! $pOrderExists) { + Notification::make() + ->title("Production Order '{$pOrder}' does not exist to get print!") + ->danger() + ->send(); + + return; + } else { + return redirect()->route('production-orders.print', ['production_order' => $pOrder]); + } + } + + public function printPanel() + { + $plantId = $this->form->getState()['plant_id'] ?? null; + $plantId = filled($plantId) ? (int) $plantId : null; + + $pOrder = $this->form->getState()['production_order'] ?? null; + $pOrder = filled($pOrder) ? trim($pOrder) : null; + + $plantCode = Plant::where('id', $plantId)->value('code'); + + if (empty($plantId)) { + Notification::make() + ->title('Plant name cannot be empty!') + ->danger() + ->send(); + + return; + } elseif (empty($pOrder)) { + Notification::make() + ->title('Production order cannot be empty!') + ->danger() + ->send(); + return; + } + + $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first(); + + if (! $pOrderExists) { + Notification::make() + ->title("Production Order '{$pOrder}' does not exist to get print!") + ->danger() + ->send(); + + return; + } else { + return redirect()->route('production-orders.printpanel', ['production_order' => $pOrder, 'plant_code' => $plantCode]); + } + + } + protected function getHeaderActions(): array { return [ diff --git a/app/Http/Controllers/ProductionOrderController.php b/app/Http/Controllers/ProductionOrderController.php index 0fce0bf..ba79e1e 100644 --- a/app/Http/Controllers/ProductionOrderController.php +++ b/app/Http/Controllers/ProductionOrderController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\ProductionOrder; +use Carbon\Carbon; use Illuminate\Http\Request; // use SimpleSoftwareIO\QrCode\Facades\QrCode; use Illuminate\Support\Facades\Log; @@ -42,8 +43,6 @@ class ProductionOrderController extends Controller $qrData = $itemCode . '|' . $serial; - Log::info($qrData); - // $qrBase64 = base64_encode( // QrCode::format('png')->size(100)->generate($qrData) // ); @@ -68,47 +67,53 @@ class ProductionOrderController extends Controller } } - // public function printpanel($production_order){ - // $order = ProductionOrder::where('production_order', $production_order)->first(); + public function printpanel($production_order, $plantCode){ + $order = ProductionOrder::where('production_order', $production_order)->first(); - // if (!$order) { - // abort(404, 'Production Order not found'); - // } - // else{ - // $fromSerial = (int) $order->from_serial_number; - // $toSerial = (int) $order->to_serial_number; - // $itemCode = $order->item->code ?? ''; - // $itemDes = $order->item->description ?? ''; - // $year = - // $month = + if (!$order) { + abort(404, 'Production Order not found'); + } + else{ + $fromSerial = (int) $order->from_serial_number; + $toSerial = (int) $order->to_serial_number; + $itemCode = $order->item->code ?? ''; + $itemDes = $order->item->description ?? ''; + $now = Carbon::now(); + $year = $now->format('y'); + $month = $now->format('m'); - // $stickers = []; + $stickers = []; - // for ($i = $fromSerial; $i <= $toSerial; $i++) - // { + for ($i = $fromSerial; $i <= $toSerial; $i++) + { - // $serial = str_pad($i, 6, '0', STR_PAD_LEFT); + $serial = str_pad($i, 6, '0', STR_PAD_LEFT); + $serialCount = substr(str_pad($i, 6, '0', STR_PAD_LEFT), -5); - // $qrData = $serial . '/' . $itemCode . '/' . $serial . '/' . $itemCode; + $qrData = $plantCode . '/' . $itemCode . '/' . $year.$month . '/' . $serialCount; - // $qrBase64 = base64_encode( - // QrCode::format('png')->size(100)->generate($qrData) - // ); + $panel = $plantCode . '/' . $itemCode . '/' . $year.$month . '/' . $serialCount; - // $stickers[] = [ - // 'serial' => $serial, - // 'qr' => 'data:image/png;base64,' . $qrBase64, - // 'production_order' => $production_order, - // 'description' => $itemDes ?? '' - // ]; - // } - // $pdf = Pdf::loadView('production-orders.printpallet', compact('stickers')) - // ->setPaper([0, 0, 170, 40]); + $qrCode = new QrCode($qrData); + $output = new Output\Png; + $qrBinary = $output->output($qrCode, 100); + $qrBase64 = base64_encode($qrBinary); - // return $pdf->stream('stickers.pdf'); - // } - // } + $stickers[] = [ + 'serial' => $panel, + 'qr' => 'data:image/png;base64,' . $qrBase64, + 'production_order' => $production_order, + 'description' => $itemDes ?? '' + ]; + } + + $pdf = Pdf::loadView('production-orders.printpanel', compact('stickers')) + ->setPaper([0, 0, 113.39, 113.39]); + + return $pdf->stream('stickers.pdf'); + } + } /** * Store a newly created resource in storage. diff --git a/resources/views/production-orders/printpanel.blade.php b/resources/views/production-orders/printpanel.blade.php new file mode 100644 index 0000000..d5ee544 --- /dev/null +++ b/resources/views/production-orders/printpanel.blade.php @@ -0,0 +1,101 @@ + + + + + + + +@foreach($stickers as $sticker) +
+ + + + + + + + + + +
+ +
+
{{ $sticker['serial'] }}
+
+
{{ $sticker['description'] }}
+
+
+@endforeach + + + diff --git a/routes/web.php b/routes/web.php index 97e0ae8..0e152ef 100644 --- a/routes/web.php +++ b/routes/web.php @@ -57,6 +57,10 @@ Route::get('/production-orders/print/{production_order}', [ProductionOrderController::class, 'print'] )->name('production-orders.print'); +Route::get('production-orders/{production_order}/{plant_code}/printpanel', + [ProductionOrderController::class, 'printpanel'] +)->name('production-orders.printpanel'); + // Route::get('/characteristic/approve', [CharacteristicApprovalController::class, 'approve']) // ->name('characteristic.approve') // ->middleware('signed');