From ad9b262b9f1da7c4a23977b431b68bca4ab78039 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 8 May 2017 10:18:44 +0700 Subject: [PATCH] Added transaction invoice print to PDF --- .env.example | 4 ++ app/Http/Controllers/TransactionsController.php | 9 +++ app/Transaction.php | 6 ++ config/app.php | 4 +- config/store.php | 7 ++ public/css/pdf.css | 67 +++++++++++++++++++ resources/lang/id/transaction.php | 2 + resources/views/layouts/pdf.blade.php | 18 ++++++ resources/views/transactions/pdf.blade.php | 86 +++++++++++++++++++++++++ resources/views/transactions/show.blade.php | 5 +- 10 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 config/store.php create mode 100755 public/css/pdf.css create mode 100644 resources/views/layouts/pdf.blade.php create mode 100644 resources/views/transactions/pdf.blade.php diff --git a/.env.example b/.env.example index 668c06f..a05039c 100644 --- a/.env.example +++ b/.env.example @@ -31,3 +31,7 @@ MAIL_ENCRYPTION=null PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= + +STORE_NAME= +STORE_ADDRESS= +STORE_PHONE= diff --git a/app/Http/Controllers/TransactionsController.php b/app/Http/Controllers/TransactionsController.php index 29d9628..98ed225 100644 --- a/app/Http/Controllers/TransactionsController.php +++ b/app/Http/Controllers/TransactionsController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Transaction; use Illuminate\Http\Request; +use PDF; class TransactionsController extends Controller { @@ -18,4 +19,12 @@ class TransactionsController extends Controller { return view('transactions.show', compact('transaction')); } + + public function pdf(Transaction $transaction) + { + // return view('transactions.pdf', compact('transaction')); + $pdf = PDF::loadView('transactions.pdf', compact('transaction')); + return $pdf->stream($transaction->invoice_no.'.faktur.pdf'); + + } } diff --git a/app/Transaction.php b/app/Transaction.php index 24c7e31..ba3b812 100644 --- a/app/Transaction.php +++ b/app/Transaction.php @@ -2,6 +2,7 @@ namespace App; +use App\User; use Illuminate\Database\Eloquent\Model; class Transaction extends Model @@ -30,4 +31,9 @@ class Transaction extends Model { return $this->payment - $this->total; } + + public function user() + { + return $this->belongsTo(User::class); + } } diff --git a/config/app.php b/config/app.php index 83a6333..ff7a3ae 100644 --- a/config/app.php +++ b/config/app.php @@ -168,6 +168,7 @@ return [ */ BackupManager\Laravel\Laravel5ServiceProvider::class, Barryvdh\Debugbar\ServiceProvider::class, + Barryvdh\DomPDF\ServiceProvider::class, Laracasts\Flash\FlashServiceProvider::class, Luthfi\FormField\FormFieldServiceProvider::class, @@ -230,8 +231,9 @@ return [ 'View' => Illuminate\Support\Facades\View::class, 'Form' => Collective\Html\FormFacade::class, - 'Html' => Collective\Html\HtmlFacade::class, 'FormField' => Luthfi\FormField\FormFieldFacade::class, + 'PDF' => Barryvdh\DomPDF\Facade::class, + 'Html' => Collective\Html\HtmlFacade::class, ], ]; diff --git a/config/store.php b/config/store.php new file mode 100644 index 0000000..0622266 --- /dev/null +++ b/config/store.php @@ -0,0 +1,7 @@ + env('STORE_NAME','Laravel'), + 'address' => env('STORE_ADDRESS'), + 'phone' => env('STORE_PHONE'), +]; \ No newline at end of file diff --git a/public/css/pdf.css b/public/css/pdf.css new file mode 100755 index 0000000..f7b9027 --- /dev/null +++ b/public/css/pdf.css @@ -0,0 +1,67 @@ +html { + margin: 25px; +} + +body { + font-family: 'Arial', sans-serif; + font-size:12px; +} + +table { + border-spacing: 1px; +} + +td, th { + padding: 0px; +} + +td { + vertical-align: top; +} + +table.main-table { + width: 100%; + border-collapse: collapse; +} + +table.main-table td, table.main-table th { + padding: 5px 3px; +} + +h1, h2, h3, h4 { + margin: 0; +} + +h2 { font-size: 20px; margin-bottom: 16px } + +.text-center { + text-align: center; + vertical-align:middle; +} + +.text-right { + text-align: right; +} + +.text-left { + text-align: left; +} + +p { + margin: 0 0 3px 0; +} + +div { + margin: 0; + padding: 0; +} + +.strong { font-weight: bold; } + +.border-bottom { + border-bottom: 1px solid #333; +} + +.page-break { + page-break-after: always; +} diff --git a/resources/lang/id/transaction.php b/resources/lang/id/transaction.php index 24dfc59..64b53d9 100644 --- a/resources/lang/id/transaction.php +++ b/resources/lang/id/transaction.php @@ -21,6 +21,7 @@ return [ 'draft_destroyed' => 'Seluruh Draft transaksi telah dihapus.', 'draft_not_found' => 'Draft transaksi tidak ditemukan.', 'items_count' => 'Jumlah Item', + 'cashier' => 'Kasir', // Actions 'proccess' => 'Proses Transaksi', @@ -28,6 +29,7 @@ return [ 'item_list_empty' => 'Masukkan setidaknya 1 item produk.', 'save' => 'Simpan Transaksi', 'created' => 'Transaksi berhasil disimpan, No. Invoice: :invoice_no', + 'invoice_print' => 'Cetak Invoice', // Attributes 'invoice_no' => 'No. Invoice', diff --git a/resources/views/layouts/pdf.blade.php b/resources/views/layouts/pdf.blade.php new file mode 100644 index 0000000..e486b45 --- /dev/null +++ b/resources/views/layouts/pdf.blade.php @@ -0,0 +1,18 @@ + + + + + + + + @yield('title') | {{ config('app.name', 'Laravel') }} + + {{ Html::style(url('css/pdf.css')) }} + + +
+ @yield('content') +
+ @yield('script') + + \ No newline at end of file diff --git a/resources/views/transactions/pdf.blade.php b/resources/views/transactions/pdf.blade.php new file mode 100644 index 0000000..4dc109c --- /dev/null +++ b/resources/views/transactions/pdf.blade.php @@ -0,0 +1,86 @@ +@extends('layouts.pdf') + +@section('title', $transaction->invoice_no . ' - ' . trans('transaction.invoice_print')) + +@section('content') + + + + + + + + + @foreach(collect($transaction->items)->chunk(10) as $chunckedItems) + + @endforeach + {{-- --}} + {{-- --}} + + +
+

{{ config('store.name') }}

+

{{ config('store.address') }} | Telp: {{ config('store.phone') }}

+
+ + + + + + + + + + + + +
{{ trans('transaction.invoice_no') }}:{{ $transaction->invoice_no }}{{ $transaction->created_at->format('d/m/Y') }}
{{ trans('transaction.cashier') }}:{{ $transaction->user->name }}{{ $transaction->created_at->format('H:i:s') }}
{{ trans('transaction.customer') }}:{{ $transaction->customer['name'] }}
{{ trans('transaction.customer_phone') }}:{{ $transaction->customer['phone'] }}
+
+
+ + + + + + + + + @foreach($chunckedItems as $key => $item) + + + + + + + + + + @endforeach + @if ($loop->last) + + + + + + + + + + + + + + + + + + + + + @endif + +
@if ($loop->first) {{ trans('product.product') }} @else   @endif
@if ($loop->first) {{ trans('product.item_qty') }} @else   @endif@if ($loop->first) {{ trans('product.price') }} ({{ trans('product.item_discount') }}) @else  
  @endif
@if ($loop->first) {{ trans('product.item_subtotal') }} @else   @endif
{{ $key + 1 }}) {{ $item['name'] }} ({{ $item['unit'] }})
{{ $item['qty'] }} + {{ formatRp($item['price']) }}
({{ formatRp($item['item_discount']) }}) +
{{ formatRp($item['subtotal']) }}
{{ trans('transaction.subtotal') }} :{{ formatRp($transaction['total'] + $discountTotal) }}
{{ trans('transaction.discount_total') }} :{{ formatRp($discountTotal) }}
{{ trans('transaction.total') }} :{{ formatRp($transaction['total']) }}
{{ trans('transaction.payment') }} :{{ formatRp($transaction->payment) }}
{{ trans('transaction.exchange') }} :{{ formatRp($transaction->getExchange()) }}
+
  
+@endsection \ No newline at end of file diff --git a/resources/views/transactions/show.blade.php b/resources/views/transactions/show.blade.php index 1d94ec1..804a67e 100644 --- a/resources/views/transactions/show.blade.php +++ b/resources/views/transactions/show.blade.php @@ -1,8 +1,9 @@ @extends('layouts.app') -@section('title', trans('transaction.detail')) +@section('title', $transaction->invoice_no . ' - ' . trans('transaction.detail')) @section('content') +
{{ link_to_route('transactions.pdf', trans('transaction.invoice_print'), [$transaction->invoice_no], ['class' => 'btn btn-info']) }}
@@ -53,7 +54,7 @@ {{ $item['qty'] }} {{ formatRp($item['subtotal']) }} - + @endforeach