-
Notifications
You must be signed in to change notification settings - Fork 3
/
AuditFileHelper.cs
123 lines (112 loc) · 5.58 KB
/
AuditFileHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.IO.Compression;
namespace Softcash.SAFT
{
public class AuditFileHelper
{
/// <summary>
/// Private constructor because we don't want/need people to instantiate this class as it only contains static helper functions.
/// </summary>
private AuditFileHelper() { }
/// <summary>
/// Deserializes Auditfile
/// </summary>
/// <param name="filePath">The path where the file will be read</param>
/// <returns></returns>
public static Auditfile.auditfile Deserialize(string filePath)
{
using (var reader = new StreamReader(filePath))
{
XmlSerializer xs = new XmlSerializer(typeof(Auditfile.auditfile));
return (Auditfile.auditfile)xs.Deserialize(reader);
}
}
/// <summary>
/// Serializes Auditfile
/// </summary>
/// <param name="auditfile">The auditfile object to serialize</param>
/// <param name="filePath">The path where the file will be written</param>
/// <param name="compression">Optional: When true file will be compressed into Zip file. Default: false</param>
public static void Serialize(Auditfile.auditfile auditfile, string filePath, bool compression = false)
{
var xmlFile = new FileInfo(filePath);
var xml = new XmlSerializer(typeof(Auditfile.auditfile));
if (compression)
{
var zipFile = new FileInfo("SAF-T Export_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");
using (FileStream zipStream = zipFile.Open(FileMode.Create))
{
using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
{
ZipArchiveEntry entry = zip.CreateEntry(xmlFile.Name);
using (var writer = new StreamWriter(entry.Open()))
{
xml.Serialize(writer, auditfile);
}
}
}
}
else
{
using (TextWriter writer = new StreamWriter(xmlFile.Open(FileMode.Create)))
{
xml.Serialize(writer, auditfile);
}
}
}
/// <summary>
/// Generates a filename string according to spec.
/// </summary>
/// <param name="organizationNumber">The organization number</param>
/// <param name="currentFile">Optional: Current file of Multi-File export</param>
/// <param name="totalFiles">Optional: Total files in Multi-File export</param>
/// <returns>Generated filename string</returns>
public static string MakeFilename(string organizationNumber, int currentFile = 1, int totalFiles = 1)
{
StringBuilder str = new StringBuilder();
str.Append("SAF-T Cash Register_"); // Type of file
str.Append(organizationNumber.Replace(" ", "") + "_"); // Organization number/identifier, trimmed
string currentTime = DateTime.Now.ToString("yyyyMMddHHmmss"); // Date and Time in the format specified by SAF-T
str.Append(currentTime); // Append current time
if (totalFiles > 1) str.Append("_" + currentFile + "_" + totalFiles); // Current export file and total amount of export files (Multi file export)
str.Append(".xml");
return str.ToString();
}
/// <summary>
/// Generates a filename string according to spec.
/// </summary>
/// <param name="auditfile">Auditfile object to get Organization Number from</param>
/// <param name="currentFile">Optional: Current file of Multi-File export</param>
/// <param name="totalFiles">Optional: Total files in Multi-File export</param>
/// <returns>Generated filename string</returns>
public static string MakeFilename(Auditfile.auditfile auditfile, int currentFile = 1, int totalFiles = 1)
{
return MakeFilename(auditfile.company.companyIdent, currentFile, totalFiles);
}
/// <summary>
/// Generate PostalAddress object from a string
/// Example: STREETNAME 1, 1234 CITY, REGION
/// </summary>
/// <param name="str">Standard postal string, Example: STREETNAME 1, 1234 CITY, REGION</param>
/// <param name="countrycode">Optional: Defaults to CountryCode.NO</param>
/// <returns>PostalAddress object generated from string</returns>
public static Auditfile.PostalAddress PostalAddressFromString(string str, Auditfile.Countrycode countrycode = Auditfile.Countrycode.NO) // STREETNAME 1, 1234 CITY, REGION
{
var arr = str.Split(',').Select((i) => { return i.Trim(); }).ToArray<string>();
Auditfile.PostalAddress address = new Auditfile.PostalAddress();
var numIndex = arr[0].LastIndexOf(" ");
address.streetname = arr[0].Substring(0, numIndex);
address.number = arr[0].Substring(numIndex, arr[0].Length - numIndex);
numIndex = arr[1].IndexOf(" ");
address.postalCode = arr[1].Substring(0, numIndex);
address.city = arr[1].Substring(numIndex, arr[1].Length - numIndex);
address.region = arr[2];
address.country = countrycode;
return address;
}
}
}