// Ýêñïîðò çàÿâîê Èìïåðèÿ-Ôàðì.


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;
  VarExportReport :string;
  VarInfo :variant;
  VarExportOutPreqOriginalPrices :boolean; // ôëàã, íóæíî ëè ýêñïîðòèðîâàòü öåíó îðèãèíàëüíóþ èç ïðàéñ-ëèñòà. èíà÷å - öåíó, óêàçàííóþ â çàÿâêå


const
   MessageSubject = 'Ýëåêòðîííàÿ çàÿâêà SolPrice';


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

    FileStr :string;
    strval :string;




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

  DestStr := DestStr + SourceStr;
end;

function ifthen_1(BoolVal :boolean; trueval,falseval :string) :string;
begin
  if boolval
  then result := trueval
  else result := falseval
end;

function GetPrice :double;
begin
  if VarExportOutPreqOriginalPrices
  then result := Dataset.fieldbyname('price_pr').asfloat
  else result := Dataset.fieldbyname('price').asfloat;
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 := '';


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

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


   AddStr(FileStr,strval);
   addstr(BodyStr,strval);

   //showmessage(asstring(varid_orgs)+':'+asstring(varname_org));

   strval := 'Êîä çàêàç÷èêà : '
            + coalesce_string(asstring(getexternalorgcode(asint64(varid_storage),varid_orgs)),
                              asstring(getexternalorgcode(id_org_main,varid_orgs)),
                              asstring(getexternalorgcode(0,varid_orgs))
                             );

   AddStr(FileStr,strval);
   addstr(BodyStr,strval);

   strval := 'Äîïîëíèòåëüíûå ñâåäåíèÿ : ';
   AddStr(FileStr,strval);
   //addstr(BodyStr,strval);

   strval := 'Êîììåíòàðèé ê çàÿâêå : '+asstring(varinfo);
   {
   if asinteger(varid_storage)<>0
   then begin
     strval:= strval + asstring( dbQueryvalue('select address from orgs where id = :id ',[coalesce_int64(asint64(varid_storage),id_org_main)]));
   end;
   }
   AddStr(FileStr,strval);
   addstr(BodyStr,strval);

   strval := 'Âðåìÿ îòïðàâëåíèÿ : '+formatdatetime('dd.mm.yyyy hh:nn:00',vardate_doc+vartime_doc);
   AddStr(FileStr,strval);
   addstr(BodyStr,strval);

   dataset.first;
   while not dataset.eof do
   begin
     addstr(Filestr,dataset.fieldbyname('quantity').asstring+#9+
            coalesce_string( dataset.fieldbyname('price_code').asstring, dataset.fieldbyname('price_code_goods').asstring)+#9+
            coalesce_string(dataset.fieldbyname('name_goods_pr').asstring,
                            dataset.fieldbyname('name').asstring)+#9+
            #9+
            //#9+
            coalesce_string(dataset.fieldbyname('name_producers_pr').asstring,
                            dataset.fieldbyname('name_producer').asstring)+
            ifthen_1(coalesce_string(dataset.fieldbyname('name_countries_pr').asstring,
                            dataset.fieldbyname('name_country').asstring)>'',
                            '/'+coalesce_string(dataset.fieldbyname('name_countries_pr').asstring,
                                                dataset.fieldbyname('name_country').asstring),
                            '')+#9+
            //stringreplace(formatfloat('#############.00##',dataset.fieldbyname('price').asfloat),decimalseparator,'.')+';'+
            bvCurrToStrF(GetPrice, ',','','',2)
           );


     dataset.next;
   end;

    ExportFilename :=  gettempdir + 'order_'+asstring(varid)+'.txt';   //getuniquefilename(gettempdir+'
    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;

    //deletefile(exportfilename);


end.
