// ïåðåäà÷à îòêàçîâ äëÿ Àááàòà


const ExportAddresses = 'a.vagin@tdabbat.ru'; // àäðåñà îòïðàâêè îòêàçîâ

//const EXPORTFILENAME = 'refusals.dbf'; // èìÿ ôàéëà ýêñïîðòà

var DATASET :TClientDataset; // äàòàñåò, ïåðåäàþùèé äàííûå
    NAME_ORG :string;  // Íàèìåíîâàíèå îðãàíèçàöèè
    NAME_ORG1 :string; // Íàèìåíîâàíèå ïîäðàçäåëåíèÿ
    NUMBERDOC :string; // íîìåð äîêóìåíòà-çàÿâêè

var
  Table :TTable;

  MailMessage :Variant;

begin

  with tMyWait.create('Èäåò ýêñïîðò îòêàçîâ') do
  try
    //ForceDirectories(ExportPath);

    TAble := TTable.create(nil);
    try
      Table.tablename := gettempdir+'R'+formatdatetime('hhnnss',now)+'.dbf';
      table.exclusive := true;
      table.tabletype := ttFoxpro;

      {
      if fileexists(BulkFILEName)
      then begin
        CopyFile(BulkFileName,Table.tablename);
        table.emptyTable;
      end
      else}
      begin
        table.fielddefs.clear;
        Table.fielddefs.add('CODEPST',ftString,10,false);
        table.fielddefs.add('NAME',ftSTring,200,false);
        table.fielddefs.add('NAMEPROIZV',ftSTring,100,false);
        Table.fielddefs.add('CODEPROIZV',ftString,10,false);
        Table.fielddefs.add('KOLVOOTKAZ',ftString,40,false);
        Table.fielddefs.add('APTEKA',ftString,40,false);
        Table.fielddefs.add('COMMENTS',ftString,100,false);
        Table.CreateTable;
      end;

      Table.open;

      Dataset.filter := '[REFUSAL] > 0';
      Dataset.filtered := true;
      try

        progressbarmax := dataset.recordcount;
        progressbarpos := 0;

        dataset.first;
        while not dataset.eof do
        begin
          table.append;
          table.fieldbyname('CODEPST').asString := dataset.fieldbyname('ID_TOVAR').asString;
          table.fieldbyname('NAME').asstring := dataset.fieldbyname('name').asstring;
          table.fieldbyname('CODEPROIZV').asString := dataset.fieldbyname('ID_PRODUCER').asString;
          table.fieldbyname('NAMEPROIZV').asstring := dataset.fieldbyname('name_PRODUCER').asstring;

          table.fieldbyname('KOLVOOTKAZ').asstring := dataset.fieldbyname('REFUSAL').asstring;

          if NAME_ORG1 > ''
          then
            table.fieldbyname('APTEKA').asstring := NAME_ORG1
          else
            table.fieldbyname('APTEKA').asstring := NAME_ORG;

          table.fieldbyname('COMMENTS').asstring := dataset.fieldbyname('causeofrefusal').asstring;
          table.post;
          dataset.next;
          IncProgress;
        end;
      finally
        dataset.filtered := false;
      end;

      table.close;



      MailMessage := CreateOleObject('mailprocessor.mpmailmessage');
      if not MailMessage.srv.connected
      then begin
        mailMessage.srv.connectwp('autouser','1');
      end;

      if not mailMessage.srv.connected then
      begin
        createhinte('Îøèáêà, ñåðâåð íå ïîäêëþ÷åí');
        exit;
      end;

    MailMessage.id := 0;
    MailMessage.recipients := exportaddresses;
    MailMessage.subject := 'îòêàçû ïî çàÿâêå '+numberdoc;

    MailMessage.addAttachment(table.TableName);
    MailMessage.save;




    finally
      Table.free
    end;

    hide; // waitform
    CreateHintI('Îïåðàöèÿ çàâåðøåíà','Ïåðåäà÷à îòêàçîâ',20);
  finally
    free
  end;


end.
