-
Notifications
You must be signed in to change notification settings - Fork 4
/
BankingAccounting.cs
147 lines (135 loc) · 5.1 KB
/
BankingAccounting.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using CodeFirstWebFramework;
namespace AccountServer {
/// <summary>
/// Common functionality from Banking & Accounting
/// </summary>
public abstract class BankingAccounting : AppModule {
public BankingAccounting() {
}
public void Names() {
// When maintaining names, use the template in banking
Module = "banking";
}
public object NamesListing() {
return Database.Query("SELECT * FROM NameAddress WHERE Type = 'O' ORDER BY Name");
}
public void Name(int id) {
// When maintaining names, use the template in banking
Module = "banking";
NameAddress record = Database.Get<NameAddress>(id);
// Can only maintain Other names here
if (record.Id == null)
record.Type = "O";
else {
checkNameType(record.Type, "O");
Title += " - " + record.Name;
}
Record = record;
}
public AjaxReturn NameSave(NameAddress json) {
checkNameType(json.Type, "O");
return SaveRecord(json, true);
}
/// <summary>
/// Get an existing transfer document, or fill in a new one
/// </summary>
internal TransferDocument GetTransferDocument(int id) {
TransferDocument header = getDocument<TransferDocument>(id);
int? acct = GetParameters["acct"].IsInteger() ? Parameters.AsInt("acct") : (int?)null;
if (header.idDocument == null) {
header.DocumentTypeId = (int)DocType.Transfer;
header.DocType = DocType.Transfer.UnCamel();
header.DocumentDate = Utils.Today;
header.DocumentName = "";
header.DocumentMemo = "Money Transfer";
if (acct != null)
header.DocumentAccountId = (int)acct;
if (GetParameters["acct2"].IsInteger())
header.TransferAccountId = int.Parse(GetParameters["acct2"]);
} else {
checkDocType(header.DocumentTypeId, DocType.Transfer);
header.TransferAccountId = Database.QueryOne("SELECT AccountId FROM Journal WHERE DocumentId = " + id + " AND JournalNum = 2").AsInt("AccountId");
checkAccountIsAcctType(header.DocumentAccountId, AcctType.Bank, AcctType.CreditCard, AcctType.Investment,
AcctType.OtherAsset, AcctType.OtherLiability);
checkAccountIsAcctType(header.TransferAccountId, AcctType.Bank, AcctType.CreditCard, AcctType.Investment,
AcctType.OtherAsset, AcctType.OtherLiability);
if (acct == null)
acct = header.DocumentAccountId;
}
return header;
}
public void Transfer(int id) {
// Use template in banking
Module = "banking";
TransferDocument header = GetTransferDocument(id);
int? acct = GetParameters["acct"].IsInteger() ? Parameters.AsInt("acct") : (int?)null;
JObject record = new JObject().AddRange("header", header,
"Account", acct,
"BankAccounts", SelectBankOrStockAccounts());
if (acct != null)
nextPreviousDocument(record, "JOIN Journal ON DocumentId = idDocument WHERE AccountId = "
+ acct + " AND DocumentTypeId = " + (int)DocType.Transfer);
Record = record;
}
public AjaxReturn TransferSave(TransferDocument json) {
Database.BeginTransaction();
checkDocType(json.DocumentTypeId, DocType.Transfer);
checkAccountIsAcctType(json.DocumentAccountId, AcctType.Bank, AcctType.CreditCard, AcctType.Investment,
AcctType.OtherAsset, AcctType.OtherLiability);
checkAccountIsAcctType(json.TransferAccountId, AcctType.Bank, AcctType.CreditCard, AcctType.Investment,
AcctType.OtherAsset, AcctType.OtherLiability);
fixNameAddress(json, "O");
JObject oldDoc = getCompleteDocument(json.idDocument);
Database.Update(json);
// Transfer has 2 journals, 1 line, no VAT
Journal journal = Database.Get(new Journal() {
DocumentId = (int)json.idDocument,
JournalNum = 1
});
journal.DocumentId = (int)json.idDocument;
journal.AccountId = json.DocumentAccountId;
journal.NameAddressId = json.DocumentNameAddressId;
journal.Amount = -json.DocumentAmount;
journal.Outstanding = -json.DocumentAmount;
journal.Memo = json.DocumentMemo;
journal.JournalNum = 1;
Database.Update(journal);
journal = Database.Get(new Journal() {
DocumentId = (int)json.idDocument,
JournalNum = 2
});
journal.DocumentId = (int)json.idDocument;
journal.AccountId = (int)json.TransferAccountId;
journal.NameAddressId = json.DocumentNameAddressId;
journal.Amount = json.DocumentAmount;
journal.Outstanding = json.DocumentAmount;
journal.Memo = json.DocumentMemo;
journal.JournalNum = 2;
Database.Update(journal);
Line line = Database.Get(new Line() {
idLine = journal.idJournal
});
line.idLine = journal.idJournal;
line.LineAmount = json.DocumentAmount;
Database.Update(line);
Database.Update(json);
JObject newDoc = getCompleteDocument(json.idDocument);
Database.AuditUpdate("Document", json.idDocument, oldDoc, newDoc);
Database.Commit();
return new AjaxReturn() { message = "Transfer saved", id = json.idDocument };
}
public AjaxReturn TransferDelete(int id) {
return deleteDocument(id, DocType.Transfer);
}
public class TransferDocument : Extended_Document {
public int? TransferAccountId;
}
}
}