Skip to content

Commit

Permalink
Calculate the charged spent time based on billing interval
Browse files Browse the repository at this point in the history
  • Loading branch information
marien-probesys committed Nov 8, 2023
1 parent 670d968 commit 95e5a27
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Controller/Tickets/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ public function create(

if ($minutesSpent > 0 && $security->isGranted('orga:create:tickets:time_spent', $organization)) {
$contract = $ticket->getOngoingContract();
$minutesCharged = $minutesSpent;

if ($contract) {
$contractAvailableMinutes = $contract->getRemainingMinutes();
$billingInterval = $contract->getBillingInterval();

// If there is more spent time than time available in the
// contract, we don't want to charge the entire time. So we
Expand All @@ -203,13 +205,17 @@ public function create(
// Calculate the remaining time, and make sure it will not
// be charged.
$minutesSpent = $minutesSpent - $contractAvailableMinutes;
$minutesCharged = $minutesSpent;
$contract = null;
} elseif ($billingInterval > 0) {
$minutesCharged = intval(ceil($minutesSpent / $billingInterval)) * $billingInterval;
$minutesCharged = min($minutesCharged, $contractAvailableMinutes);
}
}

$timeSpent = new TimeSpent();
$timeSpent->setTicket($ticket);
$timeSpent->setTime($minutesSpent);
$timeSpent->setTime($minutesCharged);
$timeSpent->setRealTime($minutesSpent);
$timeSpent->setContract($contract);

Expand Down
34 changes: 34 additions & 0 deletions tests/Controller/Tickets/MessagesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,40 @@ public function testPostCreateAcceptsTimeSpent(): void
$this->assertSame($ticket->getId(), $timeSpent->getTicket()->getId());
}

public function testPostCanAssociateTimeSpentToContract(): void
{
$client = static::createClient();
$user = UserFactory::createOne();
$client->loginUser($user->object());
$this->grantOrga($user->object(), [
'orga:create:tickets:messages',
'orga:create:tickets:time_spent',
]);
$contract = ContractFactory::createOne([
'startAt' => Time::ago(1, 'week'),
'endAt' => Time::fromNow(1, 'week'),
'maxHours' => 10,
'billingInterval' => 30,
]);
$ticket = TicketFactory::createOne([
'createdBy' => $user,
]);
$ticket->addContract($contract->object());
$messageContent = 'My message';

$client->request('POST', "/tickets/{$ticket->getUid()}/messages/new", [
'_csrf_token' => $this->generateCsrfToken($client, 'create ticket message'),
'message' => $messageContent,
'timeSpent' => 10,
]);

$timeSpent = TimeSpentFactory::first();
$this->assertSame(30, $timeSpent->getTime());
$this->assertSame(10, $timeSpent->getRealTime());
$this->assertSame($ticket->getId(), $timeSpent->getTicket()->getId());
$this->assertSame($contract->getId(), $timeSpent->getContract()->getId());
}

public function testPostCreateCanCreateTwoTimeSpentIfContractIsAlmostFinished(): void
{
$client = static::createClient();
Expand Down

0 comments on commit 95e5a27

Please sign in to comment.