Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIG] account_payment_term_surcharge: Migration to 18.0 #572

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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()
21 changes: 20 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,21 @@ class AccountPaymentTerm(models.Model):
'payment_term_id', string='Surcharges',
copy=True,
)

show_surcharge_warning = fields.Boolean(compute='_compute_surcharge_product')

@api.depends('company_id', 'surcharge_ids')
def _compute_surcharge_product(self):
"""Check if the surcharge product needs to be updated for the given company context."""
for rec in self:
if rec.surcharge_ids:
if rec.company_id: # Verificar para una compañía específica
company = rec.company_id
# Devuelve False si falta el producto de recargo
self.show_surcharge_warning = bool(company.payment_term_surcharge_product_id)
else: # Verificar todas las compañías si company_id es False
all_companies = self.env['res.company'].search([])
# Devuelve False si alguna compañía no tiene configurado el producto de recargo
rec.show_surcharge_warning = all(company.payment_term_surcharge_product_id for company in all_companies)
else:
rec.show_surcharge_warning = True
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<separator string="Surcharges"/>
<field name="surcharge_ids" nolabel="1" colspan="2"/>
</field>
<sheet position="before">
<div class="alert alert-warning" role="alert" invisible="show_surcharge_warning">
The selected company for this payment term does not have a surcharge product configured.
Please configure the product in Settings / Accounting for the selected company or companies.
</div>
</sheet>
</field>
</record>
</data>
Expand Down