Browse Source

recebe os eventos do webhook do clicksign e sinaliza quando o documento estiver assinado

1.8.1-crm-simplesip
Douglas.Strappasson 8 months ago
parent
commit
370b20da11
  1. 17
      crm/Utils.php
  2. 7
      crm/config.php
  3. 44
      crm/controller/ClickSignController.php
  4. 44
      crm/repositories/ContractRepositories.php
  5. 18
      crm/service/ClickSignService.php
  6. 54
      crm/service/ContractService.php
  7. 12
      crm/web/contractsSelect.php
  8. 5
      crm/web/js/clickSign.js
  9. 1
      imgSite/signed.svg

17
crm/Utils.php

@ -27,8 +27,8 @@ class Utils
return $link;
}
/**
* gera o icone para envio de documentos para assinatura.
/**
* gera o icone indicando que já enviou o documento para assinatura.
*/
public static function getLinkIconSended($imagem = "")
{
@ -40,6 +40,19 @@ class Utils
return $link;
}
/**
* gera o icone indicando que já enviou o documento para assinatura.
*/
public static function getLinkIconSigned($imagem = "")
{
$link = "";
$imgDef = empty($imagem) ? "imgSite/signed.svg" : $imagem;
$img = "<img src=\"$imgDef\" width=\"16\" height=\"16\" border=\"0\" title=\"Assinado!\">";
$link = "<a class=\"botao-status\" data-signed=\"true\">$img</a>";
return $link;
}
/**
* gera o icone para de processando envio.
*/

7
crm/config.php

@ -7,4 +7,9 @@
###########################################################################
//log
define('CONF_LOG_PATH', '/var/log/asterisk/crmInterno.log');
define('CONF_LOG_PATH', '/var/log/asterisk/crmInterno.log');
define ('CLOSE_EVENT', 'close');
define ('AUTO_CLOSE_EVENT', 'auto_close');
define ('DATA_LIMIT_EVENT', 'deadline');
define ('REFUSAL_EVENT', 'refusal');

44
crm/controller/ClickSignController.php

@ -5,12 +5,13 @@ require_once __DIR__ . '/../service/ContractService.php';
require_once __DIR__ . '../../Utils.php';
Utils::printToLog('ClickSignController executando.', __LINE__);
Utils::printToLog(print_r($_REQUEST, true), __LINE__);
Utils::printToLog("\n TESTEEE", __LINE__);
header("Content-Type:application/json");
if (isset($_GET['idContract']) && $_GET['idContract'] != "") {
$contractId = $_GET['idContract'];
if (isset($_GET["sendedStatus"]) && $_GET['sendedStatus'] != "" ) {
if (isset($_GET["sendedStatus"]) && $_GET['sendedStatus'] != "") {
if (ContractService::isSendedContractToClicksign($contractId)) {
Utils::printToLog("O status do contrato id $contractId é: enviado!");
$response = array(
@ -22,24 +23,51 @@ if (isset($_GET['idContract']) && $_GET['idContract'] != "") {
"sended" => "false"
);
}
echo json_encode($response);
exit;
}
ContractService::setProcessingSentToClicksignService($contractId, true); //seta o status processando como verdadeiro
$sent = ClickSignService::sendToClicksign($contractId);
$response = array(
"response" => ClickSignService::sendToClicksign($contractId)
);
ContractService::setProcessingSentToClicksignService($contractId, false); //seta o status processando como falso
$response = mb_convert_encoding($response,"ISO-8859-1","UTF-8");
$response = mb_convert_encoding($response, "ISO-8859-1", "UTF-8");
echo json_encode($response);
exit;
} else if ($_GET['signed'] && $_GET['signed'] != "") {
$json_data = file_get_contents("php://input");
$data = json_decode($json_data, true);
Utils::printToLog("testeeeee: " . print_r($data, true));
foreach ($data as $key => $value) {
if ($key == "event") {
$event = $value['name'];
switch ($event) {
case CLOSE_EVENT:
case AUTO_CLOSE_EVENT:
$eventRecord = $event;
break;
}
}
if ($key == "document") {
$documentKey = $value['key'];
}
}
Utils::printToLog('Evento recebido do ClickSign. Key: ' . $documentKey . ". Status: " . $eventRecord);
if ($eventRecord && $documentKey) {
ClickSignService::setSignedEventClicksignService($documentKey, $eventRecord);
}
} else {
return 'deu ruim';
}

44
crm/repositories/ContractRepositories.php

@ -336,6 +336,28 @@ class ContractRepositories extends Repositories
}
}
public function getSignedContractOnClicksign($contractId)
{
$sql = "SELECT signed_event_clicksign
FROM crm_contract
WHERE id_contract = '{$contractId}';";
try {
$result = $this->execQueryOnDataBase($sql);
if ($result) {
Utils::printToLog("Query executada com sucesso: " . $sql, __LINE__);
return pg_fetch_assoc($result);
}
throw new Exception("Nao foi possivel executar a query: " . $sql);
} catch (Exception $exception) {
$errorInformation = $exception->getMessage();
Utils::printToLog($errorInformation, __LINE__);
return false;
}
}
public function getProcessingSentToClicksign($contractId)
{
$sql = "SELECT processing_send_clicksign
@ -402,4 +424,26 @@ class ContractRepositories extends Repositories
return false;
}
}
/**
* seta um valor para o status de assinatura do documento.
*/
public function setSignedEventClicksign($documentKey, string $status)
{
$sql = " UPDATE crm_contract SET signed_event_clicksign = '{$status}' WHERE key_document_clicksign = '{$documentKey}' ";
try {
$result = $this->execQueryOnDataBase($sql);
if ($result) {
Utils::printToLog("Query executada com sucesso: " . $sql, __LINE__);
return true;
}
throw new Exception("Nao foi possivel executar a query: " . $sql);
} catch (Exception $exception) {
$errorInformation = $exception->getMessage();
Utils::printToLog($errorInformation, __LINE__);
return false;
}
}
}

