*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
*
*  PROCEDURE spracuj_xmlmakro - zpracovava XML EDI od makra pro budejovicky budvar
*
*
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    

PARAMETERS xml, edipor, orimeno, edimeno, kodedistred
PRIVATE hladajidedi, provozovna
LOCAL pocetfaktur, pocetdodakov, faktura1, dodavatel, polozky, polozka, pocetpoloziek, sumadokladu, faktury, datumdokladu

#DEFINE CRLF CHR(13)+CHR(10)

shipmentheader = vratsekciu("ShipmentHeader", m.xml)

doc_date  = vratsekciu("ScheduledDeliveryDateTime" , m.shipmentheader)     && 2020-08-31
IF EMPTY(m.doc_date)
    doc_date  = vratsekciu("DocumentDateTime"      , m.shipmentheader)     && 2020-08-31
ENDIF 
provozovna = vratsekciu("ID", vratsekciu("ShipToParty"          , m.shipmentheader))
dodavatel  = vratsekciu("ID", vratsekciu("ShipFromParty"        , m.shipmentheader))

doc_id  = vratsekciu("Id"    , vratsekciu("AlternateDocumentId", m.shipmentheader))
IF EMPTY(m.doc_id)
    doc_id  = vratsekciu("Id", vratsekciu("DocumentId"         , m.shipmentheader))
ENDIF 

DO CASE 
CASE ATC("<ShipmentItem", m.xml) = 0
    chybatxt = m.chybatxt + CRLF + "Subor neobsahuje zadne polozky"

CASE EMPTY(m.doc_id) .or. EMPTY(m.doc_date) .or. EMPTY(m.dodavatel) .or. EMPTY(provozovna)
    chybatxt = m.chybatxt + CRLF + "Subor neobsahuje hlavicku"

OTHERWISE 

    hladajidedi=i_max("id_edi") 
    otevri_edi_hlavpol()
    insert_edihlav()

    SELECT edihlav
    REPLACE c_faktury WITH m.doc_id
    REPLACE dat_vyst  WITH DATE( VAL(SUBSTR(m.doc_date,1,4)), VAL(SUBSTR(m.doc_date,6,2)), VAL(SUBSTR(m.doc_date,9,2)))
    REPLACE partner   WITH "Makro " + m.dodavatel

    sumadokladu=0
    FOR m.ii=1 TO 999
        polozka=vratsekciu("ShipmentItem", m.xml, m.ii)
        IF EMPTY(m.polozka)
            EXIT 
        ENDIF 
        ean      = vratsekciu("ID", vratsekciu("ItemID"            , m.polozka))
        man      = vratsekciu("ID", vratsekciu("SupplierItemID"    , m.polozka))
        nazov    =                  vratsekciu("Description"       , m.polozka,1,"C", m.kodovastranka)
        mnozstvo =                  vratsekciu("ShippedQuantity"   , m.polozka,1,"N")

        pocet_v_bal =               vratsekciu("PerPackageQuantity", m.polozka,1,"N")                     &&& nepouzije se
        pocet_v_bal = IIF(EMPTY(m.pocet_v_bal), 1, m.pocet_v_bal)

        dan      =                  vratsekciu("VATRate"           , m.polozka,1,"N")   &&& / 100 + 1

        pozice = ATC("<UnitSalePriceAmount", m.polozka)
        kus_s_cenou = SUBSTR(m.polozka, m.pozice + 11)
        kus_s_cenou = SUBSTR(m.kus_s_cenou, ATC(">", m.kus_s_cenou)+1)
        cena        = VAL(STRTRAN(m.kus_s_cenou, ".", ","))
        cena_celk   = m.cena * m.mnozstvo
        nazov       = CHRTRAN(m.nazov, "?@#", "")

        INSERT INTO edipol (id_edi,  c_data, riadok,   ean, kod_zak,   mnozstvo,   cena,    cena_celk,   dan,   nazov) ;
             VALUES (m.hladajidedi, m.cdata, m.ii+2000, m.ean, m.man  , m.mnozstvo, m.cena,  m.cena_celk, m.dan, m.nazov)
        sumadokladu = m.sumadokladu + m.cena
    ENDFOR
    RELEASE m.polozka, m.polozky 

    SELECT edipol
*    REPLACE cena WITH cena_celk/mnozstvo FOR mnozstvo<>0
    SUM cena_celk TO prachy_plus  FOR mnozstvo > 0 AND id_edi = m.hladajidedi
    SUM cena_celk TO prachy_minus FOR mnozstvo < 0 AND id_edi = m.hladajidedi

    COUNT TO plusy  FOR mnozstvo > 0 AND id_edi = m.hladajidedi
    COUNT TO minusy FOR mnozstvo < 0 AND id_edi = m.hladajidedi

    SELECT edihlav
    REPLACE suma WITH m.prachy_plus

    IF m.plusy > 0 AND m.minusy > 0
        idedi2 = i_max("id_edi")
        SELECT edihlav
        SCATTER MEMVAR 
        INSERT INTO edihlav FROM MEMVAR 
        GO BOTTOM 
        REPLACE id_edi WITH m.idedi2, suma WITH prachy_minus
        SELECT edipol
        REPLACE id_edi WITH idedi2 FOR mnozstvo < 0 AND id_edi = m.hladajidedi
    ENDIF 
    uloz_a_zavri_hlav_a_pol()

ENDCASE 
era_and_ok_or_err()
