Skip to content

Commit

Permalink
#122 [Trigger] add: trigger event creation for timespent
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Jan 31, 2023
1 parent 1fd4672 commit 80f432b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
1 change: 1 addition & 0 deletions core/modules/modDoliSIRH.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ public function init($options = '')
$extra_fields->update('fk_task', 'Tâche', 'sellist', '', 'ticket', 0, 0, 100, $param, 1, 1, '1', '','','',0);
$extra_fields->addExtraField('fk_task', 'Tâche', 'sellist', 100, null, 'ticket', 0, 0, null, $param, 1, 1, '1', '','',0); //extrafields ticket
unset($param);
$extra_fields->addExtraField('timespent', $langs->trans('MarkYourTime'), 'boolean', 100, null, 'actioncomm', 0, 0, null, 'a:1:{s:7:"options";a:1:{s:0:"";N;}}', 1, '$user->rights->projet->time', '3', '','',0, 'dolisirh@dolisirh', '$conf->dolisirh->enabled'); //extrafields ticket

// Document models
delDocumentModel('timesheetdocument_odt', 'timesheetdocument');
Expand Down
108 changes: 76 additions & 32 deletions core/triggers/interface_99_modDolisirh_DolisirhTriggers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function __construct(DoliDB $db)
$this->db = $db;

$this->name = preg_replace('/^Interface/i', '', get_class($this));
$this->family = "demo";
$this->description = "DoliSIRH triggers.";
$this->family = 'demo';
$this->description = 'DoliSIRH triggers.';
// 'development', 'experimental', 'dolibarr' or version
$this->version = '1.2.0';
$this->picto = 'dolisirh@dolisirh';
Expand Down Expand Up @@ -90,9 +90,9 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
switch ($action) {
// Actions
case 'ACTION_CREATE':
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
if (((int) $object->fk_element) > 0 && $object->elementtype == 'ticket' && preg_match('/^TICKET_/', $object->code)) {
dol_syslog("Add time spent");
dol_syslog('Add time spent');
$result= 0;
$ticket = new Ticket($this->db);
$result = $ticket->fetch($object->fk_element);
Expand All @@ -114,7 +114,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$name_message = $task->ref;

$task->addTimeSpent($user);
setEventMessages($langs->trans("MessageTimeSpentCreate").' : '.'<a href="'.DOL_URL_ROOT.'/projet/tasks/time.php?id='.$id_message.'">'.$name_message.'</a>', array());
setEventMessages($langs->trans('MessageTimeSpentCreate').' : '.'<a href="'.DOL_URL_ROOT.'/projet/tasks/time.php?id='.$id_message.'">'.$name_message.'</a>', array());
} else {
setEventMessages($task->error, $task->errors, 'errors');
return -1;
Expand All @@ -125,17 +125,61 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
return -1;
}
}
break;
if ($object->element == 'action' && $object->array_options['options_timespent'] == 1 && $object->fk_element > 0 && $object->elementtype == 'task' && !empty($object->datef)) {
require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php';
$task = new Task($this->db);
$result = $task->fetch($object->fk_element);
if ($result > 0 && $task->id > 0) {
$contactsOfTask = $task->getListContactId();
if (in_array($user->id, $contactsOfTask)) {
$task->timespent_date = $object->datep;
$task->timespent_withhour = 1;
$task->timespent_note = $object->label;
$task->timespent_duration = $object->datef - $object->datep;
$task->timespent_fk_user = $user->id;

$idMessage = $task->id;
$nameMessage = $task->ref;

if ($task->timespent_duration > 0) {
$result = $task->addTimeSpent($user);
} else {
$result = -1;
$task->error = $langs->trans('ErrorTimeSpentDurationCantBeNegative');
}

if ($result > 0) {
setEventMessages($langs->trans('MessageTimeSpentCreate') . ' : ' . '<a href="' . DOL_URL_ROOT . '/projet/tasks/time.php?id=' . $idMessage . '">' . $nameMessage . '</a>', []);
} else {
setEventMessages($task->error, $task->errors, 'errors');
return -1;
}
} else {
setEventMessages($langs->trans('ErrorUserNotAssignedToTask'), $task->errors, 'errors');
return -1;
}
} else {
setEventMessages($task->error, $task->errors, 'errors');
return -1;
}
} elseif ($object->element == 'action' && $object->array_options['options_timespent'] == 1 && $object->elementtype != 'task') {
setEventMessages('MissingTaskWithTimeSpentOption', $object->errors, 'errors');
return -1;
} elseif ($object->element == 'action' && $object->array_options['options_timespent'] == 1 && empty($object->datef)) {
setEventMessages('MissingEndDateWithTimeSpentOption', $object->errors, 'errors');
return -1;
}
break;

