* export_comenzi.prg
PROCEDURE ExportComenziXML
PARAMETERS tcFileName, tdData
    
LOCAL loXML AS MSXML2.DOMDocument.6.0
LOCAL lcXML, llSuccess
llSuccess = .F.

Try
    loXML = CREATEOBJECT("MSXML2.DOMDocument.6.0")
    loXML.async = .F.

    * Creare structura XML
    TEXT TO lcXML NOSHOW
<?xml version="1.0" encoding="UTF-8"?>
<comenzi>
</comenzi>
    ENDTEXT

    loXML.loadXML(lcXML)
    loRoot = loXML.documentElement

    * Selectam comenzile pentru export
    SELECT comenzi_service
    SET FILTER TO data_comanda = tdData AND status = "FINALIZAT"
    SCAN
        * Adaugam nodul pentru comanda
        loComanda = loXML.createElement("comanda")
        
        * Adaugam detaliile comenzii
        THIS.AddXMLNode(loXML, loComanda, "nr_comanda", nr_comanda)
        THIS.AddXMLNode(loXML, loComanda, "data", TTOC(data_comanda, 1))
        THIS.AddXMLNode(loXML, loComanda, "vin", vin)
        THIS.AddXMLNode(loXML, loComanda, "nr_inmatriculare", nr_auto)
        THIS.AddXMLNode(loXML, loComanda, "km_final", TRANSFORM(km))
        THIS.AddXMLNode(loXML, loComanda, "km_initial", "0")
        THIS.AddXMLNode(loXML, loComanda, "observatii", observatii)
        
        * Adaugam operatiile
        loOperatii = loXML.createElement("operatii")
        
        SELECT operatii
        SCAN FOR id_comanda = comenzi_service.id
            loOperatie = loXML.createElement("operatie")
            THIS.AddXMLNode(loXML, loOperatie, "cod_operatie", cod_operatie)
            THIS.AddXMLNode(loXML, loOperatie, "denumire", denumire)
            loOperatii.appendChild(loOperatie)
        ENDSCAN
        
        loComanda.appendChild(loOperatii)
        loRoot.appendChild(loComanda)
    ENDSCAN

    * Salvam XML-ul
    loXML.save(tcFileName)
    llSuccess = .T.

Catch To loEx
    MESSAGEBOX("Eroare export XML: " + loEx.Message, 16, "Eroare")
    llSuccess = .F.
Endtry

Return llSuccess

* Helper pentru adaugare noduri
PROCEDURE AddXMLNode
PARAMETERS loXML, loParent, tcName, tcValue
LOCAL loNode, loText, llSuccess
llSuccess = .F.
    
Try
    loNode = loXML.createElement(tcName)
    loText = loXML.createTextNode(tcValue)
    loNode.appendChild(loText)
    loParent.appendChild(loNode)
    llSuccess = .T.
    
Catch To loEx
    MESSAGEBOX("Eroare adaugare nod XML: " + loEx.Message, 16, "Eroare")
    llSuccess = .F.
Endtry

Return llSuccess