Initial commit
This commit is contained in:
66
application/Espo/Tools/WorkingTime/Calendar.php
Normal file
66
application/Espo/Tools/WorkingTime/Calendar.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingWeekday;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingDate;
|
||||
|
||||
use Espo\Core\Field\Date;
|
||||
|
||||
use DateTimeZone;
|
||||
|
||||
interface Calendar
|
||||
{
|
||||
/**
|
||||
* Time-zone.
|
||||
*/
|
||||
public function getTimezone(): DateTimeZone;
|
||||
|
||||
/**
|
||||
* Working weekdays.
|
||||
*
|
||||
* @return WorkingWeekday[]
|
||||
*/
|
||||
public function getWorkingWeekdays(): array;
|
||||
|
||||
/**
|
||||
* Non-working dates.
|
||||
*
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getNonWorkingDates(Date $from, Date $to): array;
|
||||
|
||||
/**
|
||||
* Working dates (exceptions).
|
||||
*
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getWorkingDates(Date $from, Date $to): array;
|
||||
}
|
||||
38
application/Espo/Tools/WorkingTime/Calendar/HavingRanges.php
Normal file
38
application/Espo/Tools/WorkingTime/Calendar/HavingRanges.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime\Calendar;
|
||||
|
||||
interface HavingRanges
|
||||
{
|
||||
/**
|
||||
* @return TimeRange[]
|
||||
*/
|
||||
public function getRanges(): array;
|
||||
}
|
||||
69
application/Espo/Tools/WorkingTime/Calendar/Time.php
Normal file
69
application/Espo/Tools/WorkingTime/Calendar/Time.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime\Calendar;
|
||||
|
||||
class Time
|
||||
{
|
||||
/**
|
||||
* @var int<0,23>
|
||||
*/
|
||||
private int $hour;
|
||||
|
||||
/**
|
||||
* @var int<0,59>
|
||||
*/
|
||||
private int $minute;
|
||||
|
||||
/**
|
||||
* @param int<0,23> $hour
|
||||
* @param int<0,59> $minute
|
||||
*/
|
||||
public function __construct(int $hour, int $minute)
|
||||
{
|
||||
$this->hour = $hour;
|
||||
$this->minute = $minute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int<0,23>
|
||||
*/
|
||||
public function getHour(): int
|
||||
{
|
||||
return $this->hour;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int<0,59>
|
||||
*/
|
||||
public function getMinute(): int
|
||||
{
|
||||
return $this->minute;
|
||||
}
|
||||
}
|
||||
53
application/Espo/Tools/WorkingTime/Calendar/TimeRange.php
Normal file
53
application/Espo/Tools/WorkingTime/Calendar/TimeRange.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime\Calendar;
|
||||
|
||||
class TimeRange
|
||||
{
|
||||
private Time $start;
|
||||
|
||||
private Time $end;
|
||||
|
||||
public function __construct(Time $start, Time $end)
|
||||
{
|
||||
$this->start = $start;
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
public function getStart(): Time
|
||||
{
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
public function getEnd(): Time
|
||||
{
|
||||
return $this->end;
|
||||
}
|
||||
}
|
||||
64
application/Espo/Tools/WorkingTime/Calendar/WorkingDate.php
Normal file
64
application/Espo/Tools/WorkingTime/Calendar/WorkingDate.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime\Calendar;
|
||||
|
||||
use Espo\Core\Field\Date;
|
||||
|
||||
class WorkingDate implements HavingRanges
|
||||
{
|
||||
private Date $date;
|
||||
|
||||
/**
|
||||
* @var TimeRange[]
|
||||
*/
|
||||
private array $ranges;
|
||||
|
||||
/**
|
||||
* @param TimeRange[] $ranges
|
||||
*/
|
||||
public function __construct(Date $date, array $ranges = [])
|
||||
{
|
||||
$this->date = $date;
|
||||
$this->ranges = $ranges;
|
||||
}
|
||||
|
||||
public function getDate(): Date
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TimeRange[]
|
||||
*/
|
||||
public function getRanges(): array
|
||||
{
|
||||
return $this->ranges;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime\Calendar;
|
||||
|
||||
class WorkingWeekday implements HavingRanges
|
||||
{
|
||||
/**
|
||||
* @var int<0,6>
|
||||
*/
|
||||
private int $weekday;
|
||||
|
||||
/**
|
||||
* @var TimeRange[]
|
||||
*/
|
||||
private array $ranges;
|
||||
|
||||
/**
|
||||
* @param int<0,6> $weekday
|
||||
* @param TimeRange[] $ranges
|
||||
*/
|
||||
public function __construct(int $weekday, array $ranges)
|
||||
{
|
||||
$this->weekday = $weekday;
|
||||
$this->ranges = $ranges;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int<0,6>
|
||||
*/
|
||||
public function getWeekday(): int
|
||||
{
|
||||
return $this->weekday;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TimeRange[]
|
||||
*/
|
||||
public function getRanges(): array
|
||||
{
|
||||
return $this->ranges;
|
||||
}
|
||||
}
|
||||
77
application/Espo/Tools/WorkingTime/CalendarFactory.php
Normal file
77
application/Espo/Tools/WorkingTime/CalendarFactory.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Core\Binding\BindingContainerBuilder;
|
||||
use Espo\Core\InjectableFactory;
|
||||
use Espo\Entities\Team;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Entities\WorkingTimeCalendar;
|
||||
|
||||
class CalendarFactory
|
||||
{
|
||||
public function __construct(private InjectableFactory $injectableFactory)
|
||||
{}
|
||||
|
||||
public function createGlobal(): GlobalCalendar
|
||||
{
|
||||
return $this->injectableFactory->create(GlobalCalendar::class);
|
||||
}
|
||||
|
||||
public function createForUser(User $user): UserCalendar
|
||||
{
|
||||
$binding = BindingContainerBuilder::create()
|
||||
->bindInstance(User::class, $user)
|
||||
->build();
|
||||
|
||||
return $this->injectableFactory->createWithBinding(UserCalendar::class, $binding);
|
||||
}
|
||||
|
||||
public function createForTeam(Team $team): TeamCalendar
|
||||
{
|
||||
$binding = BindingContainerBuilder::create()
|
||||
->bindInstance(Team::class, $team)
|
||||
->build();
|
||||
|
||||
return $this->injectableFactory->createWithBinding(TeamCalendar::class, $binding);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 8.4.0
|
||||
*/
|
||||
public function create(WorkingTimeCalendar $calendar): Calendar
|
||||
{
|
||||
$binding = BindingContainerBuilder::create()
|
||||
->bindInstance(WorkingTimeCalendar::class, $calendar)
|
||||
->build();
|
||||
|
||||
return $this->injectableFactory->createWithBinding(SpecificCalendar::class, $binding);
|
||||
}
|
||||
}
|
||||
149
application/Espo/Tools/WorkingTime/CalendarUtility.php
Normal file
149
application/Espo/Tools/WorkingTime/CalendarUtility.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Core\Field\DateTime;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class CalendarUtility
|
||||
{
|
||||
private const MAX_FIND_DAYS_PERIOD = 200;
|
||||
private const MAX_YEARS_PERIOD = 2;
|
||||
|
||||
private Calendar $calendar;
|
||||
private Extractor $extractor;
|
||||
|
||||
public function __construct(Calendar $calendar, Extractor $extractor)
|
||||
{
|
||||
$this->calendar = $calendar;
|
||||
$this->extractor = $extractor;
|
||||
}
|
||||
|
||||
public function isWorkingDay(DateTime $time): bool
|
||||
{
|
||||
$point = $time
|
||||
->withTimezone($this->calendar->getTimezone())
|
||||
->withTime(0, 0, 0);
|
||||
|
||||
return $this->extractor->extractAllDay($this->calendar, $point, $point->modify('+0 seconds')) !== [];
|
||||
}
|
||||
|
||||
public function hasWorkingTime(DateTime $from, DateTime $to): bool
|
||||
{
|
||||
return $this->extractor->extract($this->calendar, $from, $to) !== [];
|
||||
}
|
||||
|
||||
public function getSummedWorkingHours(DateTime $from, DateTime $to): float
|
||||
{
|
||||
$ranges = $this->extractor->extract($this->calendar, $from, $to);
|
||||
|
||||
$sum = 0.0;
|
||||
|
||||
foreach ($ranges as $range) {
|
||||
$sum += ($range[1]->getTimestamp() - $range[0]->getTimestamp()) / 3600;
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
public function getWorkingDays(DateTime $from, DateTime $to): int
|
||||
{
|
||||
$ranges = $this->extractor->extractAllDay($this->calendar, $from, $to);
|
||||
|
||||
return count($ranges);
|
||||
}
|
||||
|
||||
public function findClosestWorkingTime(DateTime $time): ?DateTime
|
||||
{
|
||||
$step = 10;
|
||||
$max = $time->modify('+' . self::MAX_FIND_DAYS_PERIOD . ' days');
|
||||
|
||||
$point = $time;
|
||||
|
||||
while ($point->isLessThan($max)) {
|
||||
$from = $point;
|
||||
$to = $point->modify('+' . $step . ' days');
|
||||
|
||||
$ranges = $this->extractor->extract($this->calendar, $from, $to);
|
||||
|
||||
if (count($ranges)) {
|
||||
return $time->isGreaterThan($ranges[0][0]) ?
|
||||
$time :
|
||||
$ranges[0][0];
|
||||
}
|
||||
|
||||
$point = $to;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int<1, max> $days
|
||||
*/
|
||||
public function addWorkingDays(DateTime $time, int $days): ?DateTime
|
||||
{
|
||||
/** @var int $days */
|
||||
|
||||
if ($days <= 0) {
|
||||
throw new InvalidArgumentException("Can't add non-positive days number.");
|
||||
}
|
||||
|
||||
$step = max(30, $days);
|
||||
$max = $time->modify('+' . self::MAX_YEARS_PERIOD . ' years');
|
||||
|
||||
$point = $time
|
||||
->withTimezone($this->calendar->getTimezone())
|
||||
->modify('+1 day')
|
||||
->withTime(0, 0, 0);
|
||||
|
||||
$counter = 0;
|
||||
|
||||
while ($point->isLessThan($max)) {
|
||||
$from = $point;
|
||||
$to = $point->modify('+' . $step . ' days');
|
||||
|
||||
$ranges = $this->extractor->extractAllDay($this->calendar, $from, $to);
|
||||
|
||||
foreach ($ranges as $range) {
|
||||
$counter++;
|
||||
|
||||
if ($counter === $days) {
|
||||
return $range[0];
|
||||
}
|
||||
}
|
||||
|
||||
$point = $to;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Core\Binding\BindingContainerBuilder;
|
||||
use Espo\Core\InjectableFactory;
|
||||
use Espo\Entities\Team;
|
||||
use Espo\Entities\User;
|
||||
|
||||
class CalendarUtilityFactory
|
||||
{
|
||||
public function __construct(
|
||||
private InjectableFactory $injectableFactory,
|
||||
private CalendarFactory $calendarFactory
|
||||
) {}
|
||||
|
||||
public function create(Calendar $calendar): CalendarUtility
|
||||
{
|
||||
return $this->injectableFactory->createWithBinding(
|
||||
CalendarUtility::class,
|
||||
BindingContainerBuilder::create()
|
||||
->bindInstance(Calendar::class, $calendar)
|
||||
->build()
|
||||
);
|
||||
}
|
||||
|
||||
public function createForUser(User $user): CalendarUtility
|
||||
{
|
||||
$calendar = $this->calendarFactory->createForUser($user);
|
||||
|
||||
return $this->create($calendar);
|
||||
}
|
||||
|
||||
public function createForTeam(Team $team): CalendarUtility
|
||||
{
|
||||
$calendar = $this->calendarFactory->createForTeam($team);
|
||||
|
||||
return $this->create($calendar);
|
||||
}
|
||||
|
||||
public function createGlobal(): CalendarUtility
|
||||
{
|
||||
$calendar = $this->calendarFactory->createGlobal();
|
||||
|
||||
return $this->create($calendar);
|
||||
}
|
||||
}
|
||||
410
application/Espo/Tools/WorkingTime/Extractor.php
Normal file
410
application/Espo/Tools/WorkingTime/Extractor.php
Normal file
@@ -0,0 +1,410 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use DateTimeZone;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingWeekday;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingDate;
|
||||
use Espo\Tools\WorkingTime\Calendar\HavingRanges;
|
||||
|
||||
use Espo\Core\Field\DateTime;
|
||||
use Espo\Core\Field\Date;
|
||||
|
||||
class Extractor
|
||||
{
|
||||
/**
|
||||
* @return array{DateTime,DateTime}[]
|
||||
*/
|
||||
public function extract(Calendar $calendar, DateTime $from, DateTime $to): array
|
||||
{
|
||||
$pointer = $from->withTimezone($calendar->getTimezone());
|
||||
|
||||
$fromDate = Date::fromDateTime($from->modify('-1 day')->toDateTime());
|
||||
$toDate = Date::fromDateTime($from->modify('+1 day')->toDateTime());
|
||||
|
||||
$workingDates = $calendar->getWorkingDates($fromDate, $toDate);
|
||||
$nonWorkingDates = $calendar->getNonWorkingDates($fromDate, $toDate);
|
||||
$workingWeekdays = $calendar->getWorkingWeekdays();
|
||||
|
||||
$list = [];
|
||||
|
||||
$end = $to->withTimezone($calendar->getTimezone())->withTime(23, 59, 59);
|
||||
|
||||
while ($pointer->toTimestamp() < $end->toTimestamp()) {
|
||||
$list = array_merge(
|
||||
$list,
|
||||
$this->extractIteration(
|
||||
$pointer,
|
||||
$workingDates,
|
||||
$nonWorkingDates,
|
||||
$workingWeekdays,
|
||||
),
|
||||
);
|
||||
|
||||
$pointer = $pointer->modify('+1 day');
|
||||
}
|
||||
|
||||
return $this->trim($list, $from, $to, $calendar);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{DateTime,DateTime}[] $list
|
||||
* @return array{DateTime,DateTime}[]
|
||||
*/
|
||||
private function trim(array $list, DateTime $from, DateTime $to, Calendar $calendar): array
|
||||
{
|
||||
$wasUnset = false;
|
||||
|
||||
foreach ($list as $i => $pair) {
|
||||
if ($from->isLessThan($pair[0]) || $from->isEqualTo($pair[0])) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($from->isGreaterThan($pair[1]) || $from->isEqualTo($pair[1])) {
|
||||
unset($list[$i]);
|
||||
$wasUnset = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($from->isLessThan($pair[1])) {
|
||||
$list[$i][0] = $from->withTimezone($calendar->getTimezone());
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if ($wasUnset) {
|
||||
$list = array_values($list);
|
||||
|
||||
$wasUnset = false;
|
||||
}
|
||||
|
||||
for ($i = count($list) - 1; $i >= 0; $i--) {
|
||||
$pair = $list[$i];
|
||||
|
||||
if ($to->isGreaterThan($pair[1]) || $to->isEqualTo($pair[1])) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($to->isLessThan($pair[0]) || $to->isEqualTo($pair[0])) {
|
||||
unset($list[$i]);
|
||||
$wasUnset = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($to->isGreaterThan($pair[0])) {
|
||||
$list[$i][1] = $to->withTimezone($calendar->getTimezone());
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if ($wasUnset) {
|
||||
$list = array_values($list);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{DateTime,DateTime}[]
|
||||
*/
|
||||
public function extractAllDay(Calendar $calendar,
|
||||
DateTime $from,
|
||||
DateTime $to,
|
||||
?DateTimeZone $timezone = null
|
||||
): array {
|
||||
|
||||
$timezone ??= $calendar->getTimezone();
|
||||
|
||||
$pointer = $from
|
||||
->withTimezone($timezone)
|
||||
->withTime(0, 0, 0);
|
||||
|
||||
$fromDate = Date::fromDateTime($from->modify('-1 day')->toDateTime());
|
||||
$toDate = Date::fromDateTime($from->modify('+1 day')->toDateTime());
|
||||
|
||||
$workingDates = $calendar->getWorkingDates($fromDate, $toDate);
|
||||
$nonWorkingDates = $calendar->getNonWorkingDates($fromDate, $toDate);
|
||||
$workingWeekdays = $calendar->getWorkingWeekdays();
|
||||
|
||||
$list = [];
|
||||
|
||||
$end = $to->withTimezone($timezone)->withTime(23, 59, 59);
|
||||
|
||||
while ($pointer->toTimestamp() < $end->toTimestamp()) {
|
||||
$isWorkingDay = $this->isWorkingDay(
|
||||
$pointer,
|
||||
$workingDates,
|
||||
$nonWorkingDates,
|
||||
$workingWeekdays,
|
||||
);
|
||||
|
||||
$nextPointer = $pointer->modify('+1 day');
|
||||
|
||||
if ($isWorkingDay) {
|
||||
$list[] = [
|
||||
$pointer,
|
||||
$nextPointer
|
||||
];
|
||||
}
|
||||
|
||||
$pointer = $nextPointer;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{DateTime,DateTime}[]
|
||||
*/
|
||||
public function extractInversion(Calendar $calendar, DateTime $from, DateTime $to): array
|
||||
{
|
||||
$list = $this->extract($calendar, $from, $to);
|
||||
|
||||
$timezone = $calendar->getTimezone();
|
||||
|
||||
if ($list === []) {
|
||||
return [
|
||||
[
|
||||
$from->withTimezone($timezone),
|
||||
$to->withTimezone($timezone),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
$listInverted = [];
|
||||
|
||||
$count = count($list);
|
||||
|
||||
$listInverted[] = [
|
||||
$from->withTimezone($calendar->getTimezone()),
|
||||
$list[0][0]
|
||||
];
|
||||
|
||||
for ($i = 0; $i < $count - 1; $i++) {
|
||||
$item1 = $list[$i];
|
||||
$item2 = $list[$i + 1];
|
||||
|
||||
$listInverted[] = [
|
||||
$item1[1],
|
||||
$item2[0]
|
||||
];
|
||||
}
|
||||
|
||||
$listInverted[] = [
|
||||
$list[$count - 1][1],
|
||||
$to->withTimezone($timezone)
|
||||
];
|
||||
|
||||
return $listInverted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{DateTime,DateTime}[]
|
||||
*/
|
||||
public function extractAllDayInversion(
|
||||
Calendar $calendar,
|
||||
DateTime $from,
|
||||
DateTime $to,
|
||||
?DateTimeZone $timezone = null
|
||||
): array {
|
||||
|
||||
$list = $this->extractAllDay($calendar, $from, $to, $timezone);
|
||||
|
||||
if ($list === []) {
|
||||
return [[$from, $to]];
|
||||
}
|
||||
|
||||
$count = count($list);
|
||||
|
||||
$listInverted = [];
|
||||
|
||||
$listInverted[] = [
|
||||
$from,
|
||||
$list[0][0]
|
||||
];
|
||||
|
||||
for ($i = 0; $i < $count - 1; $i++) {
|
||||
$item1 = $list[$i];
|
||||
$item2 = $list[$i + 1];
|
||||
|
||||
$listInverted[] = [
|
||||
$item1[1],
|
||||
$item2[0]
|
||||
];
|
||||
}
|
||||
|
||||
$listInverted[] = [
|
||||
$list[$count - 1][1],
|
||||
$to
|
||||
];
|
||||
|
||||
return $listInverted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingDate[] $workingDates
|
||||
* @param WorkingDate[] $nonWorkingDates
|
||||
* @param WorkingWeekday[] $workingWeekdays
|
||||
*/
|
||||
private function isWorkingDay(
|
||||
DateTime $pointer,
|
||||
array $workingDates,
|
||||
array $nonWorkingDates,
|
||||
array $workingWeekdays
|
||||
): bool {
|
||||
|
||||
if ($this->findInDateList($pointer, $nonWorkingDates)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$day1 = $this->findInDateList($pointer, $workingDates);
|
||||
|
||||
if ($day1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$day2 = $this->findInWeekdayList($pointer, $workingWeekdays);
|
||||
|
||||
if ($day2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingDate[] $workingDates
|
||||
* @param WorkingDate[] $nonWorkingDates
|
||||
* @param WorkingWeekday[] $workingWeekdays
|
||||
* @return array{DateTime,DateTime}[]
|
||||
*/
|
||||
private function extractIteration(
|
||||
DateTime $pointer,
|
||||
array $workingDates,
|
||||
array $nonWorkingDates,
|
||||
array $workingWeekdays
|
||||
): array {
|
||||
|
||||
if ($this->findInDateList($pointer, $nonWorkingDates)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$day1 = $this->findInDateList($pointer, $workingDates);
|
||||
|
||||
if ($day1) {
|
||||
return $this->extractFromDay($pointer, $day1);
|
||||
}
|
||||
|
||||
$day2 = $this->findInWeekdayList($pointer, $workingWeekdays);
|
||||
|
||||
if ($day2) {
|
||||
return $this->extractFromDay($pointer, $day2);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime $pointer
|
||||
* @param WorkingDate[] $dateList
|
||||
*/
|
||||
private function findInDateList(DateTime $pointer, array $dateList): ?WorkingDate
|
||||
{
|
||||
$day = $pointer->getDay();
|
||||
$month = $pointer->getMonth();
|
||||
$year = $pointer->getYear();
|
||||
|
||||
foreach ($dateList as $item) {
|
||||
$date = $item->getDate();
|
||||
|
||||
if (
|
||||
$date->getDay() === $day &&
|
||||
$date->getMonth() === $month &&
|
||||
$date->getYear() === $year
|
||||
) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime $pointer
|
||||
* @param WorkingWeekday[] $dayList
|
||||
*/
|
||||
private function findInWeekdayList(DateTime $pointer, array $dayList): ?WorkingWeekday
|
||||
{
|
||||
$dow = $pointer->getDayOfWeek();
|
||||
|
||||
foreach ($dayList as $item) {
|
||||
if ($item->getWeekday() === $dow) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{DateTime,DateTime}[]
|
||||
*/
|
||||
private function extractFromDay(DateTime $dateTime, HavingRanges $day): array
|
||||
{
|
||||
$pointer = $dateTime->toDateTime();
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach ($day->getRanges() as $range) {
|
||||
$start = $range->getStart();
|
||||
$end = $range->getEnd();
|
||||
|
||||
$startDateTime = DateTime::fromDateTime(
|
||||
$pointer->setTime($start->getHour(), $start->getMinute())
|
||||
);
|
||||
|
||||
$endDateTime = DateTime::fromDateTime(
|
||||
$pointer->setTime($end->getHour(), $end->getMinute())
|
||||
);
|
||||
|
||||
if ($end->getHour() === 0 && $end->getMinute() === 0) {
|
||||
$endDateTime = $endDateTime->addDays(1);
|
||||
}
|
||||
|
||||
$list[] = [$startDateTime, $endDateTime];
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
137
application/Espo/Tools/WorkingTime/GlobalCalendar.php
Normal file
137
application/Espo/Tools/WorkingTime/GlobalCalendar.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Core\Binding\BindingContainerBuilder;
|
||||
use Espo\Core\InjectableFactory;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Entities\WorkingTimeCalendar;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingWeekday;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingDate;
|
||||
use Espo\Core\Field\Date;
|
||||
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
class GlobalCalendar implements Calendar
|
||||
{
|
||||
private ?WorkingTimeCalendar $workingTimeCalendar = null;
|
||||
private ?SpecificCalendar $specificCalendar = null;
|
||||
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private Config $config,
|
||||
private InjectableFactory $injectableFactory,
|
||||
private Config\ApplicationConfig $applicationConfig,
|
||||
) {
|
||||
$this->initDefault();
|
||||
|
||||
if ($this->workingTimeCalendar) {
|
||||
$this->specificCalendar = $this->injectableFactory->createWithBinding(
|
||||
SpecificCalendar::class,
|
||||
BindingContainerBuilder::create()
|
||||
->bindInstance(WorkingTimeCalendar::class, $this->workingTimeCalendar)
|
||||
->build()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function initDefault(): void
|
||||
{
|
||||
$id = $this->config->get('workingTimeCalendarId');
|
||||
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->workingTimeCalendar = $this->entityManager->getEntityById(WorkingTimeCalendar::ENTITY_TYPE, $id);
|
||||
}
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
if ($this->specificCalendar) {
|
||||
return $this->specificCalendar->isAvailable();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getTimezone(): DateTimeZone
|
||||
{
|
||||
if ($this->specificCalendar) {
|
||||
return $this->specificCalendar->getTimezone();
|
||||
}
|
||||
|
||||
try {
|
||||
return new DateTimeZone($this->applicationConfig->getTimeZone());
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingWeekday[]
|
||||
*/
|
||||
public function getWorkingWeekdays(): array
|
||||
{
|
||||
if ($this->specificCalendar) {
|
||||
return $this->specificCalendar->getWorkingWeekdays();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getNonWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->specificCalendar) {
|
||||
return $this->specificCalendar->getNonWorkingDates($from, $to);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->specificCalendar) {
|
||||
return $this->specificCalendar->getWorkingDates($from, $to);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
196
application/Espo/Tools/WorkingTime/SpecificCalendar.php
Normal file
196
application/Espo/Tools/WorkingTime/SpecificCalendar.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Core\Utils\Config\ApplicationConfig;
|
||||
use Espo\Entities\WorkingTimeCalendar;
|
||||
use Espo\Entities\WorkingTimeRange;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Query\Part\Condition;
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
use Espo\ORM\Query\Part\Where\OrGroup;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingWeekday;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingDate;
|
||||
use Espo\Core\Field\Date;
|
||||
use Espo\Tools\WorkingTime\Util\CalendarUtil;
|
||||
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @since 8.4.0
|
||||
*/
|
||||
class SpecificCalendar implements Calendar
|
||||
{
|
||||
private ?CalendarUtil $util = null;
|
||||
|
||||
/** @var ?array{WorkingDate[], WorkingDate[]} */
|
||||
private ?array $cache = null;
|
||||
private ?string $cacheKey = null;
|
||||
private DateTimeZone $timezone;
|
||||
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private WorkingTimeCalendar $workingTimeCalendar,
|
||||
ApplicationConfig $applicationConfig,
|
||||
) {
|
||||
try {
|
||||
$this->timezone = new DateTimeZone($applicationConfig->getTimeZone());
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
$this->util = new CalendarUtil($this->workingTimeCalendar);
|
||||
|
||||
$this->timezone = $this->workingTimeCalendar->getTimeZone() ?? $this->timezone;
|
||||
}
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getTimezone(): DateTimeZone
|
||||
{
|
||||
return $this->timezone;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingWeekday[]
|
||||
*/
|
||||
public function getWorkingWeekdays(): array
|
||||
{
|
||||
return $this->workingTimeCalendar->getWorkingWeekdays();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getNonWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
return $this->getDates($from, $to)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
return $this->getDates($from, $to)[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{WorkingDate[], WorkingDate[]}
|
||||
*/
|
||||
private function getDates(Date $from, Date $to): array
|
||||
{
|
||||
$cacheKey = $from->toString() . '-' . $to->toString();
|
||||
|
||||
if ($this->cacheKey === $cacheKey) {
|
||||
assert($this->cache !== null);
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
$notWorkingList = [];
|
||||
$workingList = [];
|
||||
|
||||
$list = $this->fetchRanges($from, $to);
|
||||
|
||||
foreach ($list as $range) {
|
||||
$dates = $this->rangeToDates($range);
|
||||
|
||||
if ($range->getType() === WorkingTimeRange::TYPE_NON_WORKING) {
|
||||
$notWorkingList = array_merge($notWorkingList, $dates);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$workingList = array_merge($workingList, $dates);
|
||||
}
|
||||
|
||||
$this->cacheKey = $cacheKey;
|
||||
$this->cache = [$notWorkingList, $workingList];
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingTimeRange $range
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
private function rangeToDates(WorkingTimeRange $range): array
|
||||
{
|
||||
if (!$this->util) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->util->rangeToDates($range);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingTimeRange[]
|
||||
*/
|
||||
private function fetchRanges(Date $from, Date $to): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
$collection = $this->entityManager
|
||||
->getRDBRepositoryByClass(WorkingTimeRange::class)
|
||||
->leftJoin('calendars')
|
||||
->where(
|
||||
Condition::equal(
|
||||
Expression::column('calendars.id'),
|
||||
$this->workingTimeCalendar->getId()
|
||||
)
|
||||
)
|
||||
->where(
|
||||
OrGroup::create(
|
||||
Condition::greaterOrEqual(
|
||||
Expression::column('dateEnd'),
|
||||
$from->toString()
|
||||
),
|
||||
Condition::lessOrEqual(
|
||||
Expression::column('dateStart'),
|
||||
$to->toString()
|
||||
),
|
||||
)
|
||||
)
|
||||
->find();
|
||||
|
||||
foreach ($collection as $entity) {
|
||||
$list[] = $entity;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
244
application/Espo/Tools/WorkingTime/TeamCalendar.php
Normal file
244
application/Espo/Tools/WorkingTime/TeamCalendar.php
Normal file
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Entities\Team;
|
||||
use Espo\Entities\WorkingTimeCalendar;
|
||||
use Espo\Entities\WorkingTimeRange;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Query\Part\Condition;
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
use Espo\ORM\Query\Part\Where\OrGroup;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingWeekday;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingDate;
|
||||
|
||||
use Espo\Core\Field\Date;
|
||||
|
||||
use DateTimeZone;
|
||||
use Espo\Tools\WorkingTime\Util\CalendarUtil;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
class TeamCalendar implements Calendar
|
||||
{
|
||||
private ?WorkingTimeCalendar $workingTimeCalendar = null;
|
||||
private ?CalendarUtil $util = null;
|
||||
|
||||
/** @var ?array{WorkingDate[], WorkingDate[]} */
|
||||
private ?array $cache = null;
|
||||
private ?string $cacheKey = null;
|
||||
private DateTimeZone $timezone;
|
||||
|
||||
public function __construct(
|
||||
private Team $team,
|
||||
private EntityManager $entityManager,
|
||||
private Config $config,
|
||||
Config\ApplicationConfig $applicationConfig,
|
||||
) {
|
||||
try {
|
||||
$this->timezone = new DateTimeZone($applicationConfig->getTimeZone());
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
$this->init();
|
||||
|
||||
if (!$this->workingTimeCalendar) {
|
||||
$this->initDefault();
|
||||
}
|
||||
|
||||
if ($this->workingTimeCalendar) {
|
||||
$this->util = new CalendarUtil($this->workingTimeCalendar);
|
||||
|
||||
$this->timezone = $this->workingTimeCalendar->getTimeZone() ?? $this->timezone;
|
||||
}
|
||||
}
|
||||
|
||||
private function init(): void
|
||||
{
|
||||
$workingTimeCalendarLink = $this->team->getWorkingTimeCalendar();
|
||||
|
||||
if (!$workingTimeCalendarLink) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->workingTimeCalendar = $this->entityManager
|
||||
->getRepositoryByClass(WorkingTimeCalendar::class)
|
||||
->getById($workingTimeCalendarLink->getId());
|
||||
}
|
||||
|
||||
private function initDefault(): void
|
||||
{
|
||||
$id = $this->config->get('workingTimeCalendarId');
|
||||
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->workingTimeCalendar = $this->entityManager->getEntityById(WorkingTimeCalendar::ENTITY_TYPE, $id);
|
||||
}
|
||||
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return $this->workingTimeCalendar !== null;
|
||||
}
|
||||
|
||||
public function getTimezone(): DateTimeZone
|
||||
{
|
||||
return $this->timezone;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingWeekday[]
|
||||
*/
|
||||
public function getWorkingWeekdays(): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->workingTimeCalendar->getWorkingWeekdays();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getNonWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->getDates($from, $to)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->getDates($from, $to)[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{WorkingDate[], WorkingDate[]}
|
||||
*/
|
||||
private function getDates(Date $from, Date $to): array
|
||||
{
|
||||
$cacheKey = $from->toString() . '-' . $to->toString();
|
||||
|
||||
if ($this->cacheKey === $cacheKey) {
|
||||
assert($this->cache !== null);
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
$notWorkingList = [];
|
||||
$workingList = [];
|
||||
|
||||
$list = $this->fetchRanges($from, $to);
|
||||
|
||||
foreach ($list as $range) {
|
||||
$dates = $this->rangeToDates($range);
|
||||
|
||||
if ($range->getType() === WorkingTimeRange::TYPE_NON_WORKING) {
|
||||
$notWorkingList = array_merge($notWorkingList, $dates);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$workingList = array_merge($workingList, $dates);
|
||||
}
|
||||
|
||||
$this->cacheKey = $cacheKey;
|
||||
$this->cache = [$notWorkingList, $workingList];
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingTimeRange $range
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
private function rangeToDates(WorkingTimeRange $range): array
|
||||
{
|
||||
if (!$this->util) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->util->rangeToDates($range);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingTimeRange[]
|
||||
*/
|
||||
private function fetchRanges(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$list = [];
|
||||
|
||||
$collection = $this->entityManager
|
||||
->getRDBRepositoryByClass(WorkingTimeRange::class)
|
||||
->leftJoin('calendars')
|
||||
->where(
|
||||
Condition::equal(
|
||||
Expression::column('calendars.id'),
|
||||
$this->workingTimeCalendar->getId()
|
||||
)
|
||||
)
|
||||
->where(
|
||||
OrGroup::create(
|
||||
Condition::greaterOrEqual(
|
||||
Expression::column('dateEnd'),
|
||||
$from->toString()
|
||||
),
|
||||
Condition::lessOrEqual(
|
||||
Expression::column('dateStart'),
|
||||
$to->toString()
|
||||
),
|
||||
)
|
||||
)
|
||||
->find();
|
||||
|
||||
foreach ($collection as $entity) {
|
||||
$list[] = $entity;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
330
application/Espo/Tools/WorkingTime/UserCalendar.php
Normal file
330
application/Espo/Tools/WorkingTime/UserCalendar.php
Normal file
@@ -0,0 +1,330 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Entities\Team;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Entities\WorkingTimeCalendar;
|
||||
use Espo\Entities\WorkingTimeRange;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Query\Part\Condition;
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
use Espo\ORM\Query\Part\Where\OrGroup;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingWeekday;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingDate;
|
||||
use Espo\Core\Field\Date;
|
||||
use Espo\Tools\WorkingTime\Util\CalendarUtil;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
class UserCalendar implements Calendar
|
||||
{
|
||||
private ?WorkingTimeCalendar $workingTimeCalendar = null;
|
||||
private ?CalendarUtil $util = null;
|
||||
|
||||
/** @var ?array{WorkingDate[], WorkingDate[]} */
|
||||
private ?array $cache = null;
|
||||
private ?string $cacheKey = null;
|
||||
private DateTimeZone $timezone;
|
||||
|
||||
public function __construct(
|
||||
private User $user,
|
||||
private EntityManager $entityManager,
|
||||
private Config $config,
|
||||
Config\ApplicationConfig $applicationConfig,
|
||||
) {
|
||||
try {
|
||||
$this->timezone = new DateTimeZone($applicationConfig->getTimeZone());
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
$this->init();
|
||||
|
||||
if (!$this->workingTimeCalendar) {
|
||||
$this->initDefault();
|
||||
}
|
||||
|
||||
if ($this->workingTimeCalendar) {
|
||||
$this->util = new CalendarUtil($this->workingTimeCalendar);
|
||||
|
||||
$this->timezone = $this->workingTimeCalendar->getTimeZone() ?? $this->timezone;
|
||||
}
|
||||
}
|
||||
|
||||
private function init(): void
|
||||
{
|
||||
$workingTimeCalendarLink = $this->user->getWorkingTimeCalendar();
|
||||
|
||||
if ($workingTimeCalendarLink) {
|
||||
$this->workingTimeCalendar = $this->entityManager
|
||||
->getRepositoryByClass(WorkingTimeCalendar::class)
|
||||
->getById($workingTimeCalendarLink->getId());
|
||||
|
||||
if ($this->workingTimeCalendar) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$defaultTeamLink = $this->user->getDefaultTeam();
|
||||
|
||||
if (!$defaultTeamLink) {
|
||||
return;
|
||||
}
|
||||
|
||||
$team = $this->entityManager
|
||||
->getRepositoryByClass(Team::class)
|
||||
->getById($defaultTeamLink->getId());
|
||||
|
||||
if (!$team) {
|
||||
return;
|
||||
}
|
||||
|
||||
$workingTimeCalendarLink = $team->getWorkingTimeCalendar();
|
||||
|
||||
if (!$workingTimeCalendarLink) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->workingTimeCalendar = $this->entityManager
|
||||
->getRepositoryByClass(WorkingTimeCalendar::class)
|
||||
->getById($workingTimeCalendarLink->getId());
|
||||
}
|
||||
|
||||
private function initDefault(): void
|
||||
{
|
||||
$id = $this->config->get('workingTimeCalendarId');
|
||||
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->workingTimeCalendar = $this->entityManager->getEntityById(WorkingTimeCalendar::ENTITY_TYPE, $id);
|
||||
}
|
||||
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return $this->workingTimeCalendar !== null;
|
||||
}
|
||||
|
||||
public function getTimezone(): DateTimeZone
|
||||
{
|
||||
return $this->timezone;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingWeekday[]
|
||||
*/
|
||||
public function getWorkingWeekdays(): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->workingTimeCalendar->getWorkingWeekdays();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getNonWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->getDates($from, $to)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function getWorkingDates(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->getDates($from, $to)[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{WorkingDate[], WorkingDate[]}
|
||||
*/
|
||||
private function getDates(Date $from, Date $to): array
|
||||
{
|
||||
$cacheKey = $from->toString() . '-' . $to->toString();
|
||||
|
||||
if ($this->cacheKey === $cacheKey) {
|
||||
assert($this->cache !== null);
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
$notWorkingList = [];
|
||||
$workingList = [];
|
||||
|
||||
$listForUser = $this->fetchUserRanges($from, $to);
|
||||
$list = $this->fetchRanges($from, $to);
|
||||
|
||||
foreach ($listForUser as $range) {
|
||||
$dates = $this->rangeToDates($range);
|
||||
|
||||
if ($range->getType() === WorkingTimeRange::TYPE_NON_WORKING) {
|
||||
$notWorkingList = array_merge($notWorkingList, $dates);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$workingList = array_merge($workingList, $dates);
|
||||
}
|
||||
|
||||
$metMap = [];
|
||||
|
||||
foreach (array_merge($notWorkingList, $workingList) as $date) {
|
||||
$metMap[$date->getDate()->getString()] = true;
|
||||
}
|
||||
|
||||
foreach ($list as $range) {
|
||||
$dates = array_filter(
|
||||
$this->rangeToDates($range),
|
||||
function (WorkingDate $date) use ($metMap) {
|
||||
return !array_key_exists($date->getDate()->toString(), $metMap);
|
||||
}
|
||||
);
|
||||
|
||||
if ($range->getType() === WorkingTimeRange::TYPE_NON_WORKING) {
|
||||
$notWorkingList = array_merge($notWorkingList, $dates);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$workingList = array_merge($workingList, $dates);
|
||||
}
|
||||
|
||||
$this->cacheKey = $cacheKey;
|
||||
$this->cache = [$notWorkingList, $workingList];
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingTimeRange $range
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
private function rangeToDates(WorkingTimeRange $range): array
|
||||
{
|
||||
if (!$this->util) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->util->rangeToDates($range);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingTimeRange[]
|
||||
*/
|
||||
private function fetchUserRanges(Date $from, Date $to): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
$collection = $this->entityManager
|
||||
->getRDBRepositoryByClass(WorkingTimeRange::class)
|
||||
->leftJoin('users')
|
||||
->where(
|
||||
Condition::equal(
|
||||
Expression::column('users.id'),
|
||||
$this->user->getId()
|
||||
)
|
||||
)
|
||||
->where(
|
||||
OrGroup::create(
|
||||
Condition::greaterOrEqual(
|
||||
Expression::column('dateEnd'),
|
||||
$from->toString()
|
||||
),
|
||||
Condition::lessOrEqual(
|
||||
Expression::column('dateStart'),
|
||||
$to->toString()
|
||||
),
|
||||
)
|
||||
)
|
||||
->find();
|
||||
|
||||
foreach ($collection as $entity) {
|
||||
$list[] = $entity;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WorkingTimeRange[]
|
||||
*/
|
||||
private function fetchRanges(Date $from, Date $to): array
|
||||
{
|
||||
if ($this->workingTimeCalendar === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$list = [];
|
||||
|
||||
$collection = $this->entityManager
|
||||
->getRDBRepositoryByClass(WorkingTimeRange::class)
|
||||
->leftJoin('calendars')
|
||||
->where(
|
||||
Condition::equal(
|
||||
Expression::column('calendars.id'),
|
||||
$this->workingTimeCalendar->getId()
|
||||
)
|
||||
)
|
||||
->where(
|
||||
OrGroup::create(
|
||||
Condition::greaterOrEqual(
|
||||
Expression::column('dateEnd'),
|
||||
$from->toString()
|
||||
),
|
||||
Condition::lessOrEqual(
|
||||
Expression::column('dateStart'),
|
||||
$to->toString()
|
||||
),
|
||||
)
|
||||
)
|
||||
->find();
|
||||
|
||||
foreach ($collection as $entity) {
|
||||
$list[] = $entity;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
74
application/Espo/Tools/WorkingTime/Util/CalendarUtil.php
Normal file
74
application/Espo/Tools/WorkingTime/Util/CalendarUtil.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\WorkingTime\Util;
|
||||
|
||||
use Espo\Entities\WorkingTimeCalendar;
|
||||
use Espo\Entities\WorkingTimeRange;
|
||||
use Espo\Tools\WorkingTime\Calendar\WorkingDate;
|
||||
|
||||
class CalendarUtil
|
||||
{
|
||||
private WorkingTimeCalendar $workingTimeCalendar;
|
||||
|
||||
public function __construct(WorkingTimeCalendar $workingTimeCalendar)
|
||||
{
|
||||
$this->workingTimeCalendar = $workingTimeCalendar;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WorkingTimeRange $range
|
||||
* @return WorkingDate[]
|
||||
*/
|
||||
public function rangeToDates(WorkingTimeRange $range): array
|
||||
{
|
||||
$isWorking = $range->getType() === WorkingTimeRange::TYPE_WORKING;
|
||||
|
||||
$list = [];
|
||||
|
||||
$pointer = $range->getDateStart();
|
||||
$endPlusOne = $range->getDateEnd()->modify('+1 day');
|
||||
|
||||
$defaultTimeRanges = $this->workingTimeCalendar->getTimeRanges();
|
||||
|
||||
while ($pointer->isLessThan($endPlusOne)) {
|
||||
$timeRanges = $isWorking ? $range->getTimeRanges() : [];
|
||||
|
||||
if ($isWorking && $timeRanges === null) {
|
||||
$timeRanges = $defaultTimeRanges;
|
||||
}
|
||||
|
||||
$list[] = new WorkingDate($pointer, $timeRanges ?? []);
|
||||
|
||||
$pointer = $pointer->modify('+1 day');
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user