1 module konnexengine.money.blueprint;
2 
3 import std.conv: to;
4 import std.uuid: UUID, sha1UUID;
5 import std.datetime: SysTime, Clock, DateTime;
6 import std.format;
7 
8 import money;
9 
10 class Amount 
11 {
12 	import money;
13 	
14 	this(string symbol, double amount)
15 	{
16 		this.timestamp = Clock.currTime();
17 		auto payload = "" ~ symbol ~ amount.to!string;
18 		DateTime ts = cast(DateTime)timestamp;
19 		this.id = sha1UUID(payload, sha1UUID(payload ~ ts.toISOExtString()));
20 		this.symbol = symbol;
21 		this.amount = amount;
22 	}
23 
24 	UUID id;
25 	string symbol;
26 	double amount;
27 	SysTime timestamp;
28 }
29 
30 class Currency
31 {
32 	this (string symbol, string name, ubyte decimals) {
33 		this.id = sha1UUID(symbol, sha1UUID("name: " ~ name));
34 		this.symbol = symbol;
35 		this.name = name;
36 		this.decimals = decimals;
37 	}
38 	UUID id;
39 	string symbol;
40 	string name;
41 	ubyte decimals;
42 }
43 
44 /// `createAmountIn(T)(T t, double amount)`: `template function returns Amount`
45 Amount createAmountIn(T)(T t, double amount) {
46 	return new Amount(t.symbol, amount);
47 }
48 ///
49 unittest {
50 	auto GBP = new Currency("GBP", "Great British Pound", 2);
51 	auto amount = createAmountIn!Currency(GBP, 1000);
52 	auto ts = cast(DateTime) amount.timestamp;
53 	
54 	assert(is(typeof(GBP) == Currency));
55 	assert(is(typeof(amount) == Amount));
56 }
57 
58 /** `createMoneyIn (T)(T t)` 
59 ```d
60 template function returns currency
61 ```
62  */
63 // string createMoneyIn (T)(T t)
64 // {
65 // 	alias CUR = currency!(t.symbol);
66 // 	logInfo(format("%.2f", CUR(t.amount)));
67 // 	return format("%.2f", CUR(t.amount));
68 // }
69 ///
70 unittest {
71 	alias GBP = currency!("GBP");
72 	// auto a = createAmountIn!Currency(GBP, 1000);
73 	// auto money = createMoneyIn!Amount(a);
74 	// assert(is(typeof(money) == currency));
75 	// assert(is(typeof(a) == Amount));
76 	// assert(is(typeof(GBP) == currency));
77 }