diff --git a/migration/19.12/migrateTemplates.php b/migration/19.12/migrateTemplates.php index b860b111a5517473934773bb173efd0d3a535fab..a2c021ddf2a01da69bbf5a04697bd7b5cd299227 100644 --- a/migration/19.12/migrateTemplates.php +++ b/migration/19.12/migrateTemplates.php @@ -226,8 +226,7 @@ foreach ($customs as $custom) { 'where' => ['label = ?'], 'data' => [$customField['label']] ]); - $idNewCustomField = array_column($idNewCustomField, 'id'); - $DATA_TO_REPLACE["contact." . $customField['oldId']] = "[recipient.custom_{$idNewCustomField[0]}]"; + $DATA_TO_REPLACE["contact." . $customField['oldId']] = "[recipient.customField_{$idNewCustomField[0]['id']}]"; } $migrated = 0; diff --git a/src/app/resource/controllers/SummarySheetController.php b/src/app/resource/controllers/SummarySheetController.php index 7a179d91dabe496a1890b0d59d3753896473d4e0..deca6b8a49853ffe5b34842eec9f742bef8d38d9 100755 --- a/src/app/resource/controllers/SummarySheetController.php +++ b/src/app/resource/controllers/SummarySheetController.php @@ -16,9 +16,11 @@ namespace Resource\controllers; use Basket\models\BasketModel; use Contact\controllers\ContactController; +use CustomField\models\CustomFieldModel; use Endroid\QrCode\QrCode; use Entity\models\EntityModel; use Entity\models\ListInstanceModel; +use IndexingModel\models\IndexingModelFieldModel; use Note\models\NoteEntityModel; use Note\models\NoteModel; use Parameter\models\ParameterModel; @@ -30,6 +32,7 @@ use Slim\Http\Request; use Slim\Http\Response; use SrcCore\controllers\PreparedClauseController; use SrcCore\models\CoreConfigModel; +use SrcCore\models\DatabaseModel; use SrcCore\models\TextFormatModel; use SrcCore\models\ValidatorModel; use Status\models\StatusModel; @@ -64,10 +67,7 @@ class SummarySheetController 'where' => [$whereClause, 'res_view_letterbox.res_id in (?)'], 'data' => [$bodyData['resources']] ]); - $allResourcesInBasket = []; - foreach ($rawResourcesInBasket as $resource) { - $allResourcesInBasket[] = $resource['res_id']; - } + $allResourcesInBasket = array_column($rawResourcesInBasket, 'res_id'); $order = 'CASE res_view_letterbox.res_id '; foreach ($bodyData['resources'] as $key => $resId) { @@ -78,38 +78,87 @@ class SummarySheetController } $order .= 'END'; - $select = ['res_id', 'subject', 'alt_identifier']; - foreach ($units as $unit) { - $unit = (array)$unit; - if ($unit['unit'] == 'primaryInformations') { - $informations = ['admission_date', 'creation_date', 'doc_date', 'type_label', 'initiator', 'typist']; - $select = array_merge($select, $informations); - } elseif ($unit['unit'] == 'secondaryInformations') { - $informations = ['category_id', 'priority', 'process_limit_date', 'status']; - $select = array_merge($select, $informations); - } elseif ($unit['unit'] == 'diffusionList') { - $informations = ['destination']; - $select = array_merge($select, $informations); - } - } - - $resources = ResModel::getOnView([ - 'select' => $select, - 'where' => ['res_view_letterbox.res_id in (?)'], - 'data' => [$bodyData['resources']], - 'orderBy' => [$order] + $resourcesByModelIds = DatabaseModel::select([ + 'select' => ["string_agg(cast(res_id as text), ',') as res_ids", 'model_id'], + 'table' => ['res_letterbox'], + 'where' => ['res_id in (?)'], + 'data' => [$bodyData['resources']], + 'groupBy' => ['model_id'] ]); $pdf = new Fpdi('P', 'pt'); $pdf->setPrintHeader(false); - $resourcesIds = array_column($resources, 'res_id'); + foreach ($resourcesByModelIds as $resourcesByModelId) { + $resourcesIdsByModel = $resourcesByModelId['res_ids']; + $resourcesIdsByModel = explode(',', $resourcesIdsByModel); + + $indexingFields = IndexingModelFieldModel::get([ + 'select' => ['identifier', 'unit'], + 'where' => ['model_id = ?'], + 'data' => [$resourcesByModelId['model_id']] + ]); + $fieldsIdentifier = array_column($indexingFields, 'identifier'); + + $select = ['res_id', 'subject', 'alt_identifier']; + foreach ($units as $unit) { + $unit = (array)$unit; + if ($unit['unit'] == 'primaryInformations') { + $information = [ + 'documentDate' => 'doc_date', + 'arrivalDate' => 'admission_date', + 'initiator' => 'initiator' + ]; + $select[] = 'type_label'; + $select[] = 'creation_date'; + $select[] = 'typist'; + + foreach ($information as $key => $item) { + if (in_array($key, $fieldsIdentifier)) { + $select[] = $item; + } + } + } elseif ($unit['unit'] == 'secondaryInformations') { + $information = [ + 'priority' => 'priority', + 'processLimitDate' => 'process_limit_date', + ]; + $select[] = 'category_id'; + $select[] = 'status'; + + foreach ($information as $key => $item) { + if (in_array($key, $fieldsIdentifier)) { + $select[] = $item; + } + } + } elseif ($unit['unit'] == 'diffusionList') { + if (in_array('destination', $fieldsIdentifier)) { + $select[] = 'destination'; + } + } + } - // Data for resources - $data = SummarySheetController::prepareData(['units' => $units, 'resourcesIds' => $resourcesIds]); + $resources = ResModel::getOnView([ + 'select' => $select, + 'where' => ['res_view_letterbox.res_id in (?)'], + 'data' => [$resourcesIdsByModel], + 'orderBy' => [$order] + ]); - foreach ($resources as $resource) { - SummarySheetController::createSummarySheet($pdf, ['resource' => $resource, 'units' => $units, 'login' => $GLOBALS['userId'], 'data' => $data]); +// $pdf = new Fpdi('P', 'pt'); +// $pdf->setPrintHeader(false); + + $resourcesIds = array_column($resources, 'res_id'); + + // Data for resources + $data = SummarySheetController::prepareData(['units' => $units, 'resourcesIds' => $resourcesIds]); + + foreach ($resources as $resource) { + SummarySheetController::createSummarySheet($pdf, ['resource' => $resource, 'units' => $units, + 'login' => $GLOBALS['userId'], + 'data' => $data, + 'fieldsIdentifier' => $fieldsIdentifier]); + } } $fileContent = $pdf->Output('', 'S'); @@ -235,11 +284,13 @@ class SummarySheetController public static function createSummarySheet(Fpdi $pdf, array $args) { ValidatorModel::notEmpty($args, ['resource', 'login']); - ValidatorModel::arrayType($args, ['resource', 'units', 'data']); + ValidatorModel::arrayType($args, ['resource', 'units', 'data', 'fieldsIdentifier']); ValidatorModel::stringType($args, ['login']); - $resource = $args['resource']; - $units = $args['units']; + $resource = $args['resource']; + $units = $args['units']; + $fieldsIdentifier = $args['fieldsIdentifier']; + $pdf->AddPage(); $dimensions = $pdf->getPageDimensions(); @@ -259,7 +310,7 @@ class SummarySheetController $pdf->SetFont('', 'B', 12); $pdf->Cell(0, 20, _SUMMARY_SHEET, 0, 2, 'C', false); - + $pdf->SetFont('', '', 8); $pdf->Cell(0, 1, $resource['alt_identifier'], 0, 2, 'C', false); @@ -284,17 +335,26 @@ class SummarySheetController } foreach ($units as $key => $unit) { if ($unit['unit'] == 'primaryInformations') { - $nature = '<i>'._UNDEFINED.'</i>'; - $admissionDate = TextFormatModel::formatDate($resource['admission_date'], 'd-m-Y'); - $admissionDate = empty($admissionDate) ? '<i>'._UNDEFINED.'</i>' : "<b>{$admissionDate}</b>"; + $admissionDate = null; + if (in_array('arrivalDate', $fieldsIdentifier)) { + $admissionDate = TextFormatModel::formatDate($resource['admission_date'], 'd-m-Y'); + $admissionDate = empty($admissionDate) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$admissionDate}</b>"; + } + $creationdate = TextFormatModel::formatDate($resource['creation_date'], 'd-m-Y'); $creationdate = empty($creationdate) ? '<i>'._UNDEFINED.'</i>' : "<b>{$creationdate}</b>"; - $docDate = TextFormatModel::formatDate($resource['doc_date'], 'd-m-Y'); - $docDate = empty($docDate) ? '<i>'._UNDEFINED.'</i>' : "<b>{$docDate}</b>"; + + $docDate = null; + if (in_array('documentDate', $fieldsIdentifier)) { + $docDate = TextFormatModel::formatDate($resource['doc_date'], 'd-m-Y'); + $docDate = empty($docDate) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$docDate}</b>"; + } + if (!empty($resource['initiator'])) { $initiator = EntityModel::getByEntityId(['entityId' => $resource['initiator'], 'select' => ['short_label']]); } $initiatorEntity = empty($initiator) ? '' : "({$initiator['short_label']})"; + $typist = UserModel::getLabelledUserById(['id' => $resource['typist']]); $doctype = empty($resource['type_label']) ? '<i>'._UNDEFINED.'</i>' : "<b>{$resource['type_label']}</b>"; @@ -309,27 +369,79 @@ class SummarySheetController } $pdf->SetFont('', '', 10); - $pdf->MultiCell($widthMultiCell, 15, _DOC_DATE . " : {$docDate}", 0, 'L', false, 0, '', '', true, 0, true); - $pdf->Cell($widthCell, 15, '', 0, 0, 'L', false); - $pdf->MultiCell($widthMultiCell, 15, _ADMISSION_DATE . " : {$admissionDate}", 0, 'L', false, 1, '', '', true, 0, true); - $pdf->MultiCell($widthMultiCell, 15, _NATURE . " : {$nature}", 0, 'L', false, 0, '', '', true, 0, true); - $pdf->Cell($widthCell, 15, '', 0, 0, 'L', false); - $pdf->MultiCell($widthMultiCell, 15, _CREATED . " : {$creationdate}", 0, 'L', false, 1, '', '', true, 0, true); - $pdf->MultiCell($widthMultiCell, 15, _DOCTYPE . " : {$doctype}", 0, 'L', false, 0, '', '', true, 0, true); + + $pdf->MultiCell($widthMultiCell, 15, _TYPIST . " : <b>{$typist} {$initiatorEntity}</b>", 0, 'L', false, 0, '', '', true, 0, true); + if (isset($docDate)) { + $pdf->Cell($widthCell, 15, '', 0, 0, 'L', false); + $pdf->MultiCell($widthMultiCell, 15, _DOC_DATE . " : {$docDate}", 0, 'L', false, 1, '', '', true, 0, true); + } else { + $pdf->Cell($widthCell, 15, '', 0, 1, 'L', false); + } + + $pdf->MultiCell($widthMultiCell, 15, _CREATED . " : {$creationdate}", 0, 'L', false, 0, '', '', true, 0, true); + + if (isset($admissionDate)) { + $pdf->Cell($widthCell, 15, '', 0, 0, 'L', false); + $pdf->MultiCell($widthMultiCell, 15, _ADMISSION_DATE . " : {$admissionDate}", 0, 'L', false, 1, '', '', true, 0, true); + } else { + $pdf->Cell($widthCell, 15, '', 0, 1, 'L', false); + } + + $pdf->MultiCell($widthMultiCell * 2, 15, _DOCTYPE . " : {$doctype}", 0, 'L', false, 0, '', '', true, 0, true); $pdf->Cell($widthCell, 15, '', 0, 0, 'L', false); - $pdf->MultiCell($widthMultiCell, 15, _TYPIST . " : <b>{$typist} {$initiatorEntity}</b>", 0, 'L', false, 1, '', '', true, 0, true); } elseif ($unit['unit'] == 'secondaryInformations') { $category = ResModel::getCategoryLabel(['categoryId' => $resource['category_id']]); $category = empty($category) ? '<i>'._UNDEFINED.'</i>' : "<b>{$category}</b>"; - $status = StatusModel::getById(['id' => $resource['status'], 'select' => ['label_status']]); - $status = empty($status['label_status']) ? '<i>'._UNDEFINED.'</i>' : "<b>{$status['label_status']}</b>"; - $priority = ''; - if (!empty($resource['priority'])) { - $priority = PriorityModel::getById(['id' => $resource['priority'], 'select' => ['label']]); + + $status = StatusModel::getById(['id' => $resource['status'], 'select' => ['label_status']]); + $status = empty($status['label_status']) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$status['label_status']}</b>"; + + $priority = null; + if (in_array('priority', $fieldsIdentifier)) { + $priority = ''; + if (!empty($resource['priority'])) { + $priority = PriorityModel::getById(['id' => $resource['priority'], 'select' => ['label']]); + } + $priority = empty($priority['label']) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$priority['label']}</b>"; + } + + $processLimitDate = null; + if (in_array('processLimitDate', $fieldsIdentifier)) { + $processLimitDate = TextFormatModel::formatDate($resource['process_limit_date'], 'd-m-Y'); + $processLimitDate = empty($processLimitDate) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$processLimitDate}</b>"; + } + + // Custom fields + $customFieldsValues = ResModel::get([ + 'select' => ['custom_fields'], + 'where' => ['res_id = ?'], + 'data' => [$resource['res_id']] + ]); + // Get all the ids of the custom fields in the model + $customFieldsIds = []; + foreach ($fieldsIdentifier as $item) { + if (strpos($item, 'indexingCustomField_') !== false) { + $customFieldsIds[] = explode('_', $item)[1]; + } + } + + if (!empty($customFieldsIds)) { + // get the label of the custom fields + $customFields = CustomFieldModel::get([ + 'select' => ['id', 'label'], + 'where' => ['id in (?)'], + 'data' => [$customFieldsIds] + ]); + + $tmp = []; + foreach ($customFields as $customField) { + $tmp[$customField['id']] = $customField['label']; + } + $customFields = $tmp; + + $customFieldsValues = $customFieldsValues[0]['custom_fields'] ?? null; + $customFieldsValues = json_decode($customFieldsValues, true); } - $priority = empty($priority['label']) ? '<i>'._UNDEFINED.'</i>' : "<b>{$priority['label']}</b>"; - $processLimitDate = TextFormatModel::formatDate($resource['process_limit_date'], 'd-m-Y'); - $processLimitDate = empty($processLimitDate) ? '<i>'._UNDEFINED.'</i>' : "<b>{$processLimitDate}</b>"; $pdf->SetY($pdf->GetY() + 40); if (($pdf->GetY() + 57) > $bottomHeight) { @@ -341,9 +453,33 @@ class SummarySheetController $pdf->SetFont('', '', 10); $pdf->MultiCell($widthNotes, 30, _CATEGORY . " : {$category}", 1, 'L', false, 0, '', '', true, 0, true); + $pdf->MultiCell($widthNotes, 30, _STATUS . " : {$status}", 1, 'L', false, 1, '', '', true, 0, true); - $pdf->MultiCell($widthNotes, 30, _PRIORITY . " : {$priority}", 1, 'L', false, 0, '', '', true, 0, true); - $pdf->MultiCell($widthNotes, 30, _PROCESS_LIMIT_DATE . " : {$processLimitDate}", 1, 'L', false, 1, '', '', true, 0, true); + + $nextLine = 1; + if (isset($priority)) { + $nextLine = isset($processLimitDate) || !empty($customFieldsIds) ? 0 : 1; + $pdf->MultiCell($widthNotes, 30, _PRIORITY . " : {$priority}", 1, 'L', false, $nextLine, '', '', true, 0, true); + } + if (isset($processLimitDate)) { + $nextLine = !empty($customFieldsIds) && $nextLine == 0 ? 1 : 0; + $pdf->MultiCell($widthNotes, 30, _PROCESS_LIMIT_DATE . " : {$processLimitDate}", $nextLine, 'L', false, 1, '', '', true, 0, true); + } + + if (!empty($customFieldsIds)) { + foreach ($customFieldsIds as $customFieldsId) { + $label = $customFields[$customFieldsId]; + if (is_array($customFieldsValues[$customFieldsId])) { + $value = !empty($customFieldsValues[$customFieldsId]) ? implode(',', $customFieldsValues[$customFieldsId]) : _UNDEFINED; + } else { + $value = $customFieldsValues[$customFieldsId] ?? _UNDEFINED; + } + + $nextLine = ($nextLine + 1) % 2; + $pdf->MultiCell($widthNotes, 30, $label . " : {$value}", 1, 'L', false, $nextLine, '', '', true, 0, true); + } + } + } elseif ($unit['unit'] == 'senderRecipientInformations') { $senders = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'sender']); $recipients = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);