From d98c41871d2c924e706e997379145f337243409a Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Tue, 26 Mar 2019 18:15:14 +0100 Subject: [PATCH] FEAT #9107 Attchments external_id + shippings --- sql/develop.sql | 36 ++++++++++++++- sql/structure.sql | 14 ++++++ .../PreProcessActionController.php | 30 +------------ src/app/action/controllers/ShippingTrait.php | 31 ++++++++++++- .../ShippingTemplateController.php | 28 ++++++++++++ src/app/shipping/models/ShippingModel.php | 45 +++++++++++++++++++ 6 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 src/app/shipping/models/ShippingModel.php diff --git a/sql/develop.sql b/sql/develop.sql index d628b26fb4e..fa710eb683e 100755 --- a/sql/develop.sql +++ b/sql/develop.sql @@ -95,6 +95,21 @@ account json DEFAULT '{}', CONSTRAINT shipping_templates_pkey PRIMARY KEY (id) ) WITH (OIDS=FALSE); +DROP TABLE IF EXISTS shippings; +CREATE TABLE shippings +( +id serial NOT NULL, +user_id INTEGER NOT NULL, +attachment_id INTEGER NOT NULL, +is_version boolean NOT NULL, +options json DEFAULT '{}', +fee FLOAT NOT NULL, +recipient_entity_id INTEGER NOT NULL, +account_id character varying(64) NOT NULL, +creation_date timestamp without time zone NOT NULL, +CONSTRAINT shippings_pkey PRIMARY KEY (id) +) +WITH (OIDS=FALSE); /* SERVICES */ DO $$ BEGIN @@ -246,11 +261,30 @@ END$$; DO $$ BEGIN IF (SELECT count(attname) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'contact_addresses') AND attname = 'external_contact_id') THEN ALTER TABLE contact_addresses DROP COLUMN IF EXISTS external_id; - ALTER TABLE contact_addresses ADD COLUMN external_id json DEFAULT '{}'; + ALTER TABLE contact_addresses ADD COLUMN external_id jsonb DEFAULT '{}'; UPDATE contact_addresses SET external_id = json_build_object('m2m', external_contact_id); ALTER TABLE contact_addresses DROP COLUMN IF EXISTS external_contact_id; END IF; END$$; +DO $$ BEGIN + IF (SELECT count(column_name) from information_schema.columns where table_name = 'res_attachments' and column_name = 'external_id' and data_type != 'jsonb') THEN + ALTER TABLE res_attachments DROP COLUMN IF EXISTS external_id_tmp; + ALTER TABLE res_attachments ADD COLUMN external_id_tmp jsonb DEFAULT '{}'; + UPDATE res_attachments SET external_id_tmp = json_build_object('signatureBookId', external_id); + ALTER TABLE res_attachments DROP COLUMN IF EXISTS external_id; + ALTER TABLE res_attachments RENAME COLUMN external_id_tmp TO external_id; + END IF; +END$$; +DO $$ BEGIN + IF (SELECT count(column_name) from information_schema.columns where table_name = 'res_version_attachments' and column_name = 'external_id' and data_type != 'jsonb') THEN + ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS external_id_tmp; + ALTER TABLE res_version_attachments ADD COLUMN external_id_tmp jsonb DEFAULT '{}'; + UPDATE res_version_attachments SET external_id_tmp = json_build_object('signatureBookId', external_id); + ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS external_id; + ALTER TABLE res_version_attachments RENAME COLUMN external_id_tmp TO external_id; + END IF; +END$$; + /* RE-CREATE VIEW*/ CREATE OR REPLACE VIEW res_view_letterbox AS diff --git a/sql/structure.sql b/sql/structure.sql index 3c4a525ef6a..f7e32f73907 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -1580,6 +1580,20 @@ account json DEFAULT '{}', CONSTRAINT shipping_templates_pkey PRIMARY KEY (id) ) WITH (OIDS=FALSE); +CREATE TABLE shippings +( +id serial NOT NULL, +user_id INTEGER NOT NULL, +attachment_id INTEGER NOT NULL, +is_version boolean NOT NULL, +options json DEFAULT '{}', +fee FLOAT NOT NULL, +recipient_entity_id INTEGER NOT NULL, +account_id character varying(64) NOT NULL, +creation_date timestamp without time zone NOT NULL, +CONSTRAINT shippings_pkey PRIMARY KEY (id) +) +WITH (OIDS=FALSE); --VIEWS -- view for letterbox diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php index d2df21b3556..fd45a8f339a 100644 --- a/src/app/action/controllers/PreProcessActionController.php +++ b/src/app/action/controllers/PreProcessActionController.php @@ -18,7 +18,6 @@ use Basket\models\BasketModel; use Basket\models\GroupBasketRedirectModel; use Contact\controllers\ContactController; use Contact\models\ContactModel; -use Convert\controllers\ConvertPdfController; use Docserver\models\DocserverModel; use Doctype\models\DoctypeExtModel; use Entity\models\EntityModel; @@ -29,6 +28,7 @@ use Resource\controllers\ResourceListController; use Resource\models\ResModel; use Respect\Validation\Validator; use setasign\Fpdi\Tcpdf\Fpdi; +use Shipping\controllers\ShippingTemplateController; use Shipping\models\ShippingTemplateModel; use Slim\Http\Request; use Slim\Http\Response; @@ -444,7 +444,7 @@ class PreProcessActionController foreach ($aTemplates as $key => $value) { if (!empty($resources)) { - $templateFee = PreProcessActionController::calculFee([ + $templateFee = ShippingTemplateController::calculShippingFee([ 'fee' => $value['fee'], 'resources' => $resources ]); @@ -485,30 +485,4 @@ class PreProcessActionController return $response->withJson(['isDestinationChanging' => $changeDestination]); } - - public static function calculFee(array $aArgs) - { - $fee = 0; - foreach ($aArgs['resources'] as $value) { - $realId = 0; - if ($value['res_id'] == 0) { - $realId = $value['res_id_version']; - $isVersion = true; - } elseif ($value['res_id_version'] == 0) { - $realId = $value['res_id']; - $isVersion = false; - } - $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $value['res_id'], 'collId' => 'attachments_coll', 'isVersion' => $isVersion]); - $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedAttachment['docserver_id'], 'select' => ['path_template']]); - $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedAttachment['path']) . $convertedAttachment['filename']; - - $pdf = new Fpdi(); - $pageCount = $pdf->setSourceFile($pathToDocument); - - $attachmentFee = ($pageCount > 1) ? ($pageCount - 1) * $aArgs['fee']['nextPagePrice'] : 0 ; - $fee = $fee + $attachmentFee + $aArgs['fee']['firstPagePrice'] + $aArgs['fee']['postagePrice']; - } - - return $fee; - } } diff --git a/src/app/action/controllers/ShippingTrait.php b/src/app/action/controllers/ShippingTrait.php index 69b66e25163..62aea920752 100644 --- a/src/app/action/controllers/ShippingTrait.php +++ b/src/app/action/controllers/ShippingTrait.php @@ -17,11 +17,16 @@ use Contact\controllers\ContactController; use Contact\models\ContactModel; use Convert\controllers\ConvertPdfController; use Docserver\models\DocserverModel; +use Entity\models\EntityModel; +use Resource\models\ResModel; +use Shipping\controllers\ShippingTemplateController; +use Shipping\models\ShippingModel; use Shipping\models\ShippingTemplateModel; use SrcCore\models\CoreConfigModel; use SrcCore\models\CurlModel; use SrcCore\models\PasswordModel; use SrcCore\models\ValidatorModel; +use User\models\UserModel; trait ShippingTrait @@ -32,6 +37,10 @@ trait ShippingTrait ValidatorModel::intVal($args, ['resId']); ValidatorModel::arrayType($args, ['data']); + $currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); + $resource = ResModel::getById(['select' => ['destination'], 'resId' => $args['resId']]); + $recipientEntity = EntityModel::getByEntityId(['select' => ['id'], 'entityId' => $resource['destination']]); + $mailevaConfig = CoreConfigModel::getMailevaConfiguration(); if (empty($mailevaConfig)) { return ['errors' => ['Maileva configuration does not exist']]; @@ -42,9 +51,10 @@ trait ShippingTrait } $shippingTemplate['options'] = json_decode($shippingTemplate['options'], true); $shippingTemplate['account'] = json_decode($shippingTemplate['account'], true); + $shippingTemplate['fee'] = json_decode($shippingTemplate['fee'], true); $attachments = AttachmentModel::getOnView([ - 'select' => ['res_id', 'res_id_version', 'title', 'dest_address_id'], + 'select' => ['res_id', 'res_id_version', 'title', 'dest_address_id', 'external_id'], 'where' => ['res_id_master = ?', 'in_send_attach = ?'], 'data' => [$args['resId'], true] ]); @@ -200,6 +210,25 @@ trait ShippingTrait $errors[] = "Maileva submit failed for attachment {$attachmentId}"; continue; } + + $externalId = json_decode($attachment['external_id'], true); + $externalId['mailevaSendingId'] = $sendingId; + AttachmentModel::update(['isVersion' => $isVersion, 'set' => ['external_id' => json_encode($externalId)], 'where' => ['res_id = ?'], 'data' => [$attachmentId]]); + + $fee = ShippingTemplateController::calculShippingFee([ + 'fee' => $shippingTemplate['fee'], + 'resources' => [$attachment] + ]); + + ShippingModel::create([ + 'userId' => $currentUser['id'], + 'attachmentId' => $attachmentId, + 'isVersion' => $isVersion, + 'options' => json_encode($shippingTemplate['options']), + 'fee' => $fee, + 'recipientEntityId' => $recipientEntity['id'], + 'accountId' => $shippingTemplate['account']['id'] + ]); } if (!empty($errors)) { diff --git a/src/app/shipping/controllers/ShippingTemplateController.php b/src/app/shipping/controllers/ShippingTemplateController.php index a298d23d2a5..98ad26f2115 100755 --- a/src/app/shipping/controllers/ShippingTemplateController.php +++ b/src/app/shipping/controllers/ShippingTemplateController.php @@ -12,10 +12,13 @@ namespace Shipping\controllers; +use Convert\controllers\ConvertPdfController; +use Docserver\models\DocserverModel; use Entity\models\EntityModel; use Group\models\ServiceModel; use History\controllers\HistoryController; use Respect\Validation\Validator; +use setasign\Fpdi\Tcpdf\Fpdi; use Shipping\models\ShippingTemplateModel; use Slim\Http\Request; use Slim\Http\Response; @@ -281,4 +284,29 @@ class ShippingTemplateController 'entities' => $allEntities, ]); } + + public static function calculShippingFee(array $aArgs) + { + $fee = 0; + foreach ($aArgs['resources'] as $value) { + if (!empty($value['res_id'])) { + $isVersion = false; + $attachmentId = $value['res_id']; + } else { + $isVersion = true; + $attachmentId = $value['res_id_version']; + } + $convertedAttachment = ConvertPdfController::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $attachmentId, 'collId' => 'attachments_coll', 'isVersion' => $isVersion]); + $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedAttachment['docserver_id'], 'select' => ['path_template']]); + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedAttachment['path']) . $convertedAttachment['filename']; + + $pdf = new Fpdi(); + $pageCount = $pdf->setSourceFile($pathToDocument); + + $attachmentFee = ($pageCount > 1) ? ($pageCount - 1) * $aArgs['fee']['nextPagePrice'] : 0 ; + $fee = $fee + $attachmentFee + $aArgs['fee']['firstPagePrice'] + $aArgs['fee']['postagePrice']; + } + + return $fee; + } } diff --git a/src/app/shipping/models/ShippingModel.php b/src/app/shipping/models/ShippingModel.php new file mode 100644 index 00000000000..6df206c3035 --- /dev/null +++ b/src/app/shipping/models/ShippingModel.php @@ -0,0 +1,45 @@ +<?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 Shipping Model +* @author dev@maarch.org +*/ + +namespace Shipping\models; + +use SrcCore\models\ValidatorModel; +use SrcCore\models\DatabaseModel; + +class ShippingModel +{ + public static function create(array $args) + { + ValidatorModel::notEmpty($args, ['userId', 'attachmentId', 'accountId']); + ValidatorModel::intVal($args, ['userId', 'attachmentId', 'recipientEntityId']); + ValidatorModel::stringType($args, ['accountId']); + ValidatorModel::boolType($args, ['isVersion']); + + DatabaseModel::insert([ + 'table' => 'shippings', + 'columnsValues' => [ + 'user_id' => $args['userId'], + 'attachment_id' => $args['attachmentId'], + 'is_version' => $args['isVersion'], + 'options' => $args['options'], + 'fee' => $args['fee'], + 'recipient_entity_id' => $args['recipientEntityId'], + 'account_id' => $args['accountId'], + 'creation_date' => 'CURRENT_TIMESTAMP' + ] + ]); + + return true; + } +} -- GitLab