
// Ýêñïîðò â Ýêñåëü (Êàëåíäóëà)

const ExportDir = 'c:\temp\1C\'; // êàòàëîã ýêñïîðòà ôàéëîâ


var // ïðåäîïðåäåëåííûå ïðîãðàììîé ïåðåìåííûå

  dataset :TTable; // òàáëèöà ñ øàïêàìè íàêëàäíûõ ,êîòîðóþ ñîáèðàåò âûçûâàþùàÿ ôîðìà. Èç íåå ôîðìèðóåì âûõîäíîé ôàéë
  MinDate :tDateTime; // Ïåðâàÿ èç äàò â äèàïàçîíå
  MaxDate :tDateTime; // Âòîðàÿ (ìàêñèìàëüíàÿ) äàòà â äèàïàçîíå
  CheckOutput :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî ðàñõîäíûì äîêóìåíòàì
  CheckInput  :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî ïðèõîäíûì äîêóìåíòàì
  CheckInputMoney :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî âûïëàòàì îò ïîêóïàòåëåé


var
  TaPR :TClientdataset;
  TaRS :TClientdataset;
  SaveDialog :TSaveDialog;
  FileName :string;

  QueryPR,QueryRS :tQuery;
  thvar :variant;
  Field :TField;

  TaStorages :TClientdataset;
  strval :string;

  MYDATASET :TClientDataset;

  i:integer;
