| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 14:55:51 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * WarnAboutBills.php | 
					
						
							|  |  |  |  * Copyright (c) 2022 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | namespace FireflyIII\Jobs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  | use FireflyIII\Events\Model\Bill\WarnUserAboutBill; | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  | use FireflyIII\Events\Model\Bill\WarnUserAboutOverdueSubscriptions; | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | use FireflyIII\Models\Bill; | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  | use FireflyIII\Support\Facades\Navigation; | 
					
						
							|  |  |  | use FireflyIII\Support\JsonApi\Enrichments\SubscriptionEnrichment; | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | use Illuminate\Bus\Queueable; | 
					
						
							|  |  |  | use Illuminate\Contracts\Queue\ShouldQueue; | 
					
						
							|  |  |  | use Illuminate\Foundation\Bus\Dispatchable; | 
					
						
							|  |  |  | use Illuminate\Queue\InteractsWithQueue; | 
					
						
							|  |  |  | use Illuminate\Queue\SerializesModels; | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class WarnAboutBills | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class WarnAboutBills implements ShouldQueue | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |     use Dispatchable; | 
					
						
							|  |  |  |     use InteractsWithQueue; | 
					
						
							|  |  |  |     use Queueable; | 
					
						
							|  |  |  |     use SerializesModels; | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private Carbon $date; | 
					
						
							|  |  |  |     private bool   $force; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new job instance. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(?Carbon $date) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |         $newDate     = new Carbon(); | 
					
						
							| 
									
										
										
										
											2022-09-23 06:05:22 +02:00
										 |  |  |         $newDate->startOfDay(); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |         $this->date  = $newDate; | 
					
						
							| 
									
										
										
										
											2022-09-23 06:05:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if ($date instanceof Carbon) { | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |             $newDate    = clone $date; | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |             $newDate->startOfDay(); | 
					
						
							|  |  |  |             $this->date = $newDate; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-23 06:05:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         $this->force = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         Log::debug(sprintf('Created new WarnAboutBills("%s")', $this->date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Execute the job. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function handle(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         Log::debug(sprintf('Now at start of WarnAboutBills() job for %s.', $this->date->format('D d M Y'))); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |         foreach (User::all() as $user) { | 
					
						
							|  |  |  |             $bills   = $user->bills()->where('active', true)->get(); | 
					
						
							|  |  |  |             $overdue = []; | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |             /** @var Bill $bill */ | 
					
						
							|  |  |  |             foreach ($bills as $bill) { | 
					
						
							|  |  |  |                 Log::debug(sprintf('Now checking bill #%d ("%s")', $bill->id, $bill->name)); | 
					
						
							|  |  |  |                 $dates = $this->getDates($bill); | 
					
						
							|  |  |  |                 if ($this->needsOverdueAlert($dates)) { | 
					
						
							|  |  |  |                     $overdue[] = ['bill' => $bill, 'dates' => $dates]; | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |                 if ($this->hasDateFields($bill)) { | 
					
						
							|  |  |  |                     if ($this->needsWarning($bill, 'end_date')) { | 
					
						
							|  |  |  |                         $this->sendWarning($bill, 'end_date'); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     if ($this->needsWarning($bill, 'extension_date')) { | 
					
						
							|  |  |  |                         $this->sendWarning($bill, 'extension_date'); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |             $this->sendOverdueAlerts($user, $overdue); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         Log::debug('Done with handle()'); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function hasDateFields(Bill $bill): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (false === $bill->active) { | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |             Log::debug('Bill is not active.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null === $bill->end_date && null === $bill->extension_date) { | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |             Log::debug('Bill has no date fields.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function needsWarning(Bill $bill, string $field): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (null === $bill->{$field}) { | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $diff = $this->getDiff($bill, $field); | 
					
						
							|  |  |  |         $list = config('firefly.bill_reminder_periods'); | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         Log::debug(sprintf('Difference in days for field "%s" ("%s") is %d day(s)', $field, $bill->{$field}->format('Y-m-d'), $diff)); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         if (in_array($diff, $list, true)) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function getDiff(Bill $bill, string $field): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $today  = clone $this->date; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         $carbon = clone $bill->{$field}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         return (int)$today->diffInDays($carbon); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |     private function sendWarning(Bill $bill, string $field): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $diff = $this->getDiff($bill, $field); | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         Log::debug('Will now send warning!'); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:23:46 +02:00
										 |  |  |         event(new WarnUserAboutBill($bill, $field, $diff)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function setDate(Carbon $date): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |         $newDate    = clone $date; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $newDate->startOfDay(); | 
					
						
							|  |  |  |         $this->date = $newDate; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setForce(bool $force): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->force = $force; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private function getDates(Bill $bill): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $start      = clone $this->date; | 
					
						
							|  |  |  |         $start      = Navigation::startOfPeriod($start, $bill->repeat_freq); | 
					
						
							|  |  |  |         $end        = clone $start; | 
					
						
							|  |  |  |         $end        = Navigation::endOfPeriod($end, $bill->repeat_freq); | 
					
						
							|  |  |  |         $enrichment = new SubscriptionEnrichment(); | 
					
						
							|  |  |  |         $enrichment->setUser($bill->user); | 
					
						
							|  |  |  |         $enrichment->setStart($start); | 
					
						
							|  |  |  |         $enrichment->setEnd($end); | 
					
						
							| 
									
										
										
										
											2025-09-07 07:56:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         /** @var Bill $single */ | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |         $single     = $enrichment->enrichSingle($bill); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             'pay_dates'  => $single->meta['pay_dates'] ?? [], | 
					
						
							|  |  |  |             'paid_dates' => $single->meta['paid_dates'] ?? [], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function needsOverdueAlert(array $dates): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |         $count    = count($dates['pay_dates']) - count($dates['paid_dates']); | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         if (0 === $count || 0 === count($dates['pay_dates'])) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // the earliest date in the list of pay dates must be 48hrs or more ago.
 | 
					
						
							|  |  |  |         $earliest = new Carbon($dates['pay_dates'][0]); | 
					
						
							|  |  |  |         $earliest->startOfDay(); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:04:02 +02:00
										 |  |  |         Log::debug(sprintf('Earliest expected pay date is %s', $earliest->toAtomString())); | 
					
						
							| 
									
										
										
										
											2025-08-09 08:38:30 +02:00
										 |  |  |         $diff     = $earliest->diffInDays($this->date); | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         Log::debug(sprintf('Difference in days is %s', $diff)); | 
					
						
							|  |  |  |         if ($diff < 2) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-09 08:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |     private function sendOverdueAlerts(User $user, array $overdue): void | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-09 08:34:18 +02:00
										 |  |  |         if (count($overdue) > 0) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Will now send warning about overdue bill for user #%d.', $user->id)); | 
					
						
							|  |  |  |             event(new WarnUserAboutOverdueSubscriptions($user, $overdue)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-09 07:59:38 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-29 14:55:51 +02:00
										 |  |  | } |