diff --git a/htdocs/core/triggers/interface_20_modWorkflow_WorkflowManager.class.php b/htdocs/core/triggers/interface_20_modWorkflow_WorkflowManager.class.php index 048dc170c0a..1e98ec885bd 100644 --- a/htdocs/core/triggers/interface_20_modWorkflow_WorkflowManager.class.php +++ b/htdocs/core/triggers/interface_20_modWorkflow_WorkflowManager.class.php @@ -424,6 +424,41 @@ class InterfaceWorkflowManager extends DolibarrTriggers } } + if ($action == 'TICKET_CREATE') { + dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id); + // Auto link contract + if (!empty($conf->contract->enabled) && !empty($conf->ticket->enabled) && !empty($conf->ficheinter->enabled) && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_TICKET_LINK_CONTRACT) && !empty($conf->global->TICKET_PRODUCT_CATEGORY) && !empty($object->fk_soc)) { + + $societe = new Societe($this->db); + $company_ids = (empty($conf->global->WORKFLOW_TICKET_USE_PARENT_COMPANY_CONTRACTS)) ? [$object->fk_soc] : $societe->getParentsForCompany($object->fk_soc, [$object->fk_soc]); + + $contrat = new Contrat($this->db); + $number_contracts_found = 0; + foreach ($company_ids as $company_id) { + $contrat->socid = $company_id; + + $list = $contrat->getListOfContracts($option = 'all', $status = [Contrat::STATUS_DRAFT, Contrat::STATUS_VALIDATED], $product_categories = [$conf->global->TICKET_PRODUCT_CATEGORY], $line_status = [ContratLigne::STATUS_INITIAL, ContratLigne::STATUS_OPEN]); + if (is_array($list) && !empty($list)) { + $number_contracts_found = count($list); + if ($number_contracts_found == 1) { + $contractid = $list[0]->id; + $object->setContract($contractid); + break; + } elseif ($number_contracts_found > 1) { + foreach($list as $linked_contract) { + $object->setContract($linked_contract->id); + // don't set '$contractid' so it is not used when creating an intervention. + } + setEventMessage($langs->trans('TicketManyContractsLinked'), 'warnings'); + break; + } + } + } + if ($number_contracts_found == 0) { + setEventMessage($langs->trans('TicketNoContractFoundToLink'), 'mesgs'); + } + } + } return 0; } diff --git a/htdocs/install/mysql/migration/15.0.0-16.0.0.sql b/htdocs/install/mysql/migration/15.0.0-16.0.0.sql index 5e9a0250a38..042ae751e06 100644 --- a/htdocs/install/mysql/migration/15.0.0-16.0.0.sql +++ b/htdocs/install/mysql/migration/15.0.0-16.0.0.sql @@ -110,6 +110,8 @@ INSERT INTO llx_c_action_trigger (code,label,description,elementtype,rang) value ALTER TABLE llx_ticket ADD COLUMN date_last_msg_sent datetime AFTER date_read; +UPDATE llx_const SET name = 'WORKFLOW_TICKET_LINK_CONTRACT' WHERE name = 'TICKET_AUTO_ASSIGN_CONTRACT_CREATE'; + CREATE TABLE llx_stock_mouvement_extrafields ( rowid integer AUTO_INCREMENT PRIMARY KEY, tms timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, diff --git a/htdocs/langs/en_US/interventions.lang b/htdocs/langs/en_US/interventions.lang index ef5df43e546..7c117fcd1f2 100644 --- a/htdocs/langs/en_US/interventions.lang +++ b/htdocs/langs/en_US/interventions.lang @@ -66,3 +66,4 @@ RepeatableIntervention=Template of intervention ToCreateAPredefinedIntervention=To create a predefined or recurring intervention, create a common intervention and convert it into intervention template ConfirmReopenIntervention=Are you sure you want to open back the intervention %s? GenerateInter=Generate intervention +FichinterNoContractLinked=Intervention %s has been created without a linked contract. diff --git a/htdocs/langs/en_US/ticket.lang b/htdocs/langs/en_US/ticket.lang index 310ac8dd7f6..204f7c0ed60 100644 --- a/htdocs/langs/en_US/ticket.lang +++ b/htdocs/langs/en_US/ticket.lang @@ -258,6 +258,8 @@ TicketNotCreatedFromPublicInterface=Not available. Ticket was not created from p ErrorTicketRefRequired=Ticket reference name is required TicketsDelayForFirstResponseTooLong=Too much time elapsed since ticket opening without any answer. TicketsDelayFromLastResponseTooLong=Too much time elapsed since last answer on this ticket. +TicketNoContractFoundToLink=No contract was found to be automatically linked to this ticket. Please link a contract manually. +TicketManyContractsLinked=Many contracts have been automatically linked to this ticket. Make sure to verify which should be chosen. # # Logs diff --git a/htdocs/ticket/card.php b/htdocs/ticket/card.php index aa34c07c361..9afca68c2eb 100755 --- a/htdocs/ticket/card.php +++ b/htdocs/ticket/card.php @@ -255,22 +255,6 @@ if (empty($reshook)) { $object->add_contact($user->id, "SUPPORTTEC", 'internal'); } - // Auto assign contrat - $contractid = 0; - if (!empty($conf->global->TICKET_AUTO_ASSIGN_CONTRACT_CREATE)) { - $contrat = new Contrat($db); - $contrat->socid = $object->fk_soc; - $list = $contrat->getListOfContracts(); - - if (is_array($list) && !empty($list)) { - if (count($list) == 1) { - $contractid = $list[0]->id; - $object->setContract($contractid); - } else { - } - } - } - // Auto create fiche intervention if (!empty($conf->global->TICKET_AUTO_CREATE_FICHINTER_CREATE)) { $fichinter = new Fichinter($db); @@ -292,6 +276,7 @@ if (empty($reshook)) { setEventMessages($fichinter->error, null, 'errors'); } } + } if (!$error) {