mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-29 06:34:37 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			81 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * FrontpageController.php
 | |
|  * Copyright (c) 2019 james@firefly-iii.org
 | |
|  *
 | |
|  * This file is part of Firefly III (https://github.com/firefly-iii).
 | |
|  *
 | |
|  * This program is free software: you can redistribute it and/or modify
 | |
|  * it under the terms of the GNU Affero General Public License as
 | |
|  * published by the Free Software Foundation, either version 3 of the
 | |
|  * License, or (at your option) any later version.
 | |
|  *
 | |
|  * This program is distributed in the hope that it will be useful,
 | |
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | |
|  * GNU Affero General Public License for more details.
 | |
|  *
 | |
|  * You should have received a copy of the GNU Affero General Public License
 | |
|  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | |
|  */
 | |
| declare(strict_types=1);
 | |
| 
 | |
| namespace FireflyIII\Http\Controllers\Json;
 | |
| 
 | |
| use FireflyIII\Http\Controllers\Controller;
 | |
| use FireflyIII\Models\PiggyBank;
 | |
| use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
 | |
| use Illuminate\Http\JsonResponse;
 | |
| use Log;
 | |
| use Throwable;
 | |
| 
 | |
| /**
 | |
|  * Class FrontpageController.
 | |
|  */
 | |
| class FrontpageController extends Controller
 | |
| {
 | |
|     /**
 | |
|      * Piggy bank pie chart.
 | |
|      *
 | |
|      * @param PiggyBankRepositoryInterface $repository
 | |
|      *
 | |
|      * @return JsonResponse
 | |
|      */
 | |
|     public function piggyBanks(PiggyBankRepositoryInterface $repository): JsonResponse
 | |
|     {
 | |
|         $set  = $repository->getPiggyBanks();
 | |
|         $info = [];
 | |
|         /** @var PiggyBank $piggyBank */
 | |
|         foreach ($set as $piggyBank) {
 | |
|             $amount = $repository->getCurrentAmount($piggyBank);
 | |
|             if (1 === bccomp($amount, '0')) {
 | |
|                 // percentage!
 | |
|                 $pct = round(($amount / $piggyBank->targetamount) * 100);
 | |
| 
 | |
|                 $entry = [
 | |
|                     'id'         => $piggyBank->id,
 | |
|                     'name'       => $piggyBank->name,
 | |
|                     'amount'     => $amount,
 | |
|                     'target'     => $piggyBank->targetamount,
 | |
|                     'percentage' => $pct,
 | |
|                 ];
 | |
| 
 | |
|                 $info[] = $entry;
 | |
|             }
 | |
|         }
 | |
|         $html = '';
 | |
|         if (count($info) > 0) {
 | |
|             try {
 | |
|                 $html = view('json.piggy-banks', compact('info'))->render();
 | |
|                 // @codeCoverageIgnoreStart
 | |
|             } catch (Throwable $e) {
 | |
|                 Log::error(sprintf('Cannot render json.piggy-banks: %s', $e->getMessage()));
 | |
|                 $html = 'Could not render view.';
 | |
|             }
 | |
|             // @codeCoverageIgnoreEnd
 | |
|         }
 | |
| 
 | |
|         return response()->json(['html' => $html]);
 | |
|     }
 | |
| }
 |