Skip to content

Commit

Permalink
Merge pull request #50 from ConsoleTVs/header_pagination
Browse files Browse the repository at this point in the history
Header and pagination
  • Loading branch information
codevio committed Dec 3, 2019
2 parents 59b9934 + 655786b commit 4a2be0d
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 93 deletions.
16 changes: 16 additions & 0 deletions Classes/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ class Invoice
*/
public $due_date = null;

/**
* Invoice pagination.
*
* @var boolean
*/
public $with_pagination;

/**
* Invoice header duplication.
*
* @var boolean
*/
public $duplicate_header;

/**
* Stores the PDF object.
*
Expand Down Expand Up @@ -158,6 +172,8 @@ public function __construct($name = 'Invoice')
$this->footnote = config('invoices.footnote');
$this->tax_rates = config('invoices.tax_rates');
$this->due_date = config('invoices.due_date') != null ? Carbon::parse(config('invoices.due_date')) : null;
$this->with_pagination = config('invoices.with_pagination');
$this->duplicate_header = config('invoices.duplicate_header');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Classes/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function generate(Invoice $invoice, $template = 'default')
$options = new Options();

$options->set('isRemoteEnabled', true);
$options->set('isPhpEnabled', true);

$pdf = new Dompdf($options);

Expand All @@ -51,6 +52,8 @@ public static function generate(Invoice $invoice, $template = 'default')

$pdf->setHttpContext($context);

$GLOBALS['with_pagination'] = $invoice->with_pagination;

$pdf->loadHtml(View::make('invoices::'.$template, ['invoice' => $invoice]));
$pdf->render();

Expand Down
18 changes: 18 additions & 0 deletions Config/invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,22 @@
*/
'due_date' => date('M dS ,Y',strtotime('+3 months')),

/*
| Default pagination parameter
|--------------------------------------------------------------------------
|
| This value is the default pagination parameter.
| If true and page count are higher than 1, pagination will show at the bottom.
*/
'with_pagination' => true,

