2021-08-24 13:24:03 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-08-24 13:24:03 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Cron;
|
|
|
|
|
2021-08-24 13:57:51 +02:00
|
|
|
use App\Factory\RecurringExpenseToExpenseFactory;
|
2021-08-24 13:24:03 +02:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\RecurringExpense;
|
2021-09-17 11:32:44 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2021-08-24 13:57:51 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2021-08-24 13:24:03 +02:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
class RecurringExpensesCron
|
|
|
|
{
|
|
|
|
use Dispatchable;
|
2021-08-24 13:57:51 +02:00
|
|
|
use GeneratesCounter;
|
2021-08-24 13:24:03 +02:00
|
|
|
|
|
|
|
public $tries = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-08-24 13:24:03 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-03-17 07:01:45 +01:00
|
|
|
public function handle(): void
|
2021-08-24 13:24:03 +02:00
|
|
|
{
|
|
|
|
/* Get all expenses where the send date is less than NOW + 30 minutes() */
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('Sending recurring expenses '.Carbon::now()->format('Y-m-d h:i:s'));
|
2021-08-24 13:24:03 +02:00
|
|
|
|
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2022-11-26 04:38:09 +01:00
|
|
|
$recurring_expenses = RecurringExpense::where('next_send_date', '<=', now()->toDateTimeString())
|
|
|
|
->whereNotNull('next_send_date')
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
|
|
|
|
->where('remaining_cycles', '!=', '0')
|
|
|
|
->whereHas('company', function ($query) {
|
|
|
|
$query->where('is_disabled', 0);
|
|
|
|
})
|
|
|
|
->with('company')
|
|
|
|
->cursor();
|
|
|
|
|
|
|
|
nlog(now()->format('Y-m-d').' Generating Recurring Expenses. Count = '.$recurring_expenses->count());
|
|
|
|
|
|
|
|
$recurring_expenses->each(function ($recurring_expense, $key) {
|
|
|
|
nlog('Current date = '.now()->format('Y-m-d').' Recurring date = '.$recurring_expense->next_send_date);
|
|
|
|
|
|
|
|
if (! $recurring_expense->company->is_disabled) {
|
|
|
|
$this->generateExpense($recurring_expense);
|
|
|
|
}
|
|
|
|
});
|
2021-08-24 13:24:03 +02:00
|
|
|
} else {
|
|
|
|
//multiDB environment, need to
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
|
|
|
MultiDB::setDB($db);
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$recurring_expenses = RecurringExpense::where('next_send_date', '<=', now()->toDateTimeString())
|
|
|
|
->whereNotNull('next_send_date')
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
|
|
|
|
->where('remaining_cycles', '!=', '0')
|
|
|
|
->whereHas('company', function ($query) {
|
|
|
|
$query->where('is_disabled', 0);
|
|
|
|
})
|
|
|
|
->with('company')
|
|
|
|
->cursor();
|
|
|
|
|
|
|
|
nlog(now()->format('Y-m-d').' Generating Recurring Expenses. Count = '.$recurring_expenses->count());
|
|
|
|
|
|
|
|
$recurring_expenses->each(function ($recurring_expense, $key) {
|
|
|
|
nlog('Current date = '.now()->format('Y-m-d').' Recurring date = '.$recurring_expense->next_send_date);
|
|
|
|
|
|
|
|
if (! $recurring_expense->company->is_disabled) {
|
|
|
|
$this->generateExpense($recurring_expense);
|
|
|
|
}
|
|
|
|
});
|
2021-08-24 13:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getRecurringExpenses()
|
|
|
|
{
|
2022-11-26 04:38:09 +01:00
|
|
|
//extracting this back to the if/else block to test duplicate crons
|
2021-08-24 13:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function generateExpense(RecurringExpense $recurring_expense)
|
|
|
|
{
|
2021-08-24 13:57:51 +02:00
|
|
|
$expense = RecurringExpenseToExpenseFactory::create($recurring_expense);
|
|
|
|
$expense->save();
|
2021-08-24 13:24:03 +02:00
|
|
|
|
2021-08-24 13:57:51 +02:00
|
|
|
$expense->number = $this->getNextExpenseNumber($expense);
|
|
|
|
$expense->save();
|
2021-09-17 11:32:44 +02:00
|
|
|
|
|
|
|
$recurring_expense->next_send_date = $recurring_expense->nextSendDate();
|
2022-06-02 05:49:29 +02:00
|
|
|
$recurring_expense->next_send_date_client = $recurring_expense->next_send_date;
|
|
|
|
|
2021-09-17 11:32:44 +02:00
|
|
|
$recurring_expense->remaining_cycles = $recurring_expense->remainingCycles();
|
|
|
|
$recurring_expense->save();
|
2021-08-24 13:24:03 +02:00
|
|
|
}
|
|
|
|
}
|