Skip to content

Commit

Permalink
Parity change on assigning a resource to an event from the event modi…
Browse files Browse the repository at this point in the history
…fication page
  • Loading branch information
Elisée Chemin committed Nov 7, 2024
1 parent de56510 commit 8db5a85
Showing 1 changed file with 20 additions and 41 deletions.
61 changes: 20 additions & 41 deletions htdocs/resource/element_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/resource.lib.php';
if (isModEnabled('project')) {
require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
Expand Down Expand Up @@ -129,53 +130,31 @@
$eventDateStart = $objstat->datep;
$eventDateEnd = $objstat->datef;
$isFullDayEvent = $objstat->fulldayevent;
if (empty($eventDateEnd)) {
if ($isFullDayEvent) {
$eventDateStartArr = dol_getdate($eventDateStart);
$eventDateStart = dol_mktime(0, 0, 0, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
$eventDateEnd = dol_mktime(23, 59, 59, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
}
if (empty($eventDateEnd) && $isFullDayEvent) {
$eventDateStartArr = dol_getdate($eventDateStart);
$eventDateStart = dol_mktime(0, 0, 0, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
$eventDateEnd = dol_mktime(23, 59, 59, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
}

$sql = "SELECT er.rowid, r.ref as r_ref, ac.id as ac_id, ac.label as ac_label";
$sql .= " FROM ".MAIN_DB_PREFIX."element_resources as er";
$sql .= " INNER JOIN ".MAIN_DB_PREFIX."resource as r ON r.rowid = er.resource_id AND er.resource_type = '".$db->escape($resource_type)."'";
$sql .= " INNER JOIN ".MAIN_DB_PREFIX."actioncomm as ac ON ac.id = er.element_id AND er.element_type = '".$db->escape($objstat->element)."'";
$sql .= " WHERE er.resource_id = ".((int) $resource_id);
$sql .= " AND er.busy = 1";
$sql .= " AND (";
// Get the ressources busy during the period with the id "id"
$busyResources = getBusyResourcesInPeriod($eventDateStart, $eventDateEnd, array((int) $resource_id));

// event date start between ac.datep and ac.datep2 (if datep2 is null we consider there is no end)
$sql .= " (ac.datep <= '".$db->idate($eventDateStart)."' AND (ac.datep2 IS NULL OR ac.datep2 >= '".$db->idate($eventDateStart)."'))";
// event date end between ac.datep and ac.datep2
if (!empty($eventDateEnd)) {
$sql .= " OR (ac.datep <= '".$db->idate($eventDateEnd)."' AND (ac.datep2 >= '".$db->idate($eventDateEnd)."'))";
}
// event date start before ac.datep and event date end after ac.datep2
$sql .= " OR (";
$sql .= "ac.datep >= '".$db->idate($eventDateStart)."'";
if (!empty($eventDateEnd)) {
$sql .= " AND (ac.datep2 IS NOT NULL AND ac.datep2 <= '".$db->idate($eventDateEnd)."')";
}
$sql .= ")";

$sql .= ")";
$resql = $db->query($sql);
if (!$resql) {
// sql error
if ($busyResources === false) {
// an error occurred in the sql
$error++;
$objstat->error = $db->lasterror();
$objstat->error = $db->lasterror();
$objstat->errors[] = $objstat->error;
} else {
if ($db->num_rows($resql) > 0) {
// Resource already in use
$error++;
$objstat->error = $langs->trans('ErrorResourcesAlreadyInUse').' : ';
while ($obj = $db->fetch_object($resql)) {
$objstat->error .= '<br> - '.$langs->trans('ErrorResourceUseInEvent', $obj->r_ref, $obj->ac_label.' ['.$obj->ac_id.']');
}
$objstat->errors[] = $objstat->error;
} elseif (!empty($busyResources)) {
// Resource already in use
$error++;

$objstat->error = $langs->trans('ErrorResourcesAlreadyInUse') . ' : ' ;
foreach ($busyResources as $row) {
$objstat->error .= '<br> - '.$langs->trans('ErrorResourceUseInEvent', $row->r_ref, $row->ac_label.' ['.$row->ac_id.']');
}
$db->free($resql);

$objstat->errors[] = $objstat->error;
}
}

Expand Down

0 comments on commit 8db5a85

Please sign in to comment.