From 44697030f850f80f986a3257d20d92dc3fdad8e5 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Mon, 17 Nov 2025 21:49:58 +0700 Subject: [PATCH] feat(order): membuat tombol print di datatable - membuat fitur print - after create bisa pangsung print --- .../Datatable/Studio/Manage/OrdersTable.php | 6 + app/Livewire/Studio/Manage/Order/Create.php | 4 +- app/Livewire/Studio/Manage/Order/Index.php | 41 ++++ .../components/datatables/print.blade.php | 5 + .../studio/manage/order/index.blade.php | 217 +++++++++++++++++- 5 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 resources/views/components/datatables/print.blade.php diff --git a/app/Livewire/Datatable/Studio/Manage/OrdersTable.php b/app/Livewire/Datatable/Studio/Manage/OrdersTable.php index 45ae93e..9c1cb49 100644 --- a/app/Livewire/Datatable/Studio/Manage/OrdersTable.php +++ b/app/Livewire/Datatable/Studio/Manage/OrdersTable.php @@ -114,6 +114,12 @@ public function columns(): array ->label(function ($row) { $actions = ''; + if (auth()->user()->can('create order') && auth()->user()->can('show order')) { + $actions .= view('components.datatables.print', [ + 'id' => $row->hash, + ])->render(); + } + if (auth()->user()->can('show order')) { $actions .= view('components.datatables.show', [ 'id' => $row->hash, diff --git a/app/Livewire/Studio/Manage/Order/Create.php b/app/Livewire/Studio/Manage/Order/Create.php index 7a252cd..f213fc8 100644 --- a/app/Livewire/Studio/Manage/Order/Create.php +++ b/app/Livewire/Studio/Manage/Order/Create.php @@ -105,7 +105,9 @@ public function save() $this->toast('Order berhasil ditambahkan.'); - $this->redirectRoute('studio.manage.order.index'); + $this->redirectRoute('studio.manage.order.index', [ + 'order' => $result['data']->id, + ]); } public function updatedFormOutletId($value) diff --git a/app/Livewire/Studio/Manage/Order/Index.php b/app/Livewire/Studio/Manage/Order/Index.php index 4cb225c..77f5136 100644 --- a/app/Livewire/Studio/Manage/Order/Index.php +++ b/app/Livewire/Studio/Manage/Order/Index.php @@ -9,6 +9,7 @@ use App\Traits\WithConfirmation; use App\Traits\WithToast; use Flux\Flux; +use Livewire\Attributes\On; use Livewire\Attributes\Title; use Livewire\Component; @@ -17,6 +18,25 @@ class Index extends Component { use WithCloseModal, WithConfirmation, WithSubscribeNotification, WithToast, WithUpdateStock; + public function mount() + { + if (request()->has('order')) { + $order = Order::find(request('order')); + + if (! $order) { + return; + } + + $this->dispatch('fn:print', order: $order->hash); + + $this->js(" + const url = new URL(window.location); + url.search = ''; + window.history.replaceState({}, '', url); + "); + } + } + public function delete(Order $order) { foreach ($order->items as $item) { @@ -32,6 +52,27 @@ public function delete(Order $order) Flux::modals()->close(); } + #[On('fn:print')] + public function print(Order $order) + { + $order->load(['items', 'user', 'user.employee']); + + $this->dispatch( + 'printReceipt', + cashier: $order->user?->employee?->full_name ?? '', + customer: $order->customer?->name ?? '', + subtotal: $order->subtotal, + discount: $order->discount, + total: $order->total, + items: $order->items->map(function ($item) { + $item->name = $item->orderable?->name; + $item->total = $item->unit_price * $item->quantity; + + return $item; + })->toArray() + ); + } + public function render() { return view('livewire.studio.manage.order.index', [ diff --git a/resources/views/components/datatables/print.blade.php b/resources/views/components/datatables/print.blade.php new file mode 100644 index 0000000..9332a67 --- /dev/null +++ b/resources/views/components/datatables/print.blade.php @@ -0,0 +1,5 @@ + + + + diff --git a/resources/views/livewire/studio/manage/order/index.blade.php b/resources/views/livewire/studio/manage/order/index.blade.php index 3079d31..399251e 100644 --- a/resources/views/livewire/studio/manage/order/index.blade.php +++ b/resources/views/livewire/studio/manage/order/index.blade.php @@ -4,11 +4,14 @@ {{ $pageTitle }} @can('create order') -
+
Tambah + + Connect USB +
@endcan
@@ -19,3 +22,215 @@ class="text-sm"> @include('components.confirmation.delete') + +@script + +@endscript