begin


  QueryPR := TQuery.create(nil);
  with TMyWait.create('Èäåò ýêñïîðò íàêëàäíûõ') do
  try

      QueryRS := TQuery.create(QueryPR);
      TAPR := TClientdataset.create(QueryPR);
      taRS := Tclientdataset.create(QueryPR);
      MyDataset := Tclientdataset.create(QueryPR);

      taStorages := TClientdataset.create(QueryPR);


      readdataset(MyDataset,Dataset);

      taStorages.close;
      taStorages.fielddefs.clear;
      taStorages.fielddefs.add('id',ftinteger,0,false);
      tastorages.fielddefs.add('name',ftstring,200,false);
      taStorages.createdataset;
      taStorages.indexfieldnames := 'id';
      taStorages.logchanges := false;

      QueryPR.databasename := 'dbkassa';
      QueryRS.databasename := 'dbkassa';

      mydataset.first;
      while not mydataset.eof do
      begin
        i := mydataset.fieldbyname('sklad').asinteger;
        if i>0
        then begin
          if not tastorages.findkey([i])
          then begin
            tastorages.append;
            tastorages.fieldbyname('id').asinteger := i;
            tastorages.fieldbyname('name').asstring := vartostr(BDEQueryValue( 'select name from agents where id = :id', [i], 'dbkassa'));
            if tastorages.fieldbyname('name').asstring = ''
            then tastorages.fieldbyname('name').asstring := inttostr(i);
            tastorages.post;
          end;
        end;
        mydataset.next;
      end;

      progressbarmax := tastorages.recordcount;
      tastorages.first;
      while not tastorages.eof do
      begin

        AdvancedTExt := 'Ñêëàä: '+ tastorages.fieldbyname('name').asstring;


        with tapr do
        begin
          close;
          with fielddefs do begin
            clear;

            add('DATA',ftdate,0,false);
            add('OTGRUSKA',ftdate,0,false);
            add('NOMER',ftinteger,0,false);
            add('ID_DOC',ftstring,50,false);
            add('SKLAD',ftinteger,0,false);
            add('PO',ftstring,1,false);
            add('OSNOV',ftstring,2,false);
            add('OSNOVNAME',ftstring,200,false);
            add('AGENT',ftinteger,0,false);
            add('AGENTNAME',ftstring,200,false);
            add('AGENTINN',ftstring,50,false);

            add('SUMMA0',ftCurrency,0,false);
            add('SUMMA10',ftCurrency,0,false);
            add('SUMMA20',ftCurrency,0,false);
            add('SUMMA18',ftCurrency,0,false);
            add('SUMMA',ftCurrency,0,false);

            add('SUMMA0z',ftCurrency,0,false);
            add('SUMMA10z',ftCurrency,0,false);
            add('SUMMA20z',ftCurrency,0,false);
            add('SUMMA18z',ftCurrency,0,false);
            add('SUMMAz',ftCurrency,0,false);


            add('SUMMA0p',ftCurrency,0,false);
            add('SUMMA10p',ftCurrency,0,false);
            add('SUMMA20p',ftCurrency,0,false);
            add('SUMMA18p',ftCurrency,0,false);
            add('SUMMAp',ftCurrency,0,false);


            add('NDS10',ftCurrency,0,false);
            add('NDS20',ftCurrency,0,false);
            add('NDS18',ftCurrency,0,false);

            add('NDS10Z',ftCurrency,0,false);
            add('NDS20Z',ftCurrency,0,false);
            add('NDS18Z',ftCurrency,0,false);

            add('NDS10P',ftCurrency,0,false);
            add('NDS20P',ftCurrency,0,false);
            add('NDS18P',ftCurrency,0,false);

            add('DATEPOST',ftdate,0,false);
            //add('FACTURAPOST',ftdate,0,false);

            add('priznak',ftinteger,0,false);
            add('skladname',ftstring,200,false);

            add('prihosnov',ftstring,2,false);
            add('prihnomer',ftinteger,0,false);
            add('post',ftinteger,0,false);
            add('postname',ftstring,200,false);
            add('id_doc_post',ftstring,100,false);

          end;

          createdataset;
          LogChanges := false;
        end;

        with tars do begin
          close;
          fielddefs.clear;
          for i := 0 to tapr.fieldcount-1 do
          begin
            fielddefs.add(tapr.fields[i].fieldname,tapr.fields[i].datatype,tapr.fields[i].size,false);
          end;
          createdataset;
          LogChanges := false;
        end;

        mydataset.filtered := false;
        mydataset.filter := '[SKLAD]='+tastorages.fieldbyname('id').asstring;
        mydataset.filtered := true;

        mydataset.first;
        while not mydataset.eof do
        begin
          if (mydataset.fieldbyname('po').asstring = '+') and CheckInput
          then begin
                tapr.append;

                for i := 0 to mydataset.fieldcount-1 do
                if not mydataset.fields[i].isnull
                then begin
                  field := tapr.findfield(mydataset.fields[i].fieldname);
                  if field<>nil
                  then begin
                    field.value := mydataset.fields[i].value;
                  end;
                end;

                tapr.post;
          end
          else
          if (mydataset.fieldbyname('po').asstring = '-') and CheckOutput
          then begin
                tars.append;

                for i := 0 to mydataset.fieldcount-1 do
                if not mydataset.fields[i].isnull
                then begin
                  field := tars.findfield(mydataset.fields[i].fieldname);
                  if field<>nil
                  then begin
                    field.value := mydataset.fields[i].value;
                  end;
                end;

                tars.post;
          end;


          mydataset.next;
        end;

        Filename := includetrailingpathdelimiter(ExportDir)+
                    correctfilename('1sbtrans_'+ tastorages.fieldbyname('name').asstring+'_' +
                                    formatdatetime('dd.mm.yyyy',mindate)+'_'+formatdatetime('dd.mm.yyyy',maxdate)+'_prihod.xls');

        tablecopyd(Filename,tapr);


        Filename := includetrailingpathdelimiter(ExportDir)+
                    correctfilename('1sbtrans_'+ tastorages.fieldbyname('name').asstring+'_' +
                                    formatdatetime('dd.mm.yyyy',mindate)+'_'+formatdatetime('dd.mm.yyyy',maxdate)+'_rashod.xls');

        TAbleCopyD(Filename,tars);


        TAPR.close;
        tars.close;

        incprogress;
        tastorages.next;
      end;

      mydataset.filtered := false;

      CreateHintI('Îïåðàöèÿ ýêñïîðòà äàííûõ çàâåðøåíà');
  finally
    free;
    QueryPR.free;
  end;

end.

