Skip to content

Instantly share code, notes, and snippets.

@terjetyl
Created April 18, 2014 11:39
Show Gist options
  • Save terjetyl/11039467 to your computer and use it in GitHub Desktop.
Save terjetyl/11039467 to your computer and use it in GitHub Desktop.
Parse OCR files from nets.no
namespace NetsOcrParser
module OcrFileParser =
open System
open System.IO
open System.Globalization
type StartRecordForsendelse =
{ FormatKode : string; TjenesteKode : string; ForsendelsesType : string; RecordType : string; DataAvsender : string; ForsendelsesNummer : string; DataMottaker : string; Filler : string; }
type StartRecordOppdrag =
{ FormatKode : string; TjenesteKode : string; ForsendelsesType : string; RecordType : string; AvtaleId : string; OppdragsNummer : string; OppdragsKonto : string; Filler : string; }
type TransaksjonsRecord1 =
{ FormatKode : string; TjenesteKode : string; TransaksjonsType : string; RecordType : string; TransaksjonNummer : string; OppgjørsDato : DateTime; SentralId : string; DagKode : string; DelAvregningsNummer : string; LøpeNummer : string; Fortegn : string; Beløp : decimal; Kid : string; KortUtsteder : string; Filler : string; }
type TransaksjonsRecord2 =
{ FormatKode : string; TjenesteKode : string; TransaksjonsType : string; RecordType : string; TransaksjonNummer : string; BlankettNummer : string; AvtaleId : string; Filler : string; OppdragsDato : DateTime; DebetKonto : string; Filler2 : string; }
type SluttRecordOppdrag =
{ FormatKode : string; TjenesteKode : string; OppdragsType : string; RecordType : string; AntallTransaksjoner : int; AntallRecords : int; SumBeløp : decimal; OppgjørsDato : DateTime; FørsteOppgjørsdato : DateTime; SisteOppgjørsdato : DateTime; Filler : string; }
type SluttRecordForsendelse =
{ FormatKode : string; TjenesteKode : string; ForsendelsesType : string; RecordType : string; AntallTransaksjoner : int; AntallRecords : int; SumBeløp : decimal; OppgjørsDato : DateTime; Filler : string; }
type Transaction =
{ Part1 : TransaksjonsRecord1; Part2 : TransaksjonsRecord2; }
type OcrFile =
{ StartRecordForsendelse : StartRecordForsendelse; StartRecordOppdrag: StartRecordOppdrag; Transactions : Transaction list; SluttRecordOppdrag : SluttRecordOppdrag; SluttRecordForsendelse : SluttRecordForsendelse; }
let parse filepath =
let parseDate dateStr =
DateTime.ParseExact(dateStr, "ddMMyy", new DateTimeFormatInfo())
let parseStartRecordForsendelse (line:string) =
{ FormatKode = line.[0..1]; TjenesteKode = line.[2..3]; ForsendelsesType = line.[4..5]; RecordType = line.[6..7]; DataAvsender = line.[8..15]; ForsendelsesNummer = line.[16..22]; DataMottaker = line.[23..30]; Filler = line.[31..79]; }
let parseStartRecordOppdrag (line:string) =
{ FormatKode = line.[0..1]; TjenesteKode = line.[2..3]; ForsendelsesType = line.[4..5]; RecordType = line.[6..7]; AvtaleId = line.[8..16]; OppdragsNummer = line.[17..23]; OppdragsKonto = line.[24..34]; Filler = line.[35..79]; }
let parseSluttRecordOppdrag (line:string) =
{ FormatKode = line.[0..1]; TjenesteKode = line.[2..3]; OppdragsType = line.[4..5]; RecordType = line.[6..7]; AntallTransaksjoner = Int32.Parse(line.[8..15]); AntallRecords = Int32.Parse(line.[16..23]); SumBeløp = ((decimal)(line.[24..40]) / 100.0m); OppgjørsDato = parseDate(line.[41..46]); FørsteOppgjørsdato = parseDate(line.[47..52]); SisteOppgjørsdato = parseDate(line.[53..58]); Filler = line.[59..79]; }
let parseSluttRecordForsendelse (line:string) =
{ FormatKode = line.[0..1]; TjenesteKode = line.[2..3]; ForsendelsesType = line.[4..5]; RecordType = line.[6..7]; AntallTransaksjoner = Int32.Parse(line.[8..15]); AntallRecords = Int32.Parse(line.[16..23]); SumBeløp = ((decimal)(line.[24..40]) / 100.0m); OppgjørsDato = parseDate(line.[41..46]); Filler = line.[47..79]; }
let parseTransactionLine1 (line:string) =
{ FormatKode = line.[0..1]; TjenesteKode = line.[2..3]; TransaksjonsType = line.[4..5]; RecordType = line.[6..7]; TransaksjonNummer = line.[8..14]; OppgjørsDato = parseDate(line.[15..20]); SentralId = line.[21..22]; DagKode = line.[23..24]; DelAvregningsNummer = string line.[25]; LøpeNummer = line.[26..30]; Fortegn = string line.[31]; Beløp = ((decimal)(line.[32..48]) / 100.0m); Kid = line.[49..73]; KortUtsteder = line.[74..75]; Filler = line.[76..79]; }
let parseTransactionLine2 (line:string) =
{ FormatKode = line.[0..1]; TjenesteKode = line.[2..3]; TransaksjonsType = line.[4..5]; RecordType = line.[6..7]; TransaksjonNummer = line.[8..14]; BlankettNummer = line.[15..24]; AvtaleId = line.[25..33]; Filler = line.[34..40]; OppdragsDato = parseDate(line.[41..46]); DebetKonto = line.[47..57]; Filler2 = line.[58..79]; }
let parseTransactions (lines: string list) =
let transactions = List.map (fun x -> { Part1 = parseTransactionLine1 lines.[x]; Part2 = parseTransactionLine2 lines.[x+1]; }) [0 .. 2 .. lines.Length - 2]
transactions
let lines = File.ReadAllLines(filepath)
let startRecordForsendelse = parseStartRecordForsendelse (lines.[0])
let startRecordOppdrag = parseStartRecordOppdrag (lines.[1])
let sluttRecordOppdrag = parseSluttRecordOppdrag (lines.[lines.Length - 2])
let sluttRecordForsendelse = parseSluttRecordForsendelse (lines.[lines.Length - 1])
let transactions = parseTransactions (Array.toList lines.[2..lines.Length-2])
{ StartRecordForsendelse = startRecordForsendelse; StartRecordOppdrag = startRecordOppdrag; Transactions = transactions; SluttRecordOppdrag = sluttRecordOppdrag; SluttRecordForsendelse = sluttRecordForsendelse; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment