Einzelnen Beitrag anzeigen

kaju74

Registriert seit: 22. Okt 2003
185 Beiträge
 
#10

AW: MT940 importieren

  Alt 2. Jun 2016, 18:26
Mit Hilfe der Code-Snippets konnte ich heute einen eigenen Parser schreiben, der das Ganze schon ganz gut für mich erledigt. Ein paar Kleinigkeiten fehlen noch, aber vom Prinzip her funktioniert. Auch hier ist wieder das Problem, das ich meine eigenen Methoden nutze, vom Prinzip her dürfte die Logik aber klar sein...falls es einem hilfe

Delphi-Quellcode:
function TDataModShared.ParseContent(Parser: TNXStringParser): Boolean;
var
  Str: string;
  Item: TPaymentItem;
  Detail: TNXString;
  Process: Boolean;
  FormatSettings: TFormatSettings;
begin
  { set result }
  Result := True;

  { init vars }
  Process := False;

  { set local format settings }
  GetLocaleFormatSettings(GetSystemDefaultLangID, FormatSettings);
  FormatSettings.DateSeparator := '/';
  FormatSettings.ShortDateFormat := 'yyyy/mm/dd';
  FormatSettings.DecimalSeparator := ',';

  { endless loop... }
  while True do
  begin
    { skip to next token }
    Parser.SkipToken;

    { end of file? }
    if Parser.Token = toEOF then
      Break;

    { not an main identifier? }
    if Parser.Token <> ':then
      Continue;

    { process special parts }
    case TNXStrings.IndexOf(Parser.SkipToToken(':'), ['61', '86']) of
      0: // sales part #61
        try
          { reset process flag }
          Process := False;

          { get parsed token string }
          Str := Parser.SkipTokenString;

          { check for debit payments only! }
          if not(SameText('D', TNXStrings.Mid(Str, 11, 1))) then
          begin
            { skip to end-block, but freeze token pos }
            Parser.SkipToTokenString(':6', True);

            { continue }
            Continue;
          end;

          { reset payment item }
          FillChar(Item, SizeOf(TPaymentItem), #0);

          { update entry date }
          Item.EntryDate := StrToDateDef(
            TNXStrings.Inject('20' + TNXStrings.Left(Str, 6), [4, 6], '/'), Date, FormatSettings
          );

          { update validity date }
          Item.ValidityDate := TNXStrings.FormatDate(Item.EntryDate);

          { update amount }
          Str := TNXStrings.RestOf(Str, 12);
          Item.Amount := StrToFloatDef(TNXStrings.Before('N', Str), 0.00, FormatSettings);

          { set process flag }
          Process := True;
        except
          { catch all index errors }
        end;

      1: // sales part #86
        if Process then
        begin
          { reset process flag }
          Process := False;

          { extract whole content up to next block }
          Str := NXString.From(Parser.SkipToTokenString(':6', True)).EnsureNoSuffix(':').Text;

          { only process sepa-compatible payments }
          if not(TNXStrings.HasPrefix('1', Str)) then
            Continue;

          { create single line with all additional details }
          Str := NXString.From(Str).After('?').EnsurePrefix('?').Strip([#13, #10]).Replace(#32, '_').Text;

          { iterate through all details seperated by ?-ids }
          for Detail in NXStrings.From(Str, '?').StripBlanks do
          begin
            { create simple id#content lines }
            Detail.Inject([2], '#');

            { get detail content }
            Str := NXString.From(Detail.Text).After('#').Replace('_', #32).Text;

            { get detail by id }
            case StrToIntDef(TNXStrings.Before('#', Detail.Text), -1) of
              { purposes }
              20..29, 60..63:
                Item.Purposes := TNXStrings.Stake(Item.Purposes, Str, '');

              { sender info }
              30: Item.Sender.BIC := Str;
              31: Item.Sender.IBAN := Str;
              32, 33: Item.Sender.Identifier := Str;
            end;
          end;

          { add item to list }
          FPlugIn.AddItem(Item);
        end;
    end;
  end;
end;
LG,
kaju

Geändert von kaju74 ( 3. Jun 2016 um 07:25 Uhr)
  Mit Zitat antworten Zitat