-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnc_commodity_utilities.py
486 lines (325 loc) · 16 KB
/
gnc_commodity_utilities.py
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
import pdb
import gnucash
from gnucash import GNC_DENOM_AUTO, GNC_HOW_RND_ROUND
from gnucash.gnucash_core_c import GNC_HOW_DENOM_SIGFIG
#import gnucash_ext
import sw_app_utils
import engine_ctypes
import gnucash_log
# we may not import gnc_report_utilities in here!!
# can get circular imports
class ExchangeCost(object):
def __init__ (self, report_commodity, end_date):
pass
#for exvl in
def make_alist (self, e):
exch_val = e[1][0].total.div(e[1][1].total, GNC_DENOM_AUTO, GNC_HOW_DENOM_SIGFIG | 8*256 | GNC_HOW_RND_ROUND)
newlst = [ e[0], exch_val ]
def get_all_commodity_splits (self, currency_accounts, end_date_tp):
retval = match_commodity_splits(currency_accounts, end_date_tp, None)
def exchange_cost_totals (self, report_commodity, end_date):
# ;; sumlist: a multilevel alist. Each element has a commodity
# ;; as key, and another alist as a value. The value-alist's
# ;; elements consist of a commodity as a key, and a pair of two
# ;; value-collectors as value, e.g. with only one (the report-)
# ;; commodity DEM in the outer alist: ( {DEM ( [USD (400 .
# ;; 1000)] [FRF (300 . 100)] ) } ) where DEM,USD,FRF are
# ;; <gnc:commodity> and the numbers are a numeric-collector
# ;; which in turn store a <gnc:numeric>. In the example, USD
# ;; 400 were bought for an amount of DEM 1000, FRF 300 were
# ;; bought for DEM 100. The reason for the outer alist is that
# ;; there might be commodity transactions which do not involve
# ;; the report-commodity, but which can still be calculated
# ;; after *all* transactions are processed.
curr_accts = sw_app_utils.get_current_root_account().get_descendants_sorted()
sumlist = [ [report_commodity, [] ] ]
if len(curr_accts) > 0:
all_splts = []
for acct in curr_accts:
pass
def exchange_cost_totals (self, report_commodity, end_date):
pass
class ExchangeFn(object):
def __init__ (self, date=None):
self.date = date
self.run = self.lambda_exchange_fn
class AverageCostFn(ExchangeFn):
def __init__ (self, date, exchange_alist=None):
super(AverageCostFn,self).__init__(date)
self.exchange_alist = exchange_alist
def exchange_fn (self, foreign, domestic):
# the arguments seem to be currency commodities
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
# not implemented
pdb.set_trace()
def lambda_exchange_fn (self, foreign, domestic):
return self.exchange_fn(foreign, domestic)
class WeightedAverageFn(ExchangeFn):
def __init__ (self, date, exchange_alist=None):
super(WeightedAverageFn,self).__init__(date)
self.exchange_alist = exchange_alist
def exchange_fn (self, foreign, domestic):
# not implemented
pdb.set_trace()
def lambda_exchange_fn (self, foreign, domestic):
return self.exchange_fn(foreign, domestic)
class PriceDBLatestFn(ExchangeFn):
def __init__ (self, date, exchange_alist=None):
super(PriceDBLatestFn,self).__init__(date)
self.exchange_alist = exchange_alist
def lambda_exchange_fn (self, foreign, domestic):
#pdb.set_trace()
return exchange_by_pricedb_latest(foreign, domestic)
class PriceDBNearestFn(ExchangeFn):
def __init__ (self, date, exchange_alist=None):
super(PriceDBNearestFn,self).__init__(date)
self.exchange_alist = exchange_alist
def lambda_exchange_fn (self, foreign, domestic):
#pdb.set_trace()
return exchange_by_pricedb_nearest(foreign, domestic, self.date)
class ExchangeTimeFn(object):
def __init__ (self, date):
self.date = date
self.run = self.lambda_exchange_fn
class AverageExchangeTimeFn(ExchangeTimeFn):
def __init__ (self, date, exchange_alist=None):
super(AverageExchangeTimeFn,self).__init__(date)
self.exchange_alist = exchange_alist
def exchange_fn (self, foreign, domestic):
# the arguments seem to be currency commodities
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
self.exchange_cost = ExchangeCost()
# not fully implemented
pdb.set_trace()
def lambda_exchange_fn (self, foreign, domestic, date):
self.exchange_fn(foreign,domestic)
class WeightedAverageTimeFn(ExchangeTimeFn):
def __init__ (self, date, exchange_alist=None):
super(WeightedAverageTimeFn,self).__init__(date)
self.exchange_alist = exchange_alist
def lambda_exchange_fn (self, foreign, domestic, date):
# the arguments seem to be currency commodities
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
# not implemented
pdb.set_trace()
class ActualTransactionsTimeFn(ExchangeTimeFn):
def __init__ (self, date, exchange_alist=None):
super(ActualTransactionsTimeFn,self).__init__(date)
self.exchange_alist = exchange_alist
def lambda_exchange_fn (self, foreign, domestic, date):
# the arguments seem to be currency commodities
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
# not implemented
pdb.set_trace()
class PriceDBLatestTimeFn(ExchangeTimeFn):
def __init__ (self, date, exchange_alist=None):
super(PriceDBLatestTimeFn,self).__init__(date)
self.exchange_alist = exchange_alist
def lambda_exchange_fn (self, foreign, domestic, date):
# the arguments seem to be currency commodities
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
return exchange_by_pricedb_latest(foreign, domestic)
class PriceDBNearestTimeFn(ExchangeTimeFn):
def __init__ (self, date, exchange_alist=None):
super(PriceDBNearestTimeFn,self).__init__(date)
self.exchange_alist = exchange_alist
def lambda_exchange_fn (self, foreign, domestic, date):
# the arguments seem to be currency commodities
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
return exchange_by_pricedb_nearest(foreign, domestic, date)
def exchange_by_pricedb_latest (foreign, domestic):
# ;; This is another ready-to-use function for calculation of exchange
# ;; rates. (Note that this is already the function itself. It doesn't
# ;; return a function as opposed to make-exchange-function.) It takes
# ;; the <gnc-monetary> 'foreign' amount and the <gnc:commodity*>
# ;; 'domestic' commodity. It exchanges the amount into the domestic
# ;; currency, using the latest price from the pricedb. The function
# ;; returns a <gnc-monetary>.
#pdb.set_trace()
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
dommon = None
if isinstance(foreign,GncMonetary):
dommon = exchange_by_euro(foreign, domestic, None)
if dommon == None:
dommon = exchange_if_same(foreign, domestic)
if dommon == None:
#pdb.set_trace()
# it appears this may return a 0 price if doesnt find one
prcdb = sw_app_utils.get_current_book().get_price_db()
nrval = prcdb.convert_balance_latest_price( \
foreign.amount,foreign.commodity,domestic)
dommon = GncMonetary(domestic,nrval)
else:
# I think we need to fail here - dont know what scheme does not fail
raise TypeError("Needed GncMonetary type - provided type %s"%type(foreign))
return dommon
def exchange_by_pricedb_nearest (foreign, domestic, date):
# ;; Yet another ready-to-use function for calculation of exchange
# ;; rates. (Note that this is already the function itself. It doesn't
# ;; return a function as opposed to make-exchange-function.) It takes
# ;; the <gnc-monetary> 'foreign' amount, the <gnc:commodity*>
# ;; 'domestic' commodity *and* a <gnc:time-pair> 'date'. It exchanges
# ;; the amount into the domestic currency, using a price from the
# ;; pricedb according to the given date. The function returns a
# ;; <gnc-monetary>.
#pdb.set_trace()
gnucash_log.PDEBUG("python","foreign: %s"%str(foreign))
gnucash_log.PDEBUG("python","domestic: %s"%domestic.get_printname())
dommon = None
if isinstance(foreign,GncMonetary) and date != None:
dommon = exchange_by_euro(foreign, domestic, date)
if dommon == None:
dommon = exchange_if_same(foreign, domestic)
if dommon == None:
#pdb.set_trace()
# it appears this may return a 0 price if doesnt find one
prcdb = sw_app_utils.get_current_book().get_price_db()
nrval = prcdb.convert_balance_nearest_price( \
foreign.amount,foreign.commodity,domestic,date)
dommon = GncMonetary(domestic,nrval)
else:
# I think we need to fail here - dont know what scheme does not fail
raise TypeError("Needed GncMonetary type - provided type %s"%type(foreign))
return dommon
def match_commodity_splits (currency_accounts, end_date_tp, commodity=None):
# ;; Returns a list of all splits in the 'currency-accounts' up to
# ;; 'end-date-tp' which have two different commodities involved, one of
# ;; which is equivalent to 'commodity' (the latter constraint only if
# ;; 'commodity' != #f ).
#pdb.set_trace()
splits = []
curbook = sw_app_utils.get_current_book()
qry = gnucash.Query.CreateFor(curbook.GNC_ID_SPLIT)
qry.set_book(sw_app_utils.get_current_book())
set_match_non_voids_only(qry,sw_app_utils.get_current_book())
#qry.AddAccountMatch(currency_accounts,qry.QOF_GUID_MATCH_ANY,gnucash.QOF_QUERY_AND)
#qry.AddDateMatchTT(False,end_date_tp,True,end_date_tp,gnucash.QOF_QUERY_AND)
#engine_ctypes.AddAccountMatch(qry,currency_accounts,qry.QOF_GUID_MATCH_ANY,gnucash.QOF_QUERY_AND)
if len(currency_accounts) > 1: pdb.set_trace()
engine_ctypes.AddSingleAccountMatch(qry,currency_accounts[0],gnucash.QOF_QUERY_AND)
engine_ctypes.AddDateMatchTT(qry,False,end_date_tp,True,end_date_tp,gnucash.QOF_QUERY_AND)
# ;; Get the query result, i.e. all splits in currency
# ;; accounts.
spltlst = qry.Run(gnucash.Split)
#print("spltlst",spltlst)
# ;; Filter such that we get only those splits
# ;; which have two *different* commodities
# ;; involved.
def lambda_filter (s):
#print("inside lambda filter")
#print(dir(s))
#pdb.set_trace()
trans_comm = s.GetParent().GetCurrency()
acc_comm = s.GetAccount().GetCommodity()
if not engine_ctypes.CommodityEquiv(trans_comm,acc_comm) and \
(commodity == None or engine_ctypes.CommodityEquiv(commodity,trans_comm) or engine_ctypes.CommodityEquiv(commodity,acc_comm)):
return s
# if we dont return value what happens
# (isnt there a default return of None??)
#pdb.set_trace()
newlst = map(lambda_filter, spltlst)
#pdb.set_trace()
return newlst
def match_commodity_splits_sorted (currency_accounts, end_date_tp, commodity=None):
newsplits = match_commodity_splits(currency_accounts, end_date_tp, commodity)
# need to sort by date
# try makeing list of dates then sorting - using a cmp function likely slower
srtlst = [ (x.GetParent().RetDatePosted(), x) for x in newsplits ]
srtlst.sort()
#if len(srtlst) > 0: pdb.set_trace()
#print(srtlst)
return [ x[1] for x in srtlst ]
def set_match_non_voids_only (query, book):
#pdb.set_trace()
tmpqry = query.CreateFor(book.GNC_ID_SPLIT)
tmpqry.set_book(book)
#tmpqry.AddClearedMatch(query.CLEARED_VOIDED,gnucash.QOF_QUERY_AND)
engine_ctypes.AddClearedMatch(tmpqry,query.CLEARED_VOIDED,gnucash.QOF_QUERY_AND)
invqry = tmpqry.invert()
query.merge_in_place(invqry,gnucash.QOF_QUERY_AND)
# do we need these??
#invqry.destroy()
#tmpqry.destroy()
def exchange_if_same (foreign, domestic):
# ;; A trivial exchange function - if the "foreign" monetary amount
# ;; and the domestic currency are the same, return the foreign
# ;; amount unchanged, otherwise return 0
# ;; WARNING: many uses of exchange functions assume that the function
# ;; will return a valid <gnc:monetary>. However, this function returns
# ;; #f if the commodities don't match. Therefore, if you use this
# ;; function in a mixed commodity context, stuff will probably crash.
#pdb.set_trace()
if foreign.commodity.equiv(domestic):
return foreign
return None
def exchange_by_euro (foreign, domestic, date):
#pdb.set_trace()
if sw_app_utils.is_euro_currency(domestic) and \
sw_app_utils.is_euro_currency(foreign.commodity):
euro_val = convert_to_euro(foreign.commodity, foreign.amount)
dom_val = convert_from_euro(domestic)
dommon = GncMonetary(domestic,dom_val)
return dommon
return None
def case_exchange_fn (source_option, report_currency, to_date_tp):
#pdb.set_trace()
# this is really horrid - the primary function of this is to store the date!!
# these functions are not passed a date when called but use the stored value
if source_option == 'average-cost':
exchange_fn = AverageCostFn(to_date_tp)
elif source_option == 'weighted-average':
exchange_fn = WeightedAverageFn(to_date_tp)
elif source_option == 'pricedb-latest':
exchange_fn = PriceDBLatestFn(to_date_tp)
elif source_option == 'pricedb-nearest':
exchange_fn = PriceDBNearestFn(to_date_tp)
return exchange_fn
def case_exchange_time_fn (source_option, report_currency, commodity_list, to_date_tp, start_percent, delta_percent):
#pdb.set_trace()
# this also seems to store the date even though all functions when called
# are passed the date
if source_option == 'average-cost':
exchange_fn = AverageExchangeTimeFn(to_date_tp)
elif source_option == 'weighted-average':
exchange_fn = WeightedAverageTimeFn(to_date_tp)
elif source_option == 'actual-transactions':
exchange_fn = ActualTransactionsTimeFn(to_date_tp)
elif source_option == 'pricedb-latest':
exchange_fn = PriceDBLatestTimeFn(to_date_tp)
elif source_option == 'pricedb-nearest':
exchange_fn = PriceDBNearestTimeFn(to_date_tp)
return exchange_fn
class GncMonetary(object):
def __init__ (self, commodity, amount):
if isinstance(amount,gnucash.GncNumeric):
self.commodity = commodity
self.amount = amount
else:
#warn "wrong arguments for gnc:make-gnc-monetary: " c a)
print("wrong arguments for gnc:make-gnc-monetary: %s %s"%(commodity, amount))
pdb.set_trace()
raise TypeError("wrong arguments for gnc:make-gnc-monetary: %s %s"%(commodity, amount))
# this or a class method??
def neg (self):
newgncmon = self.__class__(self.commodity,self.amount.neg())
return newgncmon
# this is defined in html-style-info.scm
# (gnc:default-html-gnc-monetary-renderer - which calls xaccPrintAmount)
# where should this go???
def to_currency_string (self):
#pdb.set_trace()
prtinfo = sw_app_utils.CommodityPrintInfo(self.commodity,True)
#print("to_currency_string", self.commodity.get_mnemonic(), prtinfo.use_separators, prtinfo.use_symbol, prtinfo.use_locale, prtinfo.monetary, prtinfo.force_fit, prtinfo.round)
prtstr = sw_app_utils.PrintAmount(self.amount,prtinfo)
#try:
# jnkstr = prtstr.decode('utf-8')
#except Exception as errexc:
# pdb.set_trace()
return prtstr