diff --git a/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php b/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php index 0c63f95a9542..0b66428e7c0f 100644 --- a/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php +++ b/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php @@ -33,7 +33,8 @@ class StoreSchedulerRequest extends Request 'name' => ['bail', 'required', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)], 'is_paused' => 'bail|sometimes|boolean', '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', 'parameters' => 'bail|array', ]; @@ -41,4 +42,18 @@ class StoreSchedulerRequest extends Request 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; + } + + } diff --git a/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php b/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php index 7e3ec3267152..af335500c98e 100644 --- a/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php +++ b/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php @@ -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)], 'is_paused' => 'bail|sometimes|boolean', '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', 'parameters' => 'bail|array', ]; @@ -40,4 +41,17 @@ class UpdateSchedulerRequest extends Request 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; + + } + } diff --git a/app/Models/Company.php b/app/Models/Company.php index 0afac617a2ee..b612690fd4e8 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -666,4 +666,23 @@ class Company extends BaseModel return $item->id == $this->getSetting('date_format_id'); })->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; + } + } diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index a94c33d9aaed..167cd5a91ff2 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -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) { case self::FREQUENCY_DAILY: return Carbon::parse($this->next_send_date_client)->startOfDay()->addDay()->addSeconds($offset); diff --git a/app/Models/Scheduler.php b/app/Models/Scheduler.php index 50c693cee6af..bdc7eaaa497e 100644 --- a/app/Models/Scheduler.php +++ b/app/Models/Scheduler.php @@ -38,7 +38,7 @@ class Scheduler extends BaseModel 'name', 'frequency_id', 'next_run', - 'scheduled_run', + 'next_run_client', 'template', 'is_paused', 'parameters', @@ -46,6 +46,7 @@ class Scheduler extends BaseModel protected $casts = [ 'next_run' => 'datetime', + 'next_run_client' => 'datetime', 'created_at' => 'timestamp', 'updated_at' => 'timestamp', 'deleted_at' => 'timestamp', diff --git a/app/Repositories/SchedulerRepository.php b/app/Repositories/SchedulerRepository.php index 5c9b9ad4e19c..b9c1d5a49039 100644 --- a/app/Repositories/SchedulerRepository.php +++ b/app/Repositories/SchedulerRepository.php @@ -27,6 +27,8 @@ class SchedulerRepository extends BaseRepository public function save(array $data, Scheduler $scheduler): Scheduler { +nlog($data); + $scheduler->fill($data); $scheduler->save(); diff --git a/app/Services/Client/Statement.php b/app/Services/Client/Statement.php index 9b0d52e84ab9..8c7d957a1183 100644 --- a/app/Services/Client/Statement.php +++ b/app/Services/Client/Statement.php @@ -27,6 +27,7 @@ use App\Utils\Number; use App\Utils\PhantomJS\Phantom; use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\DB; class Statement { @@ -117,7 +118,7 @@ class Statement } if (\is_null($this->entity)) { - \DB::connection(config('database.default'))->beginTransaction(); + DB::connection(config('database.default'))->beginTransaction(); $this->rollback = true; diff --git a/app/Services/Scheduler/SchedulerService.php b/app/Services/Scheduler/SchedulerService.php index c149142dfeac..1c10f32fabf5 100644 --- a/app/Services/Scheduler/SchedulerService.php +++ b/app/Services/Scheduler/SchedulerService.php @@ -12,9 +12,11 @@ namespace App\Services\Scheduler; use App\Models\Client; +use App\Models\RecurringInvoice; use App\Models\Scheduler; use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesHash; +use Carbon\Carbon; class SchedulerService { @@ -56,10 +58,11 @@ class SchedulerService //work out the date range $pdf = $_client->service()->statement($statement_properties,true); - //calculate next run dates; - }); + //calculate next run dates; + $this->calculateNextRun(); + } 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. } \ No newline at end of file diff --git a/app/Transformers/SchedulerTransformer.php b/app/Transformers/SchedulerTransformer.php index 584ea4cbe9f9..3324cac4fddd 100644 --- a/app/Transformers/SchedulerTransformer.php +++ b/app/Transformers/SchedulerTransformer.php @@ -24,7 +24,7 @@ class SchedulerTransformer extends EntityTransformer 'id' => $this->encodePrimaryKey($scheduler->id), 'name' => (string) $scheduler->name, '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, 'is_paused' => (bool) $scheduler->is_paused, 'is_deleted' => (bool) $scheduler->is_deleted,