// Ýêñïîðò çàÿâîê â Ïðîòåê. Ôîðìàò èñïîëüçóåòñÿ äëÿ ýêñïîðòà â ïðîãðàììó çàÿâêè ýòîé êîìïàíèè
// ïåðâûé êëèåíò: ÌÊ Êîìïàíè - â Àðõàíãåëüñêå


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;
    DirName :string;



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 := 'Çàÿâêà '+asstring(VarID)+', äàòà/âðåìÿ: '+ formatdatetime('yyyy_mm_dd_hh-nn-ss',vardate_doc+vartime_doc);


   scripttime := vardate_doc+vartime_doc;

   AddStr(FileStr,'[Header]');

   //ÏÎÑÒÀÂÊÀ;;1000;;;XXXXXX;YYYYYY;
   strval := 'ÏÎÑÒÀÂÊÀ;;1000;;'+ // asstring(dbQueryvalue('select name from orgs where id = :id ',[id_org_main]))+';'+
            stringreplace(asstring(VarInfo),';',',',true)+';'+
            coalesce_string(asstring(getexternalorgcode(id_org_main,varid_orgs)),asstring(getexternalorgcode(0,varid_orgs)))+';'+
            coalesce_string(asstring(getexternalorgcode(asint64(varid_storage),varid_orgs)),
                              asstring(getexternalorgcode(id_org_main,varid_orgs)),
                              asstring(getexternalorgcode(0,varid_orgs)))+';';


   //MessageSubject := strval + ' ' + MessageSubject;

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

   AddStr(FileStr,StrVal);
   addstr(FileStr,'[Body]');

   dataset.first;
   while not dataset.eof do
   begin

     addstr(Filestr,
            coalesce_string( dataset.fieldbyname('price_code').asstring, dataset.fieldbyname('price_code_goods').asstring)+';'+
            //bvCurrToStrF(GetPrice,'.','','',2) +';'+
            dataset.fieldbyname('quantity').asstring+';'
           );


     dataset.next;
   end;


    ExportFilename :=  gettempdir + 'in'+asstring(VarID)+'.sst';   //getuniquefilename(gettempdir+'
    StringToFile(ExportFilename,FileStr);

    if pos('@',varexportemail)>0
    then begin

      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;

    end
    else begin // çíà÷èò òàì óêàçàí êàòàëîã, ñîõðàíÿåì â ýòîì êàòàëîãå
      DirName := includetrailingpathdelimiter(VarExportEmail);
      ForceDirectories(Dirname);
      CopyFile(ExportFilename, DirName + ExtractFileName(ExportFileName));

    end;

    deletefile(exportfilename);

end.
