diff --git a/migrations/Version20260116143205.php b/migrations/Version20260116143205.php new file mode 100644 index 0000000..deb0266 --- /dev/null +++ b/migrations/Version20260116143205.php @@ -0,0 +1,32 @@ +addSql('ALTER TABLE devis ADD signature_id VARCHAR(255) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE devis DROP signature_id'); + } +} diff --git a/src/Entity/Devis.php b/src/Entity/Devis.php index ca92816..cbc29e8 100644 --- a/src/Entity/Devis.php +++ b/src/Entity/Devis.php @@ -60,6 +60,9 @@ class Devis #[ORM\Column(nullable: true)] private ?int $devisAuditFileSize = null; + #[ORM\Column(length: 255, nullable: true)] + private ?string $signatureId = null; + public function getId(): ?int { return $this->id; @@ -337,4 +340,16 @@ class Devis return $this->devisSignFile; } + public function getSignatureId(): ?string + { + return $this->signatureId; + } + + public function setSignatureId(?string $signatureId): static + { + $this->signatureId = $signatureId; + + return $this; + } + } diff --git a/src/Service/Signature/Client.php b/src/Service/Signature/Client.php index da0f3f6..9ef3b5f 100644 --- a/src/Service/Signature/Client.php +++ b/src/Service/Signature/Client.php @@ -2,12 +2,103 @@ namespace App\Service\Signature; +use App\Entity\CustomerOrder; +use App\Entity\Devis; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\HttpKernel\KernelInterface; +use Vich\UploaderBundle\Storage\StorageInterface; + class Client { - private \Docuseal\Api $client; + private \Docuseal\Api $docuseal; + private string $baseUrl; - public function __construct() + public function __construct( + private readonly RequestStack $requestStack, + private readonly UrlGeneratorInterface $urlGenerator, + private readonly EntityManagerInterface $entityManager, + private readonly KernelInterface $kernel, + private readonly StorageInterface $storage, + ) { + // Configuration via les variables d'environnement + $key = $_ENV['ESYSIGN_APIEY'] ?? ''; + $this->baseUrl = $_ENV['ESYSIGN_URL'] ?? ''; + + // L'URL API est le point d'entrée pour le SDK Docuseal + $apiUrl = rtrim($this->baseUrl, '/') . '/api'; + $this->docuseal = new \Docuseal\Api($key, $apiUrl); + } + + /** + * Crée une soumission pour un Devis + */ + public function createSubmissionDevis(Devis $devis): string { - $this->client = new \Docuseal\Api($_ENV['ESYSIGN_APIEY'],'https://signature.esy-wev.dev/api'); + // Si aucune signature n'est lancée, on initialise la soumission + if ($devis->getSignatureId() === null) { + + // URL où le client sera redirigé après signature + $completedRedirectUrl = $this->baseUrl . $this->urlGenerator->generate( + 'app_sign_complete', + ['type' => 'devis', 'id' => $devis->getId()] + ); + + // Récupération du fichier via VichUploader (champ devisDocuSealFile) + $relativeFileUrl = $this->storage->resolveUri($devis, 'devisDocuSealFile'); + $fileUrl = $this->baseUrl . $relativeFileUrl; + + $submission = $this->docuseal->createSubmissionFromPdf([ + 'name' => 'Devis N°' . $devis->getNum(), // Correction : getNum() + 'completed_redirect_url' => $completedRedirectUrl, + 'send_email' => true, + 'documents' => [ + [ + 'name' => 'devis_' . $devis->getNum() . '.pdf', // Correction : getNum() + 'file' => $fileUrl, + ], + ], + 'submitters' => [ + [ + 'role' => 'Client', + 'email' => $devis->getCustomer()->getEmail(), + 'name' => $devis->getCustomer()->getSurname() . ' ' . $devis->getCustomer()->getName(), + 'metadata' => [ + 'id' => $devis->getId(), + 'type' => 'devis' + ] + ], + ], + ]); + + // Stockage de l'ID submitter de Docuseal dans ton entité + $devis->setSignatureId($submission['submitters'][0]['id']); + $this->entityManager->flush(); + } + + return $this->getLinkSign($devis->getSignatureId()); + } + + /** + * Retourne l'URL de signature publique pour le client + */ + public function getLinkSign(?string $submitterId): string + { + if (!$submitterId) { + throw new \InvalidArgumentException("ID Submitter absent."); + } + + $submissionData = $this->docuseal->getSubmitter($submitterId); + + return rtrim($this->baseUrl, '/') . "/s/" . $submissionData['slug']; + } + + /** + * Récupère l'état d'un signataire (pour vérifier si c'est signé) + */ + public function getSubmiter(?string $submitterId): array + { + return $this->docuseal->getSubmitter($submitterId); } }