firefly-iii/app/Jobs/WarnAboutBills.php

153 lines
4.2 KiB
PHP
Raw Normal View History

2022-03-28 05:23:46 -05:00
<?php
2022-03-29 07:55:51 -05: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 05:23:46 -05:00
namespace FireflyIII\Jobs;
use Carbon\Carbon;
use FireflyIII\Events\WarnUserAboutBill;
use FireflyIII\Models\Bill;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
/**
* Class WarnAboutBills
*/
class WarnAboutBills implements ShouldQueue
{
2022-10-30 08:24:28 -05:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2022-03-28 05:23:46 -05:00
private Carbon $date;
private bool $force;
/**
* Create a new job instance.
*/
public function __construct(?Carbon $date)
{
$newDate = new Carbon();
2022-09-22 23:05:22 -05:00
$newDate->startOfDay();
$this->date = $newDate;
2022-09-22 23:05:22 -05:00
2022-03-28 05:23:46 -05:00
if (null !== $date) {
$newDate = clone $date;
2022-03-28 05:23:46 -05:00
$newDate->startOfDay();
$this->date = $newDate;
}
2022-09-22 23:05:22 -05:00
2022-03-28 05:23:46 -05:00
$this->force = false;
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Created new WarnAboutBills("%s")', $this->date->format('Y-m-d')));
2022-03-28 05:23:46 -05:00
}
/**
* Execute the job.
*/
public function handle(): void
{
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Now at start of WarnAboutBills() job for %s.', $this->date->format('D d M Y')));
2022-03-28 05:23:46 -05:00
$bills = Bill::all();
2023-12-20 12:35:52 -06:00
2022-03-28 05:23:46 -05:00
/** @var Bill $bill */
foreach ($bills as $bill) {
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Now checking bill #%d ("%s")', $bill->id, $bill->name));
2022-03-28 05:23:46 -05: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');
}
}
}
2023-10-29 00:33:43 -05:00
app('log')->debug('Done with handle()');
2022-03-28 05:23:46 -05:00
// clear cache:
app('preferences')->mark();
}
private function hasDateFields(Bill $bill): bool
{
if (false === $bill->active) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Bill is not active.');
2023-12-20 12:35:52 -06:00
2022-03-28 05:23:46 -05:00
return false;
}
if (null === $bill->end_date && null === $bill->extension_date) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Bill has no date fields.');
2023-12-20 12:35:52 -06:00
2022-03-28 05:23:46 -05:00
return false;
}
2023-12-20 12:35:52 -06:00
2022-03-28 05:23:46 -05:00
return true;
}
private function needsWarning(Bill $bill, string $field): bool
{
2023-12-20 12:35:52 -06:00
if (null === $bill->{$field}) {
2022-03-28 05:23:46 -05:00
return false;
}
$diff = $this->getDiff($bill, $field);
$list = config('firefly.bill_reminder_periods');
2023-12-20 12:35:52 -06:00
app('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 05:23:46 -05:00
if (in_array($diff, $list, true)) {
return true;
}
2023-12-20 12:35:52 -06:00
2022-03-28 05:23:46 -05:00
return false;
}
2023-06-21 05:34:58 -05:00
private function getDiff(Bill $bill, string $field): int
{
$today = clone $this->date;
2023-12-20 12:35:52 -06:00
$carbon = clone $bill->{$field};
2023-06-21 05:34:58 -05:00
return $today->diffInDays($carbon, false);
}
2022-03-28 05:23:46 -05:00
private function sendWarning(Bill $bill, string $field): void
{
$diff = $this->getDiff($bill, $field);
2023-10-29 00:33:43 -05:00
app('log')->debug('Will now send warning!');
2022-03-28 05:23:46 -05:00
event(new WarnUserAboutBill($bill, $field, $diff));
}
public function setDate(Carbon $date): void
{
$newDate = clone $date;
$newDate->startOfDay();
$this->date = $newDate;
}
public function setForce(bool $force): void
{
$this->force = $force;
}
2022-03-29 07:55:51 -05:00
}