Skip to content

Commit

Permalink
[MIG] account_payment_term_surcharge: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
feg-adhoc committed Nov 14, 2024
1 parent 1034a4f commit 6a2d149
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 24 deletions.
1 change: 0 additions & 1 deletion account_payment_term_surcharge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from . import models
from . import wizard
from . import tests
4 changes: 2 additions & 2 deletions account_payment_term_surcharge/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##############################################################################
{
'name': 'Surcharges on payment terms',
'version': "17.0.1.1.0",
'version': "18.0.1.0.0",
'category': 'Accounting',
'sequence': 14,
'summary': 'Allow to add surcharges for invoices on payment terms',
Expand All @@ -38,6 +38,6 @@
'security/ir.model.access.csv',
'data/ir_cron_data.xml'
],
'installable': False,
'installable': True,
'application': False,
}
1 change: 0 additions & 1 deletion account_payment_term_surcharge/data/ir_cron_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="model_id" ref="model_account_move"/>
<field name="code">model._cron_recurring_surcharges_invoices()</field>
<field name="state">code</field>
Expand Down
1 change: 1 addition & 0 deletions account_payment_term_surcharge/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import account_payment_term_surcharge
from . import account_move
from . import res_company
from . import res_company_interest
23 changes: 16 additions & 7 deletions account_payment_term_surcharge/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ def create_surcharges_invoices(self):
if invoice_with_errors:
error_message = _("We couldn't run surcharges cron job in the following invoice: %s.") % invoice_with_errors
raise UserError(error_message)


def create_surcharge_invoice(self, surcharge_date, surcharge_percent):
self.ensure_one()
product = self.company_id.payment_term_surcharge_product_id
if not product:
raise UserError('Atención, debes configurar un producto por defecto para que aplique a la hora de crear las facturas de recargo')
debt = self.amount_residual
move_debit_note_wiz = self.env['account.debit.note'].with_context(active_model="account.move",
active_ids=self.ids).create({
'date': surcharge_date,
'reason': product.name,
})
move_debit_note_wiz = self.env['account.debit.note'].with_context(
active_model="account.move",
active_ids=self.ids).create({
'date': surcharge_date,
'reason': product.name,
})
debit_note = self.env['account.move'].browse(move_debit_note_wiz.create_debit().get('res_id'))
debit_note.narration = product.name + '.\n' + self.prepare_info(surcharge_date, debt, surcharge_percent)
self._add_surcharge_line(debit_note, product, debt, surcharge_date, surcharge_percent)
Expand Down Expand Up @@ -96,7 +96,7 @@ def _add_surcharge_line(self, debit_note, product, debt, to_date, surcharge):
comment = self.prepare_info(to_date, debt, surcharge)
debit_note = debit_note.with_context(check_move_validity=False)
line_vals = [Command.create({"product_id": product.id, "price_unit": (surcharge / 100) * debt, "name": product.name + '.\n' + comment})]
debit_note.write({'invoice_line_ids': line_vals})
debit_note.write({'invoice_line_ids': line_vals,'is_move_sent': True})

@api.depends('invoice_payment_term_id', 'invoice_date')
def _compute_next_surcharge(self):
Expand All @@ -118,3 +118,12 @@ def _compute_next_surcharge(self):
else:
rec.next_surcharge_date = False
rec.next_surcharge_percent = False

def action_post(self):
super().action_post()
self.avoid_surcharge_invoice = False

def action_send_invoice_mail(self):
"""Filtramos solamente las facturas que no cuenta con el is_move_sent seteado en True"""
self = self.filtered(lambda x: not x.is_move_sent)
super().action_send_invoice_mail()
37 changes: 36 additions & 1 deletion account_payment_term_surcharge/models/account_payment_term.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from odoo import fields, models
from odoo import api, fields, models
from odoo.exceptions import UserError


class AccountPaymentTerm(models.Model):
Expand All @@ -9,3 +10,37 @@ class AccountPaymentTerm(models.Model):
'payment_term_id', string='Surcharges',
copy=True,
)

def _validate_surcharge_product(self, company_id):

