Akaunting/app/Console/Commands/BillReminder.php

90 lines
2.2 KiB
PHP
Raw Normal View History

2017-09-14 19:21:00 +00:00
<?php
namespace App\Console\Commands;
2020-12-23 22:28:38 +00:00
use App\Events\Document\DocumentReminded;
2018-06-09 23:48:51 +00:00
use App\Models\Common\Company;
2020-12-23 22:28:38 +00:00
use App\Models\Document\Document;
use App\Notifications\Purchase\Bill as Notification;
2021-06-08 20:33:17 +00:00
use App\Utilities\Date;
2017-09-14 19:21:00 +00:00
use Illuminate\Console\Command;
class BillReminder extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reminder:bill';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send reminders for bills';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
2017-09-14 19:21:00 +00:00
// Get all companies
2021-10-06 14:27:30 +00:00
$companies = Company::enabled()->with('bills')->cursor();
2017-09-14 19:21:00 +00:00
foreach ($companies as $company) {
// Has company bills
2021-10-06 14:27:30 +00:00
if (!$company->bills->count()) {
continue;
}
2019-11-16 07:21:14 +00:00
$this->info('Sending bill reminders for ' . $company->name . ' company.');
2021-04-15 21:59:43 +00:00
// Set company
$company->makeCurrent();
2018-02-20 15:24:17 +00:00
2018-06-02 21:30:08 +00:00
// Don't send reminders if disabled
2019-11-16 07:21:14 +00:00
if (!setting('schedule.send_bill_reminder')) {
$this->info('Bill reminders disabled by ' . $company->name . '.');
2018-06-02 21:30:08 +00:00
continue;
}
2019-11-16 07:21:14 +00:00
$days = explode(',', setting('schedule.bill_days'));
2017-09-14 19:21:00 +00:00
foreach ($days as $day) {
2017-12-05 15:37:51 +00:00
$day = (int) trim($day);
$this->remind($day);
2017-09-14 19:21:00 +00:00
}
}
2018-02-20 15:24:17 +00:00
2021-04-15 21:59:43 +00:00
Company::forgetCurrent();
2017-09-14 19:21:00 +00:00
}
protected function remind($day)
2017-09-14 19:21:00 +00:00
{
// Get due date
$date = Date::today()->addDays($day)->toDateString();
// Get upcoming bills
2020-12-23 22:28:38 +00:00
$bills = Document::bill()->with('contact')->accrued()->notPaid()->due($date)->cursor();
2017-09-14 19:21:00 +00:00
foreach ($bills as $bill) {
2020-07-10 10:37:48 +00:00
try {
event(new DocumentReminded($bill, Notification::class));
2021-06-08 20:33:17 +00:00
} catch (\Throwable $e) {
2020-07-10 10:37:48 +00:00
$this->error($e->getMessage());
2021-06-08 20:33:17 +00:00
report($e);
2020-07-10 10:37:48 +00:00
}
2017-09-14 19:21:00 +00:00
}
}
}