-
Notifications
You must be signed in to change notification settings - Fork 4
/
JsonClasses.cs
727 lines (691 loc) · 17.9 KB
/
JsonClasses.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Web;
using System.IO;
using System.Reflection;
using Mustache;
using CodeFirstWebFramework;
#pragma warning disable 0649
namespace AccountServer {
[Table]
public class Account : JsonObject {
/// <summary>
/// For system accounts <see cref="Acct" />
/// </summary>
[Primary]
public int? idAccount;
/// <summary>
/// Account name. Subaccounts are stored as shown - e.g. "Payroll:Taxes".
/// </summary>
[Length(75)]
[Unique("AccountName_UNIQUE")]
public string AccountName;
[Length(75)]
public string AccountDescription;
/// <summary>
/// <see cref="AcctType" />
/// </summary>
[ForeignKey("AccountType")]
public int AccountTypeId;
/// <summary>
/// Code to allow sorting in non-alphabetical order in reports - e.g. to sort in same order as your accountant uses
/// </summary>
public string AccountCode;
/// <summary>
/// System account, user cannot edit
/// </summary>
public bool Protected;
/// <summary>
/// Used only during reconciliation
/// </summary>
public decimal? EndingBalance;
public int NextChequeNumber;
public int NextDepositNumber;
/// <summary>
/// User does not want to see it
/// </summary>
public bool HideAccount;
/// <summary>
/// For matching statement data pasted from web
/// </summary>
[Length(0)]
public string StatementFormat;
public override int? Id {
get { return idAccount; }
set { idAccount = value; }
}
}
[Table]
public class AccountType : JsonObject {
/// <summary>
/// <see cref="AcctType" />
/// </summary>
[Primary]
public int? idAccountType;
/// <summary>
/// P&L or Balance sheet report heading
/// </summary>
[DefaultValue("Other")]
public string Heading;
[Unique("Name_UNIQUE")]
public string AcctType;
/// <summary>
/// Reverse the sign in P & L and Balance sheet
/// </summary>
public bool Negate;
/// <summary>
/// True if appears in Balance sheet report
/// </summary>
public bool BalanceSheet;
public override int? Id {
get { return idAccountType; }
set { idAccountType = value; }
}
}
[Table]
public class AuditTrail : JsonObject {
[Primary]
public int? idAuditTrail;
[Field(Type = "dateTime")]
public DateTime DateChanged;
public string TableName;
public int? UserId;
/// <summary>
/// <see cref="AuditType" />
/// </summary>
public int ChangeType;
/// <summary>
/// Of the record being audited
/// </summary>
public int RecordId;
/// <summary>
/// Json data for the record
/// </summary>
[Length(0)]
public string Record;
public override int? Id {
get { return idAuditTrail; }
set { idAuditTrail = value; }
}
}
[Table]
public class Document : JsonObject {
[Primary]
[Field(Heading = "Trans no")]
public int? idDocument;
[Length(0)]
public string DocumentMemo;
/// <summary>
/// <see cref="DocType" />
/// </summary>
[ForeignKey("DocumentType")]
public int DocumentTypeId;
[Length(0)]
public string DocumentAddress;
public DateTime DocumentDate;
[Field(Heading = "Doc Id")]
public string DocumentIdentifier;
[Field(Type = "checkbox")]
[ForeignKey("User")]
public int? Authorised;
/// <summary>
/// Record of payment to HM which paid the vat in this document
/// </summary>
[Field(Type = "checkbox")]
public int? VatPaid;
public override int? Id {
get { return idDocument; }
set { idDocument = value; }
}
}
[Table]
public class DocumentType : JsonObject {
/// <summary>
/// <see cref="DocType" />
/// </summary>
[Primary]
public int? idDocumentType;
[Unique("DocumentName_UNIQUE")]
public string DocType;
/// <summary>
/// Id of Sales Ledger or Purchase Ledger account for invoices, payments, etc. Otherwise null
/// </summary>
[ForeignKey("Account")]
public int? PrimaryAccountId;
/// <summary>
/// "C" for Customer, "S" for Supplier, "O" for Other
/// </summary>
[Length(1)]
[DefaultValue("O")]
public string NameType;
/// <summary>
/// Natural document sign - e.g. -1 for deposits, 1 for cheques.
/// <see cref="AppModule.SignFor(DocType)"/>
/// </summary>
[DefaultValue("1")]
public int Sign;
public override int? Id {
get { return idDocumentType; }
set { idDocumentType = value; }
}
}
[Table]
public class Journal : JsonObject {
[Primary]
public int? idJournal;
[ForeignKey("Document")]
[Unique("Document_Num")]
public int DocumentId;
[ForeignKey("Account")]
public int AccountId;
[Length(75)]
public string Memo;
[Unique("Document_Num", 1)]
public int JournalNum;
public decimal Amount;
/// <summary>
/// Unpaid amount for invoices, etc.
/// </summary>
public decimal Outstanding;
/// <summary>
/// "X" = cleared, "*" = marked for clearing in an incomplete reconciliation
/// </summary>
[Length(1)]
public string Cleared;
[ForeignKey("NameAddress")]
public int? NameAddressId;
public override int? Id {
get { return idJournal; }
set { idJournal = value; }
}
}
[Table]
public class Line : JsonObject {
[ForeignKey("Journal")]
[Primary(AutoIncrement = false)]
public int? idLine;
public double Qty;
[ForeignKey("Product")]
public int? ProductId;
public decimal LineAmount;
[ForeignKey("VatCode")]
public int? VatCodeId;
public decimal VatRate;
public decimal VatAmount;
public override int? Id {
get { return idLine; }
set { idLine = value; }
}
}
[Table]
public class Member : JsonObject {
[Primary]
public int? idMember;
[Unique("MemberNo")]
public int MemberNo;
[ForeignKey("MemberType")]
public int MemberTypeId;
[ForeignKey("NameAddress")]
public int NameAddressId;
public string Title;
public string FirstName;
public string LastName;
public string Name {
get {
return FirstName + " " + LastName;
}
}
public decimal PaymentAmount;
public decimal AmountDue;
public override int? Id {
get { return idMember; }
set { idMember = value; }
}
}
[Table]
public class MemberType : JsonObject {
[Primary]
public int? idMemberType;
[Unique("MemberTypeName")]
public string MemberTypeName;
public decimal AnnualSubscription;
public int NumberOfPayments = 1;
public override int? Id {
get { return idMemberType; }
set { idMemberType = value; }
}
}
[View(@"SELECT * FROM Member
JOIN MemberType ON idMemberType = MemberTypeId
JOIN NameAddress ON idNameAddress = NameAddressId")]
public class Full_Member : Member {
[Field(Type = "string", Heading = "Type")]
public string MemberTypeName;
[Field(Type = "decimal")]
public decimal AnnualSubscription;
[Field(Type = "int", Visible = false)]
public int NumberOfPayments = 1;
[Length(0)]
[Writeable]
public string Address;
[Length(15)]
[Writeable]
public string PostCode;
[Writeable]
public string Telephone;
[Length(50)]
[Writeable]
[Field(Types = "email,textInput")]
public string Email;
[Writeable]
public string Contact;
[Field(Heading = "Left")]
[Writeable]
public bool Hidden;
}
[Table]
public class NameAddress : JsonObject {
[Primary]
public int? idNameAddress;
/// <summary>
/// "C" for Customer, "S" for Supplier, "O" for Other
/// </summary>
[Length(1)]
[Unique("Type_Name")]
public string Type;
[Length(75)]
[Nullable]
[Unique("Type_Name", 1)]
public string Name;
[Length(0)]
public string Address;
[Length(15)]
public string PostCode;
public string Telephone;
[Length(50)]
[Field(Types = "email,textInput")]
public string Email;
public string Contact;
public bool Hidden;
public override int? Id {
get { return idNameAddress; }
set { idNameAddress = value; }
}
}
/// <summary>
/// Cross-references Invoices to Payments at document level.
/// Implements a many-to-many relationship.
/// </summary>
[Table]
public class Payments : JsonObject {
[ForeignKey("Document")]
[Primary(AutoIncrement = false)]
public int? idPayment;
[ForeignKey("Document")]
[Primary(1, AutoIncrement = false)]
public int idPaid;
public decimal PaymentAmount;
public override int? Id {
get { return idPayment; }
set { idPayment = value; }
}
}
[Table]
public class Product : JsonObject {
[Primary]
public int? idProduct;
[Length(75)]
[Unique("Name_UNIQUE")]
public string ProductName;
[Length(75)]
public string ProductDescription;
public decimal UnitPrice;
[ForeignKey("VatCode")]
public int? VatCodeId;
[ForeignKey("Account")]
public int AccountId;
/// <summary>
/// Input/display unit - these are implemented in default.js,
/// and include D:H:M and H:M
/// </summary>
public int Unit;
public override int? Id {
get { return idProduct; }
set { idProduct = value; }
}
}
[Table]
public class Report : JsonObject {
[Primary]
public int? idReport;
[Length(75)]
[Unique("Name_Index", 1)]
public string ReportName;
[Length(25)]
public string ReportType;
/// <summary>
/// Json of report settings
/// </summary>
[Length(0)]
public string ReportSettings;
[Unique("Name_Index")]
public string ReportGroup;
[DefaultValue("0")]
public bool Chart;
public override int? Id {
get { return idReport; }
set { idReport = value; }
}
}
[Table]
public class Security : JsonObject {
[Primary]
public int? idSecurity;
[Unique("Name_UNIQUE")]
public string SecurityName;
[Unique("Ticker_UNIQUE")]
public string Ticker;
public override int? Id {
get { return idSecurity; }
set { idSecurity = value; }
}
}
[Table]
public class StockTransaction : JsonObject {
[ForeignKey("Journal")]
[Primary(AutoIncrement = false)]
public int? idStockTransaction;
[ForeignKey("Account")]
public int? ParentAccountId;
[ForeignKey("Security")]
public int SecurityId;
public double Quantity;
public double CostPer;
public double Price;
public override int? Id {
get { return idStockTransaction; }
set { idStockTransaction = value; }
}
}
[Table]
public class StockPrice : JsonObject {
[Primary(AutoIncrement = false)]
[ForeignKey("Security")]
public int? SecurityId;
[Primary(1, AutoIncrement = false)]
public DateTime Date;
public double Price;
}
[Table]
public class Schedule : JsonObject {
[Primary]
public int? idSchedule;
public DateTime ActionDate;
/// <summary>
/// <see cref="RepeatType"/>
/// </summary>
public int RepeatType;
[DefaultValue(1)]
public int RepeatFrequency;
[Length(75)]
public string Task;
/// <summary>
/// Url where to post data for posts, or to redirect to
/// </summary>
[Nullable]
public string Url;
/// <summary>
/// Json of transaction to post
/// </summary>
[Nullable]
[Length(0)]
public string Parameters;
/// <summary>
/// True if transaction has to be posted
/// </summary>
public bool Post;
public override int? Id {
get { return idSchedule; }
set { idSchedule = value; }
}
}
[Table]
public class Settings : CodeFirstWebFramework.Settings {
[ForeignKey("Account")]
public int? DefaultBankAccount;
[Length(75)]
public string CompanyName;
[Length(0)]
public string CompanyAddress;
public string CompanyPhone;
[Length(50)]
[Field(Types = "email,textInput")]
public string CompanyEmail;
public string WebSite;
[Length(25)]
public string VatRegistration;
[Length(25)]
public string CompanyNumber;
[DefaultValue("1")]
public int YearStartMonth;
public int YearStartDay;
[DefaultValue("1")]
public bool RecordVat;
[DefaultValue("0")]
public bool EnterGrossAmounts;
[DefaultValue("1")]
public bool Customers;
[DefaultValue("1")]
public bool Suppliers;
public bool RequireAuthorisation;
[DefaultValue("1")]
public bool Investments;
[DefaultValue("1")]
public bool Members;
[DefaultValue("14")]
public int TermsDays;
[DefaultValue("1")]
public int NextInvoiceNumber;
[DefaultValue("1")]
public int NextBillNumber;
[DefaultValue("1")]
public int NextJournalNumber;
[DefaultValue("smtp.gmail.com")]
public string MailServer;
[DefaultValue("587")]
public int MailPort;
[DefaultValue("1")]
public bool MailSSL;
[Length(50)]
public string MailUserName;
public string MailPassword;
public int NextNumber(DocType docType) {
switch (docType) {
case DocType.Invoice:
case DocType.CreditMemo:
return NextInvoiceNumber;
case DocType.Bill:
case DocType.Credit:
return NextBillNumber;
case DocType.GeneralJournal:
return NextJournalNumber;
default:
return 1;
}
}
public void RegisterNumber(CodeFirstWebFramework.AppModule module, int? docType, int current) {
switch (docType) {
case (int)DocType.Invoice:
case (int)DocType.CreditMemo:
registerNumber(module, ref NextInvoiceNumber, current);
break;
case (int)DocType.Bill:
case (int)DocType.Credit:
registerNumber(module, ref NextBillNumber, current);
break;
case (int)DocType.GeneralJournal:
registerNumber(module, ref NextJournalNumber, current);
break;
}
}
void registerNumber(CodeFirstWebFramework.AppModule module, ref int next, int current) {
if (current >= next) {
next = current + 1;
write(module);
}
}
public DateTime YearEnd(DateTime date) {
date = date.Date;
DateTime result = yearStart(date);
if (result <= date)
result = yearStart(result.AddMonths(13));
return result.AddDays(-1);
}
public DateTime YearStart(DateTime date) {
date = date.Date;
DateTime result = yearStart(date);
if (result > date)
result = yearStart(date.AddMonths(-12));
return result;
}
public DateTime QuarterStart(DateTime date) {
DateTime result = YearStart(date);
result = result.AddDays(1 - (int)result.Day);
for (DateTime end = result.AddMonths(3); end <= date; end = result.AddMonths(3))
result = end;
return result;
}
void write(CodeFirstWebFramework.AppModule module) {
module.Database.Update(this);
}
DateTime yearStart(DateTime date) {
int month = YearStartMonth;
if (month == 0) month = 1;
int day = YearStartDay;
// First day of the month
DateTime dayOfMonth = new DateTime(date.Year, month, 1);
if (day > 0) {
DayOfWeek dayOfWeek = (DayOfWeek)(day % 7);
// Find first dayOfWeek of this month
if (dayOfMonth.DayOfWeek > dayOfWeek) {
dayOfMonth = dayOfMonth.AddDays(7 - (int)dayOfMonth.DayOfWeek + (int)dayOfWeek);
} else {
dayOfMonth = dayOfMonth.AddDays((int)dayOfWeek - (int)dayOfMonth.DayOfWeek);
}
}
return dayOfMonth;
}
}
[Table]
public class VatCode : JsonObject {
[Primary]
public int? idVatCode;
[Length(25)]
[Unique("Name_UNIQUE")]
public string Code;
[Length(75)]
public string VatDescription;
public decimal Rate;
public override int? Id {
get { return idVatCode; }
set { idVatCode = value; }
}
}
[View(@"SELECT idDocument, DocumentMemo, DocumentTypeId, DocType, Sign, Authorised,
DocumentDate, Journal.NameAddressId AS DocumentNameAddressId, Name AS DocumentName, DocumentAddress, DocumentIdentifier,
Journal.Amount As AccountingAmount, Journal.Outstanding As AccountingOutstanding,
-Journal.Amount * DocumentType.Sign As DocumentAmount, -Journal.Outstanding * DocumentType.Sign As DocumentOutstanding,
Journal.AccountId AS DocumentAccountId, AccountName As DocumentAccountName, Journal.Cleared AS Clr,
VatJournal.Amount * DocumentType.Sign As DocumentVatAmount, VatPaid, Login AS AuthorisedBy
FROM Document
JOIN DocumentType ON idDocumentType = DocumentTypeId
JOIN Journal ON Journal.DocumentId = idDocument AND Journal.JournalNum = 1
JOIN Account ON idAccount = Journal.AccountId
JOIN NameAddress ON idNameAddress = Journal.NameAddressId
LEFT JOIN Journal AS VatJournal ON VatJournal.DocumentId = idDocument AND VatJournal.AccountId = 8
LEFT JOIN User ON idUser = Authorised
")]
public class Extended_Document : Document {
public string DocType;
public int Sign;
public int? DocumentNameAddressId;
[Length(75)]
public string DocumentName;
public decimal AccountingAmount;
public decimal AccountingOutstanding;
public decimal DocumentAmount;
public decimal DocumentOutstanding;
public int DocumentAccountId;
[Length(75)]
public string DocumentAccountName;
[Length(1)]
public string Clr;
public decimal? DocumentVatAmount;
public string AuthorisedBy;
}
[View(@"SELECT Line.*, Product.ProductName, Product.UnitPrice, VatCode.Code, VatCode.VatDescription,
Journal.DocumentId, Journal.JournalNum, Journal.Memo, Journal.AccountId, Account.AccountName, Account.AccountDescription
FROM Line
JOIN Product ON Product.idProduct = Line.ProductId
LEFT JOIN VatCode ON VatCode.idVatCode = Line.VatCodeId
JOIN Journal ON Journal.idJournal = Line.idLine
JOIN Account ON Account.idAccount = Journal.AccountId
")]
public class Extended_Line : Line {
[Length(75)]
public string ProductName;
public decimal UnitPrice;
[Length(25)]
public string Code;
[Length(75)]
public string VatDescription;
public int DocumentId;
public int JournalNum;
[Length(75)]
public string Memo;
public int AccountId;
[Length(75)]
public string AccountName;
[Length(75)]
public string AccountDescription;
public override int? Id {
get { return idLine; }
set { idLine = value; }
}
}
[View(@"SELECT Extended_Document.*, CASE DocumentAccountId
WHEN 1 THEN -1
WHEN 2 THEN 1
ELSE Sign
END AS VatType,
Memo,Line.VatCodeId, Line.VatRate, SUM(Line.VatAmount) VatAmount, SUM(Line.LineAmount) AS LineAmount
FROM Extended_Document
JOIN Journal ON IdDocument = DocumentId
JOIN Line ON idLine = idJournal
WHERE DocumentTypeId IN (1, 3, 4, 6)
OR Line.VatCodeId IS NOT NULL
GROUP BY idDocument, Line.VatCodeId, Line.VatRate
")]
public class Vat_Journal : Extended_Document {
[Length(2)]
public int VatType;
[Length(75)]
public string Memo;
public int? VatCodeId;
public decimal VatRate;
public decimal? VatAmount;
public decimal? LineAmount;
public override int? Id {
get { return idDocument; }
set { idDocument = value; }
}
}
#pragma warning restore 0649
}