| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * BillTransformer.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:57:18 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01: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. | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01: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. | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01: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/>. | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 19:49:29 +01:00
										 |  |  | namespace FireflyIII\Transformers; | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2022-03-29 15:10:05 +02:00
										 |  |  | use Carbon\CarbonInterface; | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | use FireflyIII\Models\Bill; | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  | use FireflyIII\Models\ObjectGroup; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2019-08-21 04:59:35 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | use FireflyIII\Repositories\Bill\BillRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  | use FireflyIII\Support\Models\BillDateCalculator; | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class BillTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  | class BillTransformer extends AbstractTransformer | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |     private BillDateCalculator      $calculator; | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |     private BillRepositoryInterface $repository; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |     private TransactionCurrency     $default; | 
					
						
							|  |  |  |     private bool                    $convertToNative; | 
					
						
							| 
									
										
										
										
											2018-09-27 07:43:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * BillTransformer constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  |     public function __construct() | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |         $this->repository      = app(BillRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->calculator      = app(BillDateCalculator::class); | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         $this->default         = Amount::getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |         $this->convertToNative = Amount::convertToNative(); | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-16 22:47:08 +01:00
										 |  |  |      * Transform the bill. | 
					
						
							| 
									
										
										
										
											2023-12-22 17:28:42 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.ExcessiveMethodLength") | 
					
						
							|  |  |  |      * @SuppressWarnings("PHPMD.NPathComplexity") | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function transform(Bill $bill): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $default           = $this->parameters->get('defaultCurrency') ?? $this->default; | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $paidData          = $this->paidData($bill); | 
					
						
							|  |  |  |         $lastPaidDate      = $this->getLastPaidDate($paidData); | 
					
						
							|  |  |  |         $start             = $this->parameters->get('start') ?? today()->subYears(10); | 
					
						
							|  |  |  |         $end               = $this->parameters->get('end') ?? today()->addYears(10); | 
					
						
							|  |  |  |         $payDates          = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate); | 
					
						
							|  |  |  |         $currency          = $bill->transactionCurrency; | 
					
						
							|  |  |  |         $notes             = $this->repository->getNoteText($bill); | 
					
						
							|  |  |  |         $notes             = '' === $notes ? null : $notes; | 
					
						
							|  |  |  |         $objectGroupId     = null; | 
					
						
							|  |  |  |         $objectGroupOrder  = null; | 
					
						
							|  |  |  |         $objectGroupTitle  = null; | 
					
						
							| 
									
										
										
										
											2023-12-22 17:28:42 +01:00
										 |  |  |         $this->repository->setUser($bill->user); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|ObjectGroup $objectGroup */ | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $objectGroup       = $bill->objectGroups->first(); | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |         if (null !== $objectGroup) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |             $objectGroupId    = $objectGroup->id; | 
					
						
							|  |  |  |             $objectGroupOrder = $objectGroup->order; | 
					
						
							| 
									
										
										
										
											2020-06-30 19:06:05 +02:00
										 |  |  |             $objectGroupTitle = $objectGroup->title; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 06:47:56 +02:00
										 |  |  |         $paidDataFormatted = []; | 
					
						
							|  |  |  |         $payDatesFormatted = []; | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |         foreach ($paidData as $object) { | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |             $date                = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2024-04-02 15:40:33 +02:00
										 |  |  |             if (null === $date) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |                 $date = today(config('app.timezone')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $object['date']      = $date->toAtomString(); | 
					
						
							| 
									
										
										
										
											2021-07-18 14:51:30 +02:00
										 |  |  |             $paidDataFormatted[] = $object; | 
					
						
							| 
									
										
										
										
											2021-04-01 06:47:56 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($payDates as $string) { | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |             $date                = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2024-04-02 15:40:33 +02:00
										 |  |  |             if (null === $date) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |                 $date = today(config('app.timezone')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $payDatesFormatted[] = $date->toAtomString(); | 
					
						
							| 
									
										
										
										
											2021-04-01 06:47:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  |         // next expected match
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $nem               = null; | 
					
						
							|  |  |  |         $nemDate           = null; | 
					
						
							|  |  |  |         $nemDiff           = trans('firefly.not_expected_period'); | 
					
						
							|  |  |  |         $firstPayDate      = $payDates[0] ?? null; | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $firstPayDate) { | 
					
						
							|  |  |  |             $nemDate = Carbon::createFromFormat('!Y-m-d', $firstPayDate, config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2024-04-02 15:40:33 +02:00
										 |  |  |             if (null === $nemDate) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |                 $nemDate = today(config('app.timezone')); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |             $nem     = $nemDate->toAtomString(); | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // nullify again when it's outside the current view range.
 | 
					
						
							| 
									
										
										
										
											2024-03-30 09:56:51 +01:00
										 |  |  |             if ( | 
					
						
							| 
									
										
										
										
											2024-03-31 03:14:08 +02:00
										 |  |  |                 (null !== $this->parameters->get('start') && $nemDate->lt($this->parameters->get('start'))) | 
					
						
							|  |  |  |                 || (null !== $this->parameters->get('end') && $nemDate->gt($this->parameters->get('end'))) | 
					
						
							| 
									
										
										
										
											2024-03-30 09:56:51 +01:00
										 |  |  |             ) { | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  |                 $nem          = null; | 
					
						
							|  |  |  |                 $nemDate      = null; | 
					
						
							|  |  |  |                 $firstPayDate = null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-04-02 06:57:31 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-18 14:51:30 +02:00
										 |  |  |         // converting back and forth is bad code but OK.
 | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  |         if (null !== $nemDate) { | 
					
						
							|  |  |  |             if ($nemDate->isToday()) { | 
					
						
							|  |  |  |                 $nemDiff = trans('firefly.today'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-07-18 14:51:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  |             $current = $payDatesFormatted[0] ?? null; | 
					
						
							|  |  |  |             if (null !== $current && !$nemDate->isToday()) { | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |                 $temp2   = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current); | 
					
						
							| 
									
										
										
										
											2024-04-02 15:40:33 +02:00
										 |  |  |                 if (null === $temp2) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |                     $temp2 = today(config('app.timezone')); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-11-25 19:29:10 +01:00
										 |  |  |                 $nemDiff = trans('firefly.bill_expected_date', ['date' => $temp2->diffForHumans(today(config('app.timezone')), CarbonInterface::DIFF_RELATIVE_TO_NOW)]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             unset($temp2); | 
					
						
							| 
									
										
										
										
											2021-07-18 14:51:30 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2025-01-18 17:20:39 +01:00
										 |  |  |             'id'                             => $bill->id, | 
					
						
							|  |  |  |             'created_at'                     => $bill->created_at->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'                     => $bill->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'currency_id'                    => (string) $bill->transaction_currency_id, | 
					
						
							|  |  |  |             'currency_code'                  => $currency->code, | 
					
						
							|  |  |  |             'currency_symbol'                => $currency->symbol, | 
					
						
							|  |  |  |             'currency_decimal_places'        => $currency->decimal_places, | 
					
						
							|  |  |  |             'native_currency_id'             => null === $default ? null : (string) $default->id, | 
					
						
							|  |  |  |             'native_currency_code'           => $default?->code, | 
					
						
							|  |  |  |             'native_currency_symbol'         => $default?->symbol, | 
					
						
							|  |  |  |             'native_currency_decimal_places' => $default?->decimal_places, | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |             'name'                           => $bill->name, | 
					
						
							|  |  |  |             'amount_min'                     => app('steam')->bcround($bill->amount_min, $currency->decimal_places), | 
					
						
							|  |  |  |             'amount_max'                     => app('steam')->bcround($bill->amount_max, $currency->decimal_places), | 
					
						
							|  |  |  |             'native_amount_min'              => $this->convertToNative ? app('steam')->bcround($bill->native_amount_min, $default->decimal_places) : null, | 
					
						
							|  |  |  |             'native_amount_max'              => $this->convertToNative ? app('steam')->bcround($bill->native_amount_max, $default->decimal_places) : null, | 
					
						
							|  |  |  |             'date'                           => $bill->date->toAtomString(), | 
					
						
							|  |  |  |             'end_date'                       => $bill->end_date?->toAtomString(), | 
					
						
							|  |  |  |             'extension_date'                 => $bill->extension_date?->toAtomString(), | 
					
						
							|  |  |  |             'repeat_freq'                    => $bill->repeat_freq, | 
					
						
							|  |  |  |             'skip'                           => $bill->skip, | 
					
						
							|  |  |  |             'active'                         => $bill->active, | 
					
						
							|  |  |  |             'order'                          => $bill->order, | 
					
						
							|  |  |  |             'notes'                          => $notes, | 
					
						
							|  |  |  |             'object_group_id'                => null !== $objectGroupId ? (string) $objectGroupId : null, | 
					
						
							|  |  |  |             'object_group_order'             => $objectGroupOrder, | 
					
						
							|  |  |  |             'object_group_title'             => $objectGroupTitle, | 
					
						
							| 
									
										
										
										
											2021-07-18 14:51:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // these fields need work:
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |             'next_expected_match'            => $nem, | 
					
						
							|  |  |  |             'next_expected_match_diff'       => $nemDiff, | 
					
						
							|  |  |  |             'pay_dates'                      => $payDatesFormatted, | 
					
						
							|  |  |  |             'paid_dates'                     => $paidDataFormatted, | 
					
						
							|  |  |  |             'links'                          => [ | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |                     'uri' => '/bills/'.$bill->id, | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Get the data the bill was paid and predict the next expected match. | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function paidData(Bill $bill): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-06 18:21:49 +02:00
										 |  |  |         app('log')->debug(sprintf('Now in paidData for bill #%d', $bill->id)); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         if (null === $this->parameters->get('start') || null === $this->parameters->get('end')) { | 
					
						
							| 
									
										
										
										
											2023-10-06 18:21:49 +02:00
										 |  |  |             app('log')->debug('parameters are NULL, return empty array'); | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |             return []; | 
					
						
							| 
									
										
										
										
											2018-02-06 18:11:33 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-01 12:18:07 +02:00
										 |  |  |         // 2023-07-1 sub one day from the start date to fix a possible bug (see #7704)
 | 
					
						
							| 
									
										
										
										
											2023-07-18 06:18:04 +02:00
										 |  |  |         // 2023-07-18 this particular date is used to search for the last paid date.
 | 
					
						
							|  |  |  |         // 2023-07-18 the cloned $searchDate is used to grab the correct transactions.
 | 
					
						
							| 
									
										
										
										
											2023-07-01 12:18:07 +02:00
										 |  |  |         /** @var Carbon $start */ | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $start        = clone $this->parameters->get('start'); | 
					
						
							|  |  |  |         $searchStart  = clone $start; | 
					
						
							| 
									
										
										
										
											2023-07-01 12:18:07 +02:00
										 |  |  |         $start->subDay(); | 
					
						
							| 
									
										
										
										
											2023-07-18 06:18:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 05:14:03 +01:00
										 |  |  |         /** @var Carbon $end */ | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $end          = clone $this->parameters->get('end'); | 
					
						
							|  |  |  |         $searchEnd    = clone $end; | 
					
						
							| 
									
										
										
										
											2024-11-02 05:14:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // move the search dates to the start of the day.
 | 
					
						
							|  |  |  |         $searchStart->startOfDay(); | 
					
						
							|  |  |  |         $searchEnd->endOfDay(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         app('log')->debug(sprintf('Parameters are start: %s end: %s', $start->format('Y-m-d'), $end->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2023-10-06 18:21:49 +02:00
										 |  |  |         app('log')->debug(sprintf('Search parameters are: start: %s', $searchStart->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2020-01-09 19:59:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         // Get from database when bill was paid.
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $set          = $this->repository->getPaidDatesInRange($bill, $searchStart, $searchEnd); | 
					
						
							| 
									
										
										
										
											2023-10-12 19:15:56 +02:00
										 |  |  |         app('log')->debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count())); | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         // Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date.
 | 
					
						
							| 
									
										
										
										
											2023-10-12 19:15:56 +02:00
										 |  |  |         app('log')->debug(sprintf('Grab last paid date from function, return %s if it comes up with nothing.', $start->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2023-07-01 12:18:07 +02:00
										 |  |  |         $lastPaidDate = $this->lastPaidDate($set, $start); | 
					
						
							| 
									
										
										
										
											2023-10-12 19:15:56 +02:00
										 |  |  |         app('log')->debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2020-01-09 19:59:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         // At this point the "next match" is exactly after the last time the bill was paid.
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |         $result       = []; | 
					
						
							| 
									
										
										
										
											2019-08-21 04:59:35 +02:00
										 |  |  |         foreach ($set as $entry) { | 
					
						
							|  |  |  |             $result[] = [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 'transaction_group_id'   => (string) $entry->transaction_group_id, | 
					
						
							|  |  |  |                 'transaction_journal_id' => (string) $entry->id, | 
					
						
							| 
									
										
										
										
											2023-10-28 15:03:33 +02:00
										 |  |  |                 'date'                   => $entry->date->format('Y-m-d'), | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |                 'date_object'            => $entry->date, | 
					
						
							| 
									
										
										
										
											2019-08-21 04:59:35 +02:00
										 |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2018-02-06 07:49:56 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Returns the latest date in the set, or start when set is empty. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function lastPaidDate(Collection $dates, Carbon $default): Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (0 === $dates->count()) { | 
					
						
							|  |  |  |             return $default; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $latest = $dates->first()->date; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         foreach ($dates as $journal) { | 
					
						
							|  |  |  |             if ($journal->date->gte($latest)) { | 
					
						
							|  |  |  |                 $latest = $journal->date; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $latest; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:36:37 +01:00
										 |  |  |     private function getLastPaidDate(array $paidData): ?Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         app('log')->debug('getLastPaidDate()'); | 
					
						
							|  |  |  |         $return = null; | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |         foreach ($paidData as $entry) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:36:37 +01:00
										 |  |  |             if (null !== $return) { | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |                 /** @var Carbon $current */ | 
					
						
							|  |  |  |                 $current = $entry['date_object']; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:36:37 +01:00
										 |  |  |                 if ($current->gt($return)) { | 
					
						
							|  |  |  |                     $return = clone $current; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 app('log')->debug(sprintf('Last paid date is: %s', $return->format('Y-m-d'))); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null === $return) { | 
					
						
							| 
									
										
										
										
											2023-11-26 07:19:57 +01:00
										 |  |  |                 /** @var Carbon $return */ | 
					
						
							|  |  |  |                 $return = $entry['date_object']; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:36:37 +01:00
										 |  |  |                 app('log')->debug(sprintf('Last paid date is: %s', $return->format('Y-m-d'))); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         app('log')->debug(sprintf('Last paid date is: "%s"', $return?->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:36:37 +01:00
										 |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |