Newer
Older
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Retrieve signed mail from external signatory book
* @author dev@maarch.org
*/
/**
* @brief Class to include the file error
*
*/
class IncludeFileError extends Exception
{
public function __construct($file)
{
$this->file = $file;
parent :: __construct('Include File \'$file\' is missing!', 1);
}
}
// Globals variables definition
$GLOBALS['batchName'] = 'retrieveMailsFromSignatoryBook';
$GLOBALS['wb'] = '';
$totalProcessedResources = 0;
// Load tools
include('batch_tools.php');

Florian Azizian
committed
$options = getopt("c:", ["config:"]);
if (empty($options['c']) && empty($options['config'])) {
print("Configuration file missing\n");
exit(101);
} elseif (!empty($options['c']) && empty($options['config'])) {
$options['config'] = $options['c'];
unset($options['c']);
}
$txt = '';
foreach (array_keys($options) as $key) {
if (isset($options[$key]) && $options[$key] == false) {
$txt .= $key . '=false,';
} else {
$txt .= $key . '=' . $options[$key] . ',';
}
}

Florian Azizian
committed
print($txt . "\n");
$GLOBALS['configFile'] = $options['config'];

Florian Azizian
committed
print("Load xml config file:" . $GLOBALS['configFile'] . "\n");
// Tests existence of config file
if (!file_exists($GLOBALS['configFile'])) {

Florian Azizian
committed
print(
"Configuration file " . $GLOBALS['configFile']
. " does not exist\n"
);
exit(102);
}
$xmlconfig = simplexml_load_file($GLOBALS['configFile']);
if ($xmlconfig == false) {

Florian Azizian
committed
print("Error on loading config file:" . $GLOBALS['configFile'] . "\n");
exit(103);
}
// Load config
$config = $xmlconfig->CONFIG;

Florian Azizian
committed
$GLOBALS['MaarchDirectory'] = (string)$config->MaarchDirectory;

Florian Azizian
committed
$GLOBALS['customId'] = (string)$config->customID;
$GLOBALS['applicationUrl'] = (string)$config->MaarchUrl;
$config = $xmlconfig->SIGNATUREBOOK;
$GLOBALS['userWS'] = (string)$config->userWS;
$GLOBALS['passwordWS'] = (string)$config->passwordWS;

Florian Azizian
committed
$GLOBALS['batchDirectory'] = $GLOBALS['MaarchDirectory'] . 'bin/signatureBook';
$validatedStatus = (string)$config->validatedStatus;
$validatedStatusOnlyVisa = (string)$config->validatedStatusOnlyVisa;
$refusedStatus = (string)$config->refusedStatus;
$validatedStatusAnnot = (string)$config->validatedStatusAnnot;
$refusedStatusAnnot = (string)$config->refusedStatusAnnot;

