Browse Source

Added transaction invoice print to PDF

pull/5/head
Nafies Luthfi 9 years ago
parent
commit
ad9b262b9f
  1. 4
      .env.example
  2. 9
      app/Http/Controllers/TransactionsController.php
  3. 6
      app/Transaction.php
  4. 4
      config/app.php
  5. 7
      config/store.php
  6. 67
      public/css/pdf.css
  7. 2
      resources/lang/id/transaction.php
  8. 18
      resources/views/layouts/pdf.blade.php
  9. 86
      resources/views/transactions/pdf.blade.php
  10. 5
      resources/views/transactions/show.blade.php

4
.env.example

@ -31,3 +31,7 @@ MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
STORE_NAME=
STORE_ADDRESS=
STORE_PHONE=

9
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');
}
}

6
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);
}
}

4
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,
],
];

7
config/store.php

@ -0,0 +1,7 @@
<?php
return [
'name' => env('STORE_NAME','Laravel'),
'address' => env('STORE_ADDRESS'),
'phone' => env('STORE_PHONE'),
];

67
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;
}

2
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: <strong>:invoice_no</strong>',
'invoice_print' => 'Cetak Invoice',
// Attributes
'invoice_no' => 'No. Invoice',

18
resources/views/layouts/pdf.blade.php

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@yield('title') | {{ config('app.name', 'Laravel') }}</title>
{{ Html::style(url('css/pdf.css')) }}
</head>
<body>
<div class="container">
@yield('content')
</div>
@yield('script')
</body>
</html>

86
resources/views/transactions/pdf.blade.php

@ -0,0 +1,86 @@
@extends('layouts.pdf')
@section('title', $transaction->invoice_no . ' - ' . trans('transaction.invoice_print'))
@section('content')
<table style="width: 760px; border-collapse: collapse; page-break-inside: avoid; /*border:1px solid #aaa;*/">
<tbody>
<tr>
<td colspan="3">
<h2 class="text-center">{{ config('store.name') }}</h2>
<p class="text-center">{{ config('store.address') }} | Telp: {{ config('store.phone') }}</p>
<br>
<table style="width:300px;margin: auto;">
<tbody>
<tr>
<td style="width:90px">{{ trans('transaction.invoice_no') }}</td>
<td>:</td>
<td class="strong">{{ $transaction->invoice_no }}</td>
<td class="text-right">{{ $transaction->created_at->format('d/m/Y') }}</td>
</tr>
<tr><td>{{ trans('transaction.cashier') }}</td><td>:</td><td>{{ $transaction->user->name }}</td><td class="text-right">{{ $transaction->created_at->format('H:i:s') }}</td></tr>
<tr><td>{{ trans('transaction.customer') }}</td><td>:</td><td colspan="2">{{ $transaction->customer['name'] }}</td></tr>
<tr><td>{{ trans('transaction.customer_phone') }}</td><td>:</td><td colspan="2">{{ $transaction->customer['phone'] }}</td></tr>
</tbody>
</table>
<br>
</td>
</tr>
<tr>
<?php $discountTotal = 0; ?>
@foreach(collect($transaction->items)->chunk(10) as $chunckedItems)
<td style="width:250px;padding-right: 10px">
<table class="main-table">
<tbody>
<tr><th colspan="3">@if ($loop->first) {{ trans('product.product') }} @else &nbsp; @endif</th></tr>
<tr>
<th class="">@if ($loop->first) {{ trans('product.item_qty') }} @else &nbsp; @endif</th>
<th class="text-right">@if ($loop->first) {{ trans('product.price') }} ({{ trans('product.item_discount') }}) @else &nbsp;<br>&nbsp; @endif</th>
<th class="text-right" style="width:90px">@if ($loop->first) {{ trans('product.item_subtotal') }} @else &nbsp; @endif</th>
</tr>
@foreach($chunckedItems as $key => $item)
<tr>
<td colspan="3">{{ $key + 1 }})&nbsp;{{ $item['name'] }} ({{ $item['unit'] }})</td>
</tr>
<tr>
<td class="text-center border-bottom">{{ $item['qty'] }}</td>
<td class="text-right border-bottom">
{{ formatRp($item['price']) }}<br>({{ formatRp($item['item_discount']) }})
</td>
<td class="text-right border-bottom">{{ formatRp($item['subtotal']) }}</td>
</tr>
<?php $discountTotal += $item['item_discount_subtotal'] ?>
@endforeach
@if ($loop->last)
<tr>
<th colspan="2" class="text-right">{{ trans('transaction.subtotal') }} :</th>
<th class="text-right">{{ formatRp($transaction['total'] + $discountTotal) }}</th>
</tr>
<tr>
<th colspan="2" class="text-right">{{ trans('transaction.discount_total') }} :</th>
<th class="text-right">{{ formatRp($discountTotal) }}</th>
</tr>
<tr>
<th colspan="2" class="text-right">{{ trans('transaction.total') }} :</th>
<th class="text-right">{{ formatRp($transaction['total']) }}</th>
</tr>
<tr>
<th colspan="2" class="text-right">{{ trans('transaction.payment') }} :</th>
<th class="text-right">{{ formatRp($transaction->payment) }}</th>
</tr>
<tr>
<th colspan="2" class="text-right">{{ trans('transaction.exchange') }} :</th>
<th class="text-right">{{ formatRp($transaction->getExchange()) }}</th>
</tr>
@endif
</tbody>
</table>
</td>
@endforeach
{{-- <td style="width:250px;">&nbsp;</td> --}}
{{-- <td style="width:250px;">&nbsp;</td> --}}
</tr>
</tbody>
</table>
@endsection

5
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')
<div class="pull-right">{{ link_to_route('transactions.pdf', trans('transaction.invoice_print'), [$transaction->invoice_no], ['class' => 'btn btn-info']) }}</div>
<h3 class="page-header">{{ $transaction->invoice_no }} <small>{{ trans('transaction.detail') }}</small></h3>
<div class="row">
<div class="col-sm-4">
@ -53,7 +54,7 @@
<td class="text-center">{{ $item['qty'] }}</td>
<td class="text-right">{{ formatRp($item['subtotal']) }}</td>
</tr>
<?php $discountTotal = $transaction['item_discount_subtotal'] ?>
<?php $discountTotal += $item['item_discount_subtotal'] ?>
@endforeach
</tbody>
<tfoot>

Loading…
Cancel
Save