-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaiStatementReader.cs
More file actions
204 lines (180 loc) · 8.4 KB
/
Copy pathBaiStatementReader.cs
File metadata and controls
204 lines (180 loc) · 8.4 KB
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
using Velixo.EBanking.BaiParsing;
using PX.Data;
using PX.Objects.CA;
using PX.SM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Velixo.EBanking
{
public class BaiStatementReader : IStatementReader
{
private const string OpeningLedgerTypeCode = "010";
private const string ClosingLedgerTypeCode = "015";
private TranslatedBaiFile _file;
public bool AllowsMultipleAccounts()
{
return true;
}
public void ExportToNew<T>(FileInfo aFileInfo, T current, out List<CABankTranHeader> statements) where T : CABankTransactionsImport, new()
{
UploadFileMaintenance fileGraph = PXGraph.CreateInstance<UploadFileMaintenance>();
statements = new List<CABankTranHeader>();
T graph = null;
if (current != null)
{
graph = current;
}
else
{
graph = PXGraph.CreateInstance<T>();
}
//Process file
bool updateCurrent = graph.CASetup.Current.ImportToSingleAccount ?? false;
bool allowEmptyFITID = graph.CASetup.Current.AllowEmptyFITID ?? false;
foreach (Group group in _file.Groups)
{
foreach(Account account in group.Accounts)
{
ProcessAccount(graph, _file.FileCreationDateTime, group.AsOfDateTime, account, updateCurrent, allowEmptyFITID);
statements.Add(graph.Header.Current);
}
}
//Attach uploaded file to statements that were created in Acumatica
if (fileGraph.SaveFile(aFileInfo, FileExistsAction.CreateVersion))
{
if (aFileInfo.UID.HasValue)
{
foreach (CABankTranHeader iStatement in statements)
{
if (Object.ReferenceEquals(graph.Header.Current, iStatement) == false)
{
graph.Header.Current = graph.Header.Search<CABankTranHeader.cashAccountID, CABankTranHeader.refNbr>(iStatement.CashAccountID, iStatement.RefNbr);
}
PXNoteAttribute.SetFileNotes(graph.Header.Cache, graph.Header.Current, aFileInfo.UID.Value);
graph.Save.Press();
}
}
}
}
private void ProcessAccount<T>(T graph, DateTime fileDate, DateTime asOfDate, Account account, bool updateCurrent, bool allowEmptyFITID = false)
where T : CABankTransactionsImport
{
CashAccount acct = graph.cashAccountByExtRef.Select(account.CustomerAccountNumber);
if (acct == null) throw new PXException(PX.Objects.CA.Messages.CashAccountWithExtRefNbrIsNotFoundInTheSystem, account.CustomerAccountNumber);
if (graph.CASetup.Current.IgnoreCuryCheckOnImport != true && acct.CuryID != account.CurrencyCode)
throw new PXException(PX.Objects.CA.Messages.CashAccountHasCurrencyDifferentFromOneInStatement, acct.CashAccountCD, acct.CuryID, account.CurrencyCode);
CABankTranHeader header = null;
if (updateCurrent == false || graph.Header.Current == null || graph.Header.Current.CashAccountID == null)
{
graph.Clear();
header = new CABankTranHeader();
header.CashAccountID = acct.CashAccountID;
header = graph.Header.Insert(header);
graph.Header.Current = header;
}
else
{
header = graph.Header.Current;
if (header.CashAccountID.HasValue)
{
if (header.CashAccountID != acct.CashAccountID)
{
throw new PXException(PX.Objects.CA.Messages.ImportedStatementIsMadeForAnotherAccount, acct.CashAccountCD, acct.Descr);
}
}
}
header.BankStatementFormat = "BAI2";
header.DocDate = fileDate.Date;
header.StartBalanceDate = asOfDate.Date; //A BAI2 file covers a single day.
header.EndBalanceDate = asOfDate.Date;
var openingLedgerFunds = account.FundsTypes.Where(ft => ft.TypeCode == OpeningLedgerTypeCode).FirstOrDefault();
if (openingLedgerFunds != null)
{
header.CuryBegBalance = BaiFileHelpers.GetAmount(openingLedgerFunds.Amount, account.CurrencyCode);
}
var closingLedgerFunds = account.FundsTypes.Where(ft => ft.TypeCode == ClosingLedgerTypeCode).FirstOrDefault();
if (closingLedgerFunds != null)
{
header.CuryEndBalance = BaiFileHelpers.GetAmount(closingLedgerFunds.Amount, account.CurrencyCode);
}
header = graph.Header.Update(header);
foreach (Detail statementDetail in account.Details)
{
CABankTran detail = new CABankTran();
detail.CashAccountID = acct.CashAccountID;
ProcessTransaction(detail, asOfDate, statementDetail);
detail = graph.Details.Insert(detail);
//Must be done after to avoid overwriting of debit by credit
ProcessTransactionAfterInsert(detail, statementDetail, account.CurrencyCode);
detail = graph.Details.Update(detail);
}
graph.Save.Press();
}
private static void ProcessTransaction(CABankTran detail, DateTime transactionDate, Detail statementDetail)
{
detail.TranCode = statementDetail.TypeCode;
detail.TranDate = transactionDate.Date;
detail.ExtTranID = (statementDetail.BankReferenceNumber == "NONREF") ? "" : RemoveLeadingZeros(statementDetail.BankReferenceNumber);
detail.ExtRefNbr = (statementDetail.CustomerReferenceNumber == "NONREF") ? "" : RemoveLeadingZeros(statementDetail.CustomerReferenceNumber);
detail.TranDesc = statementDetail.Text.Trim();
if(detail.TranDesc.Length > 256)
{
detail.TranDesc = detail.TranDesc.Substring(0, 256);
}
}
private static string RemoveLeadingZeros(string value)
{
//Reference numbers are zero padded. This will interfere with transaction matching in Acumatica.
return value.TrimStart(new Char[] { '0' }); ;
}
private static void ProcessTransactionAfterInsert(CABankTran detail, Detail statementDetail, string accountCurrencyCode)
{
TransactionType tt = TransactionType.NotApplicable;
int typeCode = int.Parse(statementDetail.TypeCode);
//As per Appendix B - Transaction Type Codes of the HSBC BAI2 Statement Message Implementation Guide
if(typeCode <= 399 || (typeCode >= 920 && typeCode <= 953))
{
tt = TransactionType.Credit;
}
else if((typeCode >= 400 && typeCode <= 699) || (typeCode >= 956))
{
tt = TransactionType.Debit;
}
switch (tt)
{
case TransactionType.Credit:
detail.CuryDebitAmt = BaiFileHelpers.GetAmount(statementDetail.Amount, accountCurrencyCode);
break;
case TransactionType.Debit:
detail.CuryCreditAmt = BaiFileHelpers.GetAmount(statementDetail.Amount, accountCurrencyCode);
break;
default:
throw new ApplicationException($"Unknown transaction type code: {statementDetail.TypeCode}; can't determine debit/credit");
}
}
public bool IsValidInput(byte[] aInput)
{
try
{
var parser = new BaiParser();
var bai = parser.Parse(aInput);
var translatedFile = BaiTranslator.Translate(bai);
return translatedFile.NumberOfGroups > 0;
}
catch(Exception ex)
{
PXTrace.WriteError(ex);
return false;
}
}
public void Read(byte[] aInput)
{
var parser = new BaiParser();
var bai = parser.Parse(aInput);
_file = BaiTranslator.Translate(bai);
}
}
}