-
Notifications
You must be signed in to change notification settings - Fork 44
/
Instrument+CoreDataClass.swift
83 lines (74 loc) · 3.13 KB
/
Instrument+CoreDataClass.swift
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
//
// Instrument+CoreDataClass.swift
// Pecunia
//
// Created by Frank Emminghaus on 25.05.21.
// Copyright © 2021 Frank Emminghaus. All rights reserved.
//
//
import Foundation
import CoreData
import HBCI4Swift
@objc(Instrument)
public class Instrument: NSManagedObject {
public class func createWithHBCIData(instrument: HBCICustodyAccountBalance.FinancialInstrument, context:NSManagedObjectContext) -> Instrument {
let result = NSEntityDescription.insertNewObject(forEntityName: "Instrument", into: context) as! Instrument;
result.isin = instrument.isin;
result.accruedInterestValue = instrument.accruedInterestValue?.value;
result.accruedInterestValueCurrency = instrument.accruedInterestValue?.currency;
result.currentPrice = instrument.currentPrice?.value;
result.currentPriceCurrency = instrument.currentPrice?.currency;
result.depotCurrency = instrument.depotCurrency;
result.depotValue = instrument.depotValue?.value;
result.depotValueCurrency = instrument.depotValue?.currency;
result.interestRate = instrument.interestRate;
result.name = instrument.name;
result.priceDate = instrument.priceDate;
result.priceLocation = instrument.priceLocation;
result.startPrice = instrument.startPrice?.value;
result.startPriceCurrency = instrument.startPrice?.currency;
result.totalNumber = instrument.totalNumber;
result.totalNumberType = instrument.numberType.rawValue;
result.wkn = instrument.wkn;
for balance in instrument.balances {
let bal = InstrumentBalance.createWithHBCIData(balance: balance, context: context);
result.addToBalances(bal);
}
return result;
}
public override func value(forKey key: String) -> Any? {
if key.contains("valueChange") {
return self.valueChange();
}
if key.contains("percentChange") {
return self.percentChange();
}
return super.value(forKey: key);
}
func valueChange() -> NSDecimalNumber {
if self.currentPrice == nil || self.startPrice == nil || self.totalNumber == nil {
return NSDecimalNumber.zero;
}
return self.currentPrice!.subtracting(self.startPrice!).multiplying(by: self.totalNumber!);
}
func percentChange() ->NSDecimalNumber {
if self.startPrice == nil || self.currentPrice == nil {
return NSDecimalNumber.zero;
}
return self.currentPrice!.subtracting(self.startPrice!).dividing(by: self.startPrice!).multiplying(byPowerOf10: 2);
}
/*
var valueChange: NSDecimalNumber {
if self.currentPrice == nil || self.startPrice == nil || self.totalNumber == nil {
return NSDecimalNumber.zero;
}
return self.currentPrice!.subtracting(self.startPrice!).multiplying(by: self.totalNumber!);
}
var percentChange: NSDecimalNumber {
if self.startPrice == nil {
return NSDecimalNumber.zero;
}
return self.valueChange.dividing(by: self.startPrice!).multiplying(byPowerOf10: 2);
}
*/
}