Sign
=============

Набор модальних окон по работе с документами на таможню

Installation
-----------

PackageModal - подписание пакета (файла/файлов) с помощью ЭЦП в модальном окне и отправка на сервер
```js
import { PackageModal } from "@mdoffice/md-component/Sign";
```

EndpointModal - модальное окно выбора Конечной точки
```js
import { EndpointModal } from "@mdoffice/md-component/Sign";
```

MsgModal - модальное окно просмотра imfx
```js
import { MsgModal } from "@mdoffice/md-component/Sign";
```

Usage
-----

```js
const packageModal = PackageModal.create({
    urlInit: `/ua/...document.initUpload?p_id=${id}`,
    urlSave: `/ua/...document.signUpload?p_id=${id}`,
    modalTitle: 'Підписання документу',
    onSuccess: (packageId) => {
        packageModal?.destroy();
        packageModal = null;

        EndpointModal.create({
            urlInit: `/ua/...document.initEndpoint`,
            urlEndpointList: `/ua/...document.findEndpoint`,
            urlSave: `/ua/...document.sendEndpoint?p_id=${id}&p_package_id=${packageId}`,
            onSuccess: () => {
                Notify.show('Пакет відправлено!', NotifyType.success);
            }
        }).open();
    }
});
```

```js
const msgModal = MsgModal.create({
    url: `/ua/...document.dmsuMsg?p_id=${docId}&p_msg=${msgId}`,
    onSign: () => {
        const packageModal = PackageModal.create({
            urlInit: `/ua/...document.initDownload?p_id=${docId}&p_msg=${msgId}`,
            urlSave: `/ua/...document.signDownload?p_id=${docId}&p_msg=${msgId}`,
            modalTitle: 'Запит на завантаження файлу повідомленя',
            onSuccess: (_packageId, data) => {
                packageModal.destroy();
                if (data?.msg) {
                    Modal.open("Повідомлення").setBody(data.msg).setFooter();
                } else {
                    msgModal?.open();
                }
            }
        });
        packageModal.open();
    }
});

msgModal.open();
```

Example
-----

```sql
PROCEDURE initUpload(p_id        IN NUMBER,
                       p_signature IN VARCHAR2 DEFAULT NULL) IS
    l_user_id NUMBER := servicecore.auth_service.get_id;
  
    l_sign_json   CLOB;
    l_sign_name   VARCHAR2(300);
    l_sign_edrpou VARCHAR2(30);
    l_sign_drfo   VARCHAR2(30);
    l_sign_error  VARCHAR2(3000);
  
    l_file_doc1    servicecore.imfx_file.t_file;
    l_file_doclist servicecore.imfx_file.t_file;
    l_package_id   NUMBER;
  
    l_result_file servicecore.soap_request.t_package_list;
  BEGIN
    IF NOT servicecore.auth_service.is_user(l_user_id) THEN
      servicecore.soap_request.json_error(p_text => 'Не авторизований');
      RETURN;
    ELSIF p_id IS NULL THEN
      servicecore.soap_request.json_error(p_text => 'Документ не знайдено');
      RETURN;
    ELSIF NOT document_model.is_owner(p_id => p_id, p_user_id => l_user_id) THEN
      servicecore.soap_request.json_error(p_text => 'Ви не маєте доступу до чужого документу');
      RETURN;
    END IF;
  
    IF p_signature IS NULL THEN
      servicecore.soap_request.json_error(p_text => 'Відсутній підпис для зчитування');
      RETURN;
    END IF;
    admin.user_eusign.parse_sign(p_signature  => p_signature,
                                 p_json       => l_sign_json,
                                 p_FullName   => l_sign_name,
                                 p_EDRPOUCode => l_sign_edrpou,
                                 p_DRFOCode   => l_sign_drfo,
                                 p_error      => l_sign_error);
  
    IF l_sign_error IS NOT NULL THEN
      servicecore.soap_request.json_error(p_text => l_sign_error);
      RETURN;
    ELSIF l_sign_name IS NULL THEN
      servicecore.soap_request.json_error(p_text => 'Помилка при зчитуванні підпису');
      RETURN;
    END IF;
  
    --make
    document_soap_file.makeDoc1XML(p_doc_id => p_id,
                                   p_file => l_file_doc1);
    document_soap_file.makeDocListXML(p_SenderCode => nvl(l_sign_drfo, l_sign_edrpou),
                                      p_SenderName => l_sign_name,
                                      p_doc_id     => p_id,
                                      p_file_doc1  => l_file_doc1,
                                      p_file       => l_file_doclist,
                                      p_with_sign  => TRUE);
  
    --save  
    l_package_id := servicecore.soap_UploadImfx1.init(p_service_code => ui_local.SERVICE_CODE,
                                                      p_doc_id       => p_id,
                                                      p_file_doc1    => l_file_doc1,
                                                      p_file_doclist => l_file_doclist,
                                                      p_calling_user => l_user_id);
  
    ----------------------------------
    servicecore.soap_request.add_package_item(p_file_list => l_result_file,
                                              p_field     => 'p_doc1',
                                              p_content   => l_file_doc1.content);
    servicecore.soap_request.add_package_item(p_file_list => l_result_file,
                                              p_field     => 'p_doclist',
                                              p_content   => l_file_doclist.content);
    servicecore.soap_request.json_package_init(p_package_id => to_char(l_package_id), p_file_list => l_result_file);
  EXCEPTION
    WHEN OTHERS THEN
      admin.logs.err(i_with_info => TRUE);
      servicecore.soap_request.json_error(p_text => 'Помилка при ініціалізації');
  END;
```

