*   *   *   *   *   *   *   *   *BEZ HADA*  *   *   *   *   *   *   *   *   *   *   *   *
*
* importuje prijemky z lea primo do doklady/obraty a pusti prepocet
*
*  title_id oznacuje cilovy sklad prijemky
*
* 2022-01-20
*
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

#DEFINE CRLF             CHR(13)+CHR(10)
#DEFINE TITLE_SKLADU     "1"
#DEFINE POCET_DNI_ZPATKY 17

o_set_point = SET("POINT")

*HTTP_READ_ONLY_MODE = .T.

IF otevri_dbf()

    SELECT disti datum_dokl, rideid_out, ride_date, line_name WHERE ISNULL(rideid_in) .and. (ISNULL(c_dokl) .or. EMPTY(c_dokl) );
           FROM  (m.gccen_path + "leo_prijemky") ORDER BY ride_date, line_name ;
           INTO CURSOR nabidka
    IF RECCOUNT() = 0
        WAIT "Nejsou žádné nepřijaké příjemky z vagonů" WINDOW TIMEOUT 2
    ELSE 
        DEFINE POPUP nabidka PROMPT FIELD ride_date + " | " + line_name FROM 1,22 FONT "Courier" TITLE "Vyber linku k příjmu"
        ON SELECTION POPUP nabidka deacti popup
        ACTIVATE SCREEN
        ACTIVATE POPUP nabidka
        IF .not. EMPTY(PROMPT())
            SCATTER MEMVAR 

            SELECT DISTI id, 00000000 as c_dokl WHERE datum_dokl = m.datum_dokl AND rideid_out = m.rideid_out AND ISNULL(c_dokl) ;
                   FROM leo_prijemky INTO CURSOR id_k_prijmu READWRITE 
                   
            IF n_slock_ex("BUF_OUT_" + m.gcpgm_id)
                SCAN 
                    DO zapis_prijemky_do_obratu WITH id_k_prijmu.id
                ENDSCAN 
                DO n_sunlock WITH ("BUF_OUT_" + m.gcpgm_id)
                
                DO ppgm_http_leo

                SELECT id_k_prijmu 
                SCAN
                    SCATTER MEMVAR 
                    SELECT leo_prijemky 
                    REPLACE FOR id = m.id c_dokl WITH m.c_dokl

                    leo_accept_prijemky (m.c_dokl)
                ENDSCAN 

                leo_tisk_rajdy (m.rideid_out)

            ELSE 
                WAIT "nemuzu zamknout semafor BUF_OUT_" WINDOW TIMEOUT 2
            ENDIF
        ENDIF 
        RELEASE POPUP nabidka
    ENDIF 

ENDIF 
DO clos_data


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE zapis_prijemky_do_obratu
PARAMETERS id 

WAIT STR(m.id) WINDOW NOWAIT 
    SELECT * where id = m.id FROM leo_prijemky INTO TABLE (m.gcpri_path + "leo_prijemka")
    SCATTER MEMVAR 

    pz1 = ALLTRIM(m.line_name) + " " + RIGHT(ride_date, 5)
    pz2 = ALLTRIM(STR(m.rideid_out))

    c_stredisk = m.gic_stredisk
    doklad   = i_max("doklad" + STR(m.gic_stredisk) + m.operace)
    c_dokl   = i_max("c_dokl")
    c_dokl_x = m.id
    provedl  = m.gcuser_id
    kdy      = DATETIME()

    operace  = "PE"
    c_mena   = 1
    platba   = "Leo"

    INSERT INTO (m.fn_doklady) FROM MEMVAR 
    SELECT obraty
    DO zamkni
    INSERT INTO obraty (c_stredisk,   operace,   c_dokl, c_karty, mnozstvi, cena, poradi ) ;
            SELECT  m.gic_stredisk, m.operace, m.c_dokl, c_karty, mnozstvi,    0, poradi FROM (m.gcpri_path + "leo_prijemka")
    UNLOCK

 * * * 
    SELECT c_karty, SUM(mnozstvi) AS mnozstvi, $0 AS zustatek, $0 AS cena_prum, 0 AS c_mena, m.gic_stredisk AS c_stredisk ;
           WHERE id = m.id FROM leo_prijemky GROUP BY c_karty INTO CURSOR prep READWRITE
    INDEX ON c_karty TAG c_karty
    INDEX ON STR(c_stredisk,12,0)+STR(c_karty,12,0) TAG csck
    DO prepocet2 WITH "prep", 0, m.c_dokl, .T., .T.
    DO pozavirej WITH "prep"

SELECT id_k_prijmu 
GATHER FIELDS c_dokl MEMVAR 
WAIT CLEAR 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE otevri_dbf
DO clos_data
chyba_pri_otevirani = .F.
TRY
    otevira_se = m.fn_karty
    USE (m.fn_karty)             ALIAS karty   IN 0 ORDER c_karty NOUPDATE
    otevira_se = m.fn_sklad
    USE (m.fn_sklad)             ALIAS sklad   IN 0
    otevira_se = m.fn_doklady
    USE (m.fn_doklady)           ALIAS doklady IN 0 ORDER c_dokl
    otevira_se = m.fn_obraty
    USE (m.fn_obraty)            ALIAS obraty  IN 0
    otevira_se = "rides"
    USE (m.gcdat_path + "rides") ALIAS rides   IN 0 NOUPDATE 
CATCH
    chyba_pri_otevirani = .T.
ENDTRY
IF chyba_pri_otevirani
    =MESSAGEBOX(jaz("prg_dbf_neprist_skuste_pozdeji")+CHR(13)+m.otevira_se,64,PROGRAM(),2000)
    RETURN .F.
ENDIF

IF FILE(m.gccen_path + "leo_prijemky.dbf")
   USE (m.gccen_path + "leo_prijemky") IN 0
ELSE
   CREATE TABLE (m.gccen_path + "leo_prijemky") free ( ;
          id I, title_id C(9), rideid_in I NULL, rideid_out I NULL, ride_date C(10), line_name C(22), c_dokl I NULL, created_by C(22) null, created_at C(22) null, acceptedby C(22) null, acceptedat C(22) null, ;
          mnozstvi Y, c_karty I, nazev C(30), c_partner I, datum_dokl D, poradi I)
   INDEX on id     TAG id
   INDEX on c_dokl TAG c_dokl
ENDIF 
RETURN .T.


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE zbytek
* takhle se se prijemka v leu oznaci jako prijata:
id_dokladu = "7"
c_dokl     = 321321
*json = http_request("catering/stock/documents/" + m.id_dokladu, "PATCH", '{ "accepted_at": "' + TTOC(DATETIME(), 3)+ '", "alto_document_id":"' + ALLTRIM(STR(m.c_dokl)) + '" }')
*json = http_request("catering/stock/documents/" + m.id_dokladu, "PATCH", '{ "accepted_at": null }')    && takhle se da akceptace zrusit


*[ "http_status":"200 OK", 
*{"id":"5","title_id":"2","ride_id_in":"519943","ride_id_out":null,"alto_document_id":"5132156","accepted_by":null,"accepted_at":null,"created_by":"1158830","created_at":"2020-01-23 11:32:15","products":[{"product_id":"178","count":"120","count_accepted":null},{"product_id":"239","count":"50","count_accepted":null}]},
*{"id":"11","title_id":"1","ride_id_in":"482077","ride_id_out":null,"alto_document_id":"1","accepted_by":null,"accepted_at":null,"created_by":"1","created_at":"2020-01-23 11:36:37","products":[{"product_id":"178","count":"12","count_accepted":null},{"product_id":"239","count":"5","count_accepted":null}]}]
