Skip to content

Instantly share code, notes, and snippets.

@cwschroeder
Last active December 31, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwschroeder/7941486 to your computer and use it in GitHub Desktop.
Save cwschroeder/7941486 to your computer and use it in GitHub Desktop.
Fake KR data for user 7261
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
using System.Diagnostics;
using System.IO;
using CsvHelper;
public enum FileType
{
Normal,
PrimaerBuchungen,
Verteilbuchungen
}
class Program
{
static void Main(string[] args)
{
var importDir = @"C:\temp\KR\upload_klr2013-12-06-20-22-95";
var exportDir = @"C:\temp\KR\upload_klr_mod_2013-12-06-20-22-95";
Directory.CreateDirectory(exportDir);
var importFiles = Directory.GetFiles(importDir, "*.csv");
foreach (var importFilePath in importFiles)
{
Console.WriteLine("Processing file: {0}.", importFilePath);
var fileType = FileType.Normal;
if (importFilePath.ToUpperInvariant().Contains("PRIMAERBUCHUNGEN"))
{
fileType = FileType.PrimaerBuchungen;
}
if (importFilePath.ToUpperInvariant().Contains("VERTEILBUCHUNGEN"))
{
fileType = FileType.Verteilbuchungen;
}
var exportFileName = new FileInfo(importFilePath).Name;
var exportFilePath = Path.Combine(exportDir, exportFileName);
var lineCnt = 0;
using (var sr = new StreamReader(importFilePath))
using (var csv = new CsvReader(sr))
using (var w = new StreamWriter(exportFilePath, false))
{
csv.Configuration.Delimiter = ";";
csv.Configuration.QuoteAllFields = true;
while (csv.Read())
{
var mandantPos = 0;
if (lineCnt++ == 0)
{
var headers = csv.FieldHeaders;
mandantPos = headers.ToList().IndexOf("mandant_knz");
if (mandantPos < 0)
{
mandantPos = headers.ToList().IndexOf("mandant");
if (mandantPos < 0)
{
throw new InvalidOperationException("Mandant position not found");
}
}
w.WriteLine("\"" + string.Join("\";\"", csv.FieldHeaders) + "\"");
}
var fields = csv.CurrentRecord;
fields[mandantPos] = "7261";
if (fileType == FileType.PrimaerBuchungen)
{
var headers = csv.FieldHeaders.ToList();
var betragPos = headers.IndexOf("betrag_f");
var v1pos = headers.IndexOf("verwendungszweck_1");
var v2pos = headers.IndexOf("verwendungszweck_2");
if (betragPos < 0 || v1pos < 0 || v2pos < 0)
{
throw new InvalidOperationException();
}
fields[betragPos] = GetModifiedBetrag(fields[betragPos]);
fields[v1pos] = "Testbetrag_1";
fields[v2pos] = "Testbetrag_2";
}
if (fileType == FileType.Verteilbuchungen)
{
var headers = csv.FieldHeaders.ToList();
var betragPos = headers.IndexOf("betrag_f");
if (betragPos < 0)
{
throw new InvalidOperationException();
}
fields[betragPos] = GetModifiedBetrag(fields[betragPos]);
}
w.WriteLine("\"" + string.Join("\";\"", fields) + "\"");
}
}
if (lineCnt == 0)
{
File.Copy(importFilePath, exportFilePath, true);
// check length
Debug.Assert(File.ReadAllLines(exportFilePath).Count() == 1, "copied file must have only 1 row.");
}
Console.WriteLine();
}
}
static string GetModifiedBetrag(string sourceBetrag)
{
var rnd = new Random();
double parsedBetrag;
if (!double.TryParse(sourceBetrag, out parsedBetrag))
{
throw new InvalidCastException("Cannot parse '" + sourceBetrag + "' as double.");
}
var modBetrag = parsedBetrag * rnd.NextDouble();
return modBetrag.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment