-
Notifications
You must be signed in to change notification settings - Fork 44
/
DepotValueEntry+CoreDataClass.swift
61 lines (55 loc) · 2.27 KB
/
DepotValueEntry+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
//
// DepotValueEntry+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(DepotValueEntry)
public class DepotValueEntry: NSManagedObject {
public class func createWithHBCIData(balance:HBCICustodyAccountBalance, context:NSManagedObjectContext) -> DepotValueEntry {
let result = NSEntityDescription.insertNewObject(forEntityName: "DepotValueEntry", into: context) as! DepotValueEntry;
result.accountNumber = balance.accountNumber;
result.bankCode = balance.bankCode;
result.date = balance.date;
if let depotValue = balance.depotValue {
result.depotValue = depotValue.value;
result.depotValueCurrency = depotValue.currency;
}
result.prepDate = balance.prepDate;
let calendar = Calendar.init(identifier: Calendar.Identifier.gregorian);
let day = calendar.component(Calendar.Component.day, from: balance.date);
let month = calendar.component(Calendar.Component.month, from: balance.date);
let year = calendar.component(Calendar.Component.year, from: balance.date);
result.day = Int32(year*10000 + month*100 + day);
for instrument in balance.instruments {
result.addToInstruments(Instrument.createWithHBCIData(instrument: instrument, context: context));
}
return result;
}
func depotChange() -> NSDecimalNumber {
var oldValue = NSDecimalNumber.zero;
if let instruments = self.instruments {
for instrument in instruments.allObjects as! [Instrument] {
if let startPrice = instrument.startPrice, let totalNumber = instrument.totalNumber {
oldValue = oldValue.adding(startPrice.multiplying(by: totalNumber));
}
}
}
if let depotValue = self.depotValue {
return depotValue.subtracting(oldValue);
} else {
return NSDecimalNumber.zero;
}
}
public override func value(forKey key: String) -> Any? {
if key.contains("depotChange") {
return self.depotChange();
}
return super.value(forKey: key);
}
}