Skip to content

Commit

Permalink
fix(TCO): TCO was not updated
Browse files Browse the repository at this point in the history
  • Loading branch information
MyvTsv authored Oct 23, 2024
1 parent bd3939d commit 26997c8
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 1 deletion.
218 changes: 218 additions & 0 deletions phpunit/functional/TicketCostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

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']));
}
}
5 changes: 4 additions & 1 deletion src/Item_Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down
45 changes: 45 additions & 0 deletions src/TicketCost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]);
}
}
}

0 comments on commit 26997c8

Please sign in to comment.