 SET NAMES utf8;

DELIMITER $$

DROP PROCEDURE IF EXISTS web_pp2$$
CREATE DEFINER=`root`@`localhost` PROCEDURE web_pp2(
IN _MODE VARCHAR(200),
IN _SessionGUID VARCHAR(38),
IN _GUID VARCHAR(38), 
IN _ParamA VARCHAR(255), -- DATA, SEARCH, TABELA, DOC_GUID, ROK
IN _ParamB VARCHAR(255), -- 
IN _ParamC VARCHAR(255), -- 
IN _ParamD VARCHAR(255), -- 
IN _ParamE MEDIUMTEXT,
IN _ParamF MEDIUMTEXT    --  DEBUG, TEST
)
/*
CALL web_pp2('MODE',@sess,@guid,'a','b','c','d','e','f');
CALL web_pp2('GET_USER',@sess,'','','','','','','');
CALL web_pp2('GET_USER_LOG',@sess,@guid,'','','','','','');
CALL web_pp2('GET_SESS',NULL,'agnieszkar','','','','','','DEBUG');
TODO: uzależnić od zwrot danych z dokumentami od aktywności zakładek wg ust. w web_vars
*/

 
sproc: BEGIN

DECLARE _OSOBAGUID VARCHAR(38);
DECLARE _KONTRGUID VARCHAR(38);
DECLARE _DOCGUID VARCHAR(38);
DECLARE _LOGSTR TEXT;
DECLARE _USERGUID VARCHAR(38);
DECLARE _USERLOGIN VARCHAR(38);
DECLARE _USERTYPE INT;
DECLARE _ADMIN INT;
DECLARE _IDN INT;
DECLARE _SEARCHTEXT VARCHAR(200);
DECLARE _OPIS MEDIUMTEXT;
DECLARE _CHUNK VARCHAR(200);
DECLARE _DEBUG BOOLEAN;
DECLARE _TEST BOOLEAN;
DECLARE _ROK INTEGER;
DECLARE _DATA DATE;
DECLARE _DATAOD DATE;
DECLARE _DATADO DATE;
/*
set @webpp2app:='M.WEB.PortaFide';   
insert ignore into grupy_osob (osobaguid,grupaguid) select guid,@webpp2app from osoby where idnuser in ('apro');
*/

SELECT value INTO @session.globals.Owner FROM _db_globals g WHERE g.key='Owner';
SET _DEBUG=_ParamF LIKE "%DEBUG%" OR _MODE LIKE "%.DEBUG",@webpp2.mode=NULL,@session.status=0,_ADMIN=0,@webpp2app='M.WEB.PortaFide';
SET _TEST=_ParamF LIKE "%TEST%" OR _MODE LIKE "%.TEST";
SET @czerwony='#F4363C',@zolty='#f4eb42',@zielony='#38c930',@ciemnozielony='#278e22',@jasnozielony='#27c91e',
@bialy='#ffffff',@czarny='#000000',@niebieski='#0039cb',@szary='#808080',@pomaranczowy='#ff8c00',@fioletowy='#800080';
SET _MODE=REPLACE(_MODE,'.DEBUG',''),_ParamF=REPLACE(_ParamF,'DEBUG','');

SELECT log_user,log_USER_GUID, IFNULL(status,"")="ACTIVE" INTO _USERLOGIN,_USERGUID,@session.status
FROM _sys_db_logons WHERE guid=_SessionGUID;

SET @webpp2.userguid=_USERGUID;

SELECT u.user_type,u.admin,o.kontrguid,0 as poziom_upr,u.status<>0 as nieaktywny
INTO _USERTYPE,_ADMIN,_KONTRGUID,@webpp2.poziom_upr,@webpp2.nieaktywny
FROM _user_accounts u, osoby o 
WHERE o.guid=u.osobaguid and u.osobaguid=_USERGUID;

SET @webpp2.user_type=_USERTYPE;

-- SET _ADMIN=_ADMIN; -- TODO: uprawnienie do administracji serwisem

IF _ADMIN THEN
    SET @webpp2.poziom_upr=1;
--    SET _KONTRGUID=web_vars_get(@webpp2app,'VAR',_SessionGUID,'kontrguid');
END IF;

IF _USERTYPE=2 THEN
    SET @webpp2.poziom_upr=0;