case 'BILL_CREATE':
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once __DIR__ . '/../../lib/dolisirh_function.lib.php';
$categories = GETPOST('categories', 'array:int');
setCategoriesObject($categories, 'invoice', false, $object);
break;

case 'BILLREC_CREATE':
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
require_once __DIR__ . '/../../lib/dolisirh_function.lib.php';
$cat = new Categorie($this->db);
Expand All @@ -152,7 +196,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

// Timesheet
case 'TIMESHEET_CREATE' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';

Expand All @@ -174,7 +218,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$now = dol_now();

if ($conf->global->DOLISIRH_PRODUCT_SERVICE_SET) {
$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities("MealTicket")))));
$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities('MealTicket')))));
$objectline->date_creation = $object->db->idate($now);
$objectline->qty = 0;
$objectline->rang = 1;
Expand All @@ -184,7 +228,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$objectline->product_type = 0;
$objectline->insert($user);

$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities("JourneySubscription")))));
$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities('JourneySubscription')))));
$objectline->date_creation = $object->db->idate($now);
$objectline->qty = 0;
$objectline->rang = 2;
Expand All @@ -194,7 +238,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$objectline->product_type = 1;
$objectline->insert($user);

$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities("13thMonthBonus")))));
$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities('13thMonthBonus')))));
$objectline->date_creation = $object->db->idate($now);
$objectline->qty = 0;
$objectline->rang = 3;
Expand All @@ -204,7 +248,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$objectline->product_type = 1;
$objectline->insert($user);

$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities("SpecialBonus")))));
$product->fetch('', dol_sanitizeFileName(dol_string_nospecial(trim($langs->transnoentities('SpecialBonus')))));
$objectline->date_creation = $object->db->idate($now);
$objectline->qty = 0;
$objectline->rang = 4;
Expand All @@ -228,7 +272,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'TIMESHEET_MODIFY' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -246,7 +290,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'TIMESHEET_DELETE' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -264,7 +308,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'TIMESHEET_VALIDATE' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -282,7 +326,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'TIMESHEET_UNVALIDATE' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -300,7 +344,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'TIMESHEET_LOCKED' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -318,7 +362,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'TIMESHEET_ARCHIVED' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -337,7 +381,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

// Certificate
case 'CERTIFICATE_CREATE' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';

Expand Down Expand Up @@ -368,7 +412,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'CERTIFICATE_MODIFY' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -386,7 +430,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'CERTIFICATE_DELETE' :
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -405,7 +449,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

case 'ECMFILES_CREATE' :
if ($object->src_object_type == 'dolisirh_timesheet') {
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . '. id=' . $object->id);
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';

require_once __DIR__ . '/../../class/timesheet.class.php';
Expand All @@ -418,7 +462,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

if (!empty($signatories) && $signatories > 0) {
foreach ($signatories as $signatory) {
$signatory->signature = $langs->transnoentities("FileGenerated");
$signatory->signature = $langs->transnoentities('FileGenerated');
$signatory->update($user, false);
}
}
Expand All @@ -437,7 +481,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'DOLISIRHSIGNATURE_ADDATTENDANT' :
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . '. id=' . $object->id);
require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -458,7 +502,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
break;

case 'DOLISIRHSIGNATURE_SIGNED' :
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . '. id=' . $object->id);
require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -482,7 +526,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

case 'DOLISIRHSIGNATURE_PENDING_SIGNATURE' :

dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . '. id=' . $object->id);
require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -504,7 +548,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

case 'DOLISIRHSIGNATURE_ABSENT' :

dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . '. id=' . $object->id);
require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -526,7 +570,7 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

case 'DOLISIRHSIGNATURE_DELETED' :

dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . '. id=' . $object->id);
require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
$now = dol_now();
$actioncomm = new ActionComm($this->db);
Expand All @@ -546,9 +590,9 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$actioncomm->create($user);
break;

default:
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
break;
default:
dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__. '. id=' .$object->id);
break;
}
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions langs/fr_FR/dolisirh.lang
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ ShowOnlyTasksWithTimeSpent = Afficher uniquement les tâches avec du temps conso
ExpectedWorkedHours = Heures de travail prévues
ConsumedWorkedHours = Heures de travail consommées
TimeSpentReportByFiscalYear = Rapport du temps consommé sur l'exercice fiscal
MarkYourTime = Pointer son temps
MissingTaskWithTimeSpentOption = Erreur : il faut choisir une tâche pour pointer son temps
MissingEndDateWithTimeSpentOption = Erreur : il faut choisir une date de fin d'événement pour pointer son temps
ErrorTimeSpentDurationCantBeNegative = Erreur : la date de fin d'événement ne peut pas être une valeur négative



Expand Down

0 comments on commit 80f432b

Please sign in to comment.