2020-09-14 13:11:46 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits\Recurring;
|
|
|
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
trait HasRecurrence
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2020-11-25 15:19:52 +01:00
|
|
|
* Calculates the first day of the month, this will ALWAYS
|
2020-09-14 13:11:46 +02:00
|
|
|
* be the first of NEXT month
|
2020-11-25 15:19:52 +01:00
|
|
|
*
|
2020-09-14 13:11:46 +02:00
|
|
|
* @param Carbon $date The given date
|
2020-11-25 15:19:52 +01:00
|
|
|
* @return Carbon The first of NEXT month
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
|
|
|
public function calculateFirstDayOfMonth($date)
|
|
|
|
{
|
|
|
|
return $date->copy()->startOfMonth()->addMonth();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the last day of the month.
|
|
|
|
*
|
|
|
|
* If it is the last day of the month - we add a month on.
|
2020-11-25 15:19:52 +01:00
|
|
|
*
|
2020-09-14 13:11:46 +02:00
|
|
|
* @param Carbon $date The start date
|
|
|
|
* @return Carbon The last day of month
|
|
|
|
*/
|
|
|
|
public function calculateLastDayOfMonth($date)
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($date->isLastOfMonth()) {
|
2020-09-14 14:13:15 +02:00
|
|
|
return $date->copy()->addMonthNoOverflow()->endOfMonth();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-14 13:11:46 +02:00
|
|
|
|
|
|
|
return $date->copy()->endOfMonth();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the day of the month, if in the past we ADD a month
|
2020-11-25 15:19:52 +01:00
|
|
|
*
|
2020-09-14 13:11:46 +02:00
|
|
|
* @param Carbon $date The start date
|
|
|
|
* @param String|Int $day_of_month The day of the month
|
|
|
|
*/
|
2020-09-14 14:13:15 +02:00
|
|
|
public function setDayOfMonth($date, $day_of_month)
|
2020-09-14 13:11:46 +02:00
|
|
|
{
|
2020-09-24 05:40:13 +02:00
|
|
|
$carbon_date = Carbon::parse($date);
|
2020-09-23 12:52:54 +02:00
|
|
|
|
2020-09-24 05:40:13 +02:00
|
|
|
$set_date = $carbon_date->copy()->setUnitNoOverflow('day', $day_of_month, 'month');
|
2020-09-14 13:11:46 +02:00
|
|
|
|
2020-09-14 14:13:15 +02:00
|
|
|
//If the set date is less than the original date we need to add a month.
|
|
|
|
//If we are overflowing dates, then we need to diff the dates and ensure it doesn't equal 0
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($set_date->lte($date) || $set_date->diffInDays($carbon_date) == 0) {
|
2020-09-14 14:13:15 +02:00
|
|
|
$set_date->addMonthNoOverflow();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-14 14:13:15 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($day_of_month == '31') {
|
2020-09-14 14:13:15 +02:00
|
|
|
$set_date->endOfMonth();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-14 14:13:15 +02:00
|
|
|
|
2020-09-14 13:11:46 +02:00
|
|
|
|
|
|
|
return $set_date;
|
|
|
|
}
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|