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

const OutputDir = 'C:\mail_xprice\out\Ìîñêîâñêàÿ\';
      Storage  = 769; // Ñêëàä, èíôîðìàöèÿ ïî êîòîðîìó ïåðåäàåòñÿ. Åñëè 0, òî íå ôèëüòðóåòñÿ


const FullDataPeriod = 30;  // ïåðèîä â äíÿõ, ÷åðåç êîòîðûé ñêðèïò áóäåò ãåíåðèðîâàòü ïîëíûé ïàêåò

const DatabaseName = 'dbkassa';

var query,Query1,Query2,Query3 :TQuery;
    CDS,cds1 :TClientDataset;
    Dir1 :string;
    Stream :tMemoryStream;
    ZipName,ZipNameFull :string;
    i :integer;
    LastID :integer;
    Lastid1 :integer;
    CurrZipName :string;
    TempDir1,Tempdir2 :string;
    CODE :string;
    CurrStorage :integer;
begin
  with TMyWait.create('Èäåò ñáîð äàííûõ ïî ñïðàâî÷íèêó çàáðàêîâàííûõ ñåðèé') do
  try
    if session.netfiledir <> getaliaspath(DatabaseName)
    then  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);
      cds1 := tclientdataset.create(Query);

       Query.databasename := Databasename;


      if storage =0
      then begin

        Query.sql.text := 'select id,name,code from agents where issklad =1 and (code >"")';
        REadDataset(CDS1,Query);
      end;


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

      LastID := AppIniFile.readinteger('export_bad_series','LastID',0);
      AppIniFile.writeinteger('export_bad_series','LastID',asinteger(BDEQueryValue('select max(id) from badseria',[null],DatabaseName)));

      LastID1 := AppIniFile.readinteger('export_bad_series','LastID1',0);
      AppIniFile.writeinteger('export_bad_series','LastID1',asinteger(BDEQueryValue('select max(id) from prodcens',[null],DatabaseName)));

      (*
      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');

      finally
        Stream.free;
      end;


      STream := TMemoryStream.create;
      try

        Query.close;
        Query.sql.add('where id > '+inttostr(LASTID));
        readdataset(cds,query);
        cds.savetostream(Stream);

        ZipName :=  GetTempDir + 'badseries.zip';
        deletefile(ZipName);

        ZipADDFS(ZipName, Stream, 'badseria.cds');

      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);

      finally
      end;

      TempDir2 := includetrailingpathdelimiter(gettempdir+inttostr(randomrange(10000,20000)));
      Forcedirectories(tempdir2);
      ClearDir(tempdir2);
      try

        query.close;
        Query.close;
        Query.sql.add('where id > '+inttostr(LASTID));
        bdebatchmovetotmp(Query,tempdir2 + 'badseria.db',false);

        Query1.sql.text := 'select a.* from badserp a join '+Quotedstr(tempdir2+'badseria')+' b on a.id = b.param';
        bdebatchmovetotmp(Query1,tempdir2 + 'badserp.db',false);

        Query2.sql.add('where id > '+inttostr(LASTID1));
        bdebatchmovetotmp(Query2,tempdir2 + 'prodcens.db',false);

        Query3.sql.text := 'select a.* from prodcm a join '+Quotedstr(tempdir2+'prodcens')+' b on a.id = b.id_reestr';
        bdebatchmovetotmp(Query3,tempdir2 + 'prodcm.db',false);

        ZipName :=  tempdir2 + 'arcsklad.zip';
        deletefile(ZipName);
        ZipADD(ZipName, 'badseria.db',tempdir2);
        ZipADD(ZipName, 'badserp.db',tempdir2);
        ZipADD(ZipName, 'prodcens.db',tempdir2);
        ZipADD(ZipName, 'prodcm.db',tempdir2);

      finally
      end;

      if cds1.active then  cds1.first; // Ðàññûëêà ïî ñêëàäàì, êîòîðûå â MP (CODE = outdir) and isSKLAD = 1
      while (storage<>0) or cds1.active and  not cds1.eof do
      begin
          if Storage<>0 then CurrStorage := Storage
          else CurrStorage := cds1.fieldbyname('id').asinteger;


          if AppIniFile.readFloat('export_bad_series','Last_Full_date_'+inttostr(CurrStorage),0)
              < date - FullDataPeriod
          then begin
            CurrZipName := ZipNameFull;
            AppIniFile.WriteFloat('export_bad_series','Last_Full_date_'+inttostr(CurrStorage),date);
          end
          else CurrZipName :=  ZipName;

          if cds1.active
          then
            code := cds1.fieldbyname('code').asstring
          else
            Code := '';

          Dir1 := includetrailingpathdelimiter(includetrailingpathdelimiter(outputdir)+CODE);
          ForceDirectories(Dir1);
          CopyFile(CurrZipName,Dir1+extractfilename(CurrZipName),false);

          if cds1.active then  cds1.next
          else break;
      end;

      if tempdir1<>'' then deletedir(tempdir1);
      if tempdir2<>'' then deletedir(tempdir2);
    finally
      Query.free;
    end;
    CreateHIntI('Ïåðåäà÷à ðååñòðîâ çàâåðøåíà');
  finally
    free
  end;
end.
