1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Console/Commands/SendRemindersCron.php

108 lines
2.6 KiB
PHP
Raw Normal View History

2020-11-04 09:43:20 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-11-04 09:43:20 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-11-04 09:43:20 +01:00
*/
namespace App\Console\Commands;
use App\Jobs\Ninja\SendReminders;
2021-02-10 03:26:05 +01:00
use App\Libraries\MultiDB;
2020-11-11 01:13:39 +01:00
use App\Models\Invoice;
use App\Models\Quote;
use App\Models\Webhook;
2020-11-04 09:43:20 +01:00
use Illuminate\Console\Command;
2021-05-31 07:27:26 +02:00
use App\Jobs\Util\WebhookHandler;
2020-11-04 09:43:20 +01:00
class SendRemindersCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ninja:send-reminders';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Force send all reminders';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
SendReminders::dispatchNow();
2020-11-11 01:13:39 +01:00
2021-05-31 07:27:26 +02:00
$this->webHookOverdueInvoices();
$this->webHookExpiredQuotes();
2020-11-11 01:13:39 +01:00
}
private function webHookOverdueInvoices()
2021-02-10 03:26:05 +01:00
{
if (! config('ninja.db.multi_db_enabled')) {
$this->executeWebhooks();
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
$this->executeWebhooks();
}
}
}
2021-03-31 23:28:51 +02:00
private function webHookExpiredQuotes()
{
}
2021-02-10 03:26:05 +01:00
private function executeWebhooks()
2020-11-11 01:13:39 +01:00
{
$invoices = Invoice::where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0)
2021-01-14 00:00:32 +01:00
->whereDate('due_date', '<=', now()->subDays(1)->startOfDay())
2020-11-11 01:13:39 +01:00
->cursor();
2020-11-25 15:19:52 +01:00
$invoices->each(function ($invoice) {
2021-06-10 03:15:21 +02:00
WebhookHandler::dispatch(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company);
2021-05-31 07:27:26 +02:00
2020-11-11 01:13:39 +01:00
});
$quotes = Quote::where('is_deleted', 0)
->where('status_id', Quote::STATUS_SENT)
2021-01-14 00:00:32 +01:00
->whereDate('due_date', '<=', now()->subDays(1)->startOfDay())
2020-11-11 01:13:39 +01:00
->cursor();
2020-11-25 15:19:52 +01:00
$quotes->each(function ($quote) {
2021-06-10 03:15:21 +02:00
WebhookHandler::dispatch(Webhook::EVENT_EXPIRED_QUOTE, $quote, $quote->company);
2020-11-11 01:13:39 +01:00
});
2020-11-04 09:43:20 +01:00
}
2021-02-10 03:26:05 +01:00
2020-11-04 09:43:20 +01:00
}