diff --git a/phpunit/functional/TicketCostTest.php b/phpunit/functional/TicketCostTest.php new file mode 100644 index 00000000000..198f744c75d --- /dev/null +++ b/phpunit/functional/TicketCostTest.php @@ -0,0 +1,218 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace tests\units; + +use DbTestCase; +use Item_Ticket; +use Ticket; +use TicketCost; +use Computer; + +class TicketCostTest extends DbTestCase +{ + private function getNewTicket() + { + return $this->createItem(Ticket::class, [ + 'name' => 'my ticket name', + 'entities_id' => $this->getTestRootEntity(true), + '_users_id_assign' => getItemByTypeName('User', 'tech', true), + 'content' => '', + ]); + } + + private function getNewComputer() + { + return $this->createItem(Computer::class, [ + 'name' => 'my computer name', + 'entities_id' => $this->getTestRootEntity(true), + ]); + } + + private function getNewTicketCost(array $values = []) + { + return $this->createItem(TicketCost::class, [ + 'name' => $values['name'] ?? 'my ticket cost name', + 'entities_id' => $this->getTestRootEntity(true), + 'tickets_id' => $values['tickets_id'], + 'cost_time' => $values['cost_time'] ?? 0., + 'cost_fixed' => $values['cost_fixed'] ?? 0., + 'cost_material' => $values['cost_material'] ?? 0., + 'actiontime' => $values['actiontime'] ?? 1, + ]); + } + + private function getNewItemTicket(array $values = []) + { + return $this->createItem(Item_Ticket::class, [ + 'tickets_id' => $values['tickets_id'], + 'itemtype' => $values['itemtype'] ?? 'Computer', + 'items_id' => $values['items_id'], + ]); + } + + public function testAddCost() + { + $computer = $this->getNewComputer(); + $this->assertSame(0., floatval($computer->fields['ticket_tco'])); + $ticket = $this->getNewTicket(); + $this->getNewItemTicket( + [ + 'tickets_id' => $ticket->getID(), + 'itemtype' => 'Computer', + 'items_id' => $computer->getID(), + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(0., floatval($computer->fields['ticket_tco'])); + $this->getNewTicketCost( + [ + 'tickets_id' => $ticket->getID(), + 'cost_material' => 10. + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(10., floatval($computer->fields['ticket_tco'])); + $this->getNewTicketCost( + [ + 'tickets_id' => $ticket->getID(), + 'cost_fixed' => 10., + 'cost_material' => 80. + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(100., floatval($computer->fields['ticket_tco'])); + } + + public function testRemoveCost() + { + $computer = $this->getNewComputer(); + $this->assertSame(0., floatval($computer->fields['ticket_tco'])); + $ticket = $this->getNewTicket(); + $this->getNewItemTicket( + [ + 'tickets_id' => $ticket->getID(), + 'itemtype' => 'Computer', + 'items_id' => $computer->getID(), + ] + ); + $ticketcost = $this->getNewTicketCost( + [ + 'tickets_id' => $ticket->getID(), + 'cost_fixed' => 10., + 'cost_material' => 80. + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(90., floatval($computer->fields['ticket_tco'])); + $ticketcost->delete(['id' => $ticketcost->getID()], true); + $computer->getFromDB($computer->getID()); + $this->assertSame(0., floatval($computer->fields['ticket_tco'])); + } + + public function testAddItem() + { + $computer = $this->getNewComputer(); + $this->assertSame(0., floatval($computer->fields['ticket_tco'])); + $ticket = $this->getNewTicket(); + $this->getNewTicketCost( + [ + 'tickets_id' => $ticket->getID(), + 'cost_fixed' => 140., + 'cost_material' => 860. + ] + ); + $this->getNewTicketCost( + [ + 'tickets_id' => $ticket->getID(), + 'cost_fixed' => 80., + 'cost_material' => 20. + ] + ); + $this->getNewItemTicket( + [ + 'tickets_id' => $ticket->getID(), + 'itemtype' => 'Computer', + 'items_id' => $computer->getID(), + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(1100., floatval($computer->fields['ticket_tco'])); + } + + public function testItemInMultipleTicket() + { + $computer = $this->getNewComputer(); + $this->assertSame(0., floatval($computer->fields['ticket_tco'])); + + $ticket = $this->getNewTicket(); + $this->getNewItemTicket( + [ + 'tickets_id' => $ticket->getID(), + 'itemtype' => 'Computer', + 'items_id' => $computer->getID(), + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(0., floatval($computer->fields['ticket_tco'])); + + $this->getNewTicketCost( + [ + 'tickets_id' => $ticket->getID(), + 'cost_material' => 10. + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(10., floatval($computer->fields['ticket_tco'])); + + $ticket2 = $this->getNewTicket(); + $this->getNewItemTicket( + [ + 'tickets_id' => $ticket2->getID(), + 'itemtype' => 'Computer', + 'items_id' => $computer->getID(), + ] + ); + $this->getNewTicketCost( + [ + 'tickets_id' => $ticket2->getID(), + 'cost_fixed' => 10., + 'cost_material' => 80. + ] + ); + $computer->getFromDB($computer->getID()); + $this->assertSame(100., floatval($computer->fields['ticket_tco'])); + } +} diff --git a/src/Item_Ticket.php b/src/Item_Ticket.php index 7c07b7f6ec6..9954145b187 100644 --- a/src/Item_Ticket.php +++ b/src/Item_Ticket.php @@ -107,6 +107,8 @@ public function post_addItem() } $ticket->update($input); + $ticket_cost = new TicketCost(); + $ticket_cost->updateTCOItem($this->fields['itemtype'], $this->fields['items_id']); parent::post_addItem(); } @@ -123,7 +125,8 @@ public function post_purgeItem() $input['_forcenotif'] = true; } $ticket->update($input); - + $ticket_cost = new TicketCost(); + $ticket_cost->updateTCOItem($this->fields['itemtype'], $this->fields['items_id']); parent::post_purgeItem(); } diff --git a/src/TicketCost.php b/src/TicketCost.php index 4a1cc3740cd..43a593a1052 100644 --- a/src/TicketCost.php +++ b/src/TicketCost.php @@ -45,4 +45,49 @@ class TicketCost extends CommonITILCost public static $items_id = 'tickets_id'; public static $rightname = 'ticketcost'; + + public function post_updateItem($history = true) + { + parent::post_updateItem($history); + + $this->verifTCOItem(); + } + + public function post_addItem() + { + parent::post_addItem(); + + $this->verifTCOItem(); + } + + public function post_purgeItem() + { + parent::post_purgeItem(); + + $this->verifTCOItem(); + } + + private function verifTCOItem(): void + { + if ($this->fields['tickets_id']) { + $item_ticket = new Item_Ticket(); + $item_tickets = $item_ticket->find([ + 'tickets_id' => $this->fields['tickets_id'] + ]); + foreach ($item_tickets as $it) { + $this->updateTCOItem($it['itemtype'], $it['items_id']); + } + } + } + + public function updateTCOItem(string $itemtype, int $items_id): void + { + $item = getItemForItemtype($itemtype); + if ($item && $item->getFromDB($items_id)) { + $item->update([ + 'id' => $items_id, + 'ticket_tco' => Ticket::computeTco($item) + ]); + } + } }