Skip to content

Commit 38b09fa

Browse files
committed
Add Method Set Total Label for change label total.
return PdfReport::of($title, $meta, $queryBuilder, $columns) ->setTotalLabel('Total'); Modify showTotal method for change object options and add option function for values "sum" and "avg". return PdfReport::of($title, $meta, $queryBuilder, $columns) ->setTotalLabel('Total') ->showTotal([ 'Percentage' => [ 'format' => 'point', 'function' => 'avg' ] ]);
1 parent fe9dd53 commit 38b09fa

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

src/ReportGenerator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ReportGenerator
2323
protected $withoutManipulation = false;
2424
protected $showMeta = true;
2525
protected $showHeader = true;
26+
protected $totalLabel = 'Grand Total';
2627

2728
public function __construct()
2829
{
@@ -99,6 +100,11 @@ public function setPaper($paper)
99100
return $this;
100101
}
101102

103+
public function setTotalLabel(string $label) {
104+
$this->totalLabel = $label;
105+
return $this;
106+
}
107+
102108
public function editColumn($columnName, Array $options)
103109
{
104110
foreach ($options as $option => $value) {
@@ -124,6 +130,8 @@ public function showTotal(Array $columns)
124130
return $this;
125131
}
126132

133+
134+
127135
public function groupBy($column)
128136
{
129137
if (is_array($column)) {

src/ReportMedia/ExcelReport.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ public function make()
5454
$styles = $this->styles;
5555
$showHeader = $this->showHeader;
5656
$showMeta = $this->showMeta;
57+
$totalLabel = $this->totalLabel;
5758
$applyFlush = $this->applyFlush;
5859
$showNumColumn = $this->showNumColumn;
5960

6061
if ($this->withoutManipulation) {
61-
$view = view('laravel-report-generator::without-manipulation-excel-template', compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'));
62+
$view = view('laravel-report-generator::without-manipulation-excel-template', compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn', 'totalLabel'));
6263
} else {
63-
$view = view('laravel-report-generator::general-excel-template', compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'));
64+
$view = view('laravel-report-generator::general-excel-template', compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn', 'totalLabel'));
6465
}
6566

6667
return new ExportView($view);

src/ReportMedia/PdfReport.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ public function make()
1919
$styles = $this->styles;
2020
$showHeader = $this->showHeader;
2121
$showMeta = $this->showMeta;
22+
$totalLabel = $this->totalLabel;
2223
$showNumColumn = $this->showNumColumn;
2324
$applyFlush = $this->applyFlush;
2425

2526
if ($this->withoutManipulation) {
26-
$html = \View::make('laravel-report-generator::without-manipulation-pdf-template', compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'))->render();
27+
$html = \View::make('laravel-report-generator::without-manipulation-pdf-template', compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn', 'totalLabel'))->render();
2728
} else {
28-
$html = \View::make('laravel-report-generator::general-pdf-template', compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'))->render();
29+
$html = \View::make('laravel-report-generator::general-pdf-template', compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn', 'totalLabel'))->render();
2930
}
3031

3132
try {

src/views/general-excel-template.blade.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@
110110
if ($isOnSameGroup === false) {
111111
echo '<tr class="f-white">';
112112
if ($showNumColumn || $grandTotalSkip > 1) {
113-
echo '<td class="bg-black" colspan="' . $grandTotalSkip . '"><b>Grand Total</b></td>';
113+
echo '<td class="bg-black" colspan="' . $grandTotalSkip . '"><b>{{ $totalLabel }} </b></td>';
114114
}
115115
$dataFound = false;
116116
foreach ($columns as $colName => $colData) {
117117
if (array_key_exists($colName, $showTotalColumns)) {
118-
if ($showTotalColumns[$colName] == 'point') {
118+
119+
if (array_key_exists('function', $showTotalColumns[$colName])){
120+
if ($showTotalColumns[$colName]['function'] == 'avg') {
121+
$total[$colName] = round($total[$colName] / $no, 2);
122+
}
123+
}
124+
125+
if (array_key_exists('format',$showTotalColumns[$colName]) && $showTotalColumns[$colName]['format'] == 'point') {
119126
echo '<td class="right bg-black"><b>' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
120127
} else {
121128
echo '<td class="right bg-black"><b>' . strtoupper($showTotalColumns[$colName]) . ' ' . number_format($total[$colName], 2, '.', ',') . '</b></td>';

src/views/general-pdf-template.blade.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,20 @@
226226
@if ($showTotalColumns != [] && $ctr > 1)
227227
<tr class="bg-black f-white">
228228
@if ($showNumColumn || $grandTotalSkip > 1)
229-
<td colspan="{{ $grandTotalSkip }}"><b>Grand Total</b></td> {{-- For Number --}}
229+
<td colspan="{{ $grandTotalSkip }}"><b>{{ $totalLabel }}</b></td> {{-- For Number --}}
230230
@endif
231231
<?php $dataFound = false; ?>
232232
@foreach ($columns as $colName => $colData)
233233
@if (array_key_exists($colName, $showTotalColumns))
234234
<?php $dataFound = true; ?>
235-
@if ($showTotalColumns[$colName] == 'point')
235+
<?php
236+
if (array_key_exists('function', $showTotalColumns[$colName])){
237+
if ($showTotalColumns[$colName]['function'] == 'avg') {
238+
$total[$colName] = round($total[$colName] / $no, 2);
239+
}
240+
}
241+
?>
242+
@if (array_key_exists('format',$showTotalColumns[$colName]) && $showTotalColumns[$colName] == 'point')
236243
<td class="right"><b>{{ number_format($total[$colName], 2, '.', ',') }}</b></td>
237244
@else
238245
<td class="right"><b>{{ strtoupper($showTotalColumns[$colName]) }} {{ number_format($total[$colName], 2, '.', ',') }}</b></td>

0 commit comments

Comments
 (0)