Skip to content

EspoCRM – Open Source CRM Application

Event date end auto calculation

Yurii31cd14d2ab00
diff --git a/application/Espo/Core/Repositories/Event.php b/application/Espo/Core/Repositories/Event.php
index ee8ba153ff..7c2dc53da2 100644
--- a/application/Espo/Core/Repositories/Event.php
+++ b/application/Espo/Core/Repositories/Event.php
@@ -29,6 +29,8 @@
namespace Espo\Core\Repositories;
+use Espo\Core\Field\Date;
+use Espo\Core\Field\DateTime as DateTimeField;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Modules\Crm\Entities\Meeting;
use Espo\Modules\Crm\Entities\Reminder;
@@ -52,6 +54,8 @@ class Event extends Database implements
use Di\DateTimeSetter;
use Di\ConfigSetter;
+ private const string FIELD_DURATION = 'duration';
+
/**
* @param array<string, mixed> $options
* @return void
@@ -112,6 +116,29 @@ class Event extends Database implements
}
}
+ if ($entity->hasAttribute(self::FIELD_DURATION)) {
+ if ($entity->get(Meeting::FIELD_DATE_START) && !$entity->get(Meeting::FIELD_DATE_END)) {
+ $start = DateTimeField::fromString($entity->get(Meeting::FIELD_DATE_START));
+ $duration = $entity->get(self::FIELD_DURATION) ?? 0;
+
+ $end = $start->addSeconds($duration);
+
+ $entity->set(Meeting::FIELD_DATE_END, $end->toString());
+ }
+
+ if ($entity->get(Meeting::FIELD_DATE_START_DATE) && !$entity->get(Meeting::FIELD_DATE_END_DATE)) {
+ $start = Date::fromString($entity->get(Meeting::FIELD_DATE_START_DATE));
+ $duration = $entity->get(self::FIELD_DURATION) ?? 0;
+
+ $days = (int) floor($duration / (3600 * 24));
+
+ $end = $start->addDays($days - 1);
+
+ $entity->set(Meeting::FIELD_DATE_END_DATE, $end->toString());
+ }
+ }
+
+
parent::beforeSave($entity, $options);
}
diff --git a/application/Espo/Modules/Crm/Entities/Call.php b/application/Espo/Modules/Crm/Entities/Call.php
index b8d30e79c2..b9835f5d28 100644
--- a/application/Espo/Modules/Crm/Entities/Call.php
+++ b/application/Espo/Modules/Crm/Entities/Call.php
@@ -102,6 +102,15 @@ class Call extends Entity
return $this;
}
+ /**
+ * @param int $duration Duration in seconds.
+ * @since 10.0.6
+ */
+ public function setDuration(int $duration): self
+ {
+ return $this->set('duration', $duration);
+ }
+
public function setAssignedUserId(?string $assignedUserId): self
{
$this->set('assignedUserId', $assignedUserId);
diff --git a/application/Espo/Modules/Crm/Entities/Meeting.php b/application/Espo/Modules/Crm/Entities/Meeting.php
index bd3e00f8bd..f13e26bb3e 100644
--- a/application/Espo/Modules/Crm/Entities/Meeting.php
+++ b/application/Espo/Modules/Crm/Entities/Meeting.php
@@ -124,6 +124,15 @@ class Meeting extends Entity
return $this;
}
+ /**
+ * @param int $duration Duration in seconds.
+ * @since 10.0.6
+ */
+ public function setDuration(int $duration): self
+ {
+ return $this->set('duration', $duration);
+ }
+
public function setAssignedUserId(?string $assignedUserId): self
{
$this->set('assignedUserId', $assignedUserId);
diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json
index 4d512bbcdb..9d07f60a57 100644
--- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json
+++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json
@@ -29,7 +29,8 @@
"type": "datetime",
"required": true,
"after": "dateStart",
- "afterOrEqual": true
+ "afterOrEqual": true,
+ "suppressValidationList": ["required"]
},
"duration": {
"type": "duration",
diff --git a/tests/integration/Espo/Event/EventTest.php b/tests/integration/Espo/Event/EventTest.php
new file mode 100644
index 0000000000..1f229cf541
--- /dev/null
+++ b/tests/integration/Espo/Event/EventTest.php
@@ -0,0 +1,100 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 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 tests\integration\Espo\Event;
+
+use Espo\Core\Field\DateTime;
+use Espo\Core\Field\DateTimeOptional;
+use Espo\Modules\Crm\Entities\Call;
+use Espo\Modules\Crm\Entities\Meeting;
+use tests\integration\Core\BaseTestCase;
+
+class EventTest extends BaseTestCase
+{
+ public function testMeetingAutomaticDateEnd(): void
+ {
+ $em = $this->getEntityManager();
+
+ $meeting = $em->getRDBRepositoryByClass(Meeting::class)->getNew();
+ $meeting->setDateStart(DateTimeOptional::fromString('2030-01-01 10:00'));
+ $meeting->setDuration(60);
+ $em->saveEntity($meeting);
+ $em->refreshEntity($meeting);
+
+ $this->assertEquals('2030-01-01 10:01:00', $meeting->getDateEnd()?->toString());
+
+ //
+
+ $meeting = $em->getRDBRepositoryByClass(Meeting::class)->getNew();
+ $meeting->setDateStart(DateTimeOptional::fromString('2030-01-01 10:00'));
+ $meeting->setDateEnd(DateTimeOptional::fromString('2030-01-01 11:00'));
+ $em->saveEntity($meeting);
+ $em->refreshEntity($meeting);
+
+ $this->assertEquals('2030-01-01 11:00:00', $meeting->getDateEnd()?->toString());
+
+ //
+
+ $meeting = $em->getRDBRepositoryByClass(Meeting::class)->getNew();
+ $meeting->setIsAllDay(true);
+ $meeting->setDateStart(DateTimeOptional::fromString('2030-01-01'));
+ $meeting->setDuration(3600 * 24 * 2);
+ $em->saveEntity($meeting);
+ $em->refreshEntity($meeting);
+
+ $this->assertEquals('2030-01-02', $meeting->getDateEnd()?->toString());
+
+ //
+
+ $meeting = $em->getRDBRepositoryByClass(Meeting::class)->getNew();
+ $meeting->setIsAllDay(true);
+ $meeting->setDateStart(DateTimeOptional::fromString('2030-01-01'));
+ $meeting->setDateEnd(DateTimeOptional::fromString('2030-01-02'));
+ $em->saveEntity($meeting);
+ $em->refreshEntity($meeting);
+
+ $this->assertEquals('2030-01-02', $meeting->getDateEnd()?->toString());
+ }
+
+ public function testCallAutomaticDateEnd(): void
+ {
+ $em = $this->getEntityManager();
+
+ $call = $em->getRDBRepositoryByClass(Call::class)->getNew();
+
+ $call->setDateStart(DateTime::fromString('2030-01-01 10:00'));
+ $call->setDuration(60);
+
+ $em->saveEntity($call);
+
+ $em->refreshEntity($call);
+
+ $this->assertEquals('2030-01-01 10:01:00', $call->getDateEnd()?->toString());
+ }
+}