// Èìïîðò èç ËÅÌÌ (Ñàðàòîâ)

var
    Header :TStringList; // ïðåäîïðåäåëåíà ïðèëîæåíèåì
    Data :TStringList; // ïðåäîïðåäåëåíà ïðèëîæåíèåì
    ExportAgent :string; // âñå îïðåäåëåíû ïðèëîæåíèåì
    Filename :string;

    ExportAgentName :string;
    ExportNakladNom :string;
    ExportNakladID_DOC :string;
    ExportNakladData :string;
    ExportNakladSklad :string;
    ExportSkladName :string;



var Invoice :variant;
    Doc :variant;
    num_doc :string;
    Item :variant;
    i :integer;
    OpenDialog :TOpenDialog;

    function GetItemValue(Item :Variant; Name :String) :string;
    begin
       try
         result := vartostr(Item.attributes.getNamedItem(Name).value);
       except
         result := ''
       end;
    end;

 begin
     if filename = ''
     then begin
       OpenDialog := TOpenDialog.create(nil);
       try
         OpenDialog.Title := 'Óêàæèòå ôàéë';
         OpenDialog.FileName  := AppIniFile.readstring('plugins_lemm','filename','');
         opendialog.initialdir := extractfilepath(opendialog.filename);
         OpenDialog.Filter := 'Ôàéëû-íàêëàäíûå(*.dax)|*.dax|Âñå ôàéëû(*.*)|*.*';
         while  OpenDialog.execute do begin
           if not fileexists(OpenDialog.FileName) then CreateHint('Óêàæèòå ïðàâèëüíî ôàéë ')
           else begin
             Filename := OpenDialog.FileName;
             AppIniFile.writestring('plugins_lemm','filename',OpenDialog.filename);
             break;
           end;
         end;
       finally
         OpenDialog.free;
       end;
     end;

     if FileName = '' then exit;

     with TMyWait.create('Èäåò èìïîðò íàêëàäíîé') do
     try

       Invoice := CreateOleObject('MSXML2.DOMDocument');
       Invoice.Async := false;
       Invoice.load(FileName);
       Invoice.setProperty('SelectionLanguage', 'XPath');

       Doc := Invoice.selectSingleNode('outdoc');
       if varisnull(Doc)  then begin
          createhint('Äîêóìåíò íåñîîòâåòñòâóþùåé ñòðóêòóðû');
          exit;
       end;
       Doc := Doc.selectSingleNode('header');
       if varisnull(Doc)  then begin
          createhint('Íå íàéäåí ýëåìåíò header â äîêóìåíòå');
          exit;
       end;


       Header.Values[ExportAgent] := Doc.attributes.getNamedItem('EXTERNAL_CODE').value;
       Item := Doc.attributes.getNamedItem('POSTAV');
       if VarIsNull(Item) then Item := Doc.attributes.getNamedItem('FIRMA');
       if not VarIsNull(Item)
       then  Header.Values[ExportAgentName] := Item.value;

       Header.Values[ExportNakladNom] := Doc.attributes.getNamedItem('NUM_DOC').value;
       Header.Values[ExportNakladID_DOC] := Doc.attributes.getNamedItem('NUM_DOC').value;

       Header.Values[ExportNakladData] := Doc.attributes.getNamedItem('DOC_DATE').value;
       Header.Values[ExportNakladSklad] := Doc.attributes.getNamedItem('ID_SUBJECT_RETAIL').value;
       Header.Values[ExportSkladName] := Doc.attributes.getNamedItem('STORE_NAME').value;



       Data.Add('kol'#9'cenanotnds'#9'nds'#9'srok'#9'em'#9'firstcena'#9+
                'seria'#9'tamog'#9'sertif'#9'sroksertif'#9'sertifname'#9+
                'name'#9'producername'#9'countryname'#9'barcode'#9'sertifname'#9+);  // çàãîëîâîê

       progressbarmax := Doc.ChildNodes.length;
       for i := 0 to Doc.childnodes.length-1 do
       begin
         IncProgress;
         Item := Doc.childnodes[i];

         data.add(GetItemValue(item,'QUANTITY')+#9+ // çíà÷åíèÿ ïîëåé â òîé æå ïîñëåäîâàòåëüíîñòè
                  GetItemValue(item,'PRICE')+#9+
                  GetItemValue(item,'STAVKA_NDS')+#9+
                  GetItemValue(item,'TIME_APPLY')+#9+
                  GetItemValue(item,'PACKS_IN_BOX')+#9+
                  GetItemValue(item,'PRICE_MANUF')+#9+
                  GetItemValue(item,'SERIES')+#9+
                  GetItemValue(item,'GTD')+#9+
                  GetItemValue(item,'CERTIFICATE_NUM')+#9+
                  GetItemValue(item,'CERTIFICATE_TIME')+#9+
                  GetItemValue(item,'CERT_ISSUER_NAME')+#9+
                  GetItemValue(item,'FULL_NAME')+#9+
                  GetItemValue(item,'PRODUCER_NAME')+#9+
                  GetItemValue(item,'PRODUCER_STATE')+#9+
                  GetItemValue(item,'BAR_CODE'))
       end;
     finally
       free
     end;

     createhint('Îïåðàöèÿ çàâåðøåíà');
 end.