END IF;

IF @webpp2.nieaktywny=1 THEN
    SET @session.status=0; -- TODO do przemyślenia
END IF;    

IF _MODE = 'GET_SESS' AND _DEBUG AND ISNULL(_SessionGUID) THEN
    SET @webpp2.mode=_MODE,@sess=NULL;
    SET _USERLOGIN=_GUID;
    SELECT guid,log_user_guid into @sess,@userguid from _sys_db_logons WHERE log_user=_USERLOGIN
    ORDER BY last_used_at desc limit 1;
    SET _SessionGUID=@sess;
    UPDATE _sys_db_logons set status='ACTIVE' where guid=_SessionGUID;
--    SELECT guid from v_wyjazdy where login=_GUID and data=current_date into @wyjazd;
-- TODO: - zastanowić się, czy wyjazdy będą definowane
    SELECT guid,log_user,@userguid FROM _sys_db_logons where guid=_SessionGUID;
    SET @session.status=1;
END IF;

IF _MODE = 'STATUS' THEN
    SET @webpp2.mode=_MODE;
    SELECT connection_id() as connection_id,@@wait_timeout,@session.status;
END IF;    


IF _MODE = 'GET_HOME' THEN 
    SET @webpp2.mode=_MODE;
    SELECT '<H1>Home page</H1>' as result;
END IF;

IF _MODE = 'GET_USER' THEN 
    SET @webpp2.mode=_MODE;
    SET @blokada_kontrah:=0;
    SET @blokada_tekst:='Blokada konta skontaktuj sie z działem kadr';
    SELECT o.guid,concat(o.imie1,' ',o.nazwisko) as imienazwisko,
    (SELECT group_concat(DISTINCT k.adres) FROM kontakty k WHERE k.kontrguid=_PLATNIKGUID and _PLATNIKGUID<>'' and k.typ_medium=3) as email,
    (SELECT group_concat(DISTINCT k.adres) FROM kontakty k WHERE k.kontrguid=_PLATNIKGUID and _PLATNIKGUID<>'' and k.typ_medium in (0,2)) as tel,
    o.user_type,
    IF(_USERLOGIN='jmam',concat_ws(' ',database(),_SessionGUID),'') as info,
    _ADMIN as administrator,u.ad_mail as usermail,
    _VarStr(IF(@blokada_kontrah,@blokada_tekst,@powitanie_dflt)) as popup, -- komunikat na starcie
    _VarInt(@blokada_kontrah) as blokada_kontrah,
    'pl' as lang

    FROM osoby o,_user_accounts u where o.guid=_USERGUID and u.osobaguid=o.guid;
END IF;

-- CALL web_pp2('GET_USER_LOG',@sess,'','','','','','','','','');
-- TODO: - przeniesc do osobnej procedury web_users

IF _MODE = 'GET_USER_LOG' THEN
    SET @webpp2.mode=_MODE;