/*
| Duplicate header parameter
|--------------------------------------------------------------------------
|
| This value is the default header parameter.
| If true header will be duplicated on each page.
*/
'duplicate_header' => false,

];
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This is a sample invoice generated using this library:
![Sample Invoice](https://i.gyazo.com/768f5b59791162e432f9cdfa15f017bc.png)

```php
$invoice = ConsoleTVs\Invoices\Classes\Invoice::make()
$invoice = \ConsoleTVs\Invoices\Classes\Invoice::make()
->addItem('Test Item', 10.25, 2, 1412)
->addItem('Test Item 2', 5, 2, 923)
->addItem('Test Item 3', 15.55, 5, 42)
Expand All @@ -29,6 +29,8 @@ $invoice = ConsoleTVs\Invoices\Classes\Invoice::make()
->addItem('Test Item 6', 6.41, 3, 452)
->addItem('Test Item 7', 2.86, 1, 1526)
->number(4021)
->with_pagination(true)
->duplicate_header(true)
->due_date(Carbon::now()->addMonths(1))
->notes('Lrem ipsum dolor sit amet, consectetur adipiscing elit.')
->customer([
Expand Down
199 changes: 109 additions & 90 deletions Templates/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,18 @@
box-shadow: inset 0 1px 1px rgba(0,0,0,.05);
}
</style>
@if($invoice->duplicate_header)
<style>
@page { margin-top: 140px;}
header {
top: -100px;
position: fixed;
}
</style>
@endif
</head>
<body>
<div style="clear:both; position:relative;">
<header>
<div style="position:absolute; left:0pt; width:250pt;">
<img class="img-rounded" height="{{ $invoice->logo_height }}" src="{{ $invoice->logo }}">
</div>
Expand All @@ -86,106 +95,116 @@
@endif
<br />
</div>
</div>
<br />
<h2>{{ $invoice->name }} {{ $invoice->number ? '#' . $invoice->number : '' }}</h2>
<div style="clear:both; position:relative;">
<div style="position:absolute; left:0pt; width:250pt;">
<h4>Business Details:</h4>
<div class="panel panel-default">
<div class="panel-body">
{!! $invoice->business_details->count() == 0 ? '<i>No business details</i><br />' : '' !!}
{{ $invoice->business_details->get('name') }}<br />
ID: {{ $invoice->business_details->get('id') }}<br />
{{ $invoice->business_details->get('phone') }}<br />
{{ $invoice->business_details->get('location') }}<br />
{{ $invoice->business_details->get('zip') }} {{ $invoice->business_details->get('city') }}
{{ $invoice->business_details->get('country') }}<br />
</div>
</div>
</div>
<div style="margin-left: 300pt;">
<h4>Customer Details:</h4>
<div class="panel panel-default">
<div class="panel-body">
{!! $invoice->customer_details->count() == 0 ? '<i>No customer details</i><br />' : '' !!}
{{ $invoice->customer_details->get('name') }}<br />
ID: {{ $invoice->customer_details->get('id') }}<br />
{{ $invoice->customer_details->get('phone') }}<br />
{{ $invoice->customer_details->get('location') }}<br />
{{ $invoice->customer_details->get('zip') }} {{ $invoice->customer_details->get('city') }}
{{ $invoice->customer_details->get('country') }}<br />
<br />
<h2>{{ $invoice->name }} {{ $invoice->number ? '#' . $invoice->number : '' }}</h2>
</header>
<main>
<div style="clear:both; position:relative;">
<div style="position:absolute; left:0pt; width:250pt;">
<h4>Business Details:</h4>
<div class="panel panel-default">
<div class="panel-body">
{!! $invoice->business_details->count() == 0 ? '<i>No business details</i><br />' : '' !!}
{{ $invoice->business_details->get('name') }}<br />
ID: {{ $invoice->business_details->get('id') }}<br />
{{ $invoice->business_details->get('phone') }}<br />
{{ $invoice->business_details->get('location') }}<br />
{{ $invoice->business_details->get('zip') }} {{ $invoice->business_details->get('city') }}
{{ $invoice->business_details->get('country') }}<br />
</div>
</div>
</div>
</div>
</div>
<h4>Items:</h4>
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Item Name</th>
<th>Price</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
@foreach ($invoice->items as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->get('id') }}</td>
<td>{{ $item->get('name') }}</td>
<td>{{ $item->get('price') }} {{ $invoice->formatCurrency()->symbol }}</td>
<td>{{ $item->get('ammount') }}</td>
<td>{{ $item->get('totalPrice') }} {{ $invoice->formatCurrency()->symbol }}</td>
</tr>
@endforeach
</tbody>
</table>
<div style="clear:both; position:relative;">
@if($invoice->notes)
<div style="position:absolute; left:0pt; width:250pt;">
<h4>Notes:</h4>
<div style="margin-left: 300pt;">
<h4>Customer Details:</h4>
<div class="panel panel-default">
<div class="panel-body">
{{ $invoice->notes }}
{!! $invoice->customer_details->count() == 0 ? '<i>No customer details</i><br />' : '' !!}
{{ $invoice->customer_details->get('name') }}<br />
ID: {{ $invoice->customer_details->get('id') }}<br />
{{ $invoice->customer_details->get('phone') }}<br />
{{ $invoice->customer_details->get('location') }}<br />
{{ $invoice->customer_details->get('zip') }} {{ $invoice->customer_details->get('city') }}
{{ $invoice->customer_details->get('country') }}<br />
</div>
</div>
</div>
@endif
<div style="margin-left: 300pt;">
<h4>Total:</h4>
<table class="table table-bordered">
<tbody>
</div>
<h4>Items:</h4>
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Item Name</th>
<th>Price</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
@foreach ($invoice->items as $item)
<tr>
<td><b>Subtotal</b></td>
<td>{{ $invoice->subTotalPriceFormatted() }} {{ $invoice->formatCurrency()->symbol }}</td>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->get('id') }}</td>
<td>{{ $item->get('name') }}</td>
<td>{{ $item->get('price') }} {{ $invoice->formatCurrency()->symbol }}</td>
<td>{{ $item->get('ammount') }}</td>
<td>{{ $item->get('totalPrice') }} {{ $invoice->formatCurrency()->symbol }}</td>
</tr>
@foreach($invoice->tax_rates as $tax_rate)
@endforeach
</tbody>
</table>
<div style="clear:both; position:relative;">
@if($invoice->notes)
<div style="position:absolute; left:0pt; width:250pt;">
<h4>Notes:</h4>
<div class="panel panel-default">
<div class="panel-body">
{{ $invoice->notes }}
</div>
</div>
</div>
@endif
<div style="margin-left: 300pt;">
<h4>Total:</h4>
<table class="table table-bordered">
<tbody>
<tr>
<td>
<b>
{{ $tax_rate['name'].' '.($tax_rate['tax_type'] == 'percentage' ? '(' . $tax_rate['tax'] . '%)' : '') }}
</b>
</td>
<td>{{ $invoice->taxPriceFormatted((object)$tax_rate) }} {{ $invoice->formatCurrency()->symbol }}</td>
<td><b>Subtotal</b></td>
<td>{{ $invoice->subTotalPriceFormatted() }} {{ $invoice->formatCurrency()->symbol }}</td>
</tr>
@endforeach
<tr>
<td><b>TOTAL</b></td>
<td><b>{{ $invoice->totalPriceFormatted() }} {{ $invoice->formatCurrency()->symbol }}</b></td>
</tr>
</tbody>
</table>
</div>
</div>
@if ($invoice->footnote)
<br /><br />
<div class="well">
{{ $invoice->footnote }}
@foreach($invoice->tax_rates as $tax_rate)
<tr>
<td>
<b>
{{ $tax_rate['name'].' '.($tax_rate['tax_type'] == 'percentage' ? '(' . $tax_rate['tax'] . '%)' : '') }}
</b>
</td>
<td>{{ $invoice->taxPriceFormatted((object)$tax_rate) }} {{ $invoice->formatCurrency()->symbol }}</td>
</tr>
@endforeach
<tr>
<td><b>TOTAL</b></td>
<td><b>{{ $invoice->totalPriceFormatted() }} {{ $invoice->formatCurrency()->symbol }}</b></td>
</tr>
</tbody>
</table>
</div>
</div>
@endif
@if ($invoice->footnote)
<br /><br />
<div class="well">
{{ $invoice->footnote }}
</div>
@endif
</main>

<!-- Page count -->
<script type="text/php">
if (isset($pdf) && $GLOBALS['with_pagination'] && $PAGE_COUNT > 1) {
$pageText = "{PAGE_NUM} of {PAGE_COUNT}";
$pdf->page_text(($pdf->get_width()/2) - (strlen($pageText) / 2), $pdf->get_height()-20, $pageText, $fontMetrics->get_font("DejaVu Sans, Arial, Helvetica, sans-serif", "normal"), 7, array(0,0,0));
}
</script>
</body>
</html>
31 changes: 31 additions & 0 deletions Traits/Setters.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,36 @@ public function due_date(Carbon $due_date = null)
return $this;
}

/**
* Show/hide the invoice pagination.
*
* @method with_pagination
*
* @param boolean $with_pagination
*
* @return self
*/
public function with_pagination($with_pagination)
{
$this->with_pagination = $with_pagination;
return $this;
}

/**
* Duplicate the header on each page.
*
* @method duplicate_header
*
* @param boolean $duplicate_header
*
* @return self
*/
public function duplicate_header($duplicate_header)
{
$this->duplicate_header = $duplicate_header;
return $this;
}



}

0 comments on commit 4a2be0d

Please sign in to comment.