*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
*     PROCEDURE spracuj_xml K.A.P. sw
*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    

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

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

pocetdodakov = 0
pocetfaktur  = 1
IF m.pocetfaktur>0
    FOR i=1 TO m.pocetfaktur
        hladajidedi=i_max("id_edi") 
        otevri_edi_hlavpol()
        insert_edihlav()

        faktura1=vratsekciu("Invoice", m.xml,m.i,"X")
        IF hladajsekciu("AccountingCustomerParty", m.faktura1)>0
            IF hladajsekciu("AccountingSupplierParty", m.faktura1)>0
                    dodavatel=vratsekciu("Party",vratsekciu("AccountingSupplierParty", m.faktura1,1,"X"),1,"X")
                    SELECT edihlav
                    REPLACE c_faktury WITH vratsekciu("ID",m.faktura1,1,"C")
                    REPLACE dat_vyst  WITH vratsekciu("IssueDate", m.faktura1,1,"D")
                    REPLACE ico       WITH STRTRAN(vratsekciu("ID",vratsekciu("PartyIdentification",m.dodavatel,1,"X"),1,"C")," ","")
                    REPLACE icdph     WITH STRTRAN(vratsekciu("CompanyID",vratsekciu("PartyTaxScheme",m.dodavatel,1,"X"),1,"C")," ","")
                    REPLACE partner   WITH vratsekciu("Name",vratsekciu("PartyName",m.dodavatel,1,"X"),1,"C",m.kodovastranka)
                    REPLACE ulica     WITH vratsekciu("StreetName",vratsekciu("PostalAddress",m.dodavatel,1),1,"C",m.kodovastranka)+" "+ALLTRIM(vratsekciu("BuildingNumber",vratsekciu("PostalAddress",m.dodavatel,1),1,"C",m.kodovastranka))
                    REPLACE mesto     WITH vratsekciu("CityName",vratsekciu("PostalAddress",m.dodavatel,1),1,"C",m.kodovastranka)
                    REPLACE psc       WITH vratsekciu("PostalZone",vratsekciu("PostalAddress",m.dodavatel,1),1,"C")
                    RELEASE m.dodavatel
                    IF m.hotel_platca_dph
                        REPLACE suma WITH vratsekciu("TaxableAmount",vratsekciu("TaxSubTotal",vratsekciu("TaxTotal",m.faktura1,1,"X"),1,"X"),1,"N")
                    ELSE
                        REPLACE suma WITH vratsekciu("TaxInclusiveAmount",vratsekciu("TaxSubTotal",vratsekciu("TaxTotal",m.faktura1,1,"X"),1,"X"),1,"N")
                    ENDIF 
                    edi_stred = STRTRAN(vratsekciu("Name",vratsekciu("PartyName",vratsekciu("Delivery",m.faktura1,1,"X"),1,"X"),1,"C", m.kodovastranka)," ","")

                    IF EMPTY(m.edi_stred)  && cezarovo
                        odberatel = vratsekciu("AccountingCustomerParty", m.faktura1,1,"X")
                        odberatel = vratsekciu("Contact", m.odberatel,1,"X")
                        edi_stred = vratsekciu("Name", m.odberatel, 1, "C")
                    ENDIF 

                    IF EMPTY(m.edi_stred)
                        odberatel = vratsekciu("AccountingCustomerParty", m.faktura1,1,"X")
                        edi_stred = vratsekciu("ID", m.odberatel, 1, "C")
                    ENDIF 

                    IF !EMPTY(m.edi_stred)
                        REPLACE edi_stred WITH m.edi_stred, edi_kod WITH ""
                    ENDIF 

                    IF hladajsekciu("InvoiceLines", m.faktura1)>0
                        polozky=vratsekciu("InvoiceLines", m.faktura1,1)
                        pocetpoloziek=hladajsekciu("InvoiceLine", m.polozky)
                        sumadokladu=0
                        FOR m.ii=1 TO m.pocetpoloziek
                            polozka = vratsekciu("InvoiceLine"     , m.polozky, m.ii)
                            kodzak  = vratsekciu("ID",vratsekciu("SellersItemIdentification",vratsekciu("Item",m.polozka,1,"X"),1,"X"),1,"C")
                            mnozstvo= vratsekciu("InvoicedQuantity", m.polozka,1,"N")
                            nazov   = vratsekciu("Description",vratsekciu("Item",m.polozka,1,"X"),1,"C", m.kodovastranka)
                            IF ATC("zaokrouhlen", m.nazov) > 0
                                mnozstvo = 0
                            ENDIF 
                            IF m.mnozstvo # 0
                                INSERT INTO edipol (id_edi, c_data , riadok, ean, kod_zak ,   mnozstvo,   nazov, cena, cena_celk, dan) VALUES ;
                                            (m.hladajidedi, m.cdata, m.ii+2000  ,  "", m.kodzak, m.mnozstvo, m.nazov, ;
                                    IIF(hotel_platca_dph,vratsekciu("UnitPrice",m.polozka,1,"N"),vratsekciu("UnitPriceTaxInclusive",m.polozka,1,"N")),;
                                    IIF(hotel_platca_dph,vratsekciu("LineExtensionAmount",m.polozka,1,"N"),vratsekciu("LineExtensionAmountTaxInclusive",m.polozka,1,"N")),;
                                    IIF(hotel_platca_dph,vratsekciu("Percent",vratsekciu("ClassifiedTaxCategory",m.polozka,1,"X"),1,"N"),0) )
                                sumadokladu=m.sumadokladu+IIF(hotel_platca_dph,vratsekciu("LineExtensionAmount",m.polozka,1,"N"),vratsekciu("LineExtensionAmountTaxInclusive",m.polozka,1,"N"))
                            ENDIF 
                        ENDFOR
                        RELEASE m.polozka 
                        druhy_doklad_z_minusu()
                        uloz_a_zavri_hlav_a_pol()
                    ELSE
                        chybatxt=m.chybatxt+CRLF+"Chybaju polozky v doklade "+ALLTRIM(edihlav.c_faktury)
                    ENDIF 
            ELSE
                chybatxt=m.chybatxt+CRLF+"Chybaju udaje o dodavatelovi v doklade "+ALLTRIM(edihlav.c_faktury)
            ENDIF 

            RELEASE m.odberatel
        ELSE
            chybatxt=m.chybatxt+CRLF+"Chybaju udaje o odberatelovi v doklade "+vratsekciu("Document_Number",m.faktura1,1)
        ENDIF 
    ENDFOR  
ELSE 
    chybatxt=m.chybatxt+CRLF+"Subor neobsahuje faktury"
ENDIF 

era_and_ok_or_err()

