From 38b7b9f2be6790079b98e3d2b8c1a7203ac27c3d Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Fri, 14 Feb 2020 11:15:16 +0100 Subject: [PATCH] FEAT #12072 TIME 1:30 Fix shipping + save recipients --- migration/20.03/2003.sql | 1 + src/app/action/controllers/ShippingTrait.php | 46 +++++++++++++------ .../contact/controllers/ContactController.php | 11 +++-- .../controllers/ShippingController.php | 25 ++++++---- src/app/shipping/models/ShippingModel.php | 21 +++++---- 5 files changed, 66 insertions(+), 38 deletions(-) diff --git a/migration/20.03/2003.sql b/migration/20.03/2003.sql index 4a4f163f327..a558374af85 100644 --- a/migration/20.03/2003.sql +++ b/migration/20.03/2003.sql @@ -809,6 +809,7 @@ DO $$ BEGIN ALTER TABLE shippings ALTER COLUMN document_type SET NOT NULL; END IF; END$$; +ALTER TABLE shippings ADD COLUMN recipients jsonb DEFAULT '[]'; TRUNCATE TABLE indexing_models; INSERT INTO indexing_models (id, category, label, "default", owner, private) VALUES (1, 'incoming', 'Courrier arrivée', TRUE, 23, FALSE); diff --git a/src/app/action/controllers/ShippingTrait.php b/src/app/action/controllers/ShippingTrait.php index 04ffef37dba..186080182e7 100644 --- a/src/app/action/controllers/ShippingTrait.php +++ b/src/app/action/controllers/ShippingTrait.php @@ -41,7 +41,7 @@ trait ShippingTrait $currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); - $resource = ResModel::getById(['select' => ['destination', 'integrations', 'subject as title', 'external_id', 'res_id'], 'resId' => $args['resId']]); + $resource = ResModel::getById(['select' => ['destination', 'integrations', 'subject as title', 'external_id', 'res_id', 'version'], 'resId' => $args['resId']]); $integrations = json_decode($resource['integrations'], true); $recipientEntity = EntityModel::getByEntityId(['select' => ['id'], 'entityId' => $resource['destination']]); @@ -109,11 +109,10 @@ trait ShippingTrait } $contacts[] = $afnorAddress; - $attachment['type'] = 'attachments_coll'; + $attachment['type'] = 'attachment'; $resourcesList[] = $attachment; } - $contactsResource = []; if (!empty($integrations['inShipping'])) { $convertedDocument = AdrModel::getDocuments([ 'select' => ['docserver_id', 'path', 'filename', 'fingerprint'], @@ -134,6 +133,7 @@ trait ShippingTrait return ['errors' => ['No contact found for resource']]; } + $contactsResource = []; foreach ($resourceContacts as $resourceContact) { $contact = ContactModel::getById(['select' => ['*'], 'id' => $resourceContact['item_id']]); if (empty($contact)) { @@ -148,7 +148,9 @@ trait ShippingTrait } $contactsResource[] = $afnorAddress; } - $resource['type'] = 'letterbox_coll'; + $contacts[] = $contactsResource; + + $resource['type'] = 'resource'; $resourcesList[] = $resource; } @@ -196,15 +198,26 @@ trait ShippingTrait continue; } - $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $resource['type']]); + $resourceIdToFind = $resId; + if ($resource['type'] == 'attachment' && $resource['status'] == 'SIGN') { + $signedAttachment = AttachmentModel::get([ + 'select' => ['res_id'], + 'where' => ['origin = ?', 'status not in (?)', 'attachment_type = ?'], + 'data' => ["{$args['resId']},res_attachments", ['OBS', 'DEL', 'TMP', 'FRZ'], 'signed_response'] + ]); + if (!empty($signedAttachment[0])) { + $resourceIdToFind = $signedAttachment[0]['res_id']; + } + } + $convertedDocument = ConvertPdfController::getConvertedPdfById(['resId' => $resourceIdToFind, 'collId' => ($resource['type'] == 'resource' ? 'letterbox_coll' : 'attachments_coll')]); $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedDocument['docserver_id'], 'select' => ['path_template']]); if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) { - $errors[] = "Docserver does not exist for resource {$resId}"; + $errors[] = "Docserver does not exist for {$resource['type']} {$resId}"; continue; } $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedDocument['path']) . $convertedDocument['filename']; if (!file_exists($pathToDocument) || !is_file($pathToDocument)) { - $errors[] = "Document not found on docserver for resource {$resId}"; + $errors[] = "Document not found on docserver for {$resource['type']} {$resId}"; continue; } @@ -219,7 +232,8 @@ trait ShippingTrait continue; } - if ($resource['type'] == 'attachments_coll') { + $recipients = []; + if ($resource['type'] == 'attachment') { $createRecipient = CurlModel::execSimple([ 'url' => $mailevaConfig['uri'] . "/mail/v1/sendings/{$sendingId}/recipients", 'bearerAuth' => ['token' => $token], @@ -239,8 +253,9 @@ trait ShippingTrait $errors[] = "Maileva recipient creation failed for resource {$resId}"; continue; } + $recipients[] = $contacts[$key]; } else { - foreach ($contactsResource as $contact) { + foreach ($contacts[$key] as $contact) { $createRecipient = CurlModel::execSimple([ 'url' => $mailevaConfig['uri'] . "/mail/v1/sendings/{$sendingId}/recipients", 'bearerAuth' => ['token' => $token], @@ -260,6 +275,7 @@ trait ShippingTrait $errors[] = "Maileva recipient creation failed for resource {$resId}"; continue 2; } + $recipients[] = $contact; } } @@ -293,22 +309,26 @@ trait ShippingTrait $externalId = json_decode($resource['external_id'], true); $externalId['mailevaSendingId'] = $sendingId; - AttachmentModel::update(['set' => ['external_id' => json_encode($externalId)], 'where' => ['res_id = ?'], 'data' => [$resId]]); + if ($resource['type'] == 'attachment') { + AttachmentModel::update(['set' => ['external_id' => json_encode($externalId)], 'where' => ['res_id = ?'], 'data' => [$resId]]); + } else { + ResModel::update(['set' => ['external_id' => json_encode($externalId)], 'where' => ['res_id = ?'], 'data' => [$resId]]); + } $fee = ShippingTemplateController::calculShippingFee([ 'fee' => $shippingTemplate['fee'], 'resources' => [$resource] ]); - $documentType = $resource['type'] == 'attachments_coll' ? 'attachment' : 'resource'; ShippingModel::create([ 'userId' => $currentUser['id'], 'documentId' => $resId, - 'documentType' => $documentType, + 'documentType' => $resource['type'], 'options' => json_encode($shippingTemplate['options']), 'fee' => $fee, 'recipientEntityId' => $recipientEntity['id'], - 'accountId' => $shippingTemplate['account']['id'] + 'accountId' => $shippingTemplate['account']['id'], + 'recipients' => json_encode($recipients) ]); } diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php index 4ef28d88ee0..ccaeb9006d2 100755 --- a/src/app/contact/controllers/ContactController.php +++ b/src/app/contact/controllers/ContactController.php @@ -700,7 +700,7 @@ class ContactController if (!empty($args['company'])) { // Ligne 1 - $afnorAddress[1] = substr($args['company'], 0, 38); + $afnorAddress[1] = trim(substr($args['company'], 0, 38)); } // Ligne 2 @@ -710,11 +710,12 @@ class ContactController 'fullName' => $args['firstname'].' '.$args['lastname'], 'strMaxLength' => 38 ]); + $afnorAddress[2] = trim($afnorAddress[2]); } // Ligne 3 if (!empty($args['address_additional1'])) { - $afnorAddress[3] = substr($args['address_additional1'], 0, 38); + $afnorAddress[3] = trim(substr($args['address_additional1'], 0, 38)); } // Ligne 4 @@ -728,17 +729,17 @@ class ContactController $args['address_street'] = preg_replace('/[^\w]/s', ' ', $args['address_street']); $args['address_street'] = strtoupper($args['address_street']); } - $afnorAddress[4] = substr($args['address_number'].' '.$args['address_street'], 0, 38); + $afnorAddress[4] = trim(substr($args['address_number'].' '.$args['address_street'], 0, 38)); // Ligne 5 if (!empty($args['address_additional2'])) { - $afnorAddress[5] = substr($args['address_additional2'], 0, 38); + $afnorAddress[5] = trim(substr($args['address_additional2'], 0, 38)); } // Ligne 6 $args['address_postcode'] = strtoupper($args['address_postcode']); $args['address_town'] = strtoupper($args['address_town']); - $afnorAddress[6] = substr($args['address_postcode'].' '.$args['address_town'], 0, 38); + $afnorAddress[6] = trim(substr($args['address_postcode'].' '.$args['address_town'], 0, 38)); return $afnorAddress; } diff --git a/src/app/shipping/controllers/ShippingController.php b/src/app/shipping/controllers/ShippingController.php index 1a9b3040209..00e9020ec47 100644 --- a/src/app/shipping/controllers/ShippingController.php +++ b/src/app/shipping/controllers/ShippingController.php @@ -58,18 +58,23 @@ class ShippingController foreach ($shippingsModel as $shipping) { $recipientEntityLabel = EntityModel::getById(['id' => $shipping['recipient_entity_id'], 'select' => ['entity_label']]); $recipientEntityLabel = $recipientEntityLabel['entity_label']; - - $userLabel = UserModel::getLabelledUserById(['id' => $shipping['user_id']]); + $recipients = json_decode($shipping['recipients'], true); + $contacts = []; + foreach ($recipients as $recipient) { + $contacts[] = ['company' => $recipient[1], 'contactLabel' => $recipient[2]]; + } $shippings[] = [ - 'id' => $shipping['id'], - 'documentId' => $shipping['document_id'], - 'documentType' => $shipping['document_type'], - 'userId' => $shipping['user_id'], - 'userLabel' => $userLabel, - 'fee' => $shipping['fee'], - 'recipientEntityId' => $shipping['recipient_entity_id'], - 'recipientEntityLabel' => $recipientEntityLabel + 'id' => $shipping['id'], + 'documentId' => $shipping['document_id'], + 'documentType' => $shipping['document_type'], + 'userId' => $shipping['user_id'], + 'userLabel' => UserModel::getLabelledUserById(['id' => $shipping['user_id']]), + 'fee' => $shipping['fee'], + 'creationDate' => $shipping['creation_date'], + 'recipientEntityId' => $shipping['recipient_entity_id'], + 'recipientEntityLabel' => $recipientEntityLabel, + 'recipients' => $contacts ]; } diff --git a/src/app/shipping/models/ShippingModel.php b/src/app/shipping/models/ShippingModel.php index 670e0aa7546..cb6c633dbc9 100644 --- a/src/app/shipping/models/ShippingModel.php +++ b/src/app/shipping/models/ShippingModel.php @@ -21,21 +21,22 @@ class ShippingModel { public static function create(array $args) { - ValidatorModel::notEmpty($args, ['userId', 'documentId', 'documentType', 'accountId']); + ValidatorModel::notEmpty($args, ['userId', 'documentId', 'documentType', 'accountId', 'recipients']); ValidatorModel::intVal($args, ['userId', 'documentId', 'recipientEntityId']); - ValidatorModel::stringType($args, ['accountId', 'documentType']); + ValidatorModel::stringType($args, ['accountId', 'documentType', 'recipients']); DatabaseModel::insert([ 'table' => 'shippings', 'columnsValues' => [ - 'user_id' => $args['userId'], - 'document_id' => $args['documentId'], - 'document_type' => $args['documentType'], - 'options' => $args['options'], - 'fee' => $args['fee'], - 'recipient_entity_id' => $args['recipientEntityId'], - 'account_id' => $args['accountId'], - 'creation_date' => 'CURRENT_TIMESTAMP' + 'user_id' => $args['userId'], + 'document_id' => $args['documentId'], + 'document_type' => $args['documentType'], + 'options' => $args['options'], + 'fee' => $args['fee'], + 'recipient_entity_id' => $args['recipientEntityId'], + 'recipients' => $args['recipients'], + 'account_id' => $args['accountId'], + 'creation_date' => 'CURRENT_TIMESTAMP' ] ]); -- GitLab