"""Validate if the surcharge product is set based on the company_id context."""
if company_id: # Verificar para una compañía específica
company = self.env['res.company'].browse(company_id)
if not company.payment_term_surcharge_product_id:
raise UserError("The surcharge product is not set in the selected company's settings.")

else: # Verificar todas las compañías si company_id es False
all_companies = self.env['res.company'].search([])
if not all(company.payment_term_surcharge_product_id for company in all_companies):
raise UserError("Not all companies have a surcharge product set up. "
"Consider leaving the payment term without a specific company "
"and configure 'Default payment term surcharge product' for all companies.")

def restrict_surcharge(self, vals):
"""Restrict surcharge modification if payment_term_surcharge_product_id is not set."""
company_id = vals.get('company_id', self.company_id.id or False)

if vals.get('surcharge_ids') or self.surcharge_ids:
self._validate_surcharge_product(company_id)

return vals

@api.depends('company_id', 'surcharge_ids')
def create(self, vals):
vals = self.restrict_surcharge(vals)
return super(AccountPaymentTerm, self).create(vals)

@api.depends('company_id', 'surcharge_ids')
def write(self, vals):
vals = self.restrict_surcharge(vals)
return super(AccountPaymentTerm, self).write(vals)
13 changes: 13 additions & 0 deletions account_payment_term_surcharge/models/res_company_interest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from odoo import models


class ResCompanyInterest(models.Model):
_inherit = 'res.company.interest'

def _prepare_interest_invoice(self, partner, debt, to_date, journal):
res = super()._prepare_interest_invoice(partner, debt, to_date, journal)

res.update({
'is_move_sent': True,
})
return res
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import odoo.tests.common as common
from odoo.tests import tagged
from odoo.tests import tagged, common
from odoo import Command, fields
from datetime import timedelta

Expand All @@ -9,13 +8,12 @@ class TestAccountPaymentTermSurcharge(common.TransactionCase):
def setUp(self):
super().setUp()
self.today = fields.Date.today()
self.first_company = self.env['res.company'].search([('name', '=', 'Muebleria US')], limit=1)
self.partner_ri = self.env['res.partner'].search([('name', '=', 'ADHOC SA')], limit=1)
self.first_company_journal = self.env.ref('account.1_sale')
self.first_company = self.env['res.company'].search([], limit=1)
self.partner_ri = self.env['res.partner'].search([], limit=1)
self.first_company_journal = self.env['account.journal'].search([('company_id', '=', self.first_company.id),('type', '=', 'sale')])

self.product_surcharge = self.env.ref('product.product_product_16')
self.env['res.config.settings'].search([('company_id', '=', self.first_company.id)]).payment_term_surcharge_product_id = self.product_surcharge.id

self.first_company.payment_term_surcharge_product_id = self.product_surcharge.id
self.payment_term = self.env['account.payment.term'].create({
'name': 'Test payment term'
})
Expand Down Expand Up @@ -46,8 +44,8 @@ def test_payment_term_surcharge(self):
}),
]
})
invoice.avoid_surcharge_invoice = False
invoice.action_post()
invoice.avoid_surcharge_invoice = False

invoice._cron_recurring_surcharges_invoices()
self.assertFalse(invoice.next_surcharge_date, "La proxima fecha de recargo no es la correspondiente ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<field name="to_check" position="after">
<field name="checked" position="after">
<field name="avoid_surcharge_invoice"/>
</field>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<odoo>
<data>
<record id="view_payment_term_surcharge_tree" model="ir.ui.view">
<field name="name">account.payment.term.surcharge.tree</field>
<field name="name">account.payment.term.surcharge.list</field>
<field name="model">account.payment.term.surcharge</field>
<field name="arch" type="xml">
<tree string="Payment Term Surcharges">
<list string="Payment Term Surcharges">
<field name="sequence" widget="handle"/>
<field name="surcharge"/>
<field name="days"/>
<field name="option" string=""/>
<field name="day_of_the_month" string="Day of the month"/>
</tree>
</list>
</field>
</record>

Expand Down

0 comments on commit 6a2d149

Please sign in to comment.