-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: Allow to assign contracts to tickets
- Loading branch information
Showing
20 changed files
with
742 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
// This file is part of Bileto. | ||
// Copyright 2022-2023 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Platforms\MariaDBPlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Version20230925082614CreateTicketContract extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Create the ticket_contract table.'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$dbPlatform = $this->connection->getDatabasePlatform(); | ||
if ($dbPlatform instanceof PostgreSQLPlatform) { | ||
$this->addSql(<<<SQL | ||
CREATE TABLE ticket_contract ( | ||
ticket_id INT NOT NULL, | ||
contract_id INT NOT NULL, | ||
PRIMARY KEY(ticket_id, contract_id) | ||
) | ||
SQL); | ||
$this->addSql('CREATE INDEX IDX_6CE6D4D8700047D2 ON ticket_contract (ticket_id)'); | ||
$this->addSql('CREATE INDEX IDX_6CE6D4D82576E0FD ON ticket_contract (contract_id)'); | ||
$this->addSql(<<<SQL | ||
ALTER TABLE ticket_contract | ||
ADD CONSTRAINT FK_6CE6D4D8700047D2 | ||
FOREIGN KEY (ticket_id) | ||
REFERENCES ticket (id) | ||
ON DELETE CASCADE | ||
NOT DEFERRABLE INITIALLY IMMEDIATE | ||
SQL); | ||
$this->addSql(<<<SQL | ||
ALTER TABLE ticket_contract | ||
ADD CONSTRAINT FK_6CE6D4D82576E0FD | ||
FOREIGN KEY (contract_id) | ||
REFERENCES contract (id) | ||
ON DELETE CASCADE | ||
NOT DEFERRABLE INITIALLY IMMEDIATE | ||
SQL); | ||
} elseif ($dbPlatform instanceof MariaDBPlatform) { | ||
$this->addSql(<<<SQL | ||
CREATE TABLE ticket_contract ( | ||
ticket_id INT NOT NULL, | ||
contract_id INT NOT NULL, | ||
INDEX IDX_6CE6D4D8700047D2 (ticket_id), | ||
INDEX IDX_6CE6D4D82576E0FD (contract_id), | ||
PRIMARY KEY(ticket_id, contract_id) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB | ||
SQL); | ||
$this->addSql(<<<SQL | ||
ALTER TABLE ticket_contract | ||
ADD CONSTRAINT FK_6CE6D4D8700047D2 | ||
FOREIGN KEY (ticket_id) | ||
REFERENCES ticket (id) | ||
ON DELETE CASCADE | ||
SQL); | ||
$this->addSql(<<<SQL | ||
ALTER TABLE ticket_contract | ||
ADD CONSTRAINT FK_6CE6D4D82576E0FD | ||
FOREIGN KEY (contract_id) | ||
REFERENCES contract (id) | ||
ON DELETE CASCADE | ||
SQL); | ||
} | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$dbPlatform = $this->connection->getDatabasePlatform(); | ||
if ($dbPlatform instanceof PostgreSQLPlatform) { | ||
$this->addSql('ALTER TABLE ticket_contract DROP CONSTRAINT FK_6CE6D4D8700047D2'); | ||
$this->addSql('ALTER TABLE ticket_contract DROP CONSTRAINT FK_6CE6D4D82576E0FD'); | ||
$this->addSql('DROP TABLE ticket_contract'); | ||
} elseif ($dbPlatform instanceof MariaDBPlatform) { | ||
$this->addSql('ALTER TABLE ticket_contract DROP FOREIGN KEY FK_6CE6D4D8700047D2'); | ||
$this->addSql('ALTER TABLE ticket_contract DROP FOREIGN KEY FK_6CE6D4D82576E0FD'); | ||
$this->addSql('DROP TABLE ticket_contract'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
// This file is part of Bileto. | ||
// Copyright 2022-2023 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
namespace App\Controller\Tickets; | ||
|
||
use App\Controller\BaseController; | ||
use App\Entity\Ticket; | ||
use App\Repository\ContractRepository; | ||
use App\Repository\TicketRepository; | ||
use App\Service\Sorter\ContractSorter; | ||
use App\Utils\ConstraintErrorsFormatter; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class ContractsController extends BaseController | ||
{ | ||
#[Route('/tickets/{uid}/contracts/edit', name: 'edit ticket contracts', methods: ['GET', 'HEAD'])] | ||
public function edit( | ||
Ticket $ticket, | ||
ContractRepository $contractRepository, | ||
ContractSorter $contractSorter, | ||
): Response { | ||
$organization = $ticket->getOrganization(); | ||
$this->denyAccessUnlessGranted('orga:update:tickets:contracts', $organization); | ||
|
||
/** @var \App\Entity\User $user */ | ||
$user = $this->getUser(); | ||
|
||
if (!$ticket->hasActor($user)) { | ||
$this->denyAccessUnlessGranted('orga:see:tickets:all', $organization); | ||
} | ||
|
||
$ongoingContracts = $contractRepository->findOngoingByOrganization($organization); | ||
$contractSorter->sort($ongoingContracts); | ||
$initialOngoingContract = $ticket->getOngoingContract(); | ||
|
||
return $this->render('tickets/contracts/edit.html.twig', [ | ||
'ticket' => $ticket, | ||
'ongoingContracts' => $ongoingContracts, | ||
'ongoingContractUid' => $initialOngoingContract ? $initialOngoingContract->getUid() : null, | ||
]); | ||
} | ||
|
||
#[Route('/tickets/{uid}/contracts/edit', name: 'update ticket contracts', methods: ['POST'])] | ||
public function update( | ||
Ticket $ticket, | ||
Request $request, | ||
ContractRepository $contractRepository, | ||
TicketRepository $ticketRepository, | ||
ContractSorter $contractSorter, | ||
TranslatorInterface $translator, | ||
): Response { | ||
$organization = $ticket->getOrganization(); | ||
$this->denyAccessUnlessGranted('orga:update:tickets:contracts', $organization); | ||
|
||
/** @var \App\Entity\User $user */ | ||
$user = $this->getUser(); | ||
|
||
if (!$ticket->hasActor($user)) { | ||
$this->denyAccessUnlessGranted('orga:see:tickets:all', $organization); | ||
} | ||
|
||
$ongoingContractUid = $request->request->getString('ongoingContractUid'); | ||
|
||
$csrfToken = $request->request->getString('_csrf_token'); | ||
|
||
$ongoingContracts = $contractRepository->findOngoingByOrganization($organization); | ||
$contractSorter->sort($ongoingContracts); | ||
$initialOngoingContract = $ticket->getOngoingContract(); | ||
|
||
if (!$this->isCsrfTokenValid('update ticket contracts', $csrfToken)) { | ||
return $this->renderBadRequest('tickets/contracts/edit.html.twig', [ | ||
'ticket' => $ticket, | ||
'ongoingContracts' => $ongoingContracts, | ||
'ongoingContractUid' => $ongoingContractUid, | ||
'error' => $translator->trans('csrf.invalid', [], 'errors'), | ||
]); | ||
} | ||
|
||
$newOngoingContract = null; | ||
foreach ($ongoingContracts as $contract) { | ||
if ($contract->getUid() === $ongoingContractUid) { | ||
$newOngoingContract = $contract; | ||
} | ||
} | ||
|
||
if ($initialOngoingContract) { | ||
$ticket->removeContract($initialOngoingContract); | ||
} | ||
|
||
if ($newOngoingContract) { | ||
$ticket->addContract($newOngoingContract); | ||
} | ||
|
||
$ticketRepository->save($ticket, true); | ||
|
||
return $this->redirectToRoute('ticket', [ | ||
'uid' => $ticket->getUid(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.