From eac65986a6e8c1755b5c15a107368fb5b40534d3 Mon Sep 17 00:00:00 2001 From: nurafrinaalimi16 Date: Mon, 29 Apr 2024 16:56:53 +0800 Subject: [PATCH] pdf result --- app/Http/Controllers/ResultController.php | 130 ++++++++ app/Result.php | 16 + composer.json | 1 + composer.lock | 288 +++++++++++++++++- config/app.php | 5 +- .../js/components/demo/admin/home/index.vue | 26 +- resources/views/report/result.blade.php | 92 ++++++ routes/web.php | 26 +- 8 files changed, 577 insertions(+), 7 deletions(-) create mode 100644 app/Http/Controllers/ResultController.php create mode 100644 resources/views/report/result.blade.php diff --git a/app/Http/Controllers/ResultController.php b/app/Http/Controllers/ResultController.php new file mode 100644 index 0000000..ee6acdd --- /dev/null +++ b/app/Http/Controllers/ResultController.php @@ -0,0 +1,130 @@ + result.position_id === positionId) + // .reduce((total, result) => total + result.votes, 0); + + // return ((votes / totalVotes) * 100).toFixed(2); + // }, + + $positions = Position::all(); + + $results = Result::with('nominee')->get(); + dd($results->toArray()); + + + $pdf = App::make('dompdf.wrapper'); + $pdf->loadView('report.result', compact('results', 'positions')); + return $pdf->stream(); + } + + public function index($id) + { + $results = $this->getResult($id); + $total_votes = Result::where('election_id', $id)->count(); + $positions = \App\Position::where('election_id', $id)->get(); + $nominees = \App\Nominee::where('election_id', $id)->get(); + $no_votes = \App\Nominee::whereNotIn('id', $results->pluck('nominee_id') )->get(); + + + $pdf = App::make('dompdf.wrapper'); + $pdf->loadView('report.result', compact('results', 'positions','nominees', 'no_votes', 'total_votes')); + return $pdf->stream(); + } + + private function getResult($id) + { + return \App\Result::select(DB::raw('position_id,nominee_id,count(*) as votes')) + ->groupBy('position_id', 'nominee_id') + ->where('election_id', $id) + ->orderBy('votes', 'DESC') + ->get(); + } + + + /** + * Show the form for creating a new resource. + * + * @return \Illuminate\Http\Response + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function show($id) + { + // + } + + /** + * Show the form for editing the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function edit($id) + { + // + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param int $id + * @return \Illuminate\Http\Response + */ + public function update(Request $request, $id) + { + // + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + // + } +} diff --git a/app/Result.php b/app/Result.php index 0a6e458..5e7d12f 100644 --- a/app/Result.php +++ b/app/Result.php @@ -3,9 +3,25 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class Result extends Model { protected $table = 'result'; protected $fillable = ['voter_id', 'nominee_id', 'position_id', 'election_id']; + // protected $appends = ['votes']; + + + + function nominee() : BelongsTo { + return $this->belongsTo(Nominee::class); + } + + public function votes($nominee_id) + { + return $this->where('nominee_id','=',$nominee_id)->count(); + } + + + } diff --git a/composer.json b/composer.json index 646bd4d..8ac56e4 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "type": "project", "require": { "php": ">=5.6.4", + "barryvdh/laravel-dompdf": "v0.8.4", "cloudinary/cloudinary_php": "^2.3", "doctrine/dbal": "^2.5", "guzzlehttp/guzzle": "^6.5", diff --git a/composer.lock b/composer.lock index 43fb51d..ac2cf52 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,68 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0a5b2fadbc0e0e1ff0d4762c70a70818", + "content-hash": "1e25a85ac1ac7795e1f547dd83318564", "packages": [ + { + "name": "barryvdh/laravel-dompdf", + "version": "v0.8.4", + "source": { + "type": "git", + "url": "https://github.com/barryvdh/laravel-dompdf.git", + "reference": "3fd817065e1c820b1ddace8b2bf65ca45088df4f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barryvdh/laravel-dompdf/zipball/3fd817065e1c820b1ddace8b2bf65ca45088df4f", + "reference": "3fd817065e1c820b1ddace8b2bf65ca45088df4f", + "shasum": "" + }, + "require": { + "dompdf/dompdf": "^0.8", + "illuminate/support": "5.5.x|5.6.x|5.7.x|5.8.x", + "php": ">=7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.8-dev" + }, + "laravel": { + "providers": [ + "Barryvdh\\DomPDF\\ServiceProvider" + ], + "aliases": { + "PDF": "Barryvdh\\DomPDF\\Facade" + } + } + }, + "autoload": { + "psr-4": { + "Barryvdh\\DomPDF\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "A DOMPDF Wrapper for Laravel", + "keywords": [ + "dompdf", + "laravel", + "pdf" + ], + "support": { + "issues": "https://github.com/barryvdh/laravel-dompdf/issues", + "source": "https://github.com/barryvdh/laravel-dompdf/tree/master" + }, + "time": "2019-02-26T18:07:43+00:00" + }, { "name": "cloudinary/cloudinary_php", "version": "2.3.0", @@ -692,6 +752,78 @@ ], "time": "2020-05-25T17:44:05+00:00" }, + { + "name": "dompdf/dompdf", + "version": "v0.8.6", + "source": { + "type": "git", + "url": "https://github.com/dompdf/dompdf.git", + "reference": "db91d81866c69a42dad1d2926f61515a1e3f42c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dompdf/dompdf/zipball/db91d81866c69a42dad1d2926f61515a1e3f42c5", + "reference": "db91d81866c69a42dad1d2926f61515a1e3f42c5", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-mbstring": "*", + "phenx/php-font-lib": "^0.5.2", + "phenx/php-svg-lib": "^0.3.3", + "php": "^7.1" + }, + "require-dev": { + "mockery/mockery": "^1.3", + "phpunit/phpunit": "^7.5", + "squizlabs/php_codesniffer": "^3.5" + }, + "suggest": { + "ext-gd": "Needed to process images", + "ext-gmagick": "Improves image processing performance", + "ext-imagick": "Improves image processing performance", + "ext-zlib": "Needed for pdf stream compression" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-develop": "0.7-dev" + } + }, + "autoload": { + "psr-4": { + "Dompdf\\": "src/" + }, + "classmap": [ + "lib/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1" + ], + "authors": [ + { + "name": "Fabien Ménager", + "email": "fabien.menager@gmail.com" + }, + { + "name": "Brian Sweeney", + "email": "eclecticgeek@gmail.com" + }, + { + "name": "Gabriel Bull", + "email": "me@gabrielbull.com" + } + ], + "description": "DOMPDF is a CSS 2.1 compliant HTML to PDF converter", + "homepage": "https://github.com/dompdf/dompdf", + "support": { + "issues": "https://github.com/dompdf/dompdf/issues", + "source": "https://github.com/dompdf/dompdf/tree/master" + }, + "time": "2020-08-30T22:54:22+00:00" + }, { "name": "dragonmantank/cron-expression", "version": "v2.3.1", @@ -2261,6 +2393,95 @@ }, "time": "2022-02-16T17:07:03+00:00" }, + { + "name": "phenx/php-font-lib", + "version": "0.5.6", + "source": { + "type": "git", + "url": "https://github.com/dompdf/php-font-lib.git", + "reference": "a1681e9793040740a405ac5b189275059e2a9863" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dompdf/php-font-lib/zipball/a1681e9793040740a405ac5b189275059e2a9863", + "reference": "a1681e9793040740a405ac5b189275059e2a9863", + "shasum": "" + }, + "require": { + "ext-mbstring": "*" + }, + "require-dev": { + "symfony/phpunit-bridge": "^3 || ^4 || ^5 || ^6" + }, + "type": "library", + "autoload": { + "psr-4": { + "FontLib\\": "src/FontLib" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1-or-later" + ], + "authors": [ + { + "name": "Fabien Ménager", + "email": "fabien.menager@gmail.com" + } + ], + "description": "A library to read, parse, export and make subsets of different types of font files.", + "homepage": "https://github.com/PhenX/php-font-lib", + "support": { + "issues": "https://github.com/dompdf/php-font-lib/issues", + "source": "https://github.com/dompdf/php-font-lib/tree/0.5.6" + }, + "time": "2024-01-29T14:45:26+00:00" + }, + { + "name": "phenx/php-svg-lib", + "version": "0.3.4", + "source": { + "type": "git", + "url": "https://github.com/PhenX/php-svg-lib.git", + "reference": "f627771eb854aa7f45f80add0f23c6c4d67ea0f2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PhenX/php-svg-lib/zipball/f627771eb854aa7f45f80add0f23c6c4d67ea0f2", + "reference": "f627771eb854aa7f45f80add0f23c6c4d67ea0f2", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0", + "sabberworm/php-css-parser": "^8.3" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Svg\\": "src/Svg" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0" + ], + "authors": [ + { + "name": "Fabien Ménager", + "email": "fabien.menager@gmail.com" + } + ], + "description": "A library to read, parse and export to PDF SVG files.", + "homepage": "https://github.com/PhenX/php-svg-lib", + "support": { + "issues": "https://github.com/PhenX/php-svg-lib/issues", + "source": "https://github.com/PhenX/php-svg-lib/tree/0.3.4" + }, + "time": "2021-10-18T02:13:32+00:00" + }, { "name": "phpoption/phpoption", "version": "1.8.1", @@ -2870,6 +3091,71 @@ ], "time": "2021-09-25T23:07:42+00:00" }, + { + "name": "sabberworm/php-css-parser", + "version": "v8.5.1", + "source": { + "type": "git", + "url": "https://github.com/MyIntervals/PHP-CSS-Parser.git", + "reference": "4a3d572b0f8b28bb6fd016ae8bbfc445facef152" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/MyIntervals/PHP-CSS-Parser/zipball/4a3d572b0f8b28bb6fd016ae8bbfc445facef152", + "reference": "4a3d572b0f8b28bb6fd016ae8bbfc445facef152", + "shasum": "" + }, + "require": { + "ext-iconv": "*", + "php": ">=5.6.20" + }, + "require-dev": { + "phpunit/phpunit": "^5.7.27" + }, + "suggest": { + "ext-mbstring": "for parsing UTF-8 CSS" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "9.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Sabberworm\\CSS\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Raphael Schweikert" + }, + { + "name": "Oliver Klee", + "email": "github@oliverklee.de" + }, + { + "name": "Jake Hotson", + "email": "jake.github@qzdesign.co.uk" + } + ], + "description": "Parser for CSS Files written in PHP", + "homepage": "https://www.sabberworm.com/blog/2010/6/10/php-css-parser", + "keywords": [ + "css", + "parser", + "stylesheet" + ], + "support": { + "issues": "https://github.com/MyIntervals/PHP-CSS-Parser/issues", + "source": "https://github.com/MyIntervals/PHP-CSS-Parser/tree/v8.5.1" + }, + "time": "2024-02-15T16:41:13+00:00" + }, { "name": "shrikeh/teapot", "version": "v2.3.1", diff --git a/config/app.php b/config/app.php index e456d4e..f09d330 100644 --- a/config/app.php +++ b/config/app.php @@ -6,7 +6,7 @@ return [ | My Configuration |------------------------------------------------------------------------- | - | + | */ 'nominee_image' => 'images/nominee/default.jpg', @@ -195,6 +195,8 @@ return [ // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, + Barryvdh\DomPDF\ServiceProvider::class, + ], @@ -244,6 +246,7 @@ return [ 'URL' => Illuminate\Support\Facades\URL::class, 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, + 'PDF' => Barryvdh\DomPDF\Facade::class, ], diff --git a/resources/assets/js/components/demo/admin/home/index.vue b/resources/assets/js/components/demo/admin/home/index.vue index e03242d..5888f8d 100644 --- a/resources/assets/js/components/demo/admin/home/index.vue +++ b/resources/assets/js/components/demo/admin/home/index.vue @@ -3,7 +3,7 @@

Undian Terkini

- @@ -37,6 +37,9 @@ Keputusan Undian + + Muat Turun PDF + @@ -60,7 +63,8 @@