diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php index 6dd483af2457572ae2341b1788fe6d4b629eba23..4785f655c0c8365d63b0cb125e36c651aa9a7c09 100755 --- a/src/app/attachment/controllers/AttachmentController.php +++ b/src/app/attachment/controllers/AttachmentController.php @@ -596,7 +596,7 @@ class AttachmentController $docserverType = DocserverTypeModel::getById(['id' => $docserver['docserver_type_id'], 'select' => ['fingerprint_mode']]); $fingerprint = StoreController::getFingerPrint(['filePath' => $pathToDocument, 'mode' => $docserverType['fingerprint_mode']]); if (empty($document['fingerprint'])) { - AttachmentModel::update(['set' => ['fingerprint' => $fingerprint], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]); + AttachmentModel::update(['set' => ['fingerprint' => $fingerprint], 'where' => ['res_id = ?'], 'data' => [$args['id']]]); $document['fingerprint'] = $fingerprint; } diff --git a/src/app/external/exportSeda/controllers/ExportSEDATrait.php b/src/app/external/exportSeda/controllers/ExportSEDATrait.php index c78ae82f78973ebb301f690db10ce6797a0365e3..13b5df2e5560745d2c73933c8e29e50e8aa9926a 100644 --- a/src/app/external/exportSeda/controllers/ExportSEDATrait.php +++ b/src/app/external/exportSeda/controllers/ExportSEDATrait.php @@ -17,11 +17,15 @@ use Docserver\models\DocserverModel; use Docserver\models\DocserverTypeModel; use Doctype\models\DoctypeModel; use Entity\models\EntityModel; +use Entity\models\ListInstanceModel; use ExportSeda\controllers\ExportSEDATrait; use ExportSeda\controllers\SedaController; use MessageExchange\models\MessageExchangeModel; +use Note\controllers\NoteController; use Resource\controllers\StoreController; +use Resource\controllers\SummarySheetController; use Resource\models\ResModel; +use setasign\Fpdi\Tcpdf\Fpdi; use SrcCore\models\CoreConfigModel; use SrcCore\models\ValidatorModel; @@ -79,8 +83,33 @@ trait ExportSEDATrait 'resource' => $resource, 'senderOrgRegNumber' => $config['exportSeda']['senderOrgRegNumber'], 'entity' => $entity, - 'doctype' => $doctype - ])['archivalData']; + 'doctype' => $doctype, + 'getFile' => true + ]); + + if (!empty($initData['errors'])) { + return ['errors' => $initData['errors']]; + } else { + $initData = $initData['archivalData']; + } + + $dataObjectPackage = []; + $initialArchiveUnits = $initData['archiveUnits']; + foreach ($initialArchiveUnits as $archiveUnit) { + $archiveFound = false; + foreach ($args['data']['archives'] as $userArchiveUnit) { + if ($userArchiveUnit['id'] == $archiveUnit['id']) { + $archiveUnit['descriptionLevel'] = $userArchiveUnit['descriptionLevel']; + $dataObjectPackage[] = $archiveUnit; + $archiveFound = true; + break; + } + } + if (!$archiveFound) { + $archiveUnit['descriptionLevel'] = 'Item'; + $dataObjectPackage = $archiveUnit; + } + } $data = [ 'type' => 'ArchiveTransfer', @@ -103,6 +132,127 @@ trait ExportSEDATrait return true; } + public static function getAttachmentFilePath($args = []) + { + $document['docserver_id'] = $args['data']['docserver_id']; + $document['path'] = $args['data']['path']; + $document['filename'] = $args['data']['filename']; + $document['fingerprint'] = $args['data']['fingerprint']; + + $docserver = DocserverModel::getByDocserverId(['docserverId' => $document['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]); + if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) { + return ['errors' => 'Docserver does not exist']; + } + + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $document['path']) . $document['filename']; + + if (!file_exists($pathToDocument)) { + return ['errors' => 'Attachment not found on docserver']; + } + + $docserverType = DocserverTypeModel::getById(['id' => $docserver['docserver_type_id'], 'select' => ['fingerprint_mode']]); + $fingerprint = StoreController::getFingerPrint(['filePath' => $pathToDocument, 'mode' => $docserverType['fingerprint_mode']]); + if (empty($document['fingerprint'])) { + AttachmentModel::update(['set' => ['fingerprint' => $fingerprint], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]); + $document['fingerprint'] = $fingerprint; + } + + if (!empty($document['fingerprint']) && $document['fingerprint'] != $fingerprint) { + return ['errors' => 'Fingerprints do not match']; + } + + $fileContent = file_exists($pathToDocument); + if ($fileContent === false) { + return ['errors' => 'Document not found on docserver']; + } + + return ['filePath' => $pathToDocument]; + } + + public static function getNoteFilePath($args = []) + { + $encodedDocument = NoteController::getEncodedPdfByIds(['ids' => [$args['id']]]); + + $tmpPath = CoreConfigModel::getTmpPath(); + $filePath = $tmpPath . 'note_' . $args['id'] . '.pdf'; + file_put_contents($filePath, base64_decode($encodedDocument['encodedDocument'])); + + return ['filePath' => $filePath]; + } + + public static function getEmailFilePath($args = []) + { + $body = str_replace('###', ';', $args['data']['body']); + $sender = json_decode($args['data']['sender'], true); + $data = "Courriel n°" . $args['data']['id'] . "\nDe : " . $sender['email'] . "\nPour : " . implode(", ", json_decode($args['data']['recipients'], true)) . "\nObjet : " . $args['data']['object'] . "\n\n" . strip_tags(html_entity_decode($body)); + + $pdf = new Fpdi('P', 'pt'); + $pdf->setPrintHeader(false); + $pdf->AddPage(); + $pdf->MultiCell(0, 10, $data, 0, 'L'); + + $tmpPath = CoreConfigModel::getTmpPath(); + $filePath = $tmpPath . 'email_' . $args['data']['id'] . '.pdf'; + $pdf->Output($filePath, "F"); + + return ['filePath' => $filePath]; + } + + public static function getSummarySheetFilePath($args = []) + { + $units = []; + $units[] = ['unit' => 'primaryInformations']; + $units[] = ['unit' => 'secondaryInformations', 'label' => _SECONDARY_INFORMATION]; + $units[] = ['unit' => 'senderRecipientInformations', 'label' => _DEST_INFORMATION]; + $units[] = ['unit' => 'diffusionList', 'label' => _DIFFUSION_LIST]; + $units[] = ['unit' => 'visaWorkflow', 'label' => _VISA_WORKFLOW]; + $units[] = ['unit' => 'opinionWorkflow', 'label' => _AVIS_WORKFLOW]; + $units[] = ['unit' => 'notes', 'label' => _NOTES_COMMENT]; + + $tmpIds = [$args['resId']]; + $data = []; + foreach ($units as $unit) { + if ($unit['unit'] == 'opinionWorkflow') { + $data['listInstancesOpinion'] = ListInstanceModel::get([ + 'select' => ['item_id', 'process_date', 'res_id'], + 'where' => ['difflist_type = ?', 'res_id in (?)'], + 'data' => ['AVIS_CIRCUIT', $tmpIds], + 'orderBy' => ['listinstance_id'] + ]); + } elseif ($unit['unit'] == 'visaWorkflow') { + $data['listInstancesVisa'] = ListInstanceModel::get([ + 'select' => ['item_id', 'requested_signature', 'process_date', 'res_id'], + 'where' => ['difflist_type = ?', 'res_id in (?)'], + 'data' => ['VISA_CIRCUIT', $tmpIds], + 'orderBy' => ['listinstance_id'] + ]); + } elseif ($unit['unit'] == 'diffusionList') { + $data['listInstances'] = ListInstanceModel::get([ + 'select' => ['item_id', 'item_type', 'item_mode', 'res_id'], + 'where' => ['difflist_type = ?', 'res_id in (?)'], + 'data' => ['entity_id', $tmpIds], + 'orderBy' => ['listinstance_id'] + ]); + } + } + + $mainResource = ResModel::getOnView([ + 'select' => ['process_limit_date', 'status', 'alt_identifier', 'subject', 'priority', 'res_id', 'admission_date', 'creation_date', 'doc_date', 'initiator', 'typist', 'type_label', 'destination', 'filename'], + 'where' => ['res_id = ?'], + 'data' => [$args['resId']] + ]); + + $pdf = new Fpdi('P', 'pt'); + $pdf->setPrintHeader(false); + SummarySheetController::createSummarySheet($pdf, ['resource' => $mainResource[0], 'units' => $units, 'login' => $GLOBALS['login'], 'data' => $data]); + + $tmpPath = CoreConfigModel::getTmpPath(); + $summarySheetFilePath = $tmpPath . "summarySheet_".$args['resId'] . "_" . $aArgs['userId'] . "_" . rand() . ".pdf"; + $pdf->Output($summarySheetFilePath, 'F'); + + return ['filePath' => $summarySheetFilePath]; + } + public static function generateSEDAPackage(array $args) { $encodedFile = ''; diff --git a/src/app/external/exportSeda/controllers/SedaController.php b/src/app/external/exportSeda/controllers/SedaController.php index 8719c37004578864f11f89b9ada4b05fa89b3279..69d6b96cdc87bf2d2121824de7006b7a30054ac1 100755 --- a/src/app/external/exportSeda/controllers/SedaController.php +++ b/src/app/external/exportSeda/controllers/SedaController.php @@ -15,14 +15,19 @@ namespace ExportSeda\controllers; use Attachment\models\AttachmentModel; +use Convert\models\AdrModel; +use Docserver\models\DocserverModel; +use Docserver\models\DocserverTypeModel; use Doctype\models\DoctypeModel; use Email\models\EmailModel; use Entity\models\EntityModel; +use ExportSeda\controllers\ExportSEDATrait; use Folder\models\FolderModel; use Group\controllers\PrivilegeController; use Note\models\NoteModel; use Resource\controllers\ResController; use Resource\controllers\ResourceListController; +use Resource\controllers\StoreController; use Resource\models\ResModel; use Respect\Validation\Validator; use Slim\Http\Request; @@ -79,7 +84,13 @@ class SedaController 'senderOrgRegNumber' => $config['exportSeda']['senderOrgRegNumber'], 'entity' => $entity, 'doctype' => $doctype - ])['archivalData']; + ]); + + if (!empty($return['errors'])) { + return $response->withStatus(400)->withJson(['errors' => $return['errors']]); + } else { + $return = $return['archivalData']; + } $archivalAgreements = SedaController::getArchivalAgreements([ 'config' => $config, @@ -121,62 +132,123 @@ class SedaController 'archiveId' => 'archive_' . $args['resource']['res_id'] ] ], - 'archiveUnits' => [ - [ - 'id' => 'letterbox_' . $args['resource']['res_id'], - 'label' => $args['resource']['subject'], - 'type' => 'mainDocument', - 'descriptionLevel' => 'Item' - ] - ] + 'archiveUnits' => [] ]; + $document = ResModel::getById(['select' => ['docserver_id', 'path', 'filename', 'version', 'fingerprint'], 'resId' => $args['resource']['res_id']]); + if (!empty($document['docserver_id']) && empty($document['filename'])) { + $convertedDocument = AdrModel::getDocuments([ + 'select' => ['docserver_id', 'path', 'filename', 'fingerprint'], + 'where' => ['res_id = ?', 'type = ?', 'version = ?'], + 'data' => [$args['resource']['res_id'], 'SIGN', $document['version']], + 'limit' => 1 + ]); + $document = $convertedDocument[0] ?? $document; + + $docserver = DocserverModel::getByDocserverId(['docserverId' => $document['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]); + if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) { + return ['errors' => 'Docserver does not exist']; + } + + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $document['path']) . $document['filename']; + if (!file_exists($pathToDocument)) { + return ['errors' => 'Document not found on docserver']; + } + + $docserverType = DocserverTypeModel::getById(['id' => $docserver['docserver_type_id'], 'select' => ['fingerprint_mode']]); + $fingerprint = StoreController::getFingerPrint(['filePath' => $pathToDocument, 'mode' => $docserverType['fingerprint_mode']]); + if (empty($convertedDocument) && empty($document['fingerprint'])) { + ResModel::update(['set' => ['fingerprint' => $fingerprint], 'where' => ['res_id = ?'], 'data' => [$args['resource']['res_id']]]); + $document['fingerprint'] = $fingerprint; + } + + if ($document['fingerprint'] != $fingerprint) { + return ['errors' => 'Fingerprints do not match']; + } + + $fileContent = file_exists($pathToDocument); + if ($fileContent === false) { + return ['errors' => 'Document not found on docserver']; + } + + $return['data']['archiveUnits'][0] = [ + 'id' => 'letterbox_' . $args['resource']['res_id'], + 'label' => $args['resource']['subject'], + 'type' => 'mainDocument', + 'descriptionLevel' => 'Item' + ]; + + if ($args['getFile']) { + $return['data']['archiveUnits'][0]['filePath'] = $pathToDocument; + } + } + $attachments = AttachmentModel::get([ - 'select' => ['res_id', 'title'], - 'where' => ['res_id_master = ?', 'status not in (?)', 'attachment_type not in (?)'], - 'data' => [$args['resource']['res_id'], ['DEL', 'OBS', 'TMP'], ['signed_response']], + 'select' => ['res_id', 'title', 'docserver_id', 'path', 'filename', 'res_id_master', 'fingerprint'], + 'where' => ['res_id_master = ?', 'status in (?)'], + 'data' => [$args['resource']['res_id'], ['A_TRA', 'TRA']], 'orderBy' => ['modification_date DESC'] ]); foreach ($attachments as $attachment) { - $return['archiveUnits'][] = [ + $tmpAttachment = [ 'id' => 'attachment_' . $attachment['res_id'], 'label' => $attachment['title'], 'type' => 'attachment', 'descriptionLevel' => 'Item' ]; + if ($args['getFile']) { + $attachment = ExportSEDATrait::getAttachmentFilePath(['data' => $attachment]); + $tmpAttachment['filePath'] = $attachment['filePath']; + } + $return['archiveUnits'][] = $tmpAttachment; } $notes = NoteModel::get(['select' => ['note_text', 'id'], 'where' => ['identifier = ?'], 'data' => [$args['resource']['res_id']]]); foreach ($notes as $note) { - $return['archiveUnits'][] = [ + $tmpNote = [ 'id' => 'note_' . $note['id'], 'label' => $note['note_text'], 'type' => 'note', 'descriptionLevel' => 'Item' ]; + if ($args['getFile']) { + $note = ExportSEDATrait::getNoteFilePath(['id' => $note['id']]); + $tmpNote['filePath'] = $note['filePath']; + } + $return['archiveUnits'][] = $tmpNote; } $emails = EmailModel::get([ - 'select' => ['object', 'id'], + 'select' => ['object', 'id', 'body', 'sender', 'recipients'], 'where' => ['document->>\'id\' = ?', 'status = ?'], 'data' => [$args['resource']['res_id'], 'SENT'], 'orderBy' => ['send_date desc'] ]); foreach ($emails as $email) { - $return['archiveUnits'][] = [ - 'id' => 'note_' . $email['id'], + $tmpEmail = [ + 'id' => 'email_' . $email['id'], 'label' => $email['object'], 'type' => 'email', 'descriptionLevel' => 'Item' ]; + if ($args['getFile']) { + $email = ExportSEDATrait::getEmailFilePath(['data' => $email]); + $tmpEmail['filePath'] = $email['filePath']; + } + $return['archiveUnits'][] = $tmpEmail; } - $return['archiveUnits'][] = [ + $tmpSummarySheet = [ 'id' => 'summarySheet_' . $args['resource']['res_id'], 'label' => 'Fiche de liaison', 'type' => 'summarySheet', 'descriptionLevel' => 'Item' ]; + if ($args['getFile']) { + $summarySheet = ExportSEDATrait::getSummarySheetFilePath(['resId' => $args['resource']['res_id']]); + $tmpSummarySheet['filePath'] = $summarySheet['filePath']; + } + $return['archiveUnits'][] = $tmpSummarySheet; $linkedResourcesIds = json_decode($args['resource']['linked_resources'], true); if (!empty($linkedResourcesIds)) {