18
crm/service/ClickSignService.php

@ -191,6 +191,21 @@ class ClickSignService
throw new \Exception('Falha ao definir o valor da chave do documento enviado ao clicksign');
}
public static function setSignedEventClicksignService($documentKey, string $status)
{
$contractRepositories = new ContractRepositories();
$setStatus = $contractRepositories->setSignedEventClicksign($documentKey, $status);
if ($setStatus) {
return true;
}
Utils::printToLog("Falha ao definir o valor do status de assinatura do documento enviado ao clicksign = $documentKey");
throw new \Exception('Falha ao definir o valor do status de assinatura do documento enviado ao clicksign');
}
private static function sendDataToModelService()
{
Utils::printToLog("Enviando dados ao modelo de documento. \n", __LINE__);
@ -200,8 +215,9 @@ class ClickSignService
$templateCode = $contract['templates'];
$template = TemplatesService::getNameTemplateByCodeFromDb($templateCode);
$templateName = $template['name'];
$documentName = $contract['contratante'];
$patchFile = "/$templateName/contrato_teste_api.docx";
$patchFile = "/$templateName/$documentName.docx";
//$currentDate = Utils::getStringCurrentDate();
$currentDate = date('d \d\e F \d\e Y');

54
crm/service/ContractService.php

@ -130,6 +130,32 @@ class ContractService
return false;
}
/**
* verifica no banco de dados se o contrato está marcado com algum status que indique a assinatura do documento enviado.
*/
public static function isSignedContractOnClicksign($contractId)
{
if (!self::$contractRepo) {
self::$contractRepo = new ContractRepositories();
}
$response = self::$contractRepo->getSignedContractOnClicksign($contractId);
if ($response) {
$status = $response['signed_event_clicksign'];
switch ($status) {
case CLOSE_EVENT:
case AUTO_CLOSE_EVENT:
case DATA_LIMIT_EVENT:
case REFUSAL_EVENT:
return true;
}
}
Utils::printToLog("Não foi possível obter ou não existe status de assinatura do contrato! \n" . print_r($response, true), __LINE__);
return false;
}
/**verifica se o contrato está processando o envio */
public static function isProcessingSentToClicksign($contractId)
{
@ -151,19 +177,19 @@ class ContractService
return false;
}
/**verifica se o contrato está processando o envio */
public static function setProcessingSentToClicksignService($contractId, bool $status)
{
if (!self::$contractRepo) {
self::$contractRepo = new ContractRepositories();
}
$response = self::$contractRepo->setProcessingSentToClicksign($contractId, $status);
if ($response) {
return true;
}
Utils::printToLog("Não foi possível setar o valor da flag de processamento de envio do contrato! \n" . print_r($response, true), __LINE__);
return false;
/**verifica se o contrato está processando o envio */
public static function setProcessingSentToClicksignService($contractId, bool $status)
{
if (!self::$contractRepo) {
self::$contractRepo = new ContractRepositories();
}
$response = self::$contractRepo->setProcessingSentToClicksign($contractId, $status);
if ($response) {
return true;
}
Utils::printToLog("Não foi possível setar o valor da flag de processamento de envio do contrato! \n" . print_r($response, true), __LINE__);
return false;
}
}

12
crm/web/contractsSelect.php

@ -27,12 +27,18 @@ if ($result) {
$edit = GetLinkFormUpdate("&id_contract=" . $idContract, "updContrato", "imgSite/edit.svg");
$send = Utils::getLinkFormEnviar($idContract);
if ($contractService->isProcessingSentToClicksign($idContract)){
$send = Utils::getLinkIconProcessingSent();
} else if ($contractService->isSendedContractToClicksign($idContract)){
}
if ($contractService->isSendedContractToClicksign($idContract)){
$send = Utils::getLinkIconSended();
} else {
$send = Utils::getLinkFormEnviar($idContract);
}
if ($contractService->isSignedContractOnClicksign($idContract)){
$send = Utils::getLinkIconSigned();
}
$htmlRows .= "

5
crm/web/js/clickSign.js

@ -14,6 +14,11 @@ document.addEventListener('click', async function (event) {
throw new Error('Documento já enviado!');
}
if (botao.dataset.signed) {
console.log('Documento já assinado!');
throw new Error('Documento já assinado!');
}
if (botao.dataset.wait) {
console.log('Documento em processamento!');
throw new Error(`Aguarde o fim do processamento de envio.`);

1
imgSite/signed.svg

@ -0,0 +1 @@
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#000000"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M2 12L7.25 17C7.25 17 8.66939 15.3778 9.875 14" stroke="#6e26fa" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> <path d="M8 12L13.25 17L22 7" stroke="#6e26fa" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> <path d="M16 7L12.5 11" stroke="#6e26fa" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>

After

Width:  |  Height:  |  Size: 634 B

Loading…
Cancel
Save