mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Fixes for recurring scheduling
This commit is contained in:
parent
038d5e0406
commit
3c4bc4a516
53
app/Console/Commands/RecurringCommand.php
Normal file
53
app/Console/Commands/RecurringCommand.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\Cron\RecurringInvoicesCron;
|
||||
use App\Models\Design;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class RecurringCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'ninja:send-recurring';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Sends the recurring invoices';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
RecurringInvoicesCron::dispatchNow();
|
||||
}
|
||||
}
|
@ -14,12 +14,15 @@ namespace App\Jobs\Cron;
|
||||
use App\Jobs\RecurringInvoice\SendRecurring;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Models\RecurringInvoice;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class RecurringInvoicesCron
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
@ -41,25 +44,34 @@ class RecurringInvoicesCron
|
||||
|
||||
if (! config('ninja.db.multi_db_enabled')) {
|
||||
|
||||
$recurring_invoices = RecurringInvoice::where('next_send_date', '<=', Carbon::now()->addMinutes(30))->cursor();
|
||||
$recurring_invoices = RecurringInvoice::whereDate('next_send_date', '=', now())->cursor();
|
||||
|
||||
Log::info(Carbon::now()->addMinutes(30).' Sending Recurring Invoices. Count = '.$recurring_invoices->count());
|
||||
Log::info(now()->format('Y-m-d') . ' Sending Recurring Invoices. Count = '.$recurring_invoices->count().' On Database # '.$db);
|
||||
|
||||
$recurring_invoices->each(function ($recurring_invoice, $key) {
|
||||
|
||||
info("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date);
|
||||
|
||||
$recurring_invoices->each(function ($recurring_invoice, $key) {
|
||||
SendRecurring::dispatch($recurring_invoice, $recurring_invoice->company->db);
|
||||
|
||||
});
|
||||
|
||||
} else {
|
||||
//multiDB environment, need to
|
||||
foreach (MultiDB::$dbs as $db) {
|
||||
|
||||
MultiDB::setDB($db);
|
||||
|
||||
$recurring_invoices = RecurringInvoice::where('next_send_date', '<=', Carbon::now()->addMinutes(30))->cursor();
|
||||
$recurring_invoices = RecurringInvoice::whereDate('next_send_date', '=', now())->cursor();
|
||||
|
||||
Log::info(Carbon::now()->addMinutes(30).' Sending Recurring Invoices. Count = '.$recurring_invoices->count().' On Database # '.$db);
|
||||
Log::info(now()->format('Y-m-d') . ' Sending Recurring Invoices. Count = '.$recurring_invoices->count().' On Database # '.$db);
|
||||
|
||||
$recurring_invoices->each(function ($recurring_invoice, $key) {
|
||||
|
||||
info("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date);
|
||||
|
||||
SendRecurring::dispatch($recurring_invoice, $recurring_invoice->company->db);
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -54,15 +54,19 @@ class SendRecurring implements ShouldQueue
|
||||
*/
|
||||
public function handle() : void
|
||||
{
|
||||
info(" in the handle ");
|
||||
|
||||
// Generate Standard Invoice
|
||||
$invoice = RecurringInvoiceToInvoiceFactory::create($this->recurring_invoice, $this->recurring_invoice->client);
|
||||
|
||||
$invoice = $invoice->service()
|
||||
->markSent()
|
||||
->applyRecurringNumber()
|
||||
->applyNumber()
|
||||
->createInvitations()
|
||||
->save();
|
||||
|
||||
info("Invoice {$invoice->number} created");
|
||||
|
||||
$invoice->invitations->each(function ($invitation) use ($invoice) {
|
||||
|
||||
$email_builder = (new InvoiceEmail())->build($invitation);
|
||||
@ -76,6 +80,7 @@ class SendRecurring implements ShouldQueue
|
||||
if($invoice->client->getSetting('auto_bill_date') == 'on_send_date' && $this->recurring_invoice->auto_bill_enabled)
|
||||
$invoice->service()->autoBill()->save();
|
||||
|
||||
info("updating recurring invoice dates");
|
||||
/* Set next date here to prevent a recurring loop forming */
|
||||
$this->recurring_invoice->next_send_date = $this->recurring_invoice->nextSendDate()->format('Y-m-d');
|
||||
$this->recurring_invoice->remaining_cycles = $this->recurring_invoice->remainingCycles();
|
||||
@ -85,6 +90,10 @@ class SendRecurring implements ShouldQueue
|
||||
if ($this->recurring_invoice->remaining_cycles == 0)
|
||||
$this->recurring_invoice->setCompleted();
|
||||
|
||||
info($this->recurring_invoice->next_send_date);
|
||||
info($this->recurring_invoice->remaining_cycles);
|
||||
info($this->recurring_invoice->last_sent_date);
|
||||
|
||||
$this->recurring_invoice->save();
|
||||
|
||||
if ($invoice->invitations->count() > 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user