```sql
PROCEDURE signUpload(p_id         IN NUMBER,
                       p_package_id IN VARCHAR2 DEFAULT NULL,
                       p_doc1       IN VARCHAR2,
                       p_doclist    IN VARCHAR2) IS
    l_user_id NUMBER := servicecore.auth_service.get_id;
  
    l_file servicecore.soap_UploadImfx1.t_signature;
  BEGIN
    IF NOT servicecore.auth_service.is_user(l_user_id) THEN
      util.redirect_to_home;
      RETURN;
    ELSIF p_id IS NULL THEN
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! Документ не знайдено"}');
      RETURN;
    ELSIF NOT document_model.is_owner(p_id => p_id, p_user_id => l_user_id) THEN
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! Ви не маєте доступу до чужого документу"}');
      RETURN;
    ELSIF NOT servicecore.soap_UploadImfx1.exists_package(p_service_code => ui_local.SERVICE_CODE,
                                                          p_doc_id       => p_id,
                                                          p_package_id   => p_package_id) THEN
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! Пакет не знайденно"}');
      RETURN;
    END IF;
  
    l_file.doc1    := p_doc1;
    l_file.doclist := p_doclist;
  
    servicecore.soap_UploadImfx1.save_signature(p_package_id   => p_package_id,
                                                p_file         => l_file,
                                                p_calling_user => l_user_id);
    ----------------------------------
    servicecore.util.mime_header_json;
    htp.prn('{"ok": true}');
  EXCEPTION
    WHEN OTHERS THEN
      admin.logs.err(i_with_info => TRUE);
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка при збереженні підписів"}');
  END;
```

```sql
PROCEDURE initEndpoint IS
    l_user_id NUMBER := servicecore.auth_service.get_id;
  
    l_user_fio   VARCHAR2(300);
    l_user_email VARCHAR2(300);
  BEGIN
    IF NOT servicecore.auth_service.is_user(l_user_id) THEN
      util.redirect_to_home;
      RETURN;
    END IF;
  
    l_user_fio   := servicecore.users_service.get_name(p_user_id => l_user_id);
    l_user_email := servicecore.users_service.get_email(p_user_id => l_user_id);
    
    servicecore.soap_request.json_endpoint_init(p_user_fio => l_user_fio, p_user_email => l_user_email);
  END;
```