--    SET _ODDATY=_VarDate(IF(_PARAM_A="","2021-11-01",_PARAM_A)),_DODATY=_VarDate(IF(_PARAM_B="","2100-12-31",_PARAM_B))+interval 1 day;
    SELECT l.czas,
    CONCAT(
    IF(_USERLOGIN='jmamx',CONCAT(l.ip,' ',l.czynnosc,' '),''),
    (select c.nazwa from log_czynnosci c WHERE c.idn=l.czynnosc),' '
    ,IF(l.czynnosc IN (5), _VarStr(left(if(l.compressed,uncompress(l.info),l.info),255))
    -- ,IF(l.czynnosc IN (2), CONCAT("Nowe zamówienie:",ifnull(z.nrzamw,'?'))
    ,'')
    ) as info
    FROM log l
--    LEFT JOIN zmng z ON z.guid=l.guidlink
    WHERE l.osobaguid=_GUID 
    -- and l.czas BETWEEN _ODDATY AND _DODATY
    -- and l.czynnosc=5 
    -- and ip like "%HOST:localhost%"
    HAVING info not like "%<?xml%"
    ORDER BY l.czas DESC LIMIT 15;
END IF;



IF _MODE IN ('GET_RODZAJE_DOC') THEN
    SET @webpp2.mode=_MODE;
    SELECT idn,opis as nazwa FROM rodzaje_doc 
    WHERE app="PL" 
    ORDER BY idn;
END IF;

/*
lista dokumentów dla pracownika
SET @guid='{A0408528-469C-4B57-B29D-96AECBE22258}';
stare: CALL web_pp2('GET_DOC_LIST',@sess,'','',@guid,'','','',''); 
nowe - user z sesji: 
CALL web_pp2('GET_DOC_LIST',@sess,'','','','','','',''); 
*/
IF _MODE='GET_DOC_LIST' THEN
    SET @webpp2.mode=_MODE;
    SET _OsobaGUID=IF(_GUID='',_UserGUID,_GUID);
    SET @webpp2.rodzaj=_VarInt(_ParamA);

/*    
    INSERT INTO log (useridn,osobaguid,ip,miejsce,czynnosc,info,guidlink,table_link) 
    SELECT DISTINCT _USERLOGIN,_USERGUID,@webpp2app, 2001, 2, 'Odczytanie dokumentu',e.klucz,e.tabela FROM dokumenty e WHERE length(e.dokument)>0 AND e.klucz=_GUID;
*/    
    SELECT GUID,rodzaj,nazwa,doc_opis,alocaltime, 
        (SELECT opis FROM rodzaje_doc WHERE idn=rodzaj) as rodzaj_nazwa
    FROM dokumenty WHERE klucz=_OsobaGUID AND (@webpp2.rodzaj=0 OR rodzaj=@webpp2.rodzaj);
END IF;

/*
guid konkretnego dokumentu
SET @docguid='{B621A6AE-DEC7-44DC-961F-E428932A0D8C}';
CALL web_pp2('GET_DOC',@sess,@docguid,'','','','','','DEBUG'); 
*/

IF _MODE IN ('GET_DOC') THEN
    SET @webpp2.mode=_MODE;
    SET _OsobaGUID=_UserGUID;
    SELECT d.GUID,REPLACE(to_base64(d.dokument),'\n','') as dokument,d.nazwa,d.doc_opis,d.user,d.typbloba,d.typ,
        IFNULL(b.mime,'') as mime,IFNULL(b.opis,'') as typbloba_str
    FROM dokumenty d
    LEFT JOIN typy_bloba b ON d.typbloba=b.idn
    WHERE d.guid=_GUID AND d.klucz=_OsobaGUID;
END IF;



IF IFNULL(@session.status,0)=0 AND NOT _DEBUG THEN
    SET @webpp2.mode=_MODE;
    SET _MODE='NOT.LOGGED';
ELSE
    UPDATE _sys_db_logons SET PING_AT=now(),LAST_USED_AT=now() WHERE guid=_SessionGUID;
END IF;

IF _DEBUG AND IFNULL(@session.status,0)=0 THEN
    SET @session.status=1,_USERLOGIN='jmam';SELECT guid,1 INTO _USERGUID,_ADMIN from osoby where idnuser=_USERLOGIN;
END IF;

/*
CALL web_pp2('BLOB_INIT',@sess,'','',@zmguid,@docguid,@ziguid,'','','','');
CALL web_pp2('BLOB_GET',@sess,'','',@zmguid,@docguid,'','','','','');
UPDATE e_dok SET dokument='YmFzZTY0dGV4dA==',nazwa='test.txt',doc_opis='opis pliku';
CALL web_pp2('BLOB_UPDATE',@sess,'','',@zmguid,@docguid,@ziguid,'','','','');
CALL web_pp2('BLOB_DONE',@sess,'','','','','','','','','');
select user,guid,idn,nazwa,length(dokument),tabela,klucz,alocaltime from dokumenty where guid not like '{%}' order by idn desc limit 8;select * from dokumenty_links;
*/

IF _MODE IN ('BLOB_INIT','BLOB_GET')  THEN
    SET @webpp2.mode=_MODE;
    SET _DocGUID=IF(_ParamA='',CONCAT('PP2',uuid()),_ParamA);
    
    DROP TEMPORARY TABLE IF EXISTS e_dok;
    CREATE TEMPORARY TABLE e_dok as (
      SELECT GUID,replace(to_base64(dokument),'\n','') as dokument,nazwa,doc_opis,user,typbloba,klucz FROM dokumenty WHERE guid=_DocGUID
    );

    IF (SELECT COUNT(*) FROM e_dok)=0 AND _MODE='BLOB_INIT' THEN
        INSERT INTO e_dok (guid,klucz) SELECT _DocGUID,_GUID;
    END IF;
    IF _MODE='BLOB_GET' THEN
        SELECT * from e_dok;
        DROP TEMPORARY TABLE IF EXISTS e_dok;
    ELSE
        SELECT "OK" as db_status;
    END IF;
END IF;

IF _MODE = 'BLOB_LIST' THEN
    SET @webpp2.mode=_MODE;
    SELECT guid, nazwa, ifnull(doc_opis,''), alocaltime FROM dokumenty WHERE klucz=_GUID;
END IF;

IF _MODE = 'BLOB_UPDATE' THEN
-- tu przekazujemy guid dokumentu
    SET @webpp2.mode=_MODE,_DocGUID=IF(_ParamA='',(select guid from e_dok limit 1),_ParamA);
    SET @webpp2.db_status='OK';
    SELECT count(*) INTO @isuser FROM _user_accounts WHERE osobaguid=_GUID;

    IF NOT (@iszmng OR @isuser) THEN
        SET @webpp2.db_status='ERR: GUID jest niepoprawny';
        SELECT @webpp2.db_status as db_status;
        LEAVE sproc;
    END IF;
-- TODO: do jakiej tabeli załącznik
    SET @tabela=IF(@isuser,'osoby','osoby');
    
    INSERT IGNORE INTO dokumenty (guid,doc_status) SELECT guid,0 FROM e_dok;
    UPDATE e_dok e, dokumenty d
    SET
      d.nazwa=replace(e.nazwa,' ','_'),d.doc_opis=e.doc_opis,
      d.dokument=from_base64(e.dokument),
      d.rodzaj=1,d.typbloba=e.typbloba,d.link=1,d.typ=0,d.doc_status=0,
      d.url=replace(e.nazwa,' ','_'),
      d.tabela=@tabela,d.poleklucz='guid',
      d.klucz=_GUID,
      d.user=_USERLOGIN
    WHERE d.guid=e.guid /* and d.doc_status<0 */ and e.guid=_DocGUID; -- why?
    UPDATE dokumenty d,typy_bloba b SET d.url=CONCAT(replace(d.url,b.ext,''),b.ext),d.nazwa=d.url 
    WHERE d.guid=_DocGUID and d.typbloba=b.idn and b.ext<>'';
/*    IF _ZIGUID<>'' THEN
        SELECT guid_instancji FROM zmpo_instancje WHERE guid=_ZIGUID INTO _IGUID;
        INSERT INTO dokumenty_links (guid,objguid,objtable) SELECT _DocGUID,_ZIGUID,'zmpo_instancje';
    END IF;
*/
    INSERT INTO log (useridn,osobaguid,ip,miejsce,czynnosc,info,guidlink,table_link) 
    SELECT e.user,_USERGUID,@webpp2app, 2001, 2, 'Dodanie dokumentu z aplikacji PP2',e.klucz,@tabela FROM e_dok e;
    SELECT "OK" as db_status;
END IF;

IF _MODE IN ('BLOB_DONE') THEN
    SET @webpp2.mode=_MODE;
    SET @webpp2.db_status=IFNULL(@webpp2.db_status,'OK');

    DROP TEMPORARY TABLE IF EXISTS e_dok;
    SELECT @webpp2.db_status as db_status;
END IF;

/*
CALL web_pp2('CHECK_PITY',@sess,@userguid,'','','','','','');
CALL web_pp2('GET_PIT_LIST',@sess,'','','','','','','');
*/

IF _MODE IN ('CHECK_PITY','GET_PIT_LIST') THEN
    SET @webpp2.mode=_MODE;
    SET _OsobaGUID=IF(_GUID='',_UserGUID,_GUID);
    SELECT po.idn, ng.Src_OsobaGUID, ng.Src_TableName, ng.Src_GUID, po.FileName,  po.Size, po.Status, ng.Src_Date, 
        ng.Src_No as Rok, ng.Descr, _VarStr(IFNULL(l.log_time,'nie pobrano')) AS Download_Date 
    FROM e_doc_ng ng 
    INNER JOIN e_doc_po po ON ng.idn = po.idnNg AND po.mode ='FILE'
    LEFT JOIN e_doc_log l ON l.idnNg = po.idn AND l.Process = 'DOWNLOAD_DOCS'  
    WHERE ng.Src_OsobaGUID = _OSOBAGUID AND ng.Src_TableName LIKE '%pit%' AND (ng.Status = 'UPO' OR  ng.Status = 'PDF') ;
END IF;


/*
lista odcinków
SET @guid='{3B967A14-4BFB-42DD-A9F0-45846AB5E8FB}';

{3B967A14-4BFB-42DD-A9F0-45846AB5E8FB} - agnieszkar
{98B29CFE-494B-4937-8097-300ED8BD276F} - annaw
IM_f7ae1a85e8b3814ddd4ef7b1bb840da3 - emilias
CALL web_pp2('GET_SESS',NULL,'agnieszkar','','','','','','DEBUG');
CALL web_pp2('GET_ODC_PLAC_LIST',@sess,'','','','','','',''); 
CALL web_pp2('GET_ODC_PLAC_LIST',@sess,@guid,'','','','','','DEBUG'); 
CALL web_pp2('GET_BADANIA_LIST',@sess,@guid,'','','','','','DEBUG'); 

*/

IF _MODE IN ('GET_ODC_PLAC_LIST','GET_BADANIA_LIST') THEN
    SET @webpp2.mode=_MODE;
    IF _DEBUG THEN 
        SELECT _USERGUID,_USERLOGIN,_SessionGUID,_GUID; 
    ELSE 
        SET _GUID='';
    END IF;
    SET _OsobaGUID=IF(_GUID='',_UserGUID,_GUID);
    SET @src_table=IF(_MODE='GET_ODC_PLAC_LIST','place','osoby');
    
    SELECT po.idn, ng.Src_OsobaGUID, ng.Src_TableName, po.FileName, po.Size, po.Status, ng.Src_Date, 
        IF(_MODE='GET_ODC_PLAC_LIST',
            (SELECT GROUP_CONCAT('Rok: ', p.Rok, ', Miesiąc: ', p.miesiac, ',Symbol listy: ', ifnull(lp.Symbol,'')) FROM place p, listy_plac lp WHERE p.guid=ng.Src_GUID and lp.idn=p.nrlisty),
            CONCAT('Badanie lekarskie, ', po.Descr)
        ) AS Descr, 
        (SELECT GROUP_CONCAT(IFNULL(l.log_time,'')) FROM e_doc_log l WHERE l.idnNg = po.idn AND l.Process = 'DOWNLOAD_DOCS') AS Download_Date 
    FROM e_doc_ng ng
    LEFT JOIN e_doc_po po ON ng.idn = po.idnNg
    WHERE ng.Src_TableName=@src_table AND ng.Status='PDF' AND ng.Src_OsobaGUID=_OsobaGUID;
END IF;

/*
stary mod:
CALL web_pp2('GET_PIT',@sess,@userguid,@idn,'','','','','');
wariant nowy bez guida osoby poniżej:
CALL web_pp2('GET_PITv2',@sess,@idn,'','','','','','');
*/

IF _MODE IN ('GET_PIT') THEN -- Stary mod zachowany dla kompatybilności
    SET @webpp2.mode=_MODE;
    SET _OsobaGUID=IF(_GUID='',_UserGUID,_GUID),_IDN=_VarINT(_ParamA);
    SELECT REPLACE(to_base64(p.Content),'\n','') as dokument, p.Status, p.Src_GUID 
    FROM  e_doc_po p 
    INNER JOIN Pit_11_8B n ON n.guid = p.Src_GUID 
    WHERE p.idn = _IDN and n.pracGUID = _OsobaGUID;
END IF;


/*
CALL web_pp2('GET_SESS',NULL,'agnieszkar','','','','','','DEBUG');
CALL web_pp2('GET_BADANIE',@sess,@idn,'','','','','','DEBUG'); 
CALL web_pp2('GET_ODC_PLAC',@sess,@idn,'','','','','','DEBUG'); 
CALL web_pp2('GET_PITv2',@sess,@idn,'','','','','','DEBUG'); 

*/

IF _MODE IN ('GET_BADANIE','GET_ODC_PLAC','GET_PITv2') THEN
    SET @webpp2.mode=_MODE,_IDN = _VarInt(_GUID);
    IF _IDN=0 THEN
        SELECT 'Pusty IDN' as status;
        LEAVE sproc;
    END IF;

    IF _DEBUG THEN 
        SELECT _USERGUID,_USERLOGIN,_SessionGUID,_IDN as e_doc_po_idn; 
    END IF;
    SET _OsobaGUID=_UserGUID;

    IF _MODE = 'GET_PITv2' THEN
        SELECT REPLACE(to_base64(p.Content),'\n','') as dokument, p.Status, p.Src_GUID, 
            (SELECT u.login FROM _user_accounts u WHERE u.osobaGUID = _OsobaGUID) as login
        FROM  e_doc_po p
        INNER JOIN Pit_11_8B n ON n.guid = p.Src_GUID and n.pracGUID = _OsobaGUID 
        -- TODO: sprawdzić czy to jest potrzebne? czy przypadkiem nie powinno być jak niżej?
        WHERE p.idn = _IDN;

        UPDATE Pit_11_8B n, e_doc_po p SET PF_pobrany=NOW() WHERE n.guid = p.Src_GUID and p.idn = _IDN AND p.status<>1;
    END IF;

    IF _MODE IN ('GET_BADANIE','GET_ODC_PLAC') THEN
        SET @src_table=IF(_MODE='GET_ODC_PLAC','place','osoby');
        SELECT REPLACE(to_base64(p.Content),'\n','') as dokument, p.Status, p.Src_GUID, 
            (SELECT u.login FROM _user_accounts u WHERE u.osobaGUID = _OsobaGUID) as login
        FROM  e_doc_po p
        INNER JOIN e_doc_ng ng ON ng.idn = p.idnNg AND ng.Src_OsobaGUID = _OsobaGUID AND ng.Src_TableName=@src_table 
        -- TODO: sprawdzić czy dla odcinkow płacowych osobaguid jest ustawiana
        WHERE p.idn = _IDN ;
        -- and EXISTS (SELECT 1 FROM place n WHERE n.guid=p.Src_GUID and n.pracGUID = _OsobaGUID);

    END IF;
    INSERT INTO e_doc_log (idnNg, UserGUID, Process, Status_After) VALUES (_IDN, _UserGUID, 'DOWNLOAD_DOCS', 'DOWNLOADED');

END IF;


/*
$sql['urlop_wyp_hist'] = "SELECT a.AbsencjaOd AS od, a.Absencjado AS do, ad.definicja,". 
$sql['urlop_op_hist'] = "SELECT a.AbsencjaOd AS od, a.Absencjado AS do, ad.definicja, ".
$sql['urlop_nz_hist'] = "SELECT a.AbsencjaOd AS od, a.Absencjado AS do, ad.definicja, ".
$sql['wnioski_urlop'] = "SELECT * FROM `absencje_dict` d WHERE symbol IN ('W', 'D', 'G');";

CALL web_pp2('GET_SESS',NULL,'ppelcner','','','','','','DEBUG');
CALL web_pp2('URLOPY_INIT',@sess,@userguid,'2024','','','','','');
CALL web_pp2('URLOPY_SEKCJE',@sess,'','','','','','','');
CALL web_pp2('URLOPY_HIST',@sess,'','','','','','','');
CALL web_pp2('URLOPY_INFO',@sess,'','','','','','','');
*/


IF _MODE IN ('URLOPY_INIT') THEN
    SET @webpp2.mode=_MODE;
    SET _OsobaGUID=IF(_GUID='',_UserGUID,_GUID),_ROK=_VarINT(_ParamA);
    CALL crm_porta_fide_tools('DASHBOARD.Create.For.User.Rok', '', _OsobaGUID, _Rok, '', '', '', '', '');
END IF;

IF _MODE IN ('URLOPY_HIST') THEN
    SET @webpp2.mode=_MODE;
    CALL crm_porta_fide_tools('DASHBOARD.Sekcje.Select.Absencje', '', '', '', '', '', '', '', '');
END IF;

IF _MODE IN ('URLOPY_SEKCJE') THEN
    SET @webpp2.mode=_MODE;
    CALL crm_porta_fide_tools('DASHBOARD.Sekcje.Select.Sekcje', '', '', '', '', '', '', '', '');
END IF;

IF _MODE IN ('URLOPY_INFO') THEN
    SET @webpp2.mode=_MODE;
    CALL crm_porta_fide_tools('DASHBOARD.Sekcje.Select.Inf', '', '', '', '', '', '', '', '');
END IF;


IF _MODE IN ('URLOPY_DONE') THEN
    SET @webpp2.mode=_MODE;
    -- TODO: CALL crm_porta_fide_tools('DASHBOARD.Done', '', _OsobaGUID, _Rok, '', '', '', '', '');
END IF;

/*
CALL web_pp2('GET_SESS',NULL,'ppelcner','','','','','','DEBUG');
CALL web_pp2('WNIOSKI',@sess,@userguid,'','','','','','');
CALL web_pp2('WNIOSKI_ARCH',@sess,@userguid,'','','','','','');
*/

IF _MODE IN ('WNIOSKI','WNIOSKI_ARCH') THEN
    SET @webpp2.mode=_MODE;
    SET _OsobaGUID=IF(_GUID='',_UserGUID,_GUID),_ROK=_VarINT(_ParamA);
    SET @arch=IF(_MODE='WNIOSKI_ARCH','1','');
    CALL crm_porta_fide_tools('Select.wnioski.moje', '', _OsobaGUID, @arch, @arch, '', '', '', '');
END IF;

/*
CALL web_pp2('GET_SESS',NULL,'ppelcner','','','','','','DEBUG');
SET @guid='guidwniosku';
CALL web_pp2('WNIOSKI_ARCH',@sess,@guid,'','','','','','');
*/

IF _MODE IN ('WNIOSKI_HIST') THEN
    SET @webpp2.mode=_MODE;

--    CALL crm_porta_fide_tools('select.hist', _GUID, '', '', '', '', '', '', '');

    SELECT count(*) INTO @madostep FROM v_crm_workflow 
    WHERE commisionguid = _GUID AND 
    (Owner_GUID = _USERGUID OR IdnUser=_USERGUID OR Decision_IdnUser=_USERGUID OR Creator_GUID=_USERGUID);
    SET @madostep=1; -- TODO: do ustalenia z AS
    IF @madostep>0 THEN 
        CALL crm_porta_fide_tools('select.hist', _GUID, '', '', '', '', '', '', '');
    ELSE
        SELECT CONCAT(@webpp2.mode, ' sesja niezgodna z GUIDem użytkownika') as db_info;
    END IF;
    
END IF;


IF _MODE='NOT.LOGGED'  THEN
    SELECT CONCAT(@webpp2.mode,' nie zalogowany lub sesja niezgodna') as db_info;
END IF;

IF @webpp2.mode IS NULL THEN
    SELECT CONCAT(_MODE,' nie zdefiniowany') as db_info;
END IF;

END $$

DELIMITER ;

SHOW WARNINGS;

/*
$Log: web_pp2.sql,v $
Revision 1.13  2026/05/28 09:21:03  apro
sev - dokumenty

Revision 1.12  2026/04/16 10:58:37  apro
GET_DOC_LIST, GET_DOC

Revision 1.11  2026/04/15 12:38:06  apro
koment

Revision 1.10  2026/04/15 12:37:29  apro
SEV: dokumenty pracownicze

Revision 1.9  2025/03/18 12:52:47  apro
$SEND$

Revision 1.8  2025/03/18 09:19:28  waliant
$SEND$ Warningi

Revision 1.7  2025/03/17 13:27:38  apro
$SEND$

Revision 1.6  2025/03/17 13:26:08  apro
$SENS$
warningi

Revision 1.5  2024/05/17 09:44:36  robert
Kontrola zgodnosci uzytkownika w sesji, mod 'WNIOSKI_HIST'

Revision 1.4  2024/05/17 05:55:06  robert
Kontrola zgodnosci uzytkownika w sesji, mod 'WNIOSKI_HIST'

Revision 1.3  2024/05/17 05:34:14  robert
Kontrola zgodnosci uzytkownika w sesji, mod 'WNIOSKI_HIST'

Revision 1.2  2024/05/16 12:16:05  apro
wnioski

Revision 1.1  2024/04/30 10:06:40  apro
v 0.1

*/
