| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * BillRepository.php | 
					
						
							|  |  |  |  * Copyright (c) 2023 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:36:43 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  | namespace FireflyIII\Repositories\UserGroups\Bill; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Models\Bill; | 
					
						
							|  |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Support\CacheProperties; | 
					
						
							|  |  |  | use FireflyIII\Support\Http\Api\ExchangeRateConverter; | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  | use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2023-12-29 08:21:14 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BillRepository | 
					
						
							| 
									
										
										
										
											2025-02-23 12:27:34 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @deprecated | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class BillRepository implements BillRepositoryInterface | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  |     use UserGroupTrait; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 14:14:33 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Correct order of piggies in case of issues. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function correctOrder(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $set     = $this->userGroup->bills()->orderBy('order', 'ASC')->get(); | 
					
						
							|  |  |  |         $current = 1; | 
					
						
							|  |  |  |         foreach ($set as $bill) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |             if ($bill->order !== $current) { | 
					
						
							| 
									
										
										
										
											2023-08-09 14:14:33 +02:00
										 |  |  |                 $bill->order = $current; | 
					
						
							|  |  |  |                 $bill->save(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ++$current; | 
					
						
							| 
									
										
										
										
											2023-08-09 14:14:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getBills(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->userGroup->bills() | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->orderBy('bills.name', 'ASC') | 
					
						
							|  |  |  |             ->get(['bills.*']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-08-09 14:14:33 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |     public function sumPaidInRange(Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-29 12:00:15 +01:00
										 |  |  |         Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         $bills     = $this->getActiveBills(); | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         $default   = app('amount')->getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         $return    = []; | 
					
						
							|  |  |  |         $converter = new ExchangeRateConverter(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         /** @var Bill $bill */ | 
					
						
							|  |  |  |         foreach ($bills as $bill) { | 
					
						
							|  |  |  |             /** @var Collection $set */ | 
					
						
							|  |  |  |             $set        = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']); | 
					
						
							|  |  |  |             $currency   = $bill->transactionCurrency; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |             $currencyId = $bill->transaction_currency_id; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 06:45:59 +01:00
										 |  |  |             $return[$currencyId] ??= [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 'currency_id'                    => (string) $currency->id, | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                 'currency_name'                  => $currency->name, | 
					
						
							|  |  |  |                 'currency_symbol'                => $currency->symbol, | 
					
						
							|  |  |  |                 'currency_code'                  => $currency->code, | 
					
						
							|  |  |  |                 'currency_decimal_places'        => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 'native_currency_id'             => (string) $default->id, | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                 'native_currency_name'           => $default->name, | 
					
						
							|  |  |  |                 'native_currency_symbol'         => $default->symbol, | 
					
						
							|  |  |  |                 'native_currency_code'           => $default->code, | 
					
						
							|  |  |  |                 'native_currency_decimal_places' => $default->decimal_places, | 
					
						
							|  |  |  |                 'sum'                            => '0', | 
					
						
							|  |  |  |                 'native_sum'                     => '0', | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |             ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /** @var TransactionJournal $transactionJournal */ | 
					
						
							|  |  |  |             foreach ($set as $transactionJournal) { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                 /** @var null|Transaction $sourceTransaction */ | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                 $sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first(); | 
					
						
							|  |  |  |                 if (null !== $sourceTransaction) { | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |                     $amount                            = $sourceTransaction->amount; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     if ((int) $sourceTransaction->foreign_currency_id === $currency->id) { | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                         // use foreign amount instead!
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                         $amount = (string) $sourceTransaction->foreign_amount; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                     // convert to native currency
 | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |                     $nativeAmount                      = $amount; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |                     if ($currencyId !== $default->id) { | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                         // get rate and convert.
 | 
					
						
							|  |  |  |                         $nativeAmount = $converter->convert($currency, $default, $transactionJournal->date, $amount); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     if ((int) $sourceTransaction->foreign_currency_id === $default->id) { | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                         // ignore conversion, use foreign amount
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                         $nativeAmount = (string) $sourceTransaction->foreign_amount; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                     $return[$currencyId]['sum']        = bcadd($return[$currencyId]['sum'], $amount); | 
					
						
							|  |  |  |                     $return[$currencyId]['native_sum'] = bcadd($return[$currencyId]['native_sum'], $nativeAmount); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-22 06:14:14 +01:00
										 |  |  |         $converter->summarize(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getActiveBills(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->userGroup->bills() | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             ->where('active', true) | 
					
						
							|  |  |  |             ->orderBy('bills.name', 'ASC') | 
					
						
							|  |  |  |             ->get(['bills.*']) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function sumUnpaidInRange(Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-29 12:00:15 +01:00
										 |  |  |         Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         $bills     = $this->getActiveBills(); | 
					
						
							|  |  |  |         $return    = []; | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         $default   = app('amount')->getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         $converter = new ExchangeRateConverter(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         /** @var Bill $bill */ | 
					
						
							|  |  |  |         foreach ($bills as $bill) { | 
					
						
							|  |  |  |             $dates = $this->getPayDatesInRange($bill, $start, $end); | 
					
						
							|  |  |  |             $count = $bill->transactionJournals()->after($start)->before($end)->count(); | 
					
						
							|  |  |  |             $total = $dates->count() - $count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($total > 0) { | 
					
						
							|  |  |  |                 $currency                          = $bill->transactionCurrency; | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |                 $currencyId                        = $bill->transaction_currency_id; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                 $average                           = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2'); | 
					
						
							|  |  |  |                 $nativeAverage                     = $converter->convert($currency, $default, $start, $average); | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |                 $return[$currencyId] ??= [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     'currency_id'                    => (string) $currency->id, | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                     'currency_name'                  => $currency->name, | 
					
						
							|  |  |  |                     'currency_symbol'                => $currency->symbol, | 
					
						
							|  |  |  |                     'currency_code'                  => $currency->code, | 
					
						
							|  |  |  |                     'currency_decimal_places'        => $currency->decimal_places, | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     'native_currency_id'             => (string) $default->id, | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |                     'native_currency_name'           => $default->name, | 
					
						
							|  |  |  |                     'native_currency_symbol'         => $default->symbol, | 
					
						
							|  |  |  |                     'native_currency_code'           => $default->code, | 
					
						
							|  |  |  |                     'native_currency_decimal_places' => $default->decimal_places, | 
					
						
							|  |  |  |                     'sum'                            => '0', | 
					
						
							|  |  |  |                     'native_sum'                     => '0', | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |                 ]; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $return[$currencyId]['sum']        = bcadd($return[$currencyId]['sum'], bcmul($average, (string) $total)); | 
					
						
							|  |  |  |                 $return[$currencyId]['native_sum'] = bcadd($return[$currencyId]['native_sum'], bcmul($nativeAverage, (string) $total)); | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-22 06:14:14 +01:00
										 |  |  |         $converter->summarize(); | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Between start and end, tells you on which date(s) the bill is expected to hit. | 
					
						
							|  |  |  |      * TODO duplicate of function in other billrepositoryinterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getPayDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $set          = new Collection(); | 
					
						
							|  |  |  |         $currentStart = clone $start; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         // app('log')->debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
 | 
					
						
							|  |  |  |         // app('log')->debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         while ($currentStart <= $end) { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             // app('log')->debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |             $nextExpectedMatch = $this->nextDateMatch($bill, $currentStart); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             // app('log')->debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |             if ($nextExpectedMatch > $end) {// If nextExpectedMatch is after end, we continue
 | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $set->push(clone $nextExpectedMatch); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             // app('log')->debug(sprintf('Now %d dates in set.', $set->count()));
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |             $nextExpectedMatch->addDay(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             // app('log')->debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
 | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |             $currentStart      = clone $nextExpectedMatch; | 
					
						
							| 
									
										
										
										
											2023-08-06 07:04:09 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Given a bill and a date, this method will tell you at which moment this bill expects its next | 
					
						
							|  |  |  |      * transaction. Whether it is there already, is not relevant. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * TODO duplicate of other repos | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function nextDateMatch(Bill $bill, Carbon $date): Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cache = new CacheProperties(); | 
					
						
							|  |  |  |         $cache->addProperty($bill->id); | 
					
						
							|  |  |  |         $cache->addProperty('nextDateMatch'); | 
					
						
							|  |  |  |         $cache->addProperty($date); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							|  |  |  |             return $cache->get(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // find the most recent date for this bill NOT in the future. Cache this date:
 | 
					
						
							|  |  |  |         $start = clone $bill->date; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while ($start < $date) { | 
					
						
							|  |  |  |             $start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $cache->store($start); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $start; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |