Skip to content

Commit 374a8ef

Browse files
authored
support assigning contract in ticket update rules (#20693)
* support assigning contract in ticket update rules * do not remove existing
1 parent 2dfc94e commit 374a8ef

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The present file will list all changes made to the project; according to the
88
### Added
99

1010
### Changed
11+
- Assign contract Ticket rule action now works with update rules.
1112

1213
### Deprecated
1314

phpunit/functional/TicketTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use CommonITILActor;
3838
use CommonITILObject;
3939
use Computer;
40+
use Contract;
4041
use CronTask;
4142
use DbTestCase;
4243
use Entity;
@@ -54,6 +55,7 @@
5455
use Supplier_Ticket;
5556
use Symfony\Component\DomCrawler\Crawler;
5657
use Ticket;
58+
use Ticket_Contract;
5759
use Ticket_User;
5860
use TicketValidation;
5961
use User;
@@ -8289,4 +8291,37 @@ public function testSelfServiceFormRendering(): void
82898291
// trigger in case of issues.
82908292
$this->assertNotEmpty($content);
82918293
}
8294+
8295+
public function testHandleAddContracts()
8296+
{
8297+
$this->login();
8298+
$contract_1 = $this->createItem(Contract::class, [
8299+
'name' => 'Contract 1',
8300+
'entities_id' => $this->getTestRootEntity(true),
8301+
]);
8302+
$contract_2 = $this->createItem(Contract::class, [
8303+
'name' => 'Contract 2',
8304+
'entities_id' => $this->getTestRootEntity(true),
8305+
]);
8306+
8307+
$ticket = $this->createItem(Ticket::class, [
8308+
'name' => 'Ticket with contracts',
8309+
'content' => 'test',
8310+
'entities_id' => $this->getTestRootEntity(true),
8311+
'_contracts_id' => $contract_1->getID(),
8312+
]);
8313+
$tc = new Ticket_Contract();
8314+
$linked_contracts = array_values($tc->find(['tickets_id' => $ticket->getID()]));
8315+
$this->assertCount(1, $linked_contracts);
8316+
$this->assertEquals($contract_1->getID(), $linked_contracts[0]['contracts_id']);
8317+
8318+
$ticket->update([
8319+
'id' => $ticket->getID(),
8320+
'_contracts_id' => $contract_2->getID(),
8321+
]);
8322+
$linked_contracts = array_values($tc->find(['tickets_id' => $ticket->getID()]));
8323+
$this->assertCount(2, $linked_contracts);
8324+
$this->assertContains($contract_1->getID(), array_column($linked_contracts, 'contracts_id'));
8325+
$this->assertContains($contract_2->getID(), array_column($linked_contracts, 'contracts_id'));
8326+
}
82928327
}

src/Ticket.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,21 @@ public function computeTakeIntoAccountDelayStat()
16391639
return 0;
16401640
}
16411641

1642+
private function handleContractInputs()
1643+
{
1644+
$contracts_id = $this->input['_contracts_id'] ?? 0;
1645+
if (!is_array($contracts_id)) {
1646+
$contracts_id = [$contracts_id];
1647+
}
1648+
$contracts_id = array_filter($contracts_id, static fn($val) => ((int) $val > 0));
1649+
$ticketcontract = new Ticket_Contract();
1650+
foreach ($contracts_id as $contract_id) {
1651+
$ticketcontract->add([
1652+
'contracts_id' => $contract_id,
1653+
'tickets_id' => $this->getID(),
1654+
]);
1655+
}
1656+
}
16421657

16431658
public function post_updateItem($history = true)
16441659
{
@@ -1788,6 +1803,9 @@ public function post_updateItem($history = true)
17881803
);
17891804
}
17901805

1806+
// Add linked contract
1807+
$this->handleContractInputs();
1808+
17911809
// Add linked project
17921810
$projects_ids = $this->input['_projects_id'] ?? [];
17931811
foreach ($projects_ids as $projects_id) {
@@ -2196,14 +2214,7 @@ public function post_addItem()
21962214
}
21972215

21982216
// Add linked contract
2199-
$contracts_id = $this->input['_contracts_id'] ?? 0;
2200-
if ($contracts_id) {
2201-
$ticketcontract = new Ticket_Contract();
2202-
$ticketcontract->add([
2203-
'contracts_id' => $this->input['_contracts_id'],
2204-
'tickets_id' => $this->getID(),
2205-
]);
2206-
}
2217+
$this->handleContractInputs();
22072218

22082219
// Add linked project
22092220
$projects_ids = $this->input['_projects_id'] ?? [];

0 commit comments

Comments
 (0)