// ïåðåäà÷à îòêàçîâ äëÿ Ôàðìíåò



var DATASET :TClientDataset; // äàòàñåò, ïåðåäàþùèé äàííûå
    NAME_ORG :string;  // Íàèìåíîâàíèå îðãàíèçàöèè
    NAME_ORG1 :string; // Íàèìåíîâàíèå ïîäðàçäåëåíèÿ
    DATEDOC :Tdatetime;
    NUMBERDOC :string; // íîìåð äîêóìåíòà-çàÿâêè
    EMAIL :string; // ïî÷òà
    INFO :string;
    ID_ORG :integer;
    ID_ORG1 :integer;
    VariantName :string;
    VariantFile :string;

var
  //Table :TTable;
  List :tStringlist;

  MailMessage :Variant;
  strval :string;

  intval :integer;
  FName :string;

begin
  if EMail = ''
  then begin
    createhintw('Â íàñòðîéêàõ êîíòðàãåíòà íå óêàçàí àäðåñ ýëåêòðîííîé ïî÷òû'#13+
                'Îïåðàöèÿ îòìåíåíà');
    exit;
  end;

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

    //TAble := TTable.create(nil);
    List := TstringList.create;
    try

      FName := gettempdir+'R'+formatdatetime('hhnnss',now)+'.txt';

      //FName := gettempdir+'R'+formatdatetime('hhnnss',now)+'.dbf';
      //Table.tablename := fname;
      //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;
        }

        List.add('NUMZ'+#9+'KOD'+#9+'NAME'+#9+'PROIZV'+#9+'COUNTRY'+#9+'KOLVO'+#9+'KODCLIENT'+#9+'NAMECLIENT'+#9+'ADRCLIENT');
      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;
          }

          strval := '';
          strval := strval + numberdoc+#9; // numz
          strval := strval +  dataset.fieldbyname('ID_TOVAR').asString+ #9;
          strval := strval +  dataset.fieldbyname('name').asstring + #9;
          strval := strval +  dataset.fieldbyname('name_producer').asstring + #9;
          strval := strval +  dataset.fieldbyname('name_country').asstring + #9;
          strval := strval +  dataset.fieldbyname('refusal').asstring + #9;

          intval := id_org1;
          if intval = 0 then intval := id_org;

          strval := strval +  asstring(intval) + #9;

          if NAME_ORG1 > ''
          then
            strval := strval +  NAME_ORG1 + #9
          else
            strval := strval +  NAME_ORG + #9;

          strval := strval + info + #9; // âìåñòî àäðåñà âîáüåì èíôî (òàì îáû÷íî àäðåñ è ðàçìåùàåòñÿ)

          list.add(strval);

          dataset.next;
          IncProgress;
        end;
      finally
        dataset.filtered := false;
      end;

      list.savetofile(fname);
      //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 := email;
    MailMessage.subject := 'îòêàçû ïî çàÿâêå '+numberdoc;

    MailMessage.addAttachment(FName);
    MailMessage.save;




    finally
      List.free;
      //Table.free
    end;

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

    free
  end;


end.
