*   *   *   *   *   *   *   *   *BEZ HADA*  *   *   *   *   *   *   *   *   *   *   *   *
*
* importuje prijemky z lea primo do databaze leo_prijemky.dbf
*
*  title_id oznacuje cilovy sklad prijemky
*
*
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

#DEFINE CRLF        CHR(13)+CHR(10)
#DEFINE TITLE_SKLADU     "1"
**#DEFINE POCET_DNI_ZPATKY  9
**#DEFINE POCET_DNI_DOPREDU 1


#DEFINE POCET_DNI_ZPATKY  0
#DEFINE POCET_DNI_DOPREDU 1



o_set_point = SET("POINT")
ACTIVATE SCREEN 

*HTTP_READ_ONLY_MODE = .T.

IF otevri_dbf()

    DO ppgm_http_leo
    import_prijemek()

ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE import_prijemek
had_leo = CREATEOBJECT("had", "Leoexpress - prijem z vlaku")
had_leo.SHOW()
had_leo.set_max(POCET_DNI_ZPATKY)
FOR iiii = - (POCET_DNI_ZPATKY) TO POCET_DNI_DOPREDU
    datum_dokl = DATE() + m.iiii
    datum_txt = LEFT(TTOC(m.datum_dokl, 3), 10)
    had_leo.increment(m.datum_txt)
    json = http_get("catering/stock/documents?date=" + m.datum_txt + "&not_accepted=true")
**    json = http_get("catering/stock/documents?date=" + m.datum_txt + "&not_accepted=false")
    IF LEN(m.json) > 44
        zpracuj_prijemky()
    ENDIF 
ENDFOR 
RELEASE had_leo


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE zpracuj_prijemky

FOR ii = 1 TO 999
   id = INT(VAL(get_string_item(m.json, "id", ii)))
   IF ISNULL(m.id) .or. (m.id = 0)
      EXIT  
   ENDIF 
   SELECT leo_prijemky
   LOCATE FOR id = m.id
   IF .not. FOUND()
       parsuj_jednu_prijemku()
   ENDIF 
ENDFOR 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE parsuj_jednu_prijemku


** {"id":"214","title_id":"9","ride_id_in":null,"ride_id_out":null,"alto_document_id":"2800","accepted_by":null,"accepted_at":null,"created_by":"1158830","created_at":"2020-06-22 08:14:01","products":[]

title_id   =     get_string_item(m.json, "title_id"        , m.ii)
acceptedby =     get_string_item(m.json, "accepted_by"     , m.ii)
acceptedat =     get_string_item(m.json, "accepted_at"     , m.ii)
created_by =     get_string_item(m.json, "created_by"      , m.ii)
created_at =     get_string_item(m.json, "created_at"      , m.ii)
c_dokl     = VAL(get_string_item(m.json, "alto_document_id", m.ii))
rideid_in  = VAL(get_string_item(m.json, "ride_id_in"      , m.ii))
rideid_out = VAL(get_string_item(m.json, "ride_id_out"     , m.ii))

IF ISNULL(m.rideid_in) &&&&&.and. ISNULL(m.c_dokl)    && importuji se jen prijemky od lea, ne z alta

    c_partner =  - INT(VAL(m.title_id))
    SELECT rides
    LOCATE FOR id = m.rideid_out
    IF FOUND()
        line_name = rides.line_name
        ride_date = rides.date
    ELSE
        STORE "" TO line_name, ride_date
    ENDIF 
    pz2 = ALLTRIM(STR(m.id)) + "L"

    products = get_array_item (m.json, "products"  , m.ii)
    ACTIVATE SCREEN 
    ? m.title_id, m.rideid_out
    FOR poradi = 1 TO 999
        kod      = get_string_item(m.products, "product_id", m.poradi)
        mnozstvi = VAL(get_string_item(m.products, "count" , m.poradi))
        IF ISNULL(m.kod) .or. EMPTY(m.kod)
            EXIT  
        ENDIF 
        
        SELECT karty
        LOCATE FOR VAL(kod) = VAL(m.kod)
        IF FOUND()
           SCATTER FIELDS nazev, c_karty MEMVAR 
       ELSE
           nazev = "-- karta -- " + ALLTRIM(kod)
           c_karty = 0
       ENDIF         
       INSERT INTO leo_prijemky FROM MEMVAR 
    ENDFOR 

ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
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.
