fixed all recurring issues #315

This commit is contained in:
denisdulici 2018-05-02 00:59:55 +03:00
parent 7e21634d77
commit c049b8ce73
7 changed files with 110 additions and 41 deletions

View File

@ -64,10 +64,14 @@ class RecurringCheck extends Command
$company->setSettings();
foreach ($company->recurring as $recur) {
$current = Date::parse($recur->schedule()->current()->getStart()->format('Y-m-d'));
if (!$current = $recur->current()) {
continue;
}
$current_date = Date::parse($current->format('Y-m-d'));
// Check if should recur today
if ($this->today->ne($current)) {
if ($this->today->ne($current_date)) {
continue;
}

View File

@ -3,14 +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;
use App\Traits\Recurring as RecurringTrait;
class Recurring extends Model
{
use RecurringTrait;
protected $table = 'recurring';
@ -29,30 +26,4 @@ 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;
}
}

View File

@ -2,6 +2,12 @@
namespace App\Traits;
use DateTime;
use DateTimeZone;
use Recurr\Rule;
use Recurr\Transformer\ArrayTransformer;
use Recurr\Transformer\ArrayTransformerConfig;
trait Recurring
{
@ -55,4 +61,92 @@ trait Recurring
'count' => (int) $request['recurring_count'],
]);
}
public function current()
{
if (!$schedule = $this->schedule()) {
return false;
}
return $schedule->current()->getStart();
}
public function next()
{
if (!$schedule = $this->schedule()) {
return false;
}
if (!$next = $schedule->next()) {
return false;
}
return $next->getStart();
}
public function first()
{
if (!$schedule = $this->schedule()) {
return false;
}
return $schedule->first()->getStart();
}
public function last()
{
if (!$schedule = $this->schedule()) {
return false;
}
return $schedule->last()->getStart();
}
public function schedule()
{
$config = new ArrayTransformerConfig();
$config->enableLastDayOfMonthFix();
$transformer = new ArrayTransformer();
$transformer->setConfig($config);
return $transformer->transform($this->getRule());
}
public function getRule()
{
$rule = (new Rule())
->setStartDate($this->getRuleStartDate())
->setTimezone($this->getRuleTimeZone())
->setFreq($this->getRuleFrequency())
->setInterval($this->interval);
// 0 means infinite
if ($this->count != 0) {
$rule->setCount($this->getRuleCount());
}
return $rule;
}
public function getRuleStartDate()
{
return new DateTime($this->started_at, new DateTimeZone($this->getRuleTimeZone()));
}
public function getRuleTimeZone()
{
return setting('general.timezone');
}
public function getRuleCount()
{
// Fix for humans
return $this->count + 1;
}
public function getRuleFrequency()
{
return strtoupper($this->frequency);
}
}

View File

@ -3,13 +3,13 @@
@section('title', trans_choice('general.bills', 1) . ': ' . $bill->bill_number)
@section('content')
@if ($bill->recurring()->count())
@if (($recurring = $invoice->recurring) && ($next = $recurring->next()))
<div class="callout callout-info">
<h4>{{ trans('recurring.recurring') }}</h4>
<p>{{ trans('recurring.message', [
'type' => mb_strtolower(trans_choice('general.bills', 1)),
'date' => $bill->recurring->schedule()->next()->getStart()->format($date_format)
'date' => $next->format($date_format)
]) }}
</p>
</div>

View File

@ -3,13 +3,13 @@
@section('title', trans('general.title.edit', ['type' => trans_choice('general.payments', 1)]))
@section('content')
@if ($payment->recurring()->count())
@if (($recurring = $invoice->recurring) && ($next = $recurring->next()))
<div class="callout callout-info">
<h4>{{ trans('recurring.recurring') }}</h4>
<p>{{ trans('recurring.message', [
'type' => mb_strtolower(trans_choice('general.payments', 1)),
'date' => $payment->recurring->schedule()->next()->getStart()->format($date_format)
'date' => $next->format($date_format)
]) }}
</p>
</div>

View File

@ -3,13 +3,13 @@
@section('title', trans_choice('general.invoices', 1) . ': ' . $invoice->invoice_number)
@section('content')
@if ($invoice->recurring()->count())
@if (($recurring = $invoice->recurring) && ($next = $recurring->next()))
<div class="callout callout-info">
<h4>{{ trans('recurring.recurring') }}</h4>
<p>{{ trans('recurring.message', [
'type' => mb_strtolower(trans_choice('general.invoices', 1)),
'date' => $invoice->recurring->schedule()->next()->getStart()->format($date_format)
'date' => $next->format($date_format)
]) }}
</p>
</div>

View File

@ -3,13 +3,13 @@
@section('title', trans('general.title.edit', ['type' => trans_choice('general.revenues', 1)]))
@section('content')
@if ($revenue->recurring()->count())
@if (($recurring = $invoice->recurring) && ($next = $recurring->next()))
<div class="callout callout-info">
<h4>{{ trans('recurring.recurring') }}</h4>
<p>{{ trans('recurring.message', [
'type' => mb_strtolower(trans_choice('general.revenues', 1)),
'date' => $revenue->recurring->schedule()->next()->getStart()->format($date_format)
'date' => $next->format($date_format)
]) }}
</p>
</div>