akaunting/app/Console/Commands/ReportReminder.php
2021-06-15 10:33:36 +03:00

82 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Common\Company;
use App\Models\Common\Report;
use App\Utilities\Reports as Utility;
use App\Utilities\Overrider;
use Date;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
class ReportReminder extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reminder:report';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Calculate reminders for reports';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
// Get all companies
$companies = Company::enabled()->withCount('reports')->cursor();
foreach ($companies as $company) {
// Has company reports
if (!$company->reports_count) {
continue;
}
$this->info('Sending invoice reminders for ' . $company->name . ' company.');
// Set company
$company->makeCurrent();
$this->remind();
}
Company::forgetCurrent();
}
protected function remind()
{
$reports = Report::orderBy('name')->get();
foreach ($reports as $report) {
if (!Utility::canShow($report->class)) {
continue;
}
$class = Utility::getClassInstance($report, false);
if (empty($class)) {
continue;
}
$ttl = 3600 * 6; // 6 hours
Cache::remember('reports.totals.' . $report->id, $ttl, function () use ($class) {
return $class->getGrandTotal();
});
}
}
}