1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 00:53:10 +01:00
invoiceninja/app/Jobs/Cron/SubscriptionCron.php

94 lines
2.1 KiB
PHP
Raw Normal View History

2021-03-05 11:18:28 +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)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Cron;
use App\Libraries\MultiDB;
2021-04-07 05:35:16 +02:00
use App\Models\Invoice;
2021-04-07 10:06:50 +02:00
use App\Utils\Traits\SubscriptionHooker;
2021-03-05 11:18:28 +01:00
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Carbon;
class SubscriptionCron
2021-03-05 11:18:28 +01:00
{
use Dispatchable;
2021-04-07 10:06:50 +02:00
use SubscriptionHooker;
2021-03-05 11:18:28 +01:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : void
{
if (! config('ninja.db.multi_db_enabled')) {
2021-04-07 10:06:50 +02:00
2021-03-06 07:19:57 +01:00
$this->loopSubscriptions();
2021-04-07 10:06:50 +02:00
2021-03-05 11:18:28 +01:00
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
2021-04-07 10:06:50 +02:00
2021-03-06 07:19:57 +01:00
$this->loopSubscriptions();
2021-03-05 11:18:28 +01:00
}
}
2021-04-07 13:05:09 +02:00
2021-03-05 11:18:28 +01:00
}
2021-03-06 01:47:05 +01:00
2021-03-06 07:19:57 +01:00
private function loopSubscriptions()
{
2021-04-07 05:35:16 +02:00
$invoices = Invoice::where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0)
2021-04-07 10:06:50 +02:00
->whereDate('due_date', '<=', now()->addDay()->startOfDay())
2021-04-07 05:35:16 +02:00
->whereNotNull('subscription_id')
->cursor();
$invoices->each(function ($invoice){
$subscription = $invoice->subscription;
2021-04-07 10:06:50 +02:00
$body = [
'context' => 'plan_expired',
'client' => $invoice->client->hashed_id,
'invoice' => $invoice->hashed_id,
'subscription' => $subscription->hashed_id,
];
$this->sendLoad($subscription, $body);
//This will send the notification daily.
//We'll need to handle this by performing some action on the invoice to either archive it or delete it?
2021-04-07 05:35:16 +02:00
});
2021-03-06 07:19:57 +01:00
}
2021-03-06 01:47:05 +01:00
2021-04-07 10:06:50 +02:00
private function handleWebhook($invoice, $subscription)
2021-03-06 01:47:05 +01:00
{
}
2021-04-07 10:06:50 +02:00
2021-03-05 11:18:28 +01:00
}