```sql
PROCEDURE findEndpoint(p_search IN VARCHAR2 DEFAULT NULL) IS
    l_user_id NUMBER := servicecore.auth_service.get_id;
  BEGIN
    IF NOT servicecore.auth_service.is_user(l_user_id) THEN
      util.redirect_to_home;
      RETURN;
    END IF;
  
    servicecore.soap_request.json_endpoint_find(p_search => p_search);
  END;
```

```sql
PROCEDURE sendEndpoint(p_id           IN VARCHAR2 DEFAULT NULL,
                         p_package_id   IN VARCHAR2 DEFAULT NULL,
                         p_sender_email IN VARCHAR2 DEFAULT NULL,
                         p_sender_name  IN VARCHAR2 DEFAULT NULL,
                         p_recipient    IN VARCHAR2 DEFAULT NULL) IS
    l_user_id NUMBER := servicecore.auth_service.get_id;
  
    l_error_list owa_util.vc_arr;
  
    l_endpoint   servicecore.soap_UploadImfx1.t_endpoint;
    l_send_error VARCHAR2(32767);
  BEGIN
    IF NOT servicecore.auth_service.is_user(l_user_id) THEN
      util.redirect_to_home;
      RETURN;
    END IF;
  
    IF p_id IS NULL THEN
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! Документ не знайдено"}');
      RETURN;
    ELSIF NOT document_model.is_owner(p_id => p_id, p_user_id => l_user_id) THEN
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! Ви не маєте доступу до чужого документу"}');
      RETURN;
    END IF;
    document_model.check_fullness(p_id => p_id, p_error_list => l_error_list);
    IF l_error_list.count > 0 THEN
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! В документі наявні помилки"}');
      RETURN;
    END IF;
  
    IF NOT servicecore.soap_UploadImfx1.exists_package(p_service_code => ui_local.SERVICE_CODE,
                                                       p_doc_id       => p_id,
                                                       p_package_id   => p_package_id) THEN
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! Пакет не знайденно"}');
      RETURN;
    END IF;
  
    l_endpoint.sender_email := servicecore.util.htmlspecialchars(servicecore.util.wordEncode(p_sender_email));
    l_endpoint.sender_name  := servicecore.util.htmlspecialchars(servicecore.util.wordEncode(p_sender_name));
    l_endpoint.recipient    := servicecore.util.htmlspecialchars(servicecore.util.wordEncode(p_recipient));
  
    servicecore.soap_UploadImfx1.send(p_service_code => ui_local.SERVICE_CODE,
                                      p_doc_id       => p_id,
                                      p_package_id   => p_package_id,
                                      p_endpoint     => l_endpoint,
                                      p_error        => l_send_error,
                                      p_calling_user => l_user_id);
  
    IF l_send_error IS NULL THEN
      document_model.action_soap(p_id           => p_id,
                                 p_uid          => servicecore.soap_UploadImfx1.get_guid(p_package_id => p_package_id),
                                 p_calling_user => l_user_id);
    
      servicecore.document_action.save_item(p_action_code  => servicecore.document_action.ACTION_CUST_SEND,
                                            p_service_code => ui_local.SERVICE_CODE,
                                            p_item_id      => p_id,
                                            p_calling_user => l_user_id);
    END IF;
    ----------------------------------
    servicecore.util.mime_header_json;
    IF l_send_error IS NOT NULL THEN
      htp.prn('{"ok": false, "msg": "' || servicecore.util.jsonSpecialChars(l_send_error) || '"}');
    ELSE
      htp.prn('{"ok": true}');
    END IF;
  EXCEPTION
    WHEN OTHERS THEN
      admin.logs.err(i_with_info => TRUE);
      servicecore.util.mime_header_json;
      htp.prn('{"ok": false, "msg": "Помилка! При збереженні підпису!"}');
  END;
```
