From c231a06cd087b513be4171c9d8d16aef6d915e44 Mon Sep 17 00:00:00 2001 From: Felipe Garcia Suez Date: Thu, 10 Oct 2024 12:47:42 -0300 Subject: [PATCH] [MIG] account_payment_term_surcharge: Migration to 18.0 --- account_payment_term_surcharge/__init__.py | 1 - .../__manifest__.py | 4 +- .../data/ir_cron_data.xml | 1 - .../models/__init__.py | 1 + .../models/account_move.py | 23 ++++++++---- .../models/account_payment_term.py | 37 ++++++++++++++++++- .../models/res_company_interest.py | 13 +++++++ .../test_account_payment_term_surcharge.py | 14 +++---- .../views/account_move_views.xml | 2 +- .../account_payment_term_surcharge_view.xml | 6 +-- 10 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 account_payment_term_surcharge/models/res_company_interest.py diff --git a/account_payment_term_surcharge/__init__.py b/account_payment_term_surcharge/__init__.py index 2c9b3790d..9b4296142 100644 --- a/account_payment_term_surcharge/__init__.py +++ b/account_payment_term_surcharge/__init__.py @@ -1,3 +1,2 @@ from . import models from . import wizard -from . import tests diff --git a/account_payment_term_surcharge/__manifest__.py b/account_payment_term_surcharge/__manifest__.py index 4a81a40ee..a497a6be7 100644 --- a/account_payment_term_surcharge/__manifest__.py +++ b/account_payment_term_surcharge/__manifest__.py @@ -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', @@ -38,6 +38,6 @@ 'security/ir.model.access.csv', 'data/ir_cron_data.xml' ], - 'installable': False, + 'installable': True, 'application': False, } diff --git a/account_payment_term_surcharge/data/ir_cron_data.xml b/account_payment_term_surcharge/data/ir_cron_data.xml index 1f94b70d6..35d1cfaae 100644 --- a/account_payment_term_surcharge/data/ir_cron_data.xml +++ b/account_payment_term_surcharge/data/ir_cron_data.xml @@ -6,7 +6,6 @@ 1 days - -1 model._cron_recurring_surcharges_invoices() code diff --git a/account_payment_term_surcharge/models/__init__.py b/account_payment_term_surcharge/models/__init__.py index e6562c4c9..190fb729b 100644 --- a/account_payment_term_surcharge/models/__init__.py +++ b/account_payment_term_surcharge/models/__init__.py @@ -2,3 +2,4 @@ from . import account_payment_term_surcharge from . import account_move from . import res_company +from . import res_company_interest diff --git a/account_payment_term_surcharge/models/account_move.py b/account_payment_term_surcharge/models/account_move.py index 48578fda3..3545e85ac 100644 --- a/account_payment_term_surcharge/models/account_move.py +++ b/account_payment_term_surcharge/models/account_move.py @@ -53,7 +53,6 @@ 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() @@ -61,11 +60,12 @@ def create_surcharge_invoice(self, surcharge_date, surcharge_percent): 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) @@ -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): @@ -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() diff --git a/account_payment_term_surcharge/models/account_payment_term.py b/account_payment_term_surcharge/models/account_payment_term.py index f8d214330..553a9f141 100644 --- a/account_payment_term_surcharge/models/account_payment_term.py +++ b/account_payment_term_surcharge/models/account_payment_term.py @@ -1,4 +1,5 @@ -from odoo import fields, models +from odoo import api, fields, models +from odoo.exceptions import UserError class AccountPaymentTerm(models.Model): @@ -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) diff --git a/account_payment_term_surcharge/models/res_company_interest.py b/account_payment_term_surcharge/models/res_company_interest.py new file mode 100644 index 000000000..91913f515 --- /dev/null +++ b/account_payment_term_surcharge/models/res_company_interest.py @@ -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 diff --git a/account_payment_term_surcharge/tests/test_account_payment_term_surcharge.py b/account_payment_term_surcharge/tests/test_account_payment_term_surcharge.py index 513c62f18..122c0c57e 100644 --- a/account_payment_term_surcharge/tests/test_account_payment_term_surcharge.py +++ b/account_payment_term_surcharge/tests/test_account_payment_term_surcharge.py @@ -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 @@ -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' }) @@ -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 ") diff --git a/account_payment_term_surcharge/views/account_move_views.xml b/account_payment_term_surcharge/views/account_move_views.xml index 470855ada..3b038e302 100644 --- a/account_payment_term_surcharge/views/account_move_views.xml +++ b/account_payment_term_surcharge/views/account_move_views.xml @@ -6,7 +6,7 @@ account.move - + diff --git a/account_payment_term_surcharge/views/account_payment_term_surcharge_view.xml b/account_payment_term_surcharge/views/account_payment_term_surcharge_view.xml index 78904f40d..924b5df51 100644 --- a/account_payment_term_surcharge/views/account_payment_term_surcharge_view.xml +++ b/account_payment_term_surcharge/views/account_payment_term_surcharge_view.xml @@ -2,16 +2,16 @@ - account.payment.term.surcharge.tree + account.payment.term.surcharge.list account.payment.term.surcharge - + - +