mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 05:04:34 -04:00
Implement next_run calculations
This commit is contained in:
parent
4fb80f98dd
commit
5caba1d2c7
@ -33,7 +33,8 @@ class StoreSchedulerRequest extends Request
|
|||||||
'name' => ['bail', 'required', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)],
|
'name' => ['bail', 'required', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)],
|
||||||
'is_paused' => 'bail|sometimes|boolean',
|
'is_paused' => 'bail|sometimes|boolean',
|
||||||
'frequency_id' => 'bail|required|integer|digits_between:1,12',
|
'frequency_id' => 'bail|required|integer|digits_between:1,12',
|
||||||
'next_run' => 'bail|required|date:Y-m-d',
|
'next_run' => 'bail|required|date:Y-m-d|after_or_equal:today',
|
||||||
|
'next_run_client' => 'bail|sometimes|date:Y-m-d',
|
||||||
'template' => 'bail|required|string',
|
'template' => 'bail|required|string',
|
||||||
'parameters' => 'bail|array',
|
'parameters' => 'bail|array',
|
||||||
];
|
];
|
||||||
@ -41,4 +42,18 @@ class StoreSchedulerRequest extends Request
|
|||||||
return $rules;
|
return $rules;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function prepareForValidation()
|
||||||
|
{
|
||||||
|
|
||||||
|
$input = $this->all();
|
||||||
|
|
||||||
|
if (array_key_exists('next_run', $input) && is_string($input['next_run']))
|
||||||
|
$this->merge(['next_run_client' => $input['next_run']]);
|
||||||
|
|
||||||
|
return $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,8 @@ class UpdateSchedulerRequest extends Request
|
|||||||
'name' => ['bail', 'sometimes', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)->ignore($this->task_scheduler->id)],
|
'name' => ['bail', 'sometimes', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)->ignore($this->task_scheduler->id)],
|
||||||
'is_paused' => 'bail|sometimes|boolean',
|
'is_paused' => 'bail|sometimes|boolean',
|
||||||
'frequency_id' => 'bail|required|integer|digits_between:1,12',
|
'frequency_id' => 'bail|required|integer|digits_between:1,12',
|
||||||
'next_run' => 'bail|required|date:Y-m-d',
|
'next_run' => 'bail|required|date:Y-m-d|after_or_equal:today',
|
||||||
|
'next_run_client' => 'bail|sometimes|date:Y-m-d',
|
||||||
'template' => 'bail|required|string',
|
'template' => 'bail|required|string',
|
||||||
'parameters' => 'bail|array',
|
'parameters' => 'bail|array',
|
||||||
];
|
];
|
||||||
@ -40,4 +41,17 @@ class UpdateSchedulerRequest extends Request
|
|||||||
return $rules;
|
return $rules;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function prepareForValidation()
|
||||||
|
{
|
||||||
|
|
||||||
|
$input = $this->all();
|
||||||
|
|
||||||
|
if (array_key_exists('next_run', $input) && is_string($input['next_run']))
|
||||||
|
$this->merge(['next_run_client' => $input['next_run']]);
|
||||||
|
|
||||||
|
return $input;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -666,4 +666,23 @@ class Company extends BaseModel
|
|||||||
return $item->id == $this->getSetting('date_format_id');
|
return $item->id == $this->getSetting('date_format_id');
|
||||||
})->first()->format;
|
})->first()->format;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function timezone_offset()
|
||||||
|
{
|
||||||
|
$offset = 0;
|
||||||
|
|
||||||
|
$entity_send_time = $this->getSetting('entity_send_time');
|
||||||
|
|
||||||
|
if ($entity_send_time == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$timezone = $this->timezone();
|
||||||
|
|
||||||
|
$offset -= $timezone->utc_offset;
|
||||||
|
$offset += ($entity_send_time * 3600);
|
||||||
|
|
||||||
|
return $offset;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -257,13 +257,6 @@ class RecurringInvoice extends BaseModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
As we are firing at UTC+0 if our offset is negative it is technically firing the day before so we always need
|
|
||||||
to add ON a day - a day = 86400 seconds
|
|
||||||
*/
|
|
||||||
// if($offset < 0)
|
|
||||||
// $offset += 86400;
|
|
||||||
|
|
||||||
switch ($this->frequency_id) {
|
switch ($this->frequency_id) {
|
||||||
case self::FREQUENCY_DAILY:
|
case self::FREQUENCY_DAILY:
|
||||||
return Carbon::parse($this->next_send_date_client)->startOfDay()->addDay()->addSeconds($offset);
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addDay()->addSeconds($offset);
|
||||||
|
@ -38,7 +38,7 @@ class Scheduler extends BaseModel
|
|||||||
'name',
|
'name',
|
||||||
'frequency_id',
|
'frequency_id',
|
||||||
'next_run',
|
'next_run',
|
||||||
'scheduled_run',
|
'next_run_client',
|
||||||
'template',
|
'template',
|
||||||
'is_paused',
|
'is_paused',
|
||||||
'parameters',
|
'parameters',
|
||||||
@ -46,6 +46,7 @@ class Scheduler extends BaseModel
|
|||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'next_run' => 'datetime',
|
'next_run' => 'datetime',
|
||||||
|
'next_run_client' => 'datetime',
|
||||||
'created_at' => 'timestamp',
|
'created_at' => 'timestamp',
|
||||||
'updated_at' => 'timestamp',
|
'updated_at' => 'timestamp',
|
||||||
'deleted_at' => 'timestamp',
|
'deleted_at' => 'timestamp',
|
||||||
|
@ -27,6 +27,8 @@ class SchedulerRepository extends BaseRepository
|
|||||||
public function save(array $data, Scheduler $scheduler): Scheduler
|
public function save(array $data, Scheduler $scheduler): Scheduler
|
||||||
{
|
{
|
||||||
|
|
||||||
|
nlog($data);
|
||||||
|
|
||||||
$scheduler->fill($data);
|
$scheduler->fill($data);
|
||||||
|
|
||||||
$scheduler->save();
|
$scheduler->save();
|
||||||
|
@ -27,6 +27,7 @@ use App\Utils\Number;
|
|||||||
use App\Utils\PhantomJS\Phantom;
|
use App\Utils\PhantomJS\Phantom;
|
||||||
use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait;
|
use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class Statement
|
class Statement
|
||||||
{
|
{
|
||||||
@ -117,7 +118,7 @@ class Statement
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (\is_null($this->entity)) {
|
if (\is_null($this->entity)) {
|
||||||
\DB::connection(config('database.default'))->beginTransaction();
|
DB::connection(config('database.default'))->beginTransaction();
|
||||||
|
|
||||||
$this->rollback = true;
|
$this->rollback = true;
|
||||||
|
|
||||||
|
@ -12,9 +12,11 @@
|
|||||||
namespace App\Services\Scheduler;
|
namespace App\Services\Scheduler;
|
||||||
|
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
|
use App\Models\RecurringInvoice;
|
||||||
use App\Models\Scheduler;
|
use App\Models\Scheduler;
|
||||||
use App\Utils\Traits\MakesDates;
|
use App\Utils\Traits\MakesDates;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
class SchedulerService
|
class SchedulerService
|
||||||
{
|
{
|
||||||
@ -56,10 +58,11 @@ class SchedulerService
|
|||||||
//work out the date range
|
//work out the date range
|
||||||
$pdf = $_client->service()->statement($statement_properties,true);
|
$pdf = $_client->service()->statement($statement_properties,true);
|
||||||
|
|
||||||
//calculate next run dates;
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//calculate next run dates;
|
||||||
|
$this->calculateNextRun();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateStatementProperties()
|
private function calculateStatementProperties()
|
||||||
@ -91,6 +94,63 @@ class SchedulerService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function calculateNextRun() :?Carbon
|
||||||
|
{
|
||||||
|
if (! $this->scheduler->next_run) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$offset = $this->scheduler->company->timezone_offset();
|
||||||
|
|
||||||
|
switch ($this->scheduler->frequency_id) {
|
||||||
|
case RecurringInvoice::FREQUENCY_DAILY:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addDay();
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_WEEKLY:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addWeek();
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addWeeks(2);
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addWeeks(4);
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_MONTHLY:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthNoOverflow();
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(2);
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(3);
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(4);
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(6);
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addYear();
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addYears(2);
|
||||||
|
break;
|
||||||
|
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
||||||
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addYears(3);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$next_run = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$this->scheduler->next_run_client = $next_run ?: null;
|
||||||
|
$this->scheduler->next_run = $next_run ? $next_run->copy()->addSeconds($offset) : null;
|
||||||
|
$this->scheduler->save();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//handle when the scheduler has been paused.
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -24,7 +24,7 @@ class SchedulerTransformer extends EntityTransformer
|
|||||||
'id' => $this->encodePrimaryKey($scheduler->id),
|
'id' => $this->encodePrimaryKey($scheduler->id),
|
||||||
'name' => (string) $scheduler->name,
|
'name' => (string) $scheduler->name,
|
||||||
'frequency_id' => (string) $scheduler->frequency_id,
|
'frequency_id' => (string) $scheduler->frequency_id,
|
||||||
'next_run' => $scheduler->next_run,
|
'next_run' => $scheduler->next_run_client->format('Y-m-d'),
|
||||||
'template' => (string) $scheduler->template,
|
'template' => (string) $scheduler->template,
|
||||||
'is_paused' => (bool) $scheduler->is_paused,
|
'is_paused' => (bool) $scheduler->is_paused,
|
||||||
'is_deleted' => (bool) $scheduler->is_deleted,
|
'is_deleted' => (bool) $scheduler->is_deleted,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user