| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BillController.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\Chart; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2016-12-11 17:30:55 +01:00
										 |  |  | use FireflyIII\Generator\Chart\Basic\GeneratorInterface; | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  | use FireflyIII\Helpers\Collector\GroupCollectorInterface; | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Models\Bill; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Bill\BillRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2015-06-03 18:22:47 +02:00
										 |  |  | use FireflyIII\Support\CacheProperties; | 
					
						
							| 
									
										
										
										
											2018-07-08 12:28:42 +02:00
										 |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							| 
									
										
										
										
											2015-06-03 21:25:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class BillController. | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class BillController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |     protected GeneratorInterface $generator; | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-21 08:06:24 +02:00
										 |  |  |      * BillController constructor. | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |     public function __construct() | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |         parent::__construct(); | 
					
						
							| 
									
										
										
										
											2016-12-11 18:34:18 +01:00
										 |  |  |         $this->generator = app(GeneratorInterface::class); | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-12-27 17:29:41 +01:00
										 |  |  |      * Shows all bills and whether or not they've been paid this month (pie chart). | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-08 12:28:42 +02:00
										 |  |  |     public function frontpage(BillRepositoryInterface $repository): JsonResponse | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $start     = session('start', today(config('app.timezone'))->startOfMonth()); | 
					
						
							|  |  |  |         $end       = session('end', today(config('app.timezone'))->endOfMonth()); | 
					
						
							|  |  |  |         $cache     = new CacheProperties(); | 
					
						
							| 
									
										
										
										
											2016-12-11 17:30:55 +01:00
										 |  |  |         $cache->addProperty($start); | 
					
						
							|  |  |  |         $cache->addProperty($end); | 
					
						
							|  |  |  |         $cache->addProperty('chart.bill.frontpage'); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             return response()->json($cache->get()); | 
					
						
							| 
									
										
										
										
											2016-12-11 17:30:55 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |         $chartData = []; | 
					
						
							|  |  |  |         $paid      = $repository->sumPaidInRange($start, $end); | 
					
						
							|  |  |  |         $unpaid    = $repository->sumUnpaidInRange($start, $end); | 
					
						
							| 
									
										
										
										
											2016-12-11 17:30:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * @var array $info | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         foreach ($paid as $info) { | 
					
						
							|  |  |  |             $amount            = $info['sum']; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $label             = (string) trans('firefly.paid_in_currency', ['currency' => $info['name']]); | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |             $chartData[$label] = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'amount'          => $amount, | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |                 'currency_symbol' => $info['symbol'], | 
					
						
							|  |  |  |                 'currency_code'   => $info['code'], | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2018-08-28 05:21:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |         /** | 
					
						
							|  |  |  |          * @var array $info | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         foreach ($unpaid as $info) { | 
					
						
							|  |  |  |             $amount            = $info['sum']; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $label             = (string) trans('firefly.unpaid_in_currency', ['currency' => $info['name']]); | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |             $chartData[$label] = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'amount'          => $amount, | 
					
						
							| 
									
										
										
										
											2023-06-02 06:38:07 +02:00
										 |  |  |                 'currency_symbol' => $info['symbol'], | 
					
						
							|  |  |  |                 'currency_code'   => $info['code'], | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2018-08-28 05:21:23 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $data      = $this->generator->multiCurrencyPieChart($chartData); | 
					
						
							| 
									
										
										
										
											2016-12-11 17:46:30 +01:00
										 |  |  |         $cache->store($data); | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-10 20:30:09 +01:00
										 |  |  |         return response()->json($data); | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 08:58:58 +02:00
										 |  |  |      * Shows overview for a single bill. | 
					
						
							| 
									
										
										
										
											2018-07-21 08:06:24 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |     public function single(Bill $bill): JsonResponse | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $cache      = new CacheProperties(); | 
					
						
							| 
									
										
										
										
											2016-12-11 17:46:30 +01:00
										 |  |  |         $cache->addProperty('chart.bill.single'); | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |         $cache->addProperty($bill->id); | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |         $cache->addProperty($this->convertToPrimary); | 
					
						
							| 
									
										
										
										
											2015-06-27 11:44:18 +02:00
										 |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2024-12-27 22:03:16 +01:00
										 |  |  |             return response()->json($cache->get()); | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $locale     = app('steam')->getLocale(); | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         /** @var GroupCollectorInterface $collector */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $collector  = app(GroupCollectorInterface::class); | 
					
						
							|  |  |  |         $journals   = $collector->setBill($bill)->getExtractedJournals(); | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 18:07:54 +01:00
										 |  |  |         // sort the other way around:
 | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |         usort( | 
					
						
							|  |  |  |             $journals, | 
					
						
							|  |  |  |             static function (array $left, array $right) { | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  |                 if ($left['date']->gt($right['date'])) { | 
					
						
							| 
									
										
										
										
											2021-01-20 18:42:07 +01:00
										 |  |  |                     return 1; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  |                 if ($left['date']->lt($right['date'])) { | 
					
						
							| 
									
										
										
										
											2021-01-20 18:42:07 +01:00
										 |  |  |                     return -1; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 18:42:07 +01:00
										 |  |  |                 return 0; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2024-12-27 07:29:17 +01:00
										 |  |  |         $currency   = $bill->transactionCurrency; | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |         if ($this->convertToPrimary) { | 
					
						
							|  |  |  |             $currency = $this->primaryCurrency; | 
					
						
							| 
									
										
										
										
											2024-12-26 11:28:31 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-19 18:07:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-23 14:53:13 +02:00
										 |  |  |         $chartData  = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 'type'            => 'line', | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 'label'           => (string) trans('firefly.min-amount'), | 
					
						
							| 
									
										
										
										
											2024-12-26 11:28:31 +01:00
										 |  |  |                 'currency_symbol' => $currency->symbol, | 
					
						
							|  |  |  |                 'currency_code'   => $currency->code, | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'entries'         => [], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'type'            => 'line', | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 'label'           => (string) trans('firefly.max-amount'), | 
					
						
							| 
									
										
										
										
											2024-12-26 11:28:31 +01:00
										 |  |  |                 'currency_symbol' => $currency->symbol, | 
					
						
							|  |  |  |                 'currency_code'   => $currency->code, | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'entries'         => [], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'type'            => 'bar', | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 'label'           => (string) trans('firefly.journal-amount'), | 
					
						
							| 
									
										
										
										
											2024-12-26 11:28:31 +01:00
										 |  |  |                 'currency_symbol' => $currency->symbol, | 
					
						
							|  |  |  |                 'currency_code'   => $currency->code, | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |                 'entries'         => [], | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2016-12-11 17:46:30 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |         $currencyId = $bill->transaction_currency_id; | 
					
						
							| 
									
										
										
										
											2024-12-27 07:29:17 +01:00
										 |  |  |         $amountMin  = $bill->amount_min; | 
					
						
							|  |  |  |         $amountMax  = $bill->amount_max; | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |         if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id) { | 
					
						
							| 
									
										
										
										
											2024-12-26 11:28:31 +01:00
										 |  |  |             $amountMin = $bill->native_amount_min; | 
					
						
							|  |  |  |             $amountMax = $bill->native_amount_max; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-30 12:31:19 +02:00
										 |  |  |         foreach ($journals as $journal) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $date                           = $journal['date']->isoFormat((string) trans('config.month_and_day_js', [], $locale)); | 
					
						
							| 
									
										
										
										
											2024-12-26 11:28:31 +01:00
										 |  |  |             $chartData[0]['entries'][$date] = $amountMin; // minimum amount of bill
 | 
					
						
							|  |  |  |             $chartData[1]['entries'][$date] = $amountMax; // maximum amount of bill
 | 
					
						
							| 
									
										
										
										
											2018-07-22 08:58:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // append amount because there are more than one per moment:
 | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |             if (!array_key_exists($date, $chartData[2]['entries'])) { | 
					
						
							| 
									
										
										
										
											2018-07-22 08:58:58 +02:00
										 |  |  |                 $chartData[2]['entries'][$date] = '0'; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-05-04 13:47:00 +02:00
										 |  |  |             $amount                         = bcmul((string) $journal['amount'], '-1'); | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |             if ($this->convertToPrimary && $currencyId !== $journal['currency_id']) { | 
					
						
							|  |  |  |                 $amount = bcmul($journal['pc_amount'] ?? '0', '-1'); | 
					
						
							| 
									
										
										
										
											2024-12-26 11:28:31 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-08-01 06:12:36 +02:00
										 |  |  |             if ($this->convertToPrimary && $currencyId === $journal['foreign_currency_id']) { | 
					
						
							|  |  |  |                 $amount = bcmul((string) $journal['pc_amount'], '-1'); | 
					
						
							| 
									
										
										
										
											2022-10-23 14:53:13 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-22 08:58:58 +02:00
										 |  |  |             $chartData[2]['entries'][$date] = bcadd($chartData[2]['entries'][$date], $amount);  // amount of journal
 | 
					
						
							| 
									
										
										
										
											2016-12-11 17:46:30 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $data       = $this->generator->multiSet($chartData); | 
					
						
							| 
									
										
										
										
											2015-06-03 21:25:11 +02:00
										 |  |  |         $cache->store($data); | 
					
						
							| 
									
										
										
										
											2015-06-02 17:44:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-10 20:30:09 +01:00
										 |  |  |         return response()->json($data); | 
					
						
							| 
									
										
										
										
											2015-05-16 09:41:14 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-20 19:56:14 +02:00
										 |  |  | } |