// Ýêñïîðò çàÿâîê â ÇÀÎ "ÐÎÑÒÀ". Âàðèàíò 2


var // ïðåäîïðåäåëåííûå ïåðåìåííûå

  Dataset :TDataset;
  VarExportEMail :string;
  VarName_Storage :variant;
  VarID_Storage :variant;
  varname_org :variant;
  varid_orgs :variant;
  VarDate_Doc :variant;
  varTime_Doc :variant;
  varNumber_doc :variant;
  VarID :variant;
  VarPreq_Sum :variant;
  VarRows_count :variant;
  VarInfo :variant;
  VarExportReport :string;
  VarExportOutPreqOriginalPrices :boolean; // ôëàã, íóæíî ëè ýêñïîðòèðîâàòü öåíó îðèãèíàëüíóþ èç ïðàéñ-ëèñòà. èíà÷å - öåíó, óêàçàííóþ â çàÿâêå


var
   MessageSubject :string = 'Çàêàç òîâàðà';

var
    MailMessage :Variant;
    ExportFilename :string;
    Bodystr :string;

    FileStr :string;
    strval :string;
    strval1 :string;

    scripttime :tdatetime;

function GetPrice :double;
begin
  if VarExportOutPreqOriginalPrices
  then result := Dataset.fieldbyname('price_pr').asfloat
  else result := Dataset.fieldbyname('price').asfloat;
end;


function GetProducerName (ProducerName,CountryName :string) :string;
begin
  result := producername;
  if countryname >''
  then result := producername + '/'+countryname;
end;


procedure AddStr(var DestStr: string; SourceStr :string);
begin
  if DestStr >'' then DestStr := DestStr + #13#10;

  DestStr := DestStr + SourceStr;
end;

procedure CheckParams;
begin
   if asint64(varid_storage)=0
   then begin
     varid_storage := id_storage_current;
   end;
   if (asint64(varid_storage)<>0) and (varname_storage='')
   then begin
     varname_storage := dbQueryvalue('select name from orgs where id = :id ',[varid_storage]);
   end;
   if asint64(varid_storage)<>0
   then begin
     id_org_main := coalesce_int64( asint64( DBQueryvalue('select id_parent_org from orgs where id = :id',[VARID_STORAGE]))
                                    ,id_org_main);
   end;
end;


begin
   CheckParams;

   filestr := '';
   bodystr := '';


   scripttime := vardate_doc+vartime_doc;


   strval := 'Çàêàç÷èê : '+ asstring(dbQueryvalue('select name from orgs where id = :id ',[id_org_main]));


   if asstring(varname_storage)<>''
   then
     strval := strval + ' (Ôèëèàë: '+varname_storage+')';

   MessageSubject := strval + ' ' + MessageSubject;


   addstr(BodyStr,strval+' ñäåëàë çàêàç '+formatdatetime('dd.mm.yyyy hh:nn:ss',scripttime));

   (*
   AddStr(FileStr,
             coalesce_string(asstring(getexternalorgcode(id_org_main,varid_orgs)),
                              asstring(getexternalorgcode(0,varid_orgs))
                             ));
   AddStr(FileStr,
              coalesce_string(asstring(getexternalorgcode(asint64(varid_storage),varid_orgs)),
                              asstring(getexternalorgcode(id_org_main,varid_orgs)),
                              asstring(getexternalorgcode(0,varid_orgs))
                             ));

   strval := asstring(varname_storage);
   if asstring(varInfo)<>''
   then begin
     if strval>'' then strval := strval + ', ';
     strval := strval + asstring(VarInfo);
   end;


   if asint64(VARID_STORAGE)<>0
   then
      strval1 := asstring(DBQueryvalue('select address from orgs where id = :id',[VARID_STORAGE]))
   else
      strval1 := asstring(DBQueryvalue('select address from orgs where id = :id',[id_org_main]));

   if strval1<>''
   then begin
     if strval>'' then strval := strval + ', ';
     strval := strval + strval1;
   end;
   AddStr(FileStr,strval);

   *)


   AddStr(FileStr,coalesce_string(asstring(varnumber_doc),asstring(varid)));

   dataset.first;
   while not dataset.eof do
   begin
     addstr(Filestr,

            coalesce_string(
                          dataset.fieldbyname('price_code').asstring,
                          dataset.fieldbyname('price_code_goods').asstring,
                          dataset.fieldbyname('price_barcode').asstring,
                          dataset.fieldbyname('price_seria').asstring)  +';'+

            coalesce_string(dataset.fieldbyname('name_goods_pr').asstring,
                            dataset.fieldbyname('name').asstring)+';'+

            //GetProducername(
            //           my_coalesce_str(dataset.fieldbyname('name_producers_pr').asstring,
            //                dataset.fieldbyname('name_producer').asstring),
            //           my_coalesce_str(dataset.fieldbyname('name_countries_pr').asstring,
            //                  dataset.fieldbyname('name_country').asstring,'')
            //           )+#9+
            dataset.fieldbyname('quantity').asstring+';'+
            bvCurrToStrF(GetPrice, '.','','',2)
           );

     dataset.next;
   end;



    ExportFilename :=  createuniquedir+'order.txt';   //getuniquefilename(gettempdir+'
    try
      StringToFile(ExportFilename,FileStr);

      MailMessage := CreateOleObject('mailprocessor.mpmailmessage');

      if not MailMessage.srv.connected
      then MailMessage.SRV.ConnectWP('Ïîëüçîâàòåëü','1');

      if not MailMessage.srv.connected
      then begin
        CreateHintE('Íå ìîãó ïðèñîåäèíèòüñÿ.');
        exit;
      end;

      MailMessage.ID := 0;
      MailMessage.Recipients :=  varexportemail;


      MailMessage.Subject := MessageSubject;

      MailMessage.Body := BodyStr;

      MailMessage.AddAttachment(ExportFilename);

      if (VarExportReport>'')
         and fileexists(VarExportReport)
      then
        MailMessage.AddAttachment(VarExportReport);

      MailMessage.Save;
    finally
      deletedir(extractfilepath(exportfilename))
    end;


end.
