-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaydayJournalUtils.cs
388 lines (336 loc) · 15.8 KB
/
PaydayJournalUtils.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using OfficeOpenXml;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace JournalGenerator.Utils
{
internal class PaydayJournalUtils
{
internal static (byte[] outputBytes, int journalEntriesCount) GenerateJournalFromIslandsbankiForeignPayments(byte[] fileBytes)
{
string json = @"{
'settings':[{
'senderTitle':'AIRBNB PAYMENTS LUXEMBOURG S.A.',
'value': 'Airbnb Experience',
'commission': '0.2',
'accountNumber': '2381',
'bankCommission': '680'
},
{
'senderTitle':'GetYourGuide Deutsch',
'value': 'GetYourGuide',
'commission': '0.3',
'accountNumber': '2382',
'bankCommission': '680'
},
{
'senderTitle':'VIATOR LIMITED 7',
'value': 'Viator',
'commission': '0.255',
'accountNumber': '2383',
'bankCommission': '680'
},
{
'senderTitle':'TRUST MY TRAVEL LIMITED',
'value': 'Bókun',
'commission': '0.015',
'accountNumber': '2384',
'bankCommission': '680'
},
{
'senderTitle':'TRUST MY TRAVEL LIMITED THE C',
'value': 'Bókun',
'commission': '0.015',
'accountNumber': '2384',
'bankCommission': '0'
},
{
'senderTitle':'Booknordics As',
'value': 'BookNordics AS',
'commission': '0.2',
'accountNumber': '2385',
'bankCommission': '680'
},
{
'senderTitle':'IPS EHF',
'value': 'Iceland Pro Services',
'commission': '0.15',
'accountNumber': '2385',
'bankCommission': '680'
}]
}";
PaydayJournalSettings settings = JsonConvert.DeserializeObject<PaydayJournalSettings>(json);
List<PaydayJournalEntry> journalEntries = new();
// Import
using (MemoryStream memStream = new(fileBytes))
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
ExcelPackage excel = new(memStream);
for (int worksheetNr = 0; worksheetNr < excel.Workbook.Worksheets.Count; worksheetNr++)
{
ExcelWorksheet worksheet = excel.Workbook.Worksheets[worksheetNr];
int? postfix = null;
bool isLast = false;
for (int rowNumber = 2; rowNumber <= worksheet.Dimension.Rows; rowNumber++)
{
// Find start of next invoice
string dateString = worksheet.GetValue<string>(rowNumber, 1);
string payer = worksheet.GetValue<string>(rowNumber, 2);
string reference = worksheet.GetValue<string>(rowNumber, 3);
string currencySymbol = worksheet.GetValue<string>(rowNumber, 4);
string sourceAmountString = worksheet.GetValue<string>(rowNumber, 5);
string destinationAmountString = worksheet.GetValue<string>(rowNumber, 6);
string status = worksheet.GetValue<string>(rowNumber, 7);
if (string.IsNullOrWhiteSpace(dateString) || string.IsNullOrWhiteSpace(payer) || string.IsNullOrWhiteSpace(destinationAmountString))
{
break;
}
DateTime date = DateTime.ParseExact(dateString, "dd.MM.yy", CultureInfo.InvariantCulture, DateTimeStyles.None);
int day = date.Day;
int month = date.Month;
int year = date.Year;
string ledgerEntryDate = $"{day}.{month}.{year}";
decimal destinationAmount = decimal.Parse(destinationAmountString.Replace(".", ""), CultureInfo.InvariantCulture);
PaydayJournalSetting paydayJournalSetting = settings.Settings.FirstOrDefault(x => x.SenderTitle.Equals(payer, StringComparison.InvariantCultureIgnoreCase));
if (paydayJournalSetting == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: No settings found for {payer}");
Console.ResetColor();
continue;
}
// Check if we need to add a day apendix
string nextLineDateString = worksheet.GetValue<string>(rowNumber + 1, 1);
if (!string.IsNullOrWhiteSpace(nextLineDateString))
{
DateTime nextLineDate = DateTime.ParseExact(nextLineDateString, "dd.MM.yy", CultureInfo.InvariantCulture, DateTimeStyles.None);
int nextLineDay = nextLineDate.Day;
int nextLineMonth = nextLineDate.Month;
int nextLineYear = nextLineDate.Year;
string nextLinePayer = worksheet.GetValue<string>(rowNumber + 1, 2);
PaydayJournalSetting nextLinePaydayJournalSetting = settings.Settings.FirstOrDefault(x => x.SenderTitle.Equals(nextLinePayer, StringComparison.InvariantCultureIgnoreCase));
if (day == nextLineDay && month == nextLineMonth && year == nextLineYear && !string.IsNullOrWhiteSpace(paydayJournalSetting.Value) && paydayJournalSetting.Value == nextLinePaydayJournalSetting.Value)
{
postfix = postfix == null ? 1 : postfix + 1;
}
else if (postfix != null)
{
isLast = true;
postfix = postfix + 1;
}
else
{
postfix = null;
}
}
else
{
postfix = null;
}
string description = $"{paydayJournalSetting.Value} - {day}.{month}.{year}" + (postfix != null && postfix.HasValue ? $" - {postfix.Value.ToString()}" : "");
if (isLast)
{
isLast = false;
postfix = null;
}
decimal amount = Math.Round(destinationAmount, 0, MidpointRounding.AwayFromZero);
decimal amountInclVat = Math.Round(paydayJournalSetting.Commission != 1 ? (destinationAmount / (1 - paydayJournalSetting.Commission)) : 0, MidpointRounding.AwayFromZero);
decimal vat = amountInclVat - amount;
journalEntries.Add(new PaydayJournalEntry
{
EntryNr = rowNumber - 1,
Date = ledgerEntryDate,
DateTimeValue = new DateTime(year, month, day),
Description = description,
Type = 1,
Key = "1110",
Amount = amountInclVat * -1, // Credit
VAT = "11",
//Reference = reference,
ReceiverKey = string.Empty
});
journalEntries.Add(new PaydayJournalEntry
{
EntryNr = rowNumber - 1,
Date = ledgerEntryDate,
Description = description,
Type = 1,
Key = paydayJournalSetting.AccountNumber,
Amount = vat,
VAT = "",
Reference = reference,
ReceiverKey = string.Empty
});
journalEntries.Add(new PaydayJournalEntry
{
EntryNr = rowNumber - 1,
Date = ledgerEntryDate,
Description = description,
Type = 1,
Key = "3200",
Amount = amount,
VAT = "",
Reference = reference,
ReceiverKey = string.Empty
});
journalEntries.Add(new PaydayJournalEntry
{
EntryNr = rowNumber - 1,
Date = ledgerEntryDate,
Description = description,
Type = 1,
Key = "2990",
Amount = paydayJournalSetting.BankCommission > 0 ? paydayJournalSetting.BankCommission : 0,
VAT = "",
Reference = reference,
ReceiverKey = string.Empty
});
journalEntries.Add(new PaydayJournalEntry
{
EntryNr = rowNumber - 1,
Date = ledgerEntryDate,
Description = description,
Type = 1,
Key = "3200",
Amount = paydayJournalSetting.BankCommission > 0 ? paydayJournalSetting.BankCommission * -1 : 0, // Credit
VAT = "",
Reference = reference,
ReceiverKey = string.Empty
});
}
}
}
// Export
if (journalEntries.Count > 0)
{
journalEntries = (from je in journalEntries
orderby je.EntryNr descending
select je).ToList();
}
using MemoryStream stream = new();
using (ExcelPackage package = new(stream))
{
int rowNumber = 1;
int numberOfColumns = 9;
int sheetNr = 1;
int entryNr = 1;
foreach (var batch in journalEntries.Chunk(9 * 5)) // 9 Entries, each 5 lines
{
var w = package.Workbook.Worksheets.Add($"Sheet{sheetNr}");
sheetNr++;
w.Cells[rowNumber, 1].Value = "Færsla nr.";
w.Cells[rowNumber, 2].Value = "Dags.";
w.Cells[rowNumber, 3].Value = "Lýsing";
w.Cells[rowNumber, 4].Value = "Tegund (1 = Fjárhagur, 2 = Viðskiptavinur, 3 = Lánardrottinn)";
w.Cells[rowNumber, 5].Value = "Lykill (fjárhagslykill eða kennitala)";
w.Cells[rowNumber, 6].Value = "Fjárhæð m/vsk";
w.Cells[rowNumber, 7].Value = "VSK (0, 11, 24) - tómt engin vsk";
w.Cells[rowNumber, 8].Value = "Tilvísun";
w.Cells[rowNumber, 9].Value = "Mótlykill";
foreach (var subBatch in batch.Chunk(5))
{
foreach (var journalEntry in subBatch)
{
if (journalEntry.Amount != 0)
{
rowNumber++;
w.Cells[rowNumber, 1].Value = entryNr;
w.Cells[rowNumber, 2].Value = journalEntry.Date;
w.Cells[rowNumber, 3].Value = journalEntry.Description;
w.Cells[rowNumber, 4].Value = journalEntry.Type;
w.Cells[rowNumber, 5].Value = journalEntry.Key;
w.Cells[rowNumber, 6].Value = journalEntry.Amount;
w.Cells[rowNumber, 7].Value = journalEntry.VAT;
//w.Cells[rowNumber, 8].Value = journalEntry.Reference;
w.Cells[rowNumber, 9].Value = journalEntry.ReceiverKey;
}
}
entryNr++;
}
w.Cells.AutoFitColumns(0); // Autofit columns for all cells
rowNumber = 1; // reset
}
package.Workbook.Properties.Title = "Journal " + DateTime.Now.ToString("yyyyMMdd");
package.Save();
}
return (stream.ToArray(), journalEntries.Count / 5);
}
}
public partial class PaydayJournalEntry
{
public int EntryNr { get; set; }
public string Date { get; set; }
public string Description { get; set; }
public int Type { get; set; }
public string Key { get; set; }
public Decimal Amount { get; set; }
public string VAT { get; set; }
public string Reference { get; set; }
public string ReceiverKey { get; set; }
public DateTime DateTimeValue { get; set; }
}
public partial class PaydayJournalSettings
{
[JsonProperty("settings")]
public List<PaydayJournalSetting> Settings { get; set; }
}
public partial class PaydayJournalSetting
{
[JsonProperty("senderTitle")]
public string SenderTitle { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("commission")]
public decimal Commission { get; set; }
[JsonProperty("accountNumber")]
public string AccountNumber { get; set; }
[JsonProperty("bankCommission")]
public decimal BankCommission { get; set; }
}
public partial class Temperatures
{
public static Temperatures FromJson(string json) => JsonConvert.DeserializeObject<Temperatures>(json, Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Temperatures self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
long l;
if (Int64.TryParse(value, out l))
{
return l;
}
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (long)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
}
}