2021-03-05 11:18:28 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-03-05 11:18:28 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-03-05 11:18:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Cron;
|
|
|
|
|
2024-04-01 03:49:22 +02:00
|
|
|
use App\Models\Company;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Invoice;
|
2024-04-01 03:49:22 +02:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
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;
|
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
class SubscriptionCron
|
2021-03-05 11:18:28 +01:00
|
|
|
{
|
|
|
|
use Dispatchable;
|
2021-04-07 10:06:50 +02:00
|
|
|
use SubscriptionHooker;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-03-05 11:18:28 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function handle(): void
|
2021-03-05 11:18:28 +01:00
|
|
|
{
|
2022-05-15 09:51:06 +02:00
|
|
|
|
2023-05-04 01:42:42 +02:00
|
|
|
Auth::logout();
|
|
|
|
|
2021-03-05 11:18:28 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2024-04-02 03:06:20 +02:00
|
|
|
|
2024-06-14 09:09:44 +02:00
|
|
|
nlog('Subscription Cron '. now()->toDateTimeString());
|
|
|
|
|
|
|
|
$this->timezoneAware();
|
2024-04-02 03:06:20 +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
|
|
|
|
2024-04-02 03:06:20 +02:00
|
|
|
nlog('Subscription Cron for ' . $db . ' ' . now()->toDateTimeString());
|
|
|
|
|
|
|
|
$this->timezoneAware();
|
|
|
|
|
2021-03-05 11:18:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-01 03:49:22 +02:00
|
|
|
|
|
|
|
//Requires the crons to be updated and set to hourly @ 00:01
|
|
|
|
private function timezoneAware()
|
|
|
|
{
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-06-17 02:14:04 +02:00
|
|
|
Invoice::query()
|
|
|
|
->with('company')
|
2024-04-01 03:49:22 +02:00
|
|
|
->where('is_deleted', 0)
|
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
|
|
|
->where('balance', '>', 0)
|
|
|
|
->where('is_proforma', 0)
|
|
|
|
->whereDate('due_date', '<=', now()->addDay()->startOfDay())
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->whereNotNull('subscription_id')
|
|
|
|
->groupBy('company_id')
|
|
|
|
->cursor()
|
2024-06-17 02:14:04 +02:00
|
|
|
->each(function ($invoice) {
|
2024-04-01 03:49:22 +02:00
|
|
|
|
2024-06-06 04:31:24 +02:00
|
|
|
/** @var \App\Models\Company $company */
|
2024-06-17 02:14:04 +02:00
|
|
|
// $company = Company::find($invoice->company_id);
|
|
|
|
$company = $invoice->company;
|
2024-04-01 03:49:22 +02:00
|
|
|
|
2024-06-03 08:34:57 +02:00
|
|
|
$timezone_now = now()->setTimezone($company->timezone()->name ?? 'Pacific/Midway');
|
2024-04-01 03:49:22 +02:00
|
|
|
|
|
|
|
//Capture companies within the window of 00:00 and 00:30
|
|
|
|
if($timezone_now->gt($timezone_now->copy()->startOfDay()) && $timezone_now->lt($timezone_now->copy()->startOfDay()->addMinutes(30))) {
|
|
|
|
|
|
|
|
Invoice::query()
|
|
|
|
->where('company_id', $company->id)
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->where('is_deleted', 0)
|
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
|
|
|
->where('is_proforma', 0)
|
|
|
|
->whereNotNull('subscription_id')
|
|
|
|
->where('balance', '>', 0)
|
2024-04-02 03:06:20 +02:00
|
|
|
->whereDate('due_date', '<=', now()->addDay()->startOfDay())
|
2024-04-01 03:49:22 +02:00
|
|
|
->cursor()
|
|
|
|
->each(function (Invoice $invoice) {
|
|
|
|
|
|
|
|
$subscription = $invoice->subscription;
|
|
|
|
|
|
|
|
$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?
|
2024-06-14 09:09:44 +02:00
|
|
|
});
|
2024-04-01 03:49:22 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2021-03-05 11:18:28 +01:00
|
|
|
}
|