diff --git a/app/Console/Commands/RecurringCheck.php b/app/Console/Commands/RecurringCheck.php index 5e7b893d3..6d4aebbd4 100644 --- a/app/Console/Commands/RecurringCheck.php +++ b/app/Console/Commands/RecurringCheck.php @@ -71,9 +71,7 @@ class RecurringCheck extends Command $recurring = $company->recurring(); foreach ($recurring as $recur) { - $schedule = $this->schedule($recur); - - $current = Date::parse($schedule->current()->getStart()); + $current = Date::parse($recur->schedule()->current()->getStart()->format('Y-m-d')); // Check if should recur today if ($this->today->ne($current)) { @@ -183,27 +181,4 @@ class RecurringCheck extends Command $user->notify(new BillNotification($clone)); } } - - protected function schedule($recur) - { - $config = new ArrayTransformerConfig(); - $config->enableLastDayOfMonthFix(); - - $transformer = new ArrayTransformer(); - $transformer->setConfig($config); - - return $transformer->transform($this->rule($recur)); - } - - protected function rule($recur) - { - $rule = (new Rule()) - ->setStartDate($recur->started_at) - ->setTimezone(setting('general.timezone')) - ->setFreq(strtoupper($recur->frequency)) - ->setInterval($recur->interval) - ->setCount($recur->count); - - return $rule; - } } diff --git a/app/Models/Common/Recurring.php b/app/Models/Common/Recurring.php index bb308c77a..925e9bdc6 100644 --- a/app/Models/Common/Recurring.php +++ b/app/Models/Common/Recurring.php @@ -3,6 +3,11 @@ namespace App\Models\Common; use App\Models\Model; +use DateTime; +use DateTimeZone; +use Recurr\Rule; +use Recurr\Transformer\ArrayTransformer; +use Recurr\Transformer\ArrayTransformerConfig; class Recurring extends Model { @@ -24,4 +29,30 @@ class Recurring extends Model { return $this->morphTo(); } + + public function schedule() + { + $config = new ArrayTransformerConfig(); + $config->enableLastDayOfMonthFix(); + + $transformer = new ArrayTransformer(); + $transformer->setConfig($config); + + return $transformer->transform($this->rule()); + } + + public function rule() + { + // 0 means infinite + $count = ($this->count == 0) ? 999 : $this->count; + + $rule = (new Rule()) + ->setStartDate(new DateTime($this->started_at, new DateTimeZone(setting('general.timezone')))) + ->setTimezone(setting('general.timezone')) + ->setFreq(strtoupper($this->frequency)) + ->setInterval($this->interval) + ->setCount($count); + + return $rule; + } } diff --git a/resources/lang/en-GB/recurring.php b/resources/lang/en-GB/recurring.php index 52f9afada..92099c71c 100644 --- a/resources/lang/en-GB/recurring.php +++ b/resources/lang/en-GB/recurring.php @@ -15,5 +15,6 @@ return [ 'weeks' => 'Week(s)', 'months' => 'Month(s)', 'years' => 'Year(s)', + 'message' => 'This is a recurring :type and the next :type will be automatically generated at :date', ]; diff --git a/resources/views/expenses/bills/show.blade.php b/resources/views/expenses/bills/show.blade.php index a2e028f00..c0ee0e328 100644 --- a/resources/views/expenses/bills/show.blade.php +++ b/resources/views/expenses/bills/show.blade.php @@ -3,6 +3,18 @@ @section('title', trans_choice('general.bills', 1) . ': ' . $bill->bill_number) @section('content') + @if ($bill->recurring()->count()) +
+

{{ trans('recurring.recurring') }}

+ +

{{ trans('recurring.message', [ + 'type' => mb_strtolower(trans_choice('general.bills', 1)), + 'date' => $bill->recurring->schedule()->next()->getStart()->format($date_format) + ]) }} +

+
+ @endif +
{{ $bill->status->name }} diff --git a/resources/views/expenses/payments/edit.blade.php b/resources/views/expenses/payments/edit.blade.php index ba25a42c0..0681d3fdf 100644 --- a/resources/views/expenses/payments/edit.blade.php +++ b/resources/views/expenses/payments/edit.blade.php @@ -3,6 +3,18 @@ @section('title', trans('general.title.edit', ['type' => trans_choice('general.payments', 1)])) @section('content') + @if ($payment->recurring()->count()) +
+

{{ trans('recurring.recurring') }}

+ +

{{ trans('recurring.message', [ + 'type' => mb_strtolower(trans_choice('general.payments', 1)), + 'date' => $payment->recurring->schedule()->next()->getStart()->format($date_format) + ]) }} +

+
+ @endif +
{!! Form::model($payment, [ diff --git a/resources/views/incomes/invoices/show.blade.php b/resources/views/incomes/invoices/show.blade.php index 39104d4fe..ea5d5f608 100644 --- a/resources/views/incomes/invoices/show.blade.php +++ b/resources/views/incomes/invoices/show.blade.php @@ -3,6 +3,18 @@ @section('title', trans_choice('general.invoices', 1) . ': ' . $invoice->invoice_number) @section('content') + @if ($invoice->recurring()->count()) +
+

{{ trans('recurring.recurring') }}

+ +

{{ trans('recurring.message', [ + 'type' => mb_strtolower(trans_choice('general.invoices', 1)), + 'date' => $invoice->recurring->schedule()->next()->getStart()->format($date_format) + ]) }} +

+
+ @endif +
{{ $invoice->status->name }} diff --git a/resources/views/incomes/revenues/edit.blade.php b/resources/views/incomes/revenues/edit.blade.php index 40e18bbba..d3c11b330 100644 --- a/resources/views/incomes/revenues/edit.blade.php +++ b/resources/views/incomes/revenues/edit.blade.php @@ -3,6 +3,18 @@ @section('title', trans('general.title.edit', ['type' => trans_choice('general.revenues', 1)])) @section('content') + @if ($revenue->recurring()->count()) +
+

{{ trans('recurring.recurring') }}

+ +

{{ trans('recurring.message', [ + 'type' => mb_strtolower(trans_choice('general.revenues', 1)), + 'date' => $revenue->recurring->schedule()->next()->getStart()->format($date_format) + ]) }} +

+
+ @endif +
{!! Form::model($revenue, [