From 306fadb26c84ff3cfdd2e0e0e780b0d645dba3d6 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Thu, 6 Feb 2020 10:27:46 +0100
Subject: [PATCH] FEAT #12072 TIME 0:45 get shippings by res id

---
 rest/index.php                                |  1 +
 .../controllers/ShippingController.php        | 70 +++++++++++++++++++
 src/app/shipping/models/ShippingModel.php     | 19 +++++
 3 files changed, 90 insertions(+)
 create mode 100644 src/app/shipping/controllers/ShippingController.php

diff --git a/rest/index.php b/rest/index.php
index 0a6933dda79..97528decc7f 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 00000000000..a2113788c81
--- /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 b0436705452..5feff4037b2 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;
+    }
 }
-- 
GitLab