Skip to content
Snippets Groups Projects
Commit a6d6677c authored by Jean-Laurent DUZANT's avatar Jean-Laurent DUZANT
Browse files

FEAT #17642 TIME 3:30 make xml proof with fastParapheur document history &&...

FEAT #17642 TIME 3:30 make xml proof with fastParapheur document history && make archivedProof.zip with all
parent 9c4ee084
No related branches found
No related tags found
No related merge requests found
...@@ -276,37 +276,60 @@ class FastParapheurController ...@@ -276,37 +276,60 @@ class FastParapheurController
{ {
$version = $args['version']; $version = $args['version'];
foreach ($args['idsToRetrieve'][$version] as $resId => $value) { foreach ($args['idsToRetrieve'][$version] as $resId => $value) {
if (empty(trim($value['external_id']))) { $fetchDate = new \DateTimeImmutable($value['external_state_fetch_date']);
$timeAgo = new \DateTimeImmutable('-30 minutes');
if (empty(trim($value['external_id'])) || (empty($value['xparaphdepot']) || is_array($value['xparaphdepot']))) {
$args['idsToRetrieve'][$version][$resId]['status'] = 'waiting'; $args['idsToRetrieve'][$version][$resId]['status'] = 'waiting';
continue; continue;
} }
$curlReturn = CurlModel::exec([ if (!empty($value['external_state_fetch_date']) && $fetchDate->getTimestamp() >= $timeAgo->getTimestamp()) {
'url' => $args['config']['data']['url'] . '/documents/v2/' . $value['external_id'] . '/history', $newDate = $fetchDate->modify('+30 minutes');
'method' => 'GET',
'options' => [
CURLOPT_SSLCERT => $args['config']['data']['certPath'],
CURLOPT_SSLCERTPASSWD => $args['config']['data']['certPass'],
CURLOPT_SSLCERTTYPE => $args['config']['data']['certType']
]
]);
if ($curlReturn['code'] == 404) { if (!empty($value['res_id_master'])) {
echo "Erreur 404 : \n" . $curlReturn['raw']; echo "PJ n° $resId du document original n° {$value['res_id_master']} : prochaine récupération disponible le " . $newDate->format('d-m-Y H:i') . PHP_EOL;
} else {
echo "Document principal n° $resId : prochaine récupération disponible le " . $newDate->format('d-m-Y H:i') . PHP_EOL;
}
$args['idsToRetrieve'][$version][$resId]['status'] = 'waiting';
continue; continue;
} }
if (!empty($curlReturn['response']['developerMessage']) && !empty($value['res_id_master'])) { $historyResponse = FastParapheurController::getDocumentHistory(['config' => $args['config'], 'documentId' => $value['external_id']]);
echo "PJ n° $resId et document original n° {$value['res_id_master']} : {$curlReturn['response']['developerMessage']} " . PHP_EOL; if (!empty($historyResponse['errors'])) {
unset($args['idsToRetrieve'][$version][$resId]); if (!empty($historyResponse['code']) && $historyResponse['code'] == 404) {
continue; echo "Erreur 404 : \n" . $historyResponse['errors'];
} elseif (!empty($curlReturn['response']['developerMessage'])) { continue;
unset($args['idsToRetrieve'][$version][$resId]); }
echo "Document principal n° $resId : {$curlReturn['response']['developerMessage']} " . PHP_EOL; if (!empty($value['res_id_master'])) {
continue; echo "PJ n° $resId et document original n° {$value['res_id_master']} : {$historyResponse['errors']} " . PHP_EOL;
unset($args['idsToRetrieve'][$version][$resId]);
continue;
} else {
unset($args['idsToRetrieve'][$version][$resId]);
echo "Document principal n° $resId : {$historyResponse['errors']} " . PHP_EOL;
continue;
}
} }
foreach ($curlReturn['response'] as $valueResponse) { // Loop on all steps of the documents (prepared, send to signature, signed etc...) $updateHistoryFetchDate = FastParapheurController::updateFetchHistoryDateByExternalId([
'type' => ($version == 'resLetterbox' ? 'resource' : 'attachment'),
'documentId' => $value['external_id']
]);
if (!empty($updateHistoryFetchDate['errors'])) {
if (!empty($value['res_id_master'])) {
echo "PJ n° $resId et document original n° {$value['res_id_master']} : {$updateHistoryFetchDate['errors']} " . PHP_EOL;
unset($args['idsToRetrieve'][$version][$resId]);
continue;
} else {
unset($args['idsToRetrieve'][$version][$resId]);
echo "Document principal n° $resId : {$updateHistoryFetchDate['errors']} " . PHP_EOL;
continue;
}
}
foreach ($historyResponse['response'] as $valueResponse) { // Loop on all steps of the documents (prepared, send to signature, signed etc...)
if ($valueResponse['stateName'] == $args['config']['data']['validatedState']) { if ($valueResponse['stateName'] == $args['config']['data']['validatedState']) {
$response = FastParapheurController::download(['config' => $args['config'], 'documentId' => $value['external_id']]); $response = FastParapheurController::download(['config' => $args['config'], 'documentId' => $value['external_id']]);
$args['idsToRetrieve'][$version][$resId]['status'] = 'validated'; $args['idsToRetrieve'][$version][$resId]['status'] = 'validated';
...@@ -314,6 +337,17 @@ class FastParapheurController ...@@ -314,6 +337,17 @@ class FastParapheurController
$args['idsToRetrieve'][$version][$resId]['encodedFile'] = $response['b64FileContent']; $args['idsToRetrieve'][$version][$resId]['encodedFile'] = $response['b64FileContent'];
$args['idsToRetrieve'][$version][$resId]['signatory_user_serial_id'] = null; $args['idsToRetrieve'][$version][$resId]['signatory_user_serial_id'] = null;
$proofDocument = FastParapheurController::makeHistoryProof([
'historyData' => $historyResponse['response'],
'metadata' => $args['idsToRetrieve'][$version][$resId],
'signEncodedFile' => $response['b64FileContent']
]);
if (!empty($proofDocument['encodedProofDocument'])) {
$args['idsToRetrieve'][$version][$resId]['log'] = $proofDocument['encodedProofDocument'];
$args['idsToRetrieve'][$version][$resId]['logFormat'] = $proofDocument['format'];
$args['idsToRetrieve'][$version][$resId]['logTitle'] = '[Faisceau de preuve]';
}
if (empty($args['config']['data']['integratedWorkflow']) || $args['config']['data']['integratedWorkflow'] == 'false') { if (empty($args['config']['data']['integratedWorkflow']) || $args['config']['data']['integratedWorkflow'] == 'false') {
$signatoryInfo = FastParapheurController::getSignatoryUserInfo([ $signatoryInfo = FastParapheurController::getSignatoryUserInfo([
'config' => $args['config'], 'config' => $args['config'],
...@@ -348,6 +382,74 @@ class FastParapheurController ...@@ -348,6 +382,74 @@ class FastParapheurController
return $args['idsToRetrieve']; return $args['idsToRetrieve'];
} }
/**
* Create proof from history data (required), signed document with metadata (optional)
*
* @param array $args historyData, signEncodedFile, metadata
*/
public static function makeHistoryProof(array $args)
{
if (!Validator::arrayType()->notEmpty()->validate($args['historyData'])) {
return ['errors' => 'historyData is not an array'];
}
$documentPathToZip = [];
$tmpPath = CoreConfigModel::getTmpPath();
if (!empty($args['signEncodedFile'] && !empty($args['metadata']))) {
$signDocumentPath = $tmpPath . 'fastSignDoc' . $GLOBALS['id'] . "_" . rand() . '.pdf';
file_put_contents($signDocumentPath, base64_decode($args['signEncodedFile']));
$filename = TextFormatModel::formatFilename(['filename' => ($args['metadata']['title'] ?? $args['metadata']['subject']) . 'pdf']);
if (file_exists($signDocumentPath) && filesize($signDocumentPath) > 0) {
$documentPathToZip[] = ['path' => $signDocumentPath, 'filename' => $filename];
}
}
$historyXml = CoreConfigModel::arrayToXml(['data' => $args['historyData'], 'xml' => false]);
$historyXmlPath = $tmpPath . 'maarchProof' . $GLOBALS['id'] . "_" . rand() . '.xml';
file_put_contents($historyXmlPath, $historyXml);
$loadedXml = CoreConfigModel::getXmlLoaded(['path' => $historyXmlPath]);
if (empty($loadedXml)) {
return ['code' => 500, 'errors' => 'Cannot create file ' . $historyXmlPath];
}
$xmlDocument = new \DOMDocument('1.0');
$xmlDocument->preserveWhiteSpace = false;
$xmlDocument->formatOutput = true;
$xmlDocument->loadXML($loadedXml->asXML());
$historyXml = $xmlDocument->saveXML();
file_put_contents($historyXmlPath, $historyXml);
$documentPathToZip[] = ['path' => $historyXmlPath, 'filename' => 'maarchProof.xml'];
$zipFileContent = null;
$zip = new \ZipArchive();
$zipFilename = $tmpPath . 'archivedProof' . $GLOBALS['id'] . '_' . rand() . '.zip';
if ($zip->open($zipFilename, \ZipArchive::CREATE) === true) {
foreach ($documentPathToZip as $document) {
if(file_exists($document['path']) && filesize($document['path']) > 0) {
$zip->addFile($document['path'], $document['filename']);
}
}
$zip->close();
$zipFileContent = file_get_contents($zipFilename);
$documentPathToZip[] = ['path' => $zipFilename];
} else {
return ['errors' => 'Cannot create archive zip'];
}
foreach ($documentPathToZip as $document) {
if(file_exists($document['path']) && filesize($document['path']) > 0) {
unlink($document['path']);
}
}
return ['format' => 'zip','encodedProofDocument' => base64_encode($zipFileContent)];
}
public static function getSignatoryUserInfo(array $args = []) public static function getSignatoryUserInfo(array $args = [])
{ {
ValidatorModel::notEmpty($args, ['resId', 'config']); ValidatorModel::notEmpty($args, ['resId', 'config']);
...@@ -975,6 +1077,102 @@ class FastParapheurController ...@@ -975,6 +1077,102 @@ class FastParapheurController
} }
} }
/**
* Recommandations minimales Fast à respecter
* Espacement minimum de 30 minutes pour le même document
*
* @param $args:
* - documentId : 'externalid' of res_letterbox
* - config : FastParapheur configuration
*/
public static function getDocumentHistory(array $args)
{
if (!Validator::notEmpty()->validate($args['documentId'])) {
return ['errors' => 'documentId not found'];
}
if (!Validator::arrayType()->notEmpty()->validate($args['config'])) {
return ['errors' => 'config is not an array'];
}
$curlReturn = CurlModel::exec([
'url' => $args['config']['data']['url'] . '/documents/v2/' . $args['documentId'] . '/history',
'method' => 'GET',
'options' => [
CURLOPT_SSLCERT => $args['config']['data']['certPath'],
CURLOPT_SSLCERTPASSWD => $args['config']['data']['certPass'],
CURLOPT_SSLCERTTYPE => $args['config']['data']['certType']
]
]);
if ($curlReturn['code'] == 404) {
return ['code' => $curlReturn['code'], 'errors' => $curlReturn['raw']];
} elseif (!empty($curlReturn['response']['developerMessage'])) {
return ['code' => $curlReturn['code'], 'errors' => $curlReturn['response']['developerMessage']];
}
return ['response' => $curlReturn['response']];
}
public static function updateFetchHistoryDateByExternalId(array $args)
{
$tag = "[updateFetchHistoryDateByExternalId] ";
if (!Validator::stringType()->notEmpty()->validate($args['type'])) {
return ['errors' => $tag . 'type is not a string'];
}
if (!Validator::notEmpty()->validate($args['documentId'])) {
return ['errors' => $tag . 'documentId is not found'];
}
if (!empty($args['type']) && $args['type'] == 'resource') {
$resource = ResModel::get([
'select' => ['res_id', 'external_id', 'external_state'],
'where' => ["external_id->>'signatureBookId' = ?"],
'data' => [$args['documentId']]
])[0];
if (empty($resource)) {
return ['errors' => $tag . 'Resource (' . $args['documentId'] .') does not exist'];
}
} else {
$resource = AttachmentModel::get([
'select' => ['res_id', 'res_id_master', 'external_id', 'external_state'],
'where' => ["external_id->>'signatureBookId' = ?"],
'data' => [$args['documentId']]
])[0];
if (empty($resource)) {
return ['errors' => $tag . 'Attachment (' . $args['documentId'] .') does not exist'];
}
}
$externalId = json_decode($resource['external_id'], true);
if (empty($externalId['signatureBookId'])) {
return ['errors' => $tag . 'Resource is not linked to Fast Parapheur'];
}
$externalState = json_decode($resource['external_state'], true);
$currentDate = new \DateTimeImmutable();
$externalState['signatureBookWorkflow']['fetchDate'] = $currentDate->format('c');
if (!empty($args['type']) && $args['type'] == 'resource') {
ResModel::update([
'where' => ['res_id = ?'],
'data' => [$resource['res_id']],
'postSet' => [
'external_state' => 'jsonb_set(external_state, \'{signatureBookWorkflow}\', \'' . json_encode($externalState['signatureBookWorkflow']) . '\'::jsonb)'
]
]);
} else {
AttachmentModel::update([
'where' => ['res_id = ?'],
'data' => [$resource['res_id']],
'postSet' => [
'external_state' => 'jsonb_set(external_state, \'{signatureBookWorkflow}\', \'' . json_encode($externalState['signatureBookWorkflow']) . '\'::jsonb)'
]
]);
}
return true;
}
public static function getRefusalMessage(array $args) public static function getRefusalMessage(array $args)
{ {
$curlReturn = CurlModel::exec([ $curlReturn = CurlModel::exec([
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment