Skip to content
Snippets Groups Projects
Verified Commit d98c4187 authored by Damien's avatar Damien
Browse files

FEAT #9107 Attchments external_id + shippings

parent 59e04b63
No related branches found
No related tags found
No related merge requests found
...@@ -95,6 +95,21 @@ account json DEFAULT '{}', ...@@ -95,6 +95,21 @@ account json DEFAULT '{}',
CONSTRAINT shipping_templates_pkey PRIMARY KEY (id) CONSTRAINT shipping_templates_pkey PRIMARY KEY (id)
) )
WITH (OIDS=FALSE); 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 */ /* SERVICES */
DO $$ BEGIN DO $$ BEGIN
...@@ -246,11 +261,30 @@ END$$; ...@@ -246,11 +261,30 @@ END$$;
DO $$ BEGIN 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 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 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); UPDATE contact_addresses SET external_id = json_build_object('m2m', external_contact_id);
ALTER TABLE contact_addresses DROP COLUMN IF EXISTS external_contact_id; ALTER TABLE contact_addresses DROP COLUMN IF EXISTS external_contact_id;
END IF; END IF;
END$$; 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*/ /* RE-CREATE VIEW*/
CREATE OR REPLACE VIEW res_view_letterbox AS CREATE OR REPLACE VIEW res_view_letterbox AS
......
...@@ -1580,6 +1580,20 @@ account json DEFAULT '{}', ...@@ -1580,6 +1580,20 @@ account json DEFAULT '{}',
CONSTRAINT shipping_templates_pkey PRIMARY KEY (id) CONSTRAINT shipping_templates_pkey PRIMARY KEY (id)
) )
WITH (OIDS=FALSE); 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 --VIEWS
-- view for letterbox -- view for letterbox
......
...@@ -18,7 +18,6 @@ use Basket\models\BasketModel; ...@@ -18,7 +18,6 @@ use Basket\models\BasketModel;
use Basket\models\GroupBasketRedirectModel; use Basket\models\GroupBasketRedirectModel;
use Contact\controllers\ContactController; use Contact\controllers\ContactController;
use Contact\models\ContactModel; use Contact\models\ContactModel;
use Convert\controllers\ConvertPdfController;
use Docserver\models\DocserverModel; use Docserver\models\DocserverModel;
use Doctype\models\DoctypeExtModel; use Doctype\models\DoctypeExtModel;
use Entity\models\EntityModel; use Entity\models\EntityModel;
...@@ -29,6 +28,7 @@ use Resource\controllers\ResourceListController; ...@@ -29,6 +28,7 @@ use Resource\controllers\ResourceListController;
use Resource\models\ResModel; use Resource\models\ResModel;
use Respect\Validation\Validator; use Respect\Validation\Validator;
use setasign\Fpdi\Tcpdf\Fpdi; use setasign\Fpdi\Tcpdf\Fpdi;
use Shipping\controllers\ShippingTemplateController;
use Shipping\models\ShippingTemplateModel; use Shipping\models\ShippingTemplateModel;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
...@@ -444,7 +444,7 @@ class PreProcessActionController ...@@ -444,7 +444,7 @@ class PreProcessActionController
foreach ($aTemplates as $key => $value) { foreach ($aTemplates as $key => $value) {
if (!empty($resources)) { if (!empty($resources)) {
$templateFee = PreProcessActionController::calculFee([ $templateFee = ShippingTemplateController::calculShippingFee([
'fee' => $value['fee'], 'fee' => $value['fee'],
'resources' => $resources 'resources' => $resources
]); ]);
...@@ -485,30 +485,4 @@ class PreProcessActionController ...@@ -485,30 +485,4 @@ class PreProcessActionController
return $response->withJson(['isDestinationChanging' => $changeDestination]); 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;
}
} }
...@@ -17,11 +17,16 @@ use Contact\controllers\ContactController; ...@@ -17,11 +17,16 @@ use Contact\controllers\ContactController;
use Contact\models\ContactModel; use Contact\models\ContactModel;
use Convert\controllers\ConvertPdfController; use Convert\controllers\ConvertPdfController;
use Docserver\models\DocserverModel; 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 Shipping\models\ShippingTemplateModel;
use SrcCore\models\CoreConfigModel; use SrcCore\models\CoreConfigModel;
use SrcCore\models\CurlModel; use SrcCore\models\CurlModel;
use SrcCore\models\PasswordModel; use SrcCore\models\PasswordModel;
use SrcCore\models\ValidatorModel; use SrcCore\models\ValidatorModel;
use User\models\UserModel;
trait ShippingTrait trait ShippingTrait
...@@ -32,6 +37,10 @@ trait ShippingTrait ...@@ -32,6 +37,10 @@ trait ShippingTrait
ValidatorModel::intVal($args, ['resId']); ValidatorModel::intVal($args, ['resId']);
ValidatorModel::arrayType($args, ['data']); 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(); $mailevaConfig = CoreConfigModel::getMailevaConfiguration();
if (empty($mailevaConfig)) { if (empty($mailevaConfig)) {
return ['errors' => ['Maileva configuration does not exist']]; return ['errors' => ['Maileva configuration does not exist']];
...@@ -42,9 +51,10 @@ trait ShippingTrait ...@@ -42,9 +51,10 @@ trait ShippingTrait
} }
$shippingTemplate['options'] = json_decode($shippingTemplate['options'], true); $shippingTemplate['options'] = json_decode($shippingTemplate['options'], true);
$shippingTemplate['account'] = json_decode($shippingTemplate['account'], true); $shippingTemplate['account'] = json_decode($shippingTemplate['account'], true);
$shippingTemplate['fee'] = json_decode($shippingTemplate['fee'], true);
$attachments = AttachmentModel::getOnView([ $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 = ?'], 'where' => ['res_id_master = ?', 'in_send_attach = ?'],
'data' => [$args['resId'], true] 'data' => [$args['resId'], true]
]); ]);
...@@ -200,6 +210,25 @@ trait ShippingTrait ...@@ -200,6 +210,25 @@ trait ShippingTrait
$errors[] = "Maileva submit failed for attachment {$attachmentId}"; $errors[] = "Maileva submit failed for attachment {$attachmentId}";
continue; 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)) { if (!empty($errors)) {
......
...@@ -12,10 +12,13 @@ ...@@ -12,10 +12,13 @@
namespace Shipping\controllers; namespace Shipping\controllers;
use Convert\controllers\ConvertPdfController;
use Docserver\models\DocserverModel;
use Entity\models\EntityModel; use Entity\models\EntityModel;
use Group\models\ServiceModel; use Group\models\ServiceModel;
use History\controllers\HistoryController; use History\controllers\HistoryController;
use Respect\Validation\Validator; use Respect\Validation\Validator;
use setasign\Fpdi\Tcpdf\Fpdi;
use Shipping\models\ShippingTemplateModel; use Shipping\models\ShippingTemplateModel;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
...@@ -281,4 +284,29 @@ class ShippingTemplateController ...@@ -281,4 +284,29 @@ class ShippingTemplateController
'entities' => $allEntities, '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;
}
} }
<?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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment