Skip to content

Add Method Set Total Label for change label total and Modify showTotal method for custom operation. #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=7.1.0",
"illuminate/support": ">=5.0",
"illuminate/support": "^9.0.0",
"maatwebsite/excel": "^3.1"
},
"autoload": {
Expand Down
21 changes: 19 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This package provides a simple pdf, csv & excel report generators to speed up yo
| 1.0 | <= 5.6 | <=7.0 | ~2.1.0 | using `chunk()` to handle big data
| 1.1 | <= 5.6 | <=7.0 | ~2.1.0 | using `cursor()` to handle big data
| 2.0 | \>= 5.5 | ^7.0 | ^3.1 | Using new version of maatwebsite (v3.1)
| 3.0 | \>= 9.0 | ^7.0 | ^3.1 | Add support Laravel x9

Find the comparison between `chunk` and `cursor` in [here](https://qiita.com/ryo511/items/ebcd1c1b2ad5addc5c9d)

Expand All @@ -17,7 +18,7 @@ Add package to your composer:

composer require jimmyjs/laravel-report-generator

If you are running Laravel > 5.5 that's all you need to do. If you are using Laravel < 5.5 add the ServiceProvider to the providers array in config/app.php
If you are running Laravel > 9.0 that's all you need to do. If you are using Laravel < 5.5 add the ServiceProvider to the providers array in config/app.php

Jimmyjs\ReportGenerator\ServiceProvider::class,

Expand Down Expand Up @@ -161,7 +162,9 @@ Or, you can total all records by group using `groupBy` method
])
->groupBy('Registered At') // Show total of value on specific group. Used with showTotal() enabled.
->showTotal([
'Total Balance' => 'point'
'Total Balance' => [
'function' => 'sum', // Allow Values sum and avg or function($total)
'format' => 'point'
])
->stream();
```
Expand Down Expand Up @@ -312,3 +315,17 @@ ExcelReport::of($title, $meta, $queryBuilder, $columns)
->simple()
->download('filename');
```
### 9. setTotalLabel($label)
**Supported Media Type**: PDF, Excel

**Description**: Set Label for Total

**Params**:
* None

**Usage:**
```php
ExcelReport::of($title, $meta, $queryBuilder, $columns)
->setTotalLabel('Average')
>make();
```
8 changes: 8 additions & 0 deletions src/ReportGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ReportGenerator
protected $withoutManipulation = false;
protected $showMeta = true;
protected $showHeader = true;
protected $totalLabel = 'Grand Total';

public function __construct()
{
Expand Down Expand Up @@ -99,6 +100,11 @@ public function setPaper($paper)
return $this;
}

public function setTotalLabel(string $label) {
$this->totalLabel = $label;
return $this;
}

public function editColumn($columnName, Array $options)
{
foreach ($options as $option => $value) {
Expand All @@ -124,6 +130,8 @@ public function showTotal(Array $columns)
return $this;
}



public function groupBy($column)
{
if (is_array($column)) {
Expand Down
5 changes: 3 additions & 2 deletions src/ReportMedia/ExcelReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ public function make()
$styles = $this->styles;
$showHeader = $this->showHeader;
$showMeta = $this->showMeta;
$totalLabel = $this->totalLabel;
$applyFlush = $this->applyFlush;
$showNumColumn = $this->showNumColumn;

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

return new ExportView($view);
Expand Down
5 changes: 3 additions & 2 deletions src/ReportMedia/PdfReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public function make()
$styles = $this->styles;
$showHeader = $this->showHeader;
$showMeta = $this->showMeta;
$totalLabel = $this->totalLabel;
$showNumColumn = $this->showNumColumn;
$applyFlush = $this->applyFlush;

if ($this->withoutManipulation) {
$html = \View::make('laravel-report-generator::without-manipulation-pdf-template', compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'))->render();
$html = \View::make('laravel-report-generator::without-manipulation-pdf-template', compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn', 'totalLabel'))->render();
} else {
$html = \View::make('laravel-report-generator::general-pdf-template', compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'))->render();
$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();
}

try {
Expand Down
60 changes: 44 additions & 16 deletions src/views/general-excel-template.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,29 @@
if ($isOnSameGroup === false) {
echo '<tr class="f-white">';
if ($showNumColumn || $grandTotalSkip > 1) {
echo '<td class="bg-black" colspan="' . $grandTotalSkip . '"><b>Grand Total</b></td>';
echo '<td class="bg-black" colspan="' . $grandTotalSkip . '"><b>'.$totalLabel.'</b></td>';
}
$dataFound = false;
foreach ($columns as $colName => $colData) {
if (array_key_exists($colName, $showTotalColumns)) {
if ($showTotalColumns[$colName] == 'point') {
echo '<td class="right bg-black"><b>' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
} else {
echo '<td class="right bg-black"><b>' . strtoupper($showTotalColumns[$colName]) . ' ' . number_format($total[$colName], 2, '.', ',') . '</b></td>';

if (array_key_exists('function', $showTotalColumns[$colName])){
$function = $showTotalColumns[$colName]['function'];
if (is_object($function) && $function instanceof Closure){
$total[$colName] = $function($total);
} else {
if ($showTotalColumns[$colName]['function'] == 'avg') {
$total[$colName] = round($total[$colName] / $no, 2);
}
}
}

if (array_key_exists('format',$showTotalColumns[$colName])){
if ($showTotalColumns[$colName]['format'] == 'point') {
echo '<td class="right bg-black"><b>' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
} else {
echo '<td class="right bg-black"><b>' . strtoupper($showTotalColumns[$colName]['format']) . ' ' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
}
}
$dataFound = true;
} else {
Expand Down Expand Up @@ -179,22 +193,36 @@
@if ($showTotalColumns != [] && $ctr > 1)
<tr class="f-white">
@if ($showNumColumn || $grandTotalSkip > 1)
<td colspan="{{ $grandTotalSkip }}" class="bg-black"><b>Grand Total</b></td> {{-- For Number --}}
<td colspan="{{ $grandTotalSkip }}" class="bg-black"><b>{{ $totalLabel }}</b></td> {{-- For Number --}}
@endif
<?php $dataFound = false; ?>
@foreach ($columns as $colName => $colData)
@if (array_key_exists($colName, $showTotalColumns))
<?php $dataFound = true; ?>
@if ($showTotalColumns[$colName] == 'point')
<td class="bg-black right"><b>{{ number_format($total[$colName], 2, '.', ',') }}</b></td>
@if (array_key_exists($colName, $showTotalColumns))
<?php $dataFound = true; ?>
<?php
if (array_key_exists('function', $showTotalColumns[$colName])){
$function = $showTotalColumns[$colName]['function'];
if (is_object($function) && $function instanceof Closure){
$total[$colName] = $function($total);
} else {
if ($showTotalColumns[$colName]['function'] == 'avg') {
$total[$colName] = round($total[$colName] / $no, 2);
}
}
}
?>
@if (array_key_exists('format',$showTotalColumns[$colName]))
@if ($showTotalColumns[$colName]['format'] == 'point')
<td class="right"><b>{{ number_format($total[$colName], 2, '.', ',') }}</b></td>
@else
<td class="right"><b>{{ strtoupper($showTotalColumns[$colName]['format']) }} {{ number_format($total[$colName], 2, '.', ',') }}</b></td>
@endif
@endif
@else
<td class="bg-black right"><b>{{ strtoupper($showTotalColumns[$colName]) }} {{ number_format($total[$colName], 2, '.', ',') }}</b></td>
@if ($dataFound)
<td></td>
@endif
@endif
@else
@if ($dataFound)
<td class="bg-black"></td>
@endif
@endif
@endforeach
</tr>
@endif
Expand Down
48 changes: 37 additions & 11 deletions src/views/general-pdf-template.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,28 @@
if ($isOnSameGroup === false) {
echo '<tr class="bg-black f-white">';
if ($showNumColumn || $grandTotalSkip > 1) {
echo '<td colspan="' . $grandTotalSkip . '"><b>Grand Total</b></td>';
echo '<td colspan="' . $grandTotalSkip . '"><b>'.$totalLabel.'</b></td>';
}
$dataFound = false;
foreach ($columns as $colName => $colData) {
if (array_key_exists($colName, $showTotalColumns)) {
if ($showTotalColumns[$colName] == 'point') {
echo '<td class="right"><b>' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
} else {
echo '<td class="right"><b>' . strtoupper($showTotalColumns[$colName]) . ' ' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
}
if (array_key_exists('function', $showTotalColumns[$colName])){
$function = $showTotalColumns[$colName]['function'];
if (is_object($function) && $function instanceof Closure){
$total[$colName] = $function($total);
} else {
if ($showTotalColumns[$colName]['function'] == 'avg') {
$total[$colName] = round($total[$colName] / $no, 2);
}
}
}
if (array_key_exists('format',$showTotalColumns[$colName])){
if ($showTotalColumns[$colName]['format'] == 'point') {
echo '<td class="right"><b>' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
} else {
echo '<td class="right"><b>' . strtoupper($showTotalColumns[$colName]['format']) . ' ' . number_format($total[$colName], 2, '.', ',') . '</b></td>';
}
}
$dataFound = true;
} else {
if ($dataFound) {
Expand Down Expand Up @@ -226,16 +238,30 @@
@if ($showTotalColumns != [] && $ctr > 1)
<tr class="bg-black f-white">
@if ($showNumColumn || $grandTotalSkip > 1)
<td colspan="{{ $grandTotalSkip }}"><b>Grand Total</b></td> {{-- For Number --}}
<td colspan="{{ $grandTotalSkip }}"><b>{{ $totalLabel }}</b></td> {{-- For Number --}}
@endif
<?php $dataFound = false; ?>
@foreach ($columns as $colName => $colData)
@if (array_key_exists($colName, $showTotalColumns))
<?php $dataFound = true; ?>
@if ($showTotalColumns[$colName] == 'point')
<td class="right"><b>{{ number_format($total[$colName], 2, '.', ',') }}</b></td>
@else
<td class="right"><b>{{ strtoupper($showTotalColumns[$colName]) }} {{ number_format($total[$colName], 2, '.', ',') }}</b></td>
<?php
if (array_key_exists('function', $showTotalColumns[$colName])){
$function = $showTotalColumns[$colName]['function'];
if (is_object($function) && $function instanceof Closure){
$total[$colName] = $function($total);
} else {
if ($showTotalColumns[$colName]['function'] == 'avg') {
$total[$colName] = round($total[$colName] / $no, 2);
}
}
}
?>
@if (array_key_exists('format',$showTotalColumns[$colName]))
@if ($showTotalColumns[$colName]['format'] == 'point')
<td class="right"><b>{{ number_format($total[$colName], 2, '.', ',') }}</b></td>
@else
<td class="right"><b>{{ strtoupper($showTotalColumns[$colName]['format']) }} {{ number_format($total[$colName], 2, '.', ',') }}</b></td>
@endif
@endif
@else
@if ($dataFound)
Expand Down