Skip to content
Snippets Groups Projects
Commit c9dd369a authored by Florian Azizian's avatar Florian Azizian
Browse files

FEAT #12346 TIME 1 end test batch process mail from signatory book

parent 2fb11ca2
No related branches found
No related tags found
No related merge requests found
......@@ -154,39 +154,28 @@ function Bt_myInclude($file)
}
}
function Bt_createAttachment($aArgs = [])
function Bt_createNote($aArgs = [])
{
if (!empty($aArgs['noteContent'])) {
if (!empty($aArgs['content'])) {
$creatorName = '';
if (!empty($aArgs['noteCreatorId'])) {
$creatorId = $aArgs['noteCreatorId'];
if (!empty($aArgs['creatorId'])) {
$creatorId = $aArgs['creatorId'];
} else {
$req = "SELECT id FROM users ORDER BY user_id='superadmin' desc limit 1";
$stmt = $GLOBALS['db']->query($req, array([]));
$reqResult = $stmt->fetchObject();
$creatorId = $reqResult->id;
$creatorName = $aArgs['noteCreatorName'] . ' : ';
$req = "SELECT id FROM users ORDER BY user_id='superadmin' desc limit 1";
$stmt = $GLOBALS['db']->query($req, []);
$reqResult = $stmt->fetchObject();
$creatorId = $reqResult->id;
$creatorName = $aArgs['creatorName'] . ' : ';
}
$GLOBALS['db']->query(
"INSERT INTO notes (identifier, user_id, creation_date, note_text) VALUES (?, ?, CURRENT_TIMESTAMP, ?)",
[$aArgs['res_id_master'], $creatorId, $creatorName . $aArgs['noteContent']]
[$aArgs['resId'], $creatorId, $creatorName . $aArgs['content']]
);
}
}
$dataValue = [];
$dataValue['resIdMaster'] = $aArgs['res_id_master'];
$dataValue['title'] = $aArgs['title'];
$dataValue['recipientId'] = $aArgs['recipient_id'];
$dataValue['recipientType'] = $aArgs['recipient_type'];
$dataValue['typist'] = $aArgs['typist'];
$dataValue['chrono'] = $aArgs['identifier'];
$dataValue['type'] = $aArgs['attachment_type'];
$dataValue['inSignatureBook'] = $aArgs['in_signature_book'];
$dataValue['encodedFile'] = $aArgs['encodedFile'];
$dataValue['format'] = $aArgs['format'];
$dataValue['status'] = $aArgs['status'];
$dataValue['originId'] = $aArgs['origin_id'];
function Bt_createAttachment($args = [])
{
$opts = [
CURLOPT_URL => $GLOBALS['applicationUrl'] . 'rest/attachments',
CURLOPT_HTTPHEADER => [
......@@ -196,58 +185,26 @@ function Bt_createAttachment($aArgs = [])
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => json_encode($dataValue),
CURLOPT_POSTFIELDS => json_encode($args),
CURLOPT_POST => true
];
$curl = curl_init();
curl_setopt_array($curl, $opts);
$rawResponse = curl_exec($curl);
$error = curl_error($curl);
$error = curl_error($curl);
if (!empty($error)) {
$GLOBALS['logger']->write($error, 'ERROR');
exit;
}
return json_decode($rawResponse, true);
}
function Bt_refusedSignedMail($aArgs = [])
{
if (!empty($aArgs['noteContent'])) {
$creatorName = '';
if (!empty($aArgs['noteCreatorId'])) {
$creatorId = $aArgs['noteCreatorId'];
} else {
$creatorId = 'superadmin';
$creatorName = $aArgs['noteCreatorName'] . ' : ';
}
$GLOBALS['db']->query(
"INSERT INTO notes (identifier, user_id, creation_date, note_text) VALUES (?, '".$creatorId."', CURRENT_TIMESTAMP, ?)",
[$aArgs['resIdMaster'], $creatorName . $aArgs['noteContent']]
);
$return = json_decode($rawResponse, true);
if (!empty($return['errors'])) {
$GLOBALS['logger']->write($return['errors'], 'ERROR');
exit;
}
$GLOBALS['db']->query("UPDATE ".$aArgs['tableAttachment']." SET status = 'A_TRA', external_id = external_id - 'signatureBookId' WHERE res_id = ?", [$aArgs['resIdAttachment']]);
$GLOBALS['db']->query('UPDATE listinstance SET process_date = NULL WHERE res_id = ? AND difflist_type = ?', [$aArgs['resIdMaster'], 'VISA_CIRCUIT']);
$GLOBALS['db']->query("UPDATE res_letterbox SET status = '" . $aArgs['refusedStatus'] . "' WHERE res_id = ?", [$aArgs['resIdMaster']]);
$historyInfo = 'La signature de la pièce jointe '.$aArgs['resIdAttachment'].' ('.$aArgs['tableAttachment'].') a été refusée dans le parapheur externe' . $aArgs['additionalHistoryInfo'];
Bt_history([
'table_name' => $aArgs['tableAttachment'],
'record_id' => $aArgs['resIdAttachment'],
'info' => $historyInfo,
'event_type' => 'UP',
'event_id' => 'attachup'
]);
Bt_history([
'table_name' => 'res_letterbox',
'record_id' => $aArgs['resIdMaster'],
'info' => $historyInfo,
'event_type' => 'ACTION#1',
'event_id' => '1'
]);
return $return;
}
function Bt_validatedMail($aArgs = [])
......
......@@ -39,8 +39,6 @@ try {
$GLOBALS['batchName'] = 'retrieveMailsFromSignatoryBook';
$GLOBALS['wb'] = '';
$totalProcessedResources = 0;
$batchDirectory = '';
$log4PhpEnabled = false;
// Open Logger
$GLOBALS['logger'] = new Logger4Php();
......@@ -161,11 +159,11 @@ try {
$path = $GLOBALS['MaarchDirectory'] . 'modules/visa/xml/remoteSignatoryBooks.xml';
}
$configRemoteSignatoryBook = [];
$configRemoteNoteBook = ['id' => 'maarchParapheur'];
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']) {
......@@ -175,6 +173,10 @@ try {
$configRemoteNoteBook['data'] = (array)$value;
}
}
} else {
$GLOBALS['logger']->write($path . ' can not be loaded', 'ERROR', 102);
echo "\nConfiguration file ".$path." can not be loaded ! \nThe batch cannot be launched !\n\n";
exit(102);
}
} else {
$GLOBALS['logger']->write($path . ' does not exist', 'ERROR', 102);
......@@ -220,7 +222,7 @@ try {
$GLOBALS['db'] = 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';
$GLOBALS['lckFile'] = $GLOBALS['batchDirectory'] . DIRECTORY_SEPARATOR . $GLOBALS['batchName'] . '.lck';
if (file_exists($GLOBALS['errorLckFile'])) {
$GLOBALS['logger']->write(
......@@ -260,14 +262,14 @@ if ($configRemoteSignatoryBook['id'] == 'ixbus') {
$GLOBALS['logger']->write('Retrieve mails sent to remote signatory book', 'INFO');
$query = "SELECT res_id, external_id->>'signatureBookId' as external_id, subject, typist, version
FROM res_letterbox WHERE external_id->>'signatureBookId' IS NOT NULL";
FROM res_letterbox WHERE external_id->>'signatureBookId' IS NOT NULL AND external_id->>'signatureBookId' <> ''";
$stmt = $GLOBALS['db']->query($query, []);
while ($reqResult = $stmt->fetchObject()) {
$idsToRetrieve['resLetterbox'][$reqResult->res_id] = $reqResult;
}
if (!empty($idsToRetrieve['resLetterbox'])) {
$retrievedLetterboxMails = \ExternalSignatoryBook\controllers\MaarchParapheurController::retrieveSignedMails(['config' => $configRemoteNoteBook, 'idsToRetrieve' => $idsToRetrieve]);
$retrievedLetterboxMails = \ExternalSignatoryBook\controllers\MaarchParapheurController::retrieveSignedMails(['config' => $configRemoteNoteBook, 'idsToRetrieve' => $idsToRetrieve]);
$retrievedMails['resLetterbox'] = $retrievedLetterboxMails['resLetterbox'];
}
......@@ -277,21 +279,22 @@ if (!empty($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) {
$GLOBALS['logger']->write('Update res_attachments : ' . $resId . '. ExternalId : ' . $value->external_id, 'INFO');
if (!empty($value->log)) {
$GLOBALS['logger']->write('Create log Attachment', 'INFO');
Bt_createAttachment([
'res_id_master' => $value->res_id_master,
'resIdMaster' => $value->res_id_master,
'title' => '[xParaph Log] ' . $value->title,
'identifier' => $value->identifier,
'recipient_id' => $value->recipient_id,
'recipient_type' => $value->recipient_type,
'chrono' => $value->identifier,
'recipientId' => $value->recipient_id,
'recipientType' => $value->recipient_type,
'typist' => $value->typist,
'format' => 'xml',
'attachment_type' => $value->attachment_type,
'in_signature_book' => 'false',
'type' => $value->attachment_type,
'inSignatureBook' => false,
'encodedFile' => $value->log,
'status' => 'TRA'
]);
......@@ -311,26 +314,23 @@ foreach ($retrievedMails['noVersion'] as $resId => $value) {
$GLOBALS['logger']->write('Create validated Attachment', 'INFO');
Bt_createAttachment([
'res_id_master' => $value->res_id_master,
'resIdMaster' => $value->res_id_master,
'title' => $value->title,
'identifier' => $value->identifier,
'recipient_id' => $value->recipient_id,
'recipient_type' => $value->recipient_type,
'chrono' => $value->identifier,
'recipientId' => $value->recipient_id,
'recipientType' => $value->recipient_type,
'typist' => $value->typist,
'format' => $value->format,
'attachment_type' => $value->attachment_type,
'in_signature_book' => 'true',
'origin_id' => $resId,
'type' => $value->attachment_type,
'status' => 'SIGN',
'encodedFile' => $value->encodedFile,
'noteContent' => $value->noteContent,
'noteCreatorId' => $value->noteCreatorId,
'noteCreatorName' => $value->noteCreatorName
'inSignatureBook' => true,
'originId' => $resId
]);
}
$GLOBALS['logger']->write('Document validated', 'INFO');
$GLOBALS['db']->query("UPDATE res_attachments SET status = 'OBS', external_id = external_id - 'signatureBookId' WHERE res_id = ?", [$resId]);
$GLOBALS['db']->query("UPDATE res_attachments SET status = 'TRA', external_id = external_id - 'signatureBookId', in_signature_book = 'false' WHERE res_id = ?", [$resId]);
if (!empty($value->onlyVisa) && $value->onlyVisa) {
$status = $validatedStatusOnlyVisa;
} else {
......@@ -339,6 +339,32 @@ foreach ($retrievedMails['noVersion'] as $resId => $value) {
Bt_validatedMail(['status' => $status, 'resId' => $value->res_id_master]);
$historyInfo = 'La signature de la pièce jointe '.$resId.' (res_attachments) a été validée dans le parapheur externe' . $additionalHistoryInfo;
} elseif ($value->status == 'refused') {
if (!empty($value->encodedFile)) {
$GLOBALS['logger']->write('Create refused Attachment', 'INFO');
Bt_createAttachment([
'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',
'encodedFile' => $value->encodedFile,
'inSignatureBook' => false
]);
}
$GLOBALS['logger']->write('Document refused', 'INFO');
$GLOBALS['db']->query('UPDATE listinstance SET process_date = NULL WHERE res_id = ? AND difflist_type = ?', [$value->res_id_master, 'VISA_CIRCUIT']);
$GLOBALS['db']->query("UPDATE res_attachments SET status = 'A_TRA', external_id = external_id - 'signatureBookId' WHERE res_id = ?", [$resId]);
$GLOBALS['db']->query("UPDATE res_letterbox SET status = '" . $refusedStatus . "' WHERE res_id = ?", [$value->res_id_master]);
$historyInfo = 'La signature de la pièce jointe '.$resId.' (res_attachments) a été refusée dans le parapheur externe' . $additionalHistoryInfo;
}
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,
......@@ -346,6 +372,7 @@ foreach ($retrievedMails['noVersion'] as $resId => $value) {
'event_type' => 'UP',
'event_id' => 'attachup'
]);
Bt_history([
'table_name' => 'res_letterbox',
'record_id' => $value->res_id_master,
......@@ -353,38 +380,7 @@ foreach ($retrievedMails['noVersion'] as $resId => $value) {
'event_type' => 'ACTION#1',
'event_id' => '1'
]);
} elseif ($value->status == 'refused') {
if (!empty($value->encodedFile)) {
$GLOBALS['logger']->write('Create refused Attachment', 'INFO');
Bt_createAttachment([
'res_id_master' => $value->res_id_master,
'title' => '[REFUSE] ' . $value->title,
'identifier' => $value->identifier,
'recipient_id' => $value->recipient_id,
'recipient_type' => $value->recipient_type,
'typist' => $value->typist,
'format' => $value->format,
'attachment_type' => $value->attachment_type,
'status' => 'A_TRA',
'encodedFile' => $value->encodedFile,
'in_signature_book' => 'false',
'noteContent' => $value->noteContent,
'noteCreatorId' => $value->noteCreatorId,
'noteCreatorName' => $value->noteCreatorName
]);
$value->noteContent = '';
}
$GLOBALS['logger']->write('Document refused', 'INFO');
Bt_refusedSignedMail([
'tableAttachment' => 'res_attachments',
'resIdAttachment' => $resId,
'refusedStatus' => $refusedStatus,
'resIdMaster' => $value->res_id_master,
'noteContent' => $value->noteContent,
'noteCreatorId' => $value->noteCreatorId,
'noteCreatorName' => $value->noteCreatorName,
'additionalHistoryInfo' => $additionalHistoryInfo
]);
$nbMailsRetrieved++;
}
}
......@@ -394,23 +390,16 @@ foreach ($retrievedMails['resLetterbox'] as $resId => $value) {
if (!empty($value->encodedFile)) {
$GLOBALS['logger']->write('Create document in res_letterbox', 'INFO');
if ($value->status =='validated') {
\SrcCore\models\DatabaseModel::delete([
'table' => 'adr_letterbox',
'where' => ['res_id = ?', 'type in (?)', 'version = ?'],
'data' => [$resId, ['SIGN', 'TNL'], $value->version]
]);
$typeToDelete = ['SIGN', 'TNL'];
} else {
\SrcCore\models\DatabaseModel::delete([
'table' => 'adr_letterbox',
'where' => ['res_id = ?', 'type in (?)', 'version = ?'],
'data' => [$resId, ['NOTE'], $value->version]
]);
$typeToDelete = ['NOTE'];
}
\SrcCore\models\DatabaseModel::delete([
'table' => 'adr_letterbox',
'where' => ['res_id = ?', 'type in (?)', 'version = ?'],
'data' => [$resId, $typeToDelete, $value->version]
]);
$adrType = 'SIGN';
if (in_array($value->status, ['refused', 'refusedNote', 'validatedNote'])) {
$adrType = 'NOTE';
}
$storeResult = \Docserver\controllers\DocserverController::storeResourceOnDocServer([
'collId' => 'letterbox_coll',
'docserverTypeId' => 'DOC',
......@@ -421,7 +410,7 @@ foreach ($retrievedMails['resLetterbox'] as $resId => $value) {
'table' => 'adr_letterbox',
'columnsValues' => [
'res_id' => $resId,
'type' => $adrType,
'type' => in_array($value->status, ['refused', 'refusedNote', 'validatedNote']) ? 'NOTE' : 'SIGN',
'docserver_id' => $storeResult['docserver_id'],
'path' => $storeResult['destination_dir'],
'filename' => $storeResult['file_destination_name'],
......@@ -430,50 +419,46 @@ foreach ($retrievedMails['resLetterbox'] as $resId => $value) {
]
]);
}
$additionalHistoryInfo = '';
if (!empty($value->workflowInfo)) {
$additionalHistoryInfo = ' : ' . $value->workflowInfo;
}
if (in_array($value->status, ['validatedNote', 'validated'])) {
$GLOBALS['logger']->write('Document validated', 'INFO');
$status = $validatedStatus;
if ($value->status == 'validatedNote') {
$status = $validatedStatusAnnot;
if (in_array($value->status, ['validatedNote', 'validated', 'refusedNote', 'refused'])) {
$additionalHistoryInfo = '';
if (!empty($value->workflowInfo)) {
$additionalHistoryInfo = ' : ' . $value->workflowInfo;
}
$GLOBALS['db']->query('UPDATE res_letterbox SET status = ? WHERE res_id = ? ', [$status, $resId]);
Bt_history([
'table_name' => 'res_letterbox',
'record_id' => $resId,
'info' => 'Le document '.$resId.' (res_letterbox) a été validé dans le parapheur externe' . $additionalHistoryInfo,
'event_type' => 'ACTION#1',
'event_id' => '1'
]);
} elseif (in_array($value->status, ['refusedNote', 'refused'])) {
$GLOBALS['logger']->write('Document refused', 'INFO');
$status = $refusedStatus;
if ($value->status == 'refusedNote') {
$status = $refusedStatusAnnot;
if (in_array($value->status, ['validatedNote', 'validated'])) {
$GLOBALS['logger']->write('Document validated', 'INFO');
$status = $validatedStatus;
if ($value->status == 'validatedNote') {
$status = $validatedStatusAnnot;
}
$history = 'Le document '.$resId.' (res_letterbox) a été validé dans le parapheur externe' . $additionalHistoryInfo;
} elseif (in_array($value->status, ['refusedNote', 'refused'])) {
$GLOBALS['logger']->write('Document refused', 'INFO');
$status = $refusedStatus;
if ($value->status == 'refusedNote') {
$status = $refusedStatusAnnot;
}
$history = 'Le document '.$resId.' (res_letterbox) a été refusé dans le parapheur externe' . $additionalHistoryInfo;
}
$GLOBALS['db']->query("UPDATE res_letterbox SET status = ? WHERE res_id = ?", [$status, $resId]);
Bt_history([
'table_name' => 'res_letterbox',
'record_id' => $resId,
'info' => 'Le document '.$resId.' (res_letterbox) a été refusé dans le parapheur externe' . $additionalHistoryInfo,
'info' => $history,
'event_type' => 'ACTION#1',
'event_id' => '1'
]);
Bt_createNote(['creatorId' => $value->noteCreatorId, 'creatorName' => $value->noteCreatorName, 'content' => $value->noteContent, 'resId' => $resId]);
$GLOBALS['db']->query("UPDATE res_letterbox SET status = ?, external_id = external_id - 'signatureBookId' WHERE res_id = ?", [$status, $resId]);
$nbMailsRetrieved++;
}
$GLOBALS['db']->query("UPDATE res_letterbox SET external_id = external_id - 'signatureBookId' WHERE res_id = ?", [$resId]);
}
$GLOBALS['logger']->write('End of process', 'INFO');
$nbMailsRetrieved = count($retrievedMails['noVersion']) + count($retrievedMails['resLetterbox']);
$GLOBALS['logger']->write($nbMailsRetrieved.' document(s) retrieved', 'INFO');
Bt_logInDataBase(
$nbMailsRetrieved,
$err,
$nbMailsRetrieved.' mail(s) retrieved'
$nbMailsRetrieved.' mail(s) retrieved from signatory book'
);
Bt_updateWorkBatch();
......
......@@ -493,13 +493,14 @@ class MaarchParapheurController
if (!empty($state['note'])) {
$aArgs['idsToRetrieve'][$version][$resId]->noteContent = $state['note'];
$userInfos = UserModel::getByExternalId([
'select' => ['id'],
'select' => ['id', 'firstname', 'lastname'],
'externalId' => $state['noteCreatorId'],
'externalName' => 'maarchParapheur'
]);
if (!empty($userInfos)) {
$aArgs['idsToRetrieve'][$version][$resId]->noteCreatorId = $userInfos['id'];
} else {
$aArgs['idsToRetrieve'][$version][$resId]->noteCreatorName = $userInfos['firstname'] . ' ' . $userInfos['lastname'];
} elseif (!empty($state['noteCreatorName'])) {
$aArgs['idsToRetrieve'][$version][$resId]->noteCreatorName = $state['noteCreatorName'];
}
}
......
......@@ -385,8 +385,6 @@ class ResController extends ResourceControlController
$convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resId'], 'collId' => 'letterbox_coll']);
if (!empty($convertedDocument['errors'])) {
return $response->withStatus(400)->withJson(['errors' => 'Conversion error : ' . $convertedDocument['errors']]);
} elseif ($document['docserver_id'] == $convertedDocument['docserver_id']) {
return $response->withStatus(400)->withJson(['errors' => 'Document can not be converted']);
}
$document = $convertedDocument;
......
......@@ -312,6 +312,7 @@ class StoreController
$externalId = json_encode($args['externalId']);
}
$inSignatureBook = isset($args['inSignatureBook']) ? $args['inSignatureBook'] : $attachmentsTypes[$args['type']]['sign'];
$preparedData = [
'title' => $args['title'] ?? null,
'identifier' => $args['chrono'] ?? null,
......@@ -326,7 +327,7 @@ class StoreController
'recipient_type' => !empty($args['recipientId']) ? $args['recipientType'] : null,
'validation_date' => $args['validationDate'] ?? null,
'effective_date' => $args['effectiveDate'] ?? null,
'in_signature_book' => $attachmentsTypes[$args['type']]['sign'] == true || !empty($args['inSignatureBook']) ? 'true' : 'false',
'in_signature_book' => $inSignatureBook ? 'true' : 'false',
'external_id' => $externalId,
'creation_date' => 'CURRENT_TIMESTAMP'
];
......
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