akaunting/app/Console/Commands/InvoiceReminder.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Console\Commands;
2018-06-10 02:48:51 +03:00
use App\Models\Common\Company;
2019-12-31 15:49:09 +03:00
use App\Models\Sale\Invoice;
use App\Notifications\Sale\Invoice as Notification;
2018-02-20 18:24:17 +03:00
use App\Utilities\Overrider;
use Date;
2017-09-14 22:21:00 +03:00
use Illuminate\Console\Command;
class InvoiceReminder extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reminder:invoice';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send reminders for invoices';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
2017-09-14 22:21:00 +03:00
// Get all companies
2019-11-16 10:21:14 +03:00
$companies = Company::enabled()->cursor();
2017-09-14 22:21:00 +03:00
foreach ($companies as $company) {
2019-11-16 10:21:14 +03:00
$this->info('Sending invoice reminders for ' . $company->name . ' company.');
2018-02-20 18:24:17 +03:00
// Set company id
session(['company_id' => $company->id]);
// Override settings and currencies
Overrider::load('settings');
Overrider::load('currencies');
2018-06-03 00:30:08 +03:00
// Don't send reminders if disabled
2019-11-16 10:21:14 +03:00
if (!setting('schedule.send_invoice_reminder')) {
$this->info('Invoice reminders disabled by ' . $company->name . '.');
2018-06-03 00:30:08 +03:00
continue;
}
2019-11-16 10:21:14 +03:00
$days = explode(',', setting('schedule.invoice_days'));
2017-09-14 22:21:00 +03:00
foreach ($days as $day) {
2017-12-05 18:37:51 +03:00
$day = (int) trim($day);
$this->remind($day, $company);
2017-09-14 22:21:00 +03:00
}
}
2018-02-20 18:24:17 +03:00
// Unset company_id
session()->forget('company_id');
2019-11-16 10:21:14 +03:00
setting()->forgetAll();
2017-09-14 22:21:00 +03:00
}
protected function remind($day, $company)
{
// Get due date
$date = Date::today()->subDays($day)->toDateString();
2019-11-16 10:21:14 +03:00
// Get upcoming invoices
$invoices = Invoice::with('contact')->accrued()->notPaid()->due($date)->cursor();
2017-09-14 22:21:00 +03:00
foreach ($invoices as $invoice) {
// Notify the customer
2019-11-16 10:21:14 +03:00
if ($invoice->contact && !empty($invoice->contact_email)) {
$invoice->contact->notify(new Notification($invoice, 'invoice_remind_customer'));
2017-12-07 12:40:58 +03:00
}
2017-09-14 22:21:00 +03:00
// Notify all users assigned to this company
foreach ($company->users as $user) {
if (!$user->can('read-notifications')) {
continue;
}
2019-11-16 10:21:14 +03:00
$user->notify(new Notification($invoice, 'invoice_remind_admin'));
2017-09-14 22:21:00 +03:00
}
}
}
}