Skip to content

Commit

Permalink
more recurring improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Sep 30, 2020
1 parent fde2813 commit 9ef11ce
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 39 deletions.
85 changes: 50 additions & 35 deletions app/Console/Commands/RecurringCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use App\Models\Banking\Transaction;
use App\Models\Common\Company;
use App\Models\Sale\Invoice;
use App\Utilities\Date;
use App\Utilities\Overrider;
use Date;
use Illuminate\Console\Command;

class RecurringCheck extends Command
Expand All @@ -31,13 +31,6 @@ class RecurringCheck extends Command
*/
protected $description = 'Check for recurring';

/**
* The current day.
*
* @var \Carbon\Carbon
*/
protected $today;

/**
* Execute the console command.
*
Expand All @@ -61,19 +54,35 @@ public function handle()
Overrider::load('settings');
Overrider::load('currencies');

$this->today = Date::today();
$today = Date::today();

foreach ($company->recurring as $recurring) {
if (!$model = $recurring->recurable) {
continue;
}

foreach ($recurring->getRecurringSchedule() as $schedule) {
$schedules = $recurring->getRecurringSchedule();

$children_count = $this->getChildrenCount($model);
$schedule_count = $schedules->count();

// All recurring created, including today
if ($children_count > ($schedule_count - 1)) {
continue;
}

// Recur only today
if ($children_count == ($schedule_count - 1)) {
$this->recur($model, $recurring->recurable_type, $today);

continue;
}

// Recur all schedules, previously failed
foreach ($schedules as $schedule) {
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));

\DB::transaction(function () use ($model, $recurring, $schedule_date) {
$this->recur($model, $recurring->recurable_type, $schedule_date);
});
$this->recur($model, $recurring->recurable_type, $schedule_date);
}
}
}
Expand All @@ -85,35 +94,32 @@ public function handle()

protected function recur($model, $type, $schedule_date)
{
// Don't recur the future
if ($schedule_date->greaterThan($this->today)) {
return;
}

if (!$clone = $this->getClone($model, $schedule_date)) {
return;
}
\DB::transaction(function () use ($model, $type, $schedule_date) {
if (!$clone = $this->getClone($model, $schedule_date)) {
return;
}

switch ($type) {
case 'App\Models\Purchase\Bill':
event(new BillCreated($clone));
switch ($type) {
case 'App\Models\Purchase\Bill':
event(new BillCreated($clone));

event(new BillRecurring($clone));
event(new BillRecurring($clone));

break;
case 'App\Models\Sale\Invoice':
event(new InvoiceCreated($clone));
break;
case 'App\Models\Sale\Invoice':
event(new InvoiceCreated($clone));

event(new InvoiceRecurring($clone));
event(new InvoiceRecurring($clone));

break;
case 'App\Models\Banking\Transaction':
event(new TransactionCreated($clone));
break;
case 'App\Models\Banking\Transaction':
event(new TransactionCreated($clone));

event(new TransactionRecurring($clone));
event(new TransactionRecurring($clone));

break;
}
break;
}
});
}

/**
Expand Down Expand Up @@ -215,6 +221,15 @@ protected function skipThisClone($model, $schedule_date)
return false;
}

protected function getChildrenCount($model)
{
$table = $this->getTable($model);

return \DB::table($table)
->where('parent_id', $model->id)
->count();
}

protected function getDateField($model)
{
if ($model instanceof Transaction) {
Expand Down
38 changes: 35 additions & 3 deletions app/Traits/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Traits;

use App\Utilities\Date;
use Recurr\Rule;
use Recurr\Transformer\ArrayTransformer;
use Recurr\Transformer\ArrayTransformerConfig;
Expand Down Expand Up @@ -59,25 +60,30 @@ public function updateRecurring()
]);
}

public function getRecurringSchedule()
public function getRecurringSchedule($set_until_date = true)
{
$config = new ArrayTransformerConfig();
$config->enableLastDayOfMonthFix();
$config->setVirtualLimit($this->getRecurringVirtualLimit());

$transformer = new ArrayTransformer();
$transformer->setConfig($config);

return $transformer->transform($this->getRecurringRule());
return $transformer->transform($this->getRecurringRule($set_until_date));
}

public function getRecurringRule()
public function getRecurringRule($set_until_date = true)
{
$rule = (new Rule())
->setStartDate($this->getRecurringRuleStartDate())
->setTimezone($this->getRecurringRuleTimeZone())
->setFreq($this->getRecurringRuleFrequency())
->setInterval($this->getRecurringRuleInterval());

if ($set_until_date) {
$rule->setUntil($this->getRecurringRuleUntilDate());
}

// 0 means infinite
if ($this->count != 0) {
$rule->setCount($this->getRecurringRuleCount());
Expand All @@ -91,6 +97,11 @@ public function getRecurringRuleStartDate()
return new \DateTime($this->started_at, new \DateTimeZone($this->getRecurringRuleTimeZone()));
}

public function getRecurringRuleUntilDate()
{
return new \DateTime(Date::today()->toDateTimeString(), new \DateTimeZone($this->getRecurringRuleTimeZone()));
}

public function getRecurringRuleTimeZone()
{
return setting('localisation.timezone');
Expand All @@ -112,6 +123,27 @@ public function getRecurringRuleInterval()
return $this->interval;
}

public function getRecurringVirtualLimit()
{
switch ($this->frequency) {
case 'yearly':
$limit = '2';
break;
case 'monthly':
$limit = '24';
break;
case 'weekly':
$limit = '104';
break;
case 'daily':
default;
$limit = '732';
break;
}

return $limit;
}

public function getCurrentRecurring()
{
if (!$schedule = $this->getRecurringSchedule()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Utilities/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function reflect(&$items, $issued_date_field)
continue;
}

foreach ($item->recurring->getRecurringSchedule() as $schedule) {
foreach ($item->recurring->getRecurringSchedule(false) as $schedule) {
$issued = Date::parse($item->$issued_date_field);
$start = $schedule->getStart();

Expand Down

0 comments on commit 9ef11ce

Please sign in to comment.