Skip to content

Commit

Permalink
[FIX] account_interests: Fix when having interval to run interest
Browse files Browse the repository at this point in the history
closes ingadhoc#654

closes ingadhoc#659

X-original-commit: d7817f6
Related: ingadhoc/odoo-academic#189
Related: ingadhoc/odoo-academic#193
Signed-off-by: Ignacio Cainelli <[email protected]>
Signed-off-by: Camila Vives <[email protected]>
Signed-off-by: rov-adhoc <[email protected]>
  • Loading branch information
rov-adhoc committed Jan 30, 2025
1 parent d547ba4 commit 38d16f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion account_interests/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##############################################################################
{
'name': 'Interests Management',
'version': "18.0.1.1.0",
'version': "18.0.1.2.0",
'category': 'Accounting',
'sequence': 14,
'summary': 'Calculate interests for selected partners',
Expand Down
33 changes: 25 additions & 8 deletions account_interests/models/res_company_interest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class ResCompanyInterest(models.Model):
required=True,
digits=(7, 4)
)
past_due_rate = fields.Float(
'Interest for Previous Period',
digits=(7, 4),
default=False,
help="If set, this rate will be used for overdue debts from previous periods. "
"If not set, the standard interest rate (rate) for the current period will be applied."
)
automatic_validation = fields.Boolean(
'Automatic Validation?',
help='Automatic Invoice Validation?',
Expand Down Expand Up @@ -157,6 +164,11 @@ def _update_deuda(self, deuda, partner, key, value):
deuda[partner] = {}
deuda[partner][key] = deuda[partner].get(key, 0) + value

def _calculate_rate(self):
if self.past_due_rate and self.env.context.get('debt_past_period'):
return self.past_due_rate
return self.rate

def _calculate_debts(self, from_date, to_date, groupby=['partner_id']):
"""
Calcula las deudas e intereses por partner.
Expand All @@ -178,15 +190,15 @@ def _calculate_debts(self, from_date, to_date, groupby=['partner_id']):
aggregates=['amount_residual:sum'],
)
for x in previous_grouped_lines:
self._update_deuda(deuda, x[0], 'Deuda periodos anteriores', x[1] * self.rate * self.interval)
self._update_deuda(deuda, x[0], 'Deuda periodos anteriores', x[1] * self.with_context(debt_past_period=True)._calculate_rate() * self.interval)

# Intereses por el último período
last_period_lines = self.env['account.move.line'].search(
self._get_move_line_domains() + [('amount_residual', '>', 0), ('date_maturity', '>=', from_date), ('date_maturity', '<', to_date)]
)
for partner, amls in last_period_lines.grouped('partner_id').items():
interest = sum(
move.amount_residual * ((to_date - move.invoice_date_due).days) * (self.rate / interest_rate[self.rule_type])
move.amount_residual * ((to_date - move.invoice_date_due).days) * (self._calculate_rate()/ interest_rate[self.rule_type])
for move, lines in amls.grouped('move_id').items()
)
self._update_deuda(deuda, partner, 'Deuda último periodo', interest)
Expand All @@ -211,7 +223,7 @@ def _calculate_debts(self, from_date, to_date, groupby=['partner_id']):
due_date = max(from_date, parts.debit_move_id.date_maturity)

days = (parts.credit_move_id.date - due_date).days
interest = parts.amount * days * (self.rate / interest_rate[self.rule_type])
interest = parts.amount * days * (self._calculate_rate() / interest_rate[self.rule_type])
self._update_deuda(deuda, move_line.partner_id, 'Deuda pagos vencidos', interest)

return deuda
Expand Down Expand Up @@ -276,11 +288,16 @@ def _prepare_info(self, to_date):
lang = self.env['res.lang']._lang_get(lang_code)
date_format = lang.date_format
to_date_format = to_date.strftime(date_format)

res = _(
'Deuda Vencida al %s con tasa de interés de %s') % (
to_date_format, self.rate)

if not self.past_due_rate:
res = _(
'Deuda Vencida al %s con tasa de interés de %s') % (
to_date_format, self.rate)
else:
res = _(
'Deuda Vencida al %s de periodos anteriores con tasa de interés de %s. '
'Deuda Vencida al %s del ultimo periodo con tasa de interés de %s',
to_date_format, self.past_due_rate, to_date_format, self.rate
)
return res

def _prepare_interest_invoice(self, partner, debt, to_date, journal):
Expand Down
2 changes: 2 additions & 0 deletions account_interests/views/res_company_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<field name="interest_product_id"/>
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
<field name="rate"/>
<field name="past_due_rate"/>
<field name="automatic_validation"/>
<field name="interval"/>
<field name="rule_type"/>
Expand All @@ -32,6 +33,7 @@
</group>
<group>
<field name="rate"/>
<field name="past_due_rate"/>
<field name="automatic_validation"/>
<field name="interval"/>
<field name="rule_type"/>
Expand Down

0 comments on commit 38d16f9

Please sign in to comment.