2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Events\Document\DocumentReminded;
|
2018-06-10 02:48:51 +03:00
|
|
|
use App\Models\Common\Company;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Models\Document\Document;
|
2020-12-25 22:30:59 +03:00
|
|
|
use App\Notifications\Purchase\Bill as Notification;
|
2021-06-08 23:33:17 +03:00
|
|
|
use App\Utilities\Date;
|
2017-09-14 22:21:00 +03:00
|
|
|
use Illuminate\Console\Command;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2020-03-13 12:44:05 +03:00
|
|
|
// Disable model cache
|
|
|
|
config(['laravel-model-caching.enabled' => false]);
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$today = Date::today();
|
|
|
|
|
|
|
|
$start_date = $today->copy()->subWeek()->toDateString() . ' 00:00:00';
|
|
|
|
$end_date = $today->copy()->addMonth()->toDateString() . ' 23:59:59';
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
// Get all companies
|
2022-06-01 10:15:55 +03:00
|
|
|
$companies = Company::whereHas('bills', function (Builder $query) use ($start_date, $end_date) {
|
|
|
|
$query->allCompanies();
|
|
|
|
$query->whereBetween('due_at', [$start_date, $end_date]);
|
|
|
|
$query->accrued();
|
|
|
|
$query->notPaid();
|
|
|
|
})
|
|
|
|
->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 bill reminders for ' . $company->name . ' company.');
|
|
|
|
|
2021-04-16 00:59:43 +03:00
|
|
|
// Set company
|
|
|
|
$company->makeCurrent();
|
2018-02-20 18:24:17 +03:00
|
|
|
|
2018-06-03 00:30:08 +03:00
|
|
|
// Don't send reminders if disabled
|
2022-06-01 10:15:55 +03:00
|
|
|
if (! setting('schedule.send_bill_reminder')) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$this->info('Bill 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.bill_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);
|
|
|
|
|
2020-04-15 11:27:58 +03:00
|
|
|
$this->remind($day);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-20 18:24:17 +03:00
|
|
|
|
2021-04-16 00:59:43 +03:00
|
|
|
Company::forgetCurrent();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:27:58 +03:00
|
|
|
protected function remind($day)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
|
|
|
// Get due date
|
|
|
|
$date = Date::today()->addDays($day)->toDateString();
|
|
|
|
|
|
|
|
// Get upcoming bills
|
2022-06-01 10:15:55 +03:00
|
|
|
$bills = Document::with('contact')->bill()->accrued()->notPaid()->due($date)->cursor();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
foreach ($bills as $bill) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->info($bill->document_number . ' bill reminded.');
|
|
|
|
|
2020-07-10 13:37:48 +03:00
|
|
|
try {
|
2020-12-25 22:30:59 +03:00
|
|
|
event(new DocumentReminded($bill, Notification::class));
|
2021-06-08 23:33:17 +03:00
|
|
|
} catch (\Throwable $e) {
|
2020-07-10 13:37:48 +03:00
|
|
|
$this->error($e->getMessage());
|
|
|
|
|
2021-06-08 23:33:17 +03:00
|
|
|
report($e);
|
2020-07-10 13:37:48 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|