Florian Azizian
committed
chdir($GLOBALS['MaarchDirectory']);
set_include_path(get_include_path() . PATH_SEPARATOR . $GLOBALS['MaarchDirectory']);
try {
Bt_myInclude($GLOBALS['MaarchDirectory'] . 'vendor/autoload.php');
// On regarde la configuration du parapheur

Florian Azizian
committed
if (file_exists($GLOBALS['MaarchDirectory'] . "custom/".$GLOBALS['customId']."/modules/visa/xml/remoteSignatoryBooks.xml")) {
$path = $GLOBALS['MaarchDirectory'] . "custom/".$GLOBALS['customId']."/modules/visa/xml/remoteSignatoryBooks.xml";
} else {
$path = $GLOBALS['MaarchDirectory'] . 'modules/visa/xml/remoteSignatoryBooks.xml';
}
if (file_exists($path)) {
$loadedXml = simplexml_load_file($path);
if ($loadedXml) {
$configRemoteSignatoryBook = [];
$configRemoteNoteBook = ['id' => 'maarchParapheur'];
$configRemoteSignatoryBook['id'] = (string)$loadedXml->signatoryBookEnabled;
foreach ($loadedXml->signatoryBook as $value) {
if ($value->id == $configRemoteSignatoryBook['id']) {
$configRemoteSignatoryBook['data'] = (array)$value;
}
if ($value->id == $configRemoteNoteBook['id']) {
$configRemoteNoteBook['data'] = (array)$value;
}
} else {

Florian Azizian
committed
Bt_writeLog(['level' => 'ERROR', 'message' => $path . ' can not be loaded']);
exit(102);

Florian Azizian
committed
Bt_writeLog(['level' => 'ERROR', 'message' => $path . ' does not exist']);
if (empty($configRemoteSignatoryBook)) {

Florian Azizian
committed
Bt_writeLog(['level' => 'ERROR', 'message' => 'no signatory book enabled']);
// On inclut la classe du parapheur activé
if (!in_array($configRemoteSignatoryBook['id'], ['maarchParapheur', 'xParaph', 'fastParapheur', 'iParapheur', 'ixbus'])) {

Florian Azizian
committed
Bt_writeLog(['level' => 'ERROR', 'message' => 'No class detected']);
} catch (IncludeFileError $e) {

Florian Azizian
committed
Bt_writeLog(['level' => 'ERROR', 'message' => 'Problem with the php include path:' .$e .' '. get_include_path()]);

Florian Azizian
committed
new \SrcCore\models\DatabasePDO(['customId' => $GLOBALS['customId']]);
$GLOBALS['errorLckFile'] = $GLOBALS['batchDirectory'] . DIRECTORY_SEPARATOR . $GLOBALS['batchName'] .'_error.lck';
$GLOBALS['lckFile'] = $GLOBALS['batchDirectory'] . DIRECTORY_SEPARATOR . $GLOBALS['batchName'] . '.lck';
if (file_exists($GLOBALS['errorLckFile'])) {

Florian Azizian
committed
Bt_writeLog(['level' => 'ERROR', 'message' => 'Error persists, please solve this before launching a new batch']);
exit(13);
}
Bt_getWorkBatch();

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Retrieve attachments sent to remote signatory book']);
$attachments = \Attachment\models\AttachmentModel::get([
'select' => ['res_id', 'external_id->>\'signatureBookId\' as external_id', 'external_id->>\'xparaphDepot\' as xparaphdepot', 'format', 'res_id_master', 'title', 'identifier', 'attachment_type', 'recipient_id', 'recipient_type', 'typist', 'origin_id', 'relation'],
'where' => ['status = ?', 'external_id->>\'signatureBookId\' IS NOT NULL', 'external_id->>\'signatureBookId\' <> \'\''],
'data' => ['FRZ']
]);

Florian Azizian
committed
$idsToRetrieve = ['noVersion' => [], 'resLetterbox' => []];

Florian Azizian
committed
foreach ($attachments as $value) {
$idsToRetrieve['noVersion'][$value['res_id']] = $value;
}
// On récupère les pj signés dans le parapheur distant

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Retrieve signed/annotated documents from remote signatory book']);
if ($configRemoteSignatoryBook['id'] == 'ixbus') {

Florian Azizian
committed
$retrievedMails = \ExternalSignatoryBook\controllers\IxbusController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'noVersion']);
} elseif ($configRemoteSignatoryBook['id'] == 'iParapheur') {

Florian Azizian
committed
$retrievedMails = \ExternalSignatoryBook\controllers\IParapheurController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'noVersion']);
} elseif ($configRemoteSignatoryBook['id'] == 'fastParapheur') {

Florian Azizian
committed
$retrievedMails = \ExternalSignatoryBook\controllers\FastParapheurController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'noVersion']);
} elseif ($configRemoteSignatoryBook['id'] == 'maarchParapheur') {

Florian Azizian
committed
$retrievedMails = \ExternalSignatoryBook\controllers\MaarchParapheurController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'noVersion']);
} elseif ($configRemoteSignatoryBook['id'] == 'xParaph') {

Florian Azizian
committed
$retrievedMails = \ExternalSignatoryBook\controllers\XParaphController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'noVersion']);

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Retrieve mails sent to remote signatory book']);
$resources = \Resource\models\ResModel::get([
'select' => ['res_id', 'external_id->>\'signatureBookId\' as external_id', 'subject', 'typist', 'version'],
'where' => ['external_id->>\'signatureBookId\' IS NOT NULL', 'external_id->>\'signatureBookId\' <> \'\'']
]);

Florian Azizian
committed
foreach ($resources as $value) {
$idsToRetrieve['resLetterbox'][$value['res_id']] = $value;

Florian Azizian
committed
if (!empty($idsToRetrieve['resLetterbox'])) {
if ($configRemoteSignatoryBook['id'] == 'maarchParapheur') {

Florian Azizian
committed
$retrievedLetterboxMails = \ExternalSignatoryBook\controllers\MaarchParapheurController::retrieveSignedMails(['config' => $configRemoteNoteBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'resLetterbox']);
} elseif ($configRemoteSignatoryBook['id'] == 'fastParapheur') {

Florian Azizian
committed
$retrievedLetterboxMails = \ExternalSignatoryBook\controllers\FastParapheurController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'resLetterbox']);
} elseif ($configRemoteSignatoryBook['id'] == 'iParapheur') {

Florian Azizian
committed
$retrievedLetterboxMails = \ExternalSignatoryBook\controllers\IParapheurController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'resLetterbox']);
} elseif ($configRemoteSignatoryBook['id'] == 'ixbus') {

Florian Azizian
committed
$retrievedLetterboxMails = \ExternalSignatoryBook\controllers\IxbusController::retrieveSignedMails(['config' => $configRemoteSignatoryBook, 'idsToRetrieve' => $idsToRetrieve, 'version' => 'resLetterbox']);
$retrievedMails['resLetterbox'] = $retrievedLetterboxMails['resLetterbox'];
}

Florian Azizian
committed
Bt_writeLog(['level' => 'ERROR', 'message' => $retrievedMails['error']]);
// On dégele les pj et on créé une nouvelle ligne si le document a été signé
$nbMailsRetrieved = 0;
foreach ($retrievedMails['noVersion'] as $resId => $value) {

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Update res_attachments : ' . $resId . '. ExternalId : ' . $value['external_id']]);

Florian Azizian
committed
if (!empty($value['log'])) {
Bt_writeLog(['level' => 'INFO', 'message' => 'Create log Attachment']);

Florian Azizian
committed
'resIdMaster' => $value['res_id_master'],
'title' => '[xParaph Log] ' . $value['title'],
'chrono' => $value['identifier'],
'recipientId' => $value['recipient_id'],
'recipientType' => $value['recipient_type'],
'typist' => $value['typist'],

Florian Azizian
committed
'type' => $value['attachment_type'],
'inSignatureBook' => false,

Florian Azizian
committed
'encodedFile' => $value['log'],
'status' => 'TRA'

Florian Azizian
committed
$additionalHistoryInfo = '';

Florian Azizian
committed
if (!empty($value['workflowInfo'])) {
$additionalHistoryInfo = ' : ' . $value['workflowInfo'];

Florian Azizian
committed
}

Florian Azizian
committed
if ($value['status'] == 'validated') {
if (!empty($value['encodedFile'])) {
\SrcCore\models\DatabaseModel::delete([
'table' => 'res_attachments',
'where' => ['res_id_master = ?', 'status = ?', 'relation = ?', 'origin = ?'],

Florian Azizian
committed
'data' => [$value['res_id_master'], 'SIGN', $value['relation'], $value['res_id'] . ',res_attachments']

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Create validated Attachment']);
Bt_createAttachment([

Florian Azizian
committed
'resIdMaster' => $value['res_id_master'],
'title' => $value['title'],
'chrono' => $value['identifier'],
'recipientId' => $value['recipient_id'],
'recipientType' => $value['recipient_type'],
'typist' => $value['typist'],
'format' => $value['format'],
'type' => 'signed_response',
'status' => 'TRA',

Florian Azizian
committed
'encodedFile' => $value['encodedFile'],
'inSignatureBook' => true,
'originId' => $resId
]);
}

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Document validated']);
\Attachment\models\AttachmentModel::update([
'set' => ['status' => 'SIGN', 'in_signature_book' => 'false'],
'postSet' => ['external_id' => "external_id - 'signatureBookId'"],
'where' => ['res_id = ?'],
'data' => [$resId]
]);
if (!empty($value['onlyVisa']) && $value['onlyVisa']) {
$status = $validatedStatusOnlyVisa;
} else {
$status = $validatedStatus;
}

Florian Azizian
committed
Bt_validatedMail(['status' => $status, 'resId' => $value['res_id_master']]);

Florian Azizian
committed
$historyInfo = 'La signature de la pièce jointe '.$resId.' (res_attachments) a été validée dans le parapheur externe' . $additionalHistoryInfo;

Florian Azizian
committed
} elseif ($value['status'] == 'refused') {
if (!empty($value['encodedFile'])) {
Bt_writeLog(['level' => 'INFO', 'message' => 'Create refused Attachment']);
Bt_createAttachment([

Florian Azizian
committed
'resIdMaster' => $value['res_id_master'],
'title' => '[REFUSE] ' . $value['title'],
'chrono' => $value['identifier'],
'recipientId' => $value['recipient_id'],
'recipientType' => $value['recipient_type'],
'typist' => $value['typist'],
'format' => $value['format'],
'type' => $value['attachment_type'],
'status' => 'A_TRA',

Florian Azizian
committed
'encodedFile' => $value['encodedFile'],
'inSignatureBook' => false
]);
}

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Document refused']);
\Entity\models\ListInstanceModel::update([
'set' => ['process_date' => null],
'where' => ['res_id = ?', 'difflist_type = ?'],
'data' => [$value['res_id_master'], 'VISA_CIRCUIT']
]);
\Attachment\models\AttachmentModel::update([
'set' => ['status' => 'A_TRA'],
'postSet' => ['external_id' => "external_id - 'signatureBookId'"],
'where' => ['res_id = ?'],
'data' => [$resId]
]);
\Resource\models\ResModel::update([
'set' => ['status' => $refusedStatus],
'where' => ['res_id = ?'],
'data' => [$value['res_id_master']]
]);
$historyInfo = 'La signature de la pièce jointe '.$resId.' (res_attachments) a été refusée dans le parapheur externe' . $additionalHistoryInfo;
}

Florian Azizian
committed
if (in_array($value['status'], ['validated', 'refused'])) {
Bt_createNote(['creatorId' => $value['noteCreatorId'], 'creatorName' => $value['noteCreatorName'], 'content' => $value['noteContent'], 'resId' => $value['res_id_master']]);
Bt_history([
'table_name' => 'res_attachments',
'record_id' => $resId,
'info' => $historyInfo,
'event_type' => 'UP',
'event_id' => 'attachup'
Bt_history([
'table_name' => 'res_letterbox',

Florian Azizian
committed
'record_id' => $value['res_id_master'],
'info' => $historyInfo,
'event_type' => 'ACTION#1',
'event_id' => '1'
$nbMailsRetrieved++;
foreach ($retrievedMails['resLetterbox'] as $resId => $value) {

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'Update res_letterbox : ' . $resId . '. SignatoryBookId : ' . $value['external_id']]);

Florian Azizian
committed
if (!empty($value['encodedFile'])) {
Bt_writeLog(['level' => 'INFO', 'message' => 'Create document in res_letterbox']);
if ($value['status'] =='validated') {
$typeToDelete = ['SIGN', 'TNL'];
$typeToDelete = ['NOTE'];
\SrcCore\models\DatabaseModel::delete([
'table' => 'adr_letterbox',
'where' => ['res_id = ?', 'type in (?)', 'version = ?'],

Florian Azizian
committed
'data' => [$resId, $typeToDelete, $value['version']]
]);
$storeResult = \Docserver\controllers\DocserverController::storeResourceOnDocServer([
'collId' => 'letterbox_coll',
'docserverTypeId' => 'DOC',

Florian Azizian
committed
'encodedResource' => $value['encodedFile'],
'format' => 'pdf'
]);
\SrcCore\models\DatabaseModel::insert([
'table' => 'adr_letterbox',
'columnsValues' => [
'res_id' => $resId,

Florian Azizian
committed
'type' => in_array($value['status'], ['refused', 'refusedNote', 'validatedNote']) ? 'NOTE' : 'SIGN',
'docserver_id' => $storeResult['docserver_id'],
'path' => $storeResult['destination_dir'],
'filename' => $storeResult['file_destination_name'],

Florian Azizian
committed
'version' => $value['version'],
'fingerprint' => empty($storeResult['fingerPrint']) ? null : $storeResult['fingerPrint']
]

Florian Azizian
committed
if (in_array($value['status'], ['validatedNote', 'validated', 'refusedNote', 'refused'])) {
$additionalHistoryInfo = '';

Florian Azizian
committed
if (!empty($value['workflowInfo'])) {
$additionalHistoryInfo = ' : ' . $value['workflowInfo'];

Florian Azizian
committed
if (in_array($value['status'], ['validatedNote', 'validated'])) {
Bt_writeLog(['level' => 'INFO', 'message' => 'Document validated']);
$status = $validatedStatus;

Florian Azizian
committed
if ($value['status'] == 'validatedNote') {
$status = $validatedStatusAnnot;
}
$history = 'Le document '.$resId.' (res_letterbox) a été validé dans le parapheur externe' . $additionalHistoryInfo;

Florian Azizian
committed
} elseif (in_array($value['status'], ['refusedNote', 'refused'])) {
Bt_writeLog(['level' => 'INFO', 'message' => 'Document refused']);
$status = $refusedStatus;

Florian Azizian
committed
if ($value['status'] == 'refusedNote') {
$status = $refusedStatusAnnot;
}
$history = 'Le document '.$resId.' (res_letterbox) a été refusé dans le parapheur externe' . $additionalHistoryInfo;
Bt_history([
'table_name' => 'res_letterbox',
'record_id' => $resId,
'info' => $history,
'event_type' => 'ACTION#1',
'event_id' => '1'
]);

Florian Azizian
committed
Bt_createNote(['creatorId' => $value['noteCreatorId'], 'creatorName' => $value['noteCreatorName'], 'content' => $value['noteContent'], 'resId' => $resId]);
\Resource\models\ResModel::update([
'set' => ['status' => $status],
'postSet' => ['external_id' => "external_id - 'signatureBookId'"],
'where' => ['res_id = ?'],
'data' => [$value['res_id_master']]
]);
$nbMailsRetrieved++;

Florian Azizian
committed
Bt_writeLog(['level' => 'INFO', 'message' => 'End of process']);
Bt_writeLog(['level' => 'INFO', 'message' => $nbMailsRetrieved.' document(s) retrieved']);

Florian Azizian
committed
Bt_logInDataBase($nbMailsRetrieved, $err, $nbMailsRetrieved.' mail(s) retrieved from signatory book');
Bt_updateWorkBatch();
exit($GLOBALS['exitCode']);