Skip to content

Commit

Permalink
Refactor query to domain + fix variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
anxodio committed Mar 19, 2024
1 parent 10e736b commit bf7acf9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
15 changes: 9 additions & 6 deletions scripts/genkwh_investments.py
Expand Up @@ -187,13 +187,16 @@ def notify(
"notify to members that have an investment going effective on the date"
c = erp()
if not effectiveon:
effectiveon = datetime.date.today()
investments = c.GenerationkwhInvestment.get_activated_investments_for_a_concret_date(
str(effectiveon))
if investments:
effectiveon = str(datetime.date.today())
investment_ids = c.GenerationkwhInvestment.search([
('active', '=', True),
('first_effective_date', '=', effectiveon),
('emission_id.type', '=', 'genkwh'),
])
if investment_ids:
step("Sending notification mails for investments: {}".format(
','.join(str(i) for i in investments)))
c.GenerationkwhInvestment.notifyInvestmentsByMail(investments)
','.join(str(i) for i in investment_ids)))
c.GenerationkwhInvestment.notifyInvestmentsByMail(investment_ids)
else:
step("There are not activated investments to notify.")
success("Done.")
Expand Down
29 changes: 3 additions & 26 deletions som_generationkwh/investment.py
Expand Up @@ -2037,33 +2037,10 @@ def create_interest_invoice(self, cursor, uid,
investment_id, vals, context)
return investment_id, error

def get_activated_investments_for_a_concret_date(
self, cursor, uid, effective_on, context=None):
"""
Returns all the investments actived on effective_on for repeating members
(so discards first time investments)
"""
cursor.execute("""
select
investment.id
from
generationkwh_investment as investment
inner join generationkwh_emission as emission
on emission.id = investment.emission_id
where
investment.active
and emission.type = 'genkwh'
and investment.first_effective_date = %(effective_on)s;
""", dict(
effective_on = isodate(effective_on),
))
result = [ id for id, in cursor.fetchall() ]
return result

def notifyInvestmentsByMail(self, cursor, uid, investments, context=None):
for investment in investments:
def notifyInvestmentsByMail(self, cursor, uid, investment_ids, context=None):
for investment_id in investment_ids:
self.send_mail(cursor, uid,
investment,
investment_id,
'generationkwh.investment',
'_investment_activated_notification_mail',
context=context)
Expand Down

0 comments on commit bf7acf9

Please sign in to comment.