2018-04-27 16:01:31 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
use App\Utilities\Date;
|
2018-05-02 00:59:55 +03:00
|
|
|
use Recurr\Rule;
|
|
|
|
use Recurr\Transformer\ArrayTransformer;
|
|
|
|
use Recurr\Transformer\ArrayTransformerConfig;
|
|
|
|
|
2018-04-27 16:01:31 +03:00
|
|
|
trait Recurring
|
|
|
|
{
|
|
|
|
public function createRecurring()
|
|
|
|
{
|
|
|
|
$request = request();
|
|
|
|
|
2018-10-09 12:45:44 +03:00
|
|
|
if ($request->get('recurring_frequency', 'no') == 'no') {
|
2018-04-27 16:01:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$frequency = ($request['recurring_frequency'] != 'custom') ? $request['recurring_frequency'] : $request['recurring_custom_frequency'];
|
2020-03-04 13:40:25 +03:00
|
|
|
$interval = (($request['recurring_frequency'] != 'custom') || ($request['recurring_interval'] < 1)) ? 1 : (int) $request['recurring_interval'];
|
2018-04-27 16:01:31 +03:00
|
|
|
$started_at = $request->get('paid_at') ?: ($request->get('invoiced_at') ?: $request->get('billed_at'));
|
|
|
|
|
|
|
|
$this->recurring()->create([
|
|
|
|
'company_id' => session('company_id'),
|
|
|
|
'frequency' => $frequency,
|
|
|
|
'interval' => $interval,
|
|
|
|
'started_at' => $started_at,
|
|
|
|
'count' => (int) $request['recurring_count'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateRecurring()
|
|
|
|
{
|
|
|
|
$request = request();
|
|
|
|
|
2018-10-09 12:45:44 +03:00
|
|
|
if ($request->get('recurring_frequency', 'no') == 'no') {
|
2018-04-27 16:01:31 +03:00
|
|
|
$this->recurring()->delete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$frequency = ($request['recurring_frequency'] != 'custom') ? $request['recurring_frequency'] : $request['recurring_custom_frequency'];
|
2020-03-04 13:40:25 +03:00
|
|
|
$interval = (($request['recurring_frequency'] != 'custom') || ($request['recurring_interval'] < 1)) ? 1 : (int) $request['recurring_interval'];
|
2018-04-27 16:01:31 +03:00
|
|
|
$started_at = $request->get('paid_at') ?: ($request->get('invoiced_at') ?: $request->get('billed_at'));
|
|
|
|
|
2018-04-27 16:48:59 +03:00
|
|
|
$recurring = $this->recurring();
|
|
|
|
|
|
|
|
if ($recurring->count()) {
|
2018-04-27 16:01:31 +03:00
|
|
|
$function = 'update';
|
|
|
|
} else {
|
|
|
|
$function = 'create';
|
|
|
|
}
|
|
|
|
|
2018-04-27 16:48:59 +03:00
|
|
|
$recurring->$function([
|
2018-04-27 16:01:31 +03:00
|
|
|
'company_id' => session('company_id'),
|
|
|
|
'frequency' => $frequency,
|
|
|
|
'interval' => $interval,
|
|
|
|
'started_at' => $started_at,
|
|
|
|
'count' => (int) $request['recurring_count'],
|
|
|
|
]);
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
public function getRecurringSchedule($set_until_date = true)
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
$config = new ArrayTransformerConfig();
|
|
|
|
$config->enableLastDayOfMonthFix();
|
2020-09-30 17:01:35 +03:00
|
|
|
$config->setVirtualLimit($this->getRecurringVirtualLimit());
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
$transformer = new ArrayTransformer();
|
|
|
|
$transformer->setConfig($config);
|
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
return $transformer->transform($this->getRecurringRule($set_until_date));
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
public function getRecurringRule($set_until_date = true)
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
$rule = (new Rule())
|
|
|
|
->setStartDate($this->getRecurringRuleStartDate())
|
|
|
|
->setTimezone($this->getRecurringRuleTimeZone())
|
|
|
|
->setFreq($this->getRecurringRuleFrequency())
|
|
|
|
->setInterval($this->getRecurringRuleInterval());
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
if ($set_until_date) {
|
|
|
|
$rule->setUntil($this->getRecurringRuleUntilDate());
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
// 0 means infinite
|
|
|
|
if ($this->count != 0) {
|
|
|
|
$rule->setCount($this->getRecurringRuleCount());
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
return $rule;
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getRecurringRuleStartDate()
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
return new \DateTime($this->started_at, new \DateTimeZone($this->getRecurringRuleTimeZone()));
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
public function getRecurringRuleUntilDate()
|
|
|
|
{
|
|
|
|
return new \DateTime(Date::today()->toDateTimeString(), new \DateTimeZone($this->getRecurringRuleTimeZone()));
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getRecurringRuleTimeZone()
|
|
|
|
{
|
|
|
|
return setting('localisation.timezone');
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getRecurringRuleCount()
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
// Fix for humans
|
|
|
|
return $this->count + 1;
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getRecurringRuleFrequency()
|
|
|
|
{
|
|
|
|
return strtoupper($this->frequency);
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getRecurringRuleInterval()
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
return $this->interval;
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-30 17:01:35 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getCurrentRecurring()
|
|
|
|
{
|
|
|
|
if (!$schedule = $this->getRecurringSchedule()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
return $schedule->current()->getStart();
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getNextRecurring()
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
if (!$schedule = $this->getRecurringSchedule()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
if (!$next = $schedule->next()) {
|
|
|
|
return false;
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
return $next->getStart();
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getFirstRecurring()
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
if (!$schedule = $this->getRecurringSchedule()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
return $schedule->first()->getStart();
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
public function getLastRecurring()
|
2018-05-02 00:59:55 +03:00
|
|
|
{
|
2020-09-27 00:33:06 +03:00
|
|
|
if (!$schedule = $this->getRecurringSchedule()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-02 00:59:55 +03:00
|
|
|
|
2020-09-27 00:33:06 +03:00
|
|
|
return $schedule->last()->getStart();
|
2018-05-02 00:59:55 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|