Skip to content

Commit

Permalink
Merge 5ceda7e into e04adc8
Browse files Browse the repository at this point in the history
  • Loading branch information
MariteSomEnergia committed Jun 2, 2020
2 parents e04adc8 + 5ceda7e commit 89e0f19
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
44 changes: 44 additions & 0 deletions som_generationkwh/investment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,50 @@ def create_divestment_invoice(self, cursor, uid,
return self.investment_actions(cursor, uid, investment_id).\
create_divestment_invoice(cursor, uid, investment_id, date_invoice, to_be_divested, irpf_amount_current_year, irpf_amount)

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

result = []

if not context:
context = {}

if 'today' in context:
today = context['today']
else:
today = date.today().strftime('%Y-%m-%d')

active_inv_ids = self.search(cursor, uid, [
('emission_id.type', '=', 'genkwh'),
('last_effective_date', '>', today)
])

standby_inv_ids = self.search(cursor, uid, [
('emission_id.type', '=', 'genkwh'),
('last_effective_date', '=', None)
])

inv_ids = active_inv_ids + standby_inv_ids

shares_data = self.read(cursor, uid, inv_ids,[
'nshares',
'member_id',
])

socis_ids = [share_data['member_id'] for share_data in shares_data]
n_socis = len(set(socis_ids))

shares = sum([share_data['nshares'] for share_data in shares_data])

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

class InvestmentProvider(ErpWrapper):

def items(self, member=None, start=None, end=None):
Expand Down
116 changes: 115 additions & 1 deletion som_generationkwh/tests/investment_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,121 @@ 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:]

inv_datas = self.Investment.read(cursor, uid, without_last_effective_date_ids, ['member_id'])
members = [inv_data['member_id'] for inv_data in inv_datas]

today = date.today().strftime('%Y-%m-%d')
yesterday = (date.today() - timedelta(days=1)).strftime('%Y-%m-%d')

self.Investment.write(cursor, uid, without_last_effective_date_ids ,{'last_effective_date':None})
self.Investment.write(cursor, uid, with_last_effective_date_ids ,{'last_effective_date': yesterday})

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

self.assertEqual(socis, len(set(members)))

def test__get_stats_investment_generation__when_last_effective_date_not_done(self):
"""
Check get_stats_investment_generation when some investements with last_effective_date not done
"""

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

inv_ids = self.Investment.search(cursor, uid, [('emission_id.type', '=', 'genkwh')])
inv_datas = self.Investment.read(cursor, uid, inv_ids, ['member_id'])
members = [inv_data['member_id'] for inv_data in inv_datas]

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:]

today = date.today().strftime('%Y-%m-%d')
yesterday = (date.today() - timedelta(days=1)).strftime('%Y-%m-%d')

self.Investment.write(cursor, uid, without_last_effective_date_ids ,{'last_effective_date':None})
self.Investment.write(cursor, uid, with_last_effective_date_ids ,{'last_effective_date': today})

ret = self.Investment.get_stats_investment_generation(cursor, uid, {'today': yesterday})
socis = ret[0]['socis']

self.assertEqual(socis, len(set(members)))

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:]

today = date.today().strftime('%Y-%m-%d')
yesterday = (date.today() - timedelta(days=1)).strftime('%Y-%m-%d')

self.Investment.write(cursor, uid, without_last_effective_date_ids ,{'last_effective_date':None , 'nshares':10})
self.Investment.write(cursor, uid, with_last_effective_date_ids ,{'last_effective_date':yesterday})

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

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

def test__get_stats_investment_generation__amount_when_last_effective_date_not_done(self):
"""
Check get_stats_investment_generation when some investements with last_effective_date not done
"""

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:]

today = date.today().strftime('%Y-%m-%d')
yesterday = (date.today() - timedelta(days=1)).strftime('%Y-%m-%d')

self.Investment.write(cursor, uid, without_last_effective_date_ids ,{'last_effective_date':None , 'nshares':10})
self.Investment.write(cursor, uid, with_last_effective_date_ids ,{'last_effective_date':today , 'nshares':10})

ret = self.Investment.get_stats_investment_generation(cursor, uid, {'today': yesterday})
amount = ret[0]['amount']

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


def test__create_divestment_invoice__withouProfitGKWH(self):
with Transaction().start(self.database) as txn:
cursor = txn.cursor
Expand Down Expand Up @@ -2377,5 +2492,4 @@ def test__divest_investment__GkWh_withOutProfit(self):
today = datetime.today().strftime("%Y-%m-%d")
self.assertEqual(last_effective_date, today)


# vim: et ts=4 sw=4

0 comments on commit 89e0f19

Please sign in to comment.