diff --git a/rest/index.php b/rest/index.php index 0a6933dda79339f1acb4412b0ee85e6015d48181..97528decc7fa1ae73efbe292f8d5fe1f17ab3405 100755 --- a/rest/index.php +++ b/rest/index.php @@ -363,6 +363,7 @@ $app->post('/resources/{resId}/linkedResources', \Resource\controllers\LinkContr $app->delete('/resources/{resId}/linkedResources/{id}', \Resource\controllers\LinkController::class . ':unlinkResources'); $app->put('/resources/{resId}/unsign', \SignatureBook\controllers\SignatureBookController::class . ':unsignResource'); $app->get('/resources/{resId}/acknowledgementReceipts', \AcknowledgementReceipt\controllers\AcknowledgementReceiptController::class . ':get'); +$app->get('/resources/{resId}/shippings', \Shipping\controllers\ShippingController::class . ':get'); $app->get('/res/{resId}/acknowledgementReceipt/{id}', \AcknowledgementReceipt\controllers\AcknowledgementReceiptController::class . ':getAcknowledgementReceipt'); $app->put('/res/resource/status', \Resource\controllers\ResController::class . ':updateStatus'); diff --git a/src/app/shipping/controllers/ShippingController.php b/src/app/shipping/controllers/ShippingController.php new file mode 100644 index 0000000000000000000000000000000000000000..a2113788c81db99f0d46c563a464cf9e459d8bd8 --- /dev/null +++ b/src/app/shipping/controllers/ShippingController.php @@ -0,0 +1,70 @@ +<?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 Controller +* @author dev@maarch.org +*/ + +namespace Shipping\controllers; + +use Attachment\models\AttachmentModel; +use Entity\models\EntityModel; +use Resource\controllers\ResController; +use Respect\Validation\Validator; +use Shipping\models\ShippingModel; +use Slim\Http\Request; +use Slim\Http\Response; +use User\models\UserModel; + +class ShippingController +{ + public static function get(Request $request, Response $response, array $args) + { + if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) { + return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']); + } + + $attachments = AttachmentModel::get([ + 'select' => ['res_id'], + 'where' => ['res_id_master = ?'], + 'data' => [$args['resId']] + ]); + + $attachments = array_column($attachments, 'res_id'); + + $shippingsModel = ShippingModel::get([ + 'select' => ['*'], + 'where' => ['(document_id = ? and document_type = ?) or (document_id in (?) and document_type = ?)'], + 'data' => [$args['resId'], 'resource', $attachments, 'attachment'] + ]); + + $shippings = []; + + 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']]); + + $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 + ]; + } + + return $response->withJson($shippings); + } +} diff --git a/src/app/shipping/models/ShippingModel.php b/src/app/shipping/models/ShippingModel.php index b0436705452a3bbd8d7386a980672126032a33ee..5feff4037b26ede263a7d3871e80e611c85dba92 100644 --- a/src/app/shipping/models/ShippingModel.php +++ b/src/app/shipping/models/ShippingModel.php @@ -41,4 +41,23 @@ class ShippingModel return true; } + + public static function get(array $args) + { + ValidatorModel::notEmpty($args, ['select']); + ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy']); + ValidatorModel::intType($args, ['limit']); + + $contacts = DatabaseModel::select([ + 'select' => $args['select'], + 'table' => ['shippings'], + 'where' => empty($args['where']) ? [] : $args['where'], + 'data' => empty($args['data']) ? [] : $args['data'], + 'order_by' => empty($args['orderBy']) ? [] : $args['orderBy'], + 'offset' => empty($args['offset']) ? 0 : $args['offset'], + 'limit' => empty($args['limit']) ? 0 : $args['limit'] + ]); + + return $contacts; + } }