//*****************************************************************
//*  © BVSOFT 2009
//*
//* ÑÊÐÈÏÒ:     ExportBadSeries.Full.To1.pas
//* ÂÅÐÑÈß:     200.04.15
//* ÏÎÄÄÅÐÆÈÂÀÅÌÛÅ ÏÐÎÃÐÀÌÌÛ:     Solaris,MailProcessor, XPrice
//* ÎÏÈÑÀÍÈÅ:     ñêðèïò ïåðåäà÷è ñïðàâî÷íèêà çàáðàêîâàííûõ ñåðèé
//*  èç öåíòðàëüíîé àïòåêè â 1 óêàçàííûé àïòå÷íûé ïóíêò (ïîëíûé ïàêåò)
//*
//*****************************************************************

const OutputDir = 'd:\mail_xprice\out\';

const DatabaseName = 'dbkassa';

var query,Query1,Query2,query3 :TQuery;
    CDS :TClientDataset;
    Dir1 :string;
    Stream :tMemoryStream;
    ZipName,ZipNameFull :string;
    i :integer;
    LastID,LastID1 :integer;
    CurrZipName :string;
    CODE :string;

    ID :integer;
    CODE :string;
    TempDir1,Tempdir2 :string;
begin
  ID := SelectOrg(atStorage);
  if id=0 then exit;

  with TMyWait.create('Èäåò ñáîð äàííûõ ïî ñïðàâî÷íèêó çàáðàêîâàííûõ ñåðèé') do
  try
    Session.netfiledir := getaliaspath(databasename);
    Query := TQuery.create(nil);
    try

      Query1 := TQuery.create(Query);
      Query2 := TQuery.create(Query);
      Query3 := TQuery.create(Query);
      CDs := TClientDataset.create(Query);

      Query.databasename := Databasename;
      Query1.databasename := Databasename;
      Query2.databasename := Databasename;
      Query3.databasename := Databasename;

     CODE := vartostr(bdequeryvalue('select code from agents where id =:id ',[id]));

     if code ='' then begin
       bvMessageError('Äàííûé ñêëàä íå íàñòðîåí äîëæíûì îáðàçîì (àëüò.êîä íå óêàçàí)');
       exit;
     end;


      (*
      STream := TMemoryStream.create;
      try

        query.close;
        Query.sql.text := 'select a.*,b.name as name_param from badseria a left join badserp b on a.param = b.id';
        readdataset(cds,query);
        cds.savetostream(Stream);

        ZipNameFull :=  GetTempDir + 'badseries_full.zip';
        deletefile(ZipNameFull);
        ZipADDFS(ZipNameFull, Stream, 'badseria.cds');

        CurrZipName := ZipNameFull;

      finally
        Stream.free;
      end;
      *)

      tempdir1 := '';
      tempdir2 := '';


      TempDir1 := includetrailingpathdelimiter(gettempdir+inttostr(randomrange(1,10000)));
      Forcedirectories(tempdir1);
      ClearDir(tempdir1);
      try

        query.close;
        Query.sql.text := 'select * from badseria';
        Query1.sql.text := 'select * from badserp';
        Query2.sql.text := 'select * from prodcens';
        Query3.sql.text := 'select * from prodcm';

        bdebatchmovetotmp(Query,tempdir1 + 'badseriaf.db',false);
        bdebatchmovetotmp(Query1,tempdir1 + 'badserp.db',false);
        bdebatchmovetotmp(Query2,tempdir1 + 'prodcensf.db',false);
        bdebatchmovetotmp(Query3,tempdir1 + 'prodcmf.db',false);

        ZipNameFull :=  tempdir1 + 'arcsklad.zip';
        deletefile(ZipNameFull);
        ZipADD(ZipNameFull, 'badseriaf.db',tempdir1);
        ZipADD(ZipNameFull, 'badserp.db',tempdir1);
        ZipADD(ZipNameFull, 'prodcensf.db',tempdir1);
        ZipADD(ZipNameFull, 'prodcmf.db',tempdir1);

        CurrZipName := ZipNameFull;

      finally
      end;


      Dir1 := includetrailingpathdelimiter(includetrailingpathdelimiter(outputdir)+CODE);
      ForceDirectories(Dir1);
      CopyFile(CurrZipName,Dir1+extractfilename(CurrZipName),false);
    finally
      Query.free;
    end;
    CreateHIntI('Ïåðåäà÷à çàáðàêîâàííûõ çàâåðøåíà');
  finally
    free
  end;
end.
