Skip to content

Commit

Permalink
Merge 2003440 into 3a98225
Browse files Browse the repository at this point in the history
  • Loading branch information
MariteSomEnergia committed May 25, 2020
2 parents 3a98225 + 2003440 commit 8984fbb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
23 changes: 23 additions & 0 deletions som_generationkwh/investment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,29 @@ def error(message):

return invoice_id, errors

def get_stats_investment_generation(self, cursor, uid, context=None):
"""
Returns a list of dict with investment GenerationKwh statistics:
res: {'account': account code
'socis': Number of 'socis' with Generation
'amount': Total investment in Generation without amortizations}
params: No params
"""

result = []

if not context:
context = {}

socis_ids = self.search(cursor, uid, [('emission_id.type', '=', 'genkwh'), ('last_effective_date', '=', False)])
n_socis = len(set(socis_ids))
shares_data = self.read(cursor, uid, socis_ids, ['nshares'])
amount = sum([share_data['nshares'] for share_data in shares_data]) * 100

result.append({'amount': amount,
'socis': n_socis})
return result

class InvestmentProvider(ErpWrapper):

def items(self, member=None, start=None, end=None):
Expand Down
49 changes: 49 additions & 0 deletions som_generationkwh/tests/investment_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1821,4 +1821,53 @@ def test__send_emails_to_investors_with_savings_in_year__when_manyMembers(self):
ret_value = self.Soci.send_emails_to_investors_with_savings_in_year(cursor, uid, year=2020)
self.assertEqual(ret_value, len(investments))

def test__get_stats_investment_generation__when_last_effective_date(self):
"""
Check get_stats_investment_generation when some investements with last_effective_date
"""

with Transaction().start(self.database) as txn:
cursor = txn.cursor
uid = txn.user

inv_ids = self.Investment.search(cursor, uid, [('emission_id.type', '=', 'genkwh')])

with_last_effective_date = len(inv_ids) / 2 if inv_ids else 0

with_last_effective_date_ids = inv_ids[:with_last_effective_date]
without_last_effective_date_ids = inv_ids[with_last_effective_date+1:]

self.Investment.write(cursor, uid, without_last_effective_date_ids ,{'last_effective_date':False})
self.Investment.write(cursor, uid, with_last_effective_date_ids ,{'last_effective_date':'2019-02-01'})

ret = self.Investment.get_stats_investment_generation(cursor, uid)
socis = ret[0]['socis']

self.assertEqual(socis,len(without_last_effective_date_ids))

def test__get_stats_investment_generation__amount_when_last_effective_date(self):
"""
Check get_stats_investment_generation when some investements with last_effective_date
"""

with Transaction().start(self.database) as txn:
cursor = txn.cursor
uid = txn.user

inv_ids = self.Investment.search(cursor, uid, [('emission_id.type', '=', 'genkwh')])

with_last_effective_date = len(inv_ids) / 2 if inv_ids else 0

with_last_effective_date_ids = inv_ids[:with_last_effective_date]
without_last_effective_date_ids = inv_ids[with_last_effective_date+1:]

self.Investment.write(cursor, uid, without_last_effective_date_ids ,{'last_effective_date':False})
self.Investment.write(cursor, uid, without_last_effective_date_ids ,{'nshares':10})
self.Investment.write(cursor, uid, with_last_effective_date_ids ,{'last_effective_date':'2019-02-01'})

ret = self.Investment.get_stats_investment_generation(cursor, uid)
amount = ret[0]['amount']

self.assertEqual(amount, len(without_last_effective_date_ids) * 1000)

# vim: et ts=4 sw=4

0 comments on commit 8984fbb

Please sign in to comment.