From 26de286d5ce0426ac2008f736c419cfd22890796 Mon Sep 17 00:00:00 2001 From: Raf Ven Date: Thu, 19 Dec 2019 11:28:06 +0100 Subject: [PATCH] [MIG] account_payment_partner: Migration to 13.0 --- account_payment_partner/README.rst | 10 +- account_payment_partner/__manifest__.py | 4 +- account_payment_partner/models/__init__.py | 2 +- .../models/account_invoice.py | 154 -------------- .../models/account_move.py | 136 ++++++++++++ .../models/account_move_line.py | 19 +- .../models/account_payment_mode.py | 4 +- account_payment_partner/models/res_partner.py | 5 +- account_payment_partner/readme/CONFIGURE.rst | 1 - .../static/description/index.html | 8 +- .../tests/test_account_payment_partner.py | 199 +++++++++++------- .../views/account_invoice_view.xml | 85 -------- .../views/account_move_view.xml | 57 +++++ .../views/report_invoice.xml | 2 +- 14 files changed, 345 insertions(+), 341 deletions(-) delete mode 100644 account_payment_partner/models/account_invoice.py create mode 100644 account_payment_partner/models/account_move.py delete mode 100644 account_payment_partner/readme/CONFIGURE.rst delete mode 100644 account_payment_partner/views/account_invoice_view.xml create mode 100644 account_payment_partner/views/account_move_view.xml diff --git a/account_payment_partner/README.rst b/account_payment_partner/README.rst index cfd6e5dd0..9a9c10ef5 100644 --- a/account_payment_partner/README.rst +++ b/account_payment_partner/README.rst @@ -14,13 +14,13 @@ Account Payment Partner :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fbank--payment-lightgray.png?logo=github - :target: https://github.com/OCA/bank-payment/tree/12.0/account_payment_partner + :target: https://github.com/OCA/bank-payment/tree/13.0/account_payment_partner :alt: OCA/bank-payment .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/bank-payment-12-0/bank-payment-12-0-account_payment_partner + :target: https://translation.odoo-community.org/projects/bank-payment-13-0/bank-payment-13-0-account_payment_partner :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/173/12.0 + :target: https://runbot.odoo-community.org/runbot/173/13.0 :alt: Try me on Runbot |badge1| |badge2| |badge3| |badge4| |badge5| @@ -84,7 +84,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -128,6 +128,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -This module is part of the `OCA/bank-payment `_ project on GitHub. +This module is part of the `OCA/bank-payment `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_payment_partner/__manifest__.py b/account_payment_partner/__manifest__.py index 09edf2670..a7441ec1c 100644 --- a/account_payment_partner/__manifest__.py +++ b/account_payment_partner/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Account Payment Partner", - "version": "12.0.1.0.0", + "version": "13.0.1.0.0", "category": "Banking addons", "license": "AGPL-3", "summary": "Adds payment mode on partners and invoices", @@ -14,7 +14,7 @@ "depends": ["account_payment_mode"], "data": [ "views/res_partner_view.xml", - "views/account_invoice_view.xml", + "views/account_move_view.xml", "views/account_move_line.xml", "views/account_payment_mode.xml", "views/report_invoice.xml", diff --git a/account_payment_partner/models/__init__.py b/account_payment_partner/models/__init__.py index 5b2627a54..75c6130c6 100644 --- a/account_payment_partner/models/__init__.py +++ b/account_payment_partner/models/__init__.py @@ -1,4 +1,4 @@ from . import res_partner -from . import account_invoice +from . import account_move from . import account_move_line from . import account_payment_mode diff --git a/account_payment_partner/models/account_invoice.py b/account_payment_partner/models/account_invoice.py deleted file mode 100644 index 40bb645cf..000000000 --- a/account_payment_partner/models/account_invoice.py +++ /dev/null @@ -1,154 +0,0 @@ -# Copyright 2014-16 Akretion - Alexis de Lattre -# Copyright 2014 Serv. Tecnol. Avanzados - Pedro M. Baeza -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from odoo import _, api, fields, models -from odoo.exceptions import ValidationError - - -class AccountInvoice(models.Model): - _inherit = "account.invoice" - - payment_mode_id = fields.Many2one( - comodel_name="account.payment.mode", - string="Payment Mode", - ondelete="restrict", - readonly=True, - states={"draft": [("readonly", False)]}, - ) - bank_account_required = fields.Boolean( - related="payment_mode_id.payment_method_id.bank_account_required", readonly=True - ) - partner_bank_id = fields.Many2one(ondelete="restrict") - - @api.onchange("partner_id", "company_id") - def _onchange_partner_id(self): - res = super(AccountInvoice, self)._onchange_partner_id() - if self.partner_id: - if self.type == "in_invoice": - pay_mode = self.with_context( - force_company=self.company_id.id - ).partner_id.supplier_payment_mode_id - self.payment_mode_id = pay_mode - if ( - pay_mode - and pay_mode.payment_type == "outbound" - and pay_mode.payment_method_id.bank_account_required - and self.commercial_partner_id.bank_ids - ): - self.partner_bank_id = self.commercial_partner_id.bank_ids.filtered( - lambda b: b.company_id == self.company_id or not b.company_id - )[:1] - else: - self.partner_bank_id = False - - elif self.type == "out_invoice": - # No bank account assignation is done here as this is only - # needed for printing purposes and it can conflict with - # SEPA direct debit payments. Current report prints it. - self.payment_mode_id = self.with_context( - force_company=self.company_id.id - ).partner_id.customer_payment_mode_id - else: - self.payment_mode_id = False - if self.type == "in_invoice": - self.partner_bank_id = False - return res - - @api.onchange("payment_mode_id") - def _onchange_payment_mode_id(self): - pay_mode = self.payment_mode_id - if ( - pay_mode - and pay_mode.payment_type == "outbound" - and not pay_mode.payment_method_id.bank_account_required - ): - self.partner_bank_id = False - elif not self.payment_mode_id: - self.partner_bank_id = False - - @api.model - def create(self, vals): - """Fill the payment_mode_id from the partner if none is provided on - creation, using same method as upstream.""" - onchanges = {"_onchange_partner_id": ["payment_mode_id"]} - for onchange_method, changed_fields in onchanges.items(): - if any(f not in vals for f in changed_fields): - invoice = self.new(vals) - getattr(invoice, onchange_method)() - for field in changed_fields: - if field not in vals and invoice[field]: - vals[field] = invoice._fields[field].convert_to_write( - invoice[field], invoice - ) - return super(AccountInvoice, self).create(vals) - - @api.model - def line_get_convert(self, line, part): - """Copy payment mode from invoice to account move line""" - res = super(AccountInvoice, self).line_get_convert(line, part) - if line.get("type") == "dest" and line.get("invoice_id"): - invoice = self.browse(line["invoice_id"]) - res["payment_mode_id"] = invoice.payment_mode_id.id or False - return res - - # I think copying payment mode from invoice to refund by default - # is a good idea because the most common way of "paying" a refund is to - # deduct it on the payment of the next invoice (and OCA/bank-payment - # allows to have negative payment lines since March 2016) - @api.model - def _prepare_refund( - self, invoice, date_invoice=None, date=None, description=None, journal_id=None - ): - vals = super(AccountInvoice, self)._prepare_refund( - invoice, - date_invoice=date_invoice, - date=date, - description=description, - journal_id=journal_id, - ) - vals["payment_mode_id"] = invoice.payment_mode_id.id - if invoice.type == "in_invoice": - vals["partner_bank_id"] = invoice.partner_bank_id.id - return vals - - @api.constrains("company_id", "payment_mode_id") - def _check_payment_mode_company_constrains(self): - for rec in self.sudo(): - if rec.payment_mode_id and rec.company_id != rec.payment_mode_id.company_id: - raise ValidationError( - _( - "The company of the invoice %s does not match " - "with that of the payment mode" - ) - % rec.name - ) - - @api.constrains("partner_id", "partner_bank_id") - def validate_partner_bank_id(self): - """Inhibit the validation of the bank account by default, as core - rules are not the expected one for the bank-payment suite. - """ - if self.env.context.get("use_old_partner_bank_id_check"): - super().validate_partner_bank_id() - - def partner_banks_to_show(self): - self.ensure_one() - if self.partner_bank_id: - return self.partner_bank_id - if self.payment_mode_id.show_bank_account_from_journal: - if self.payment_mode_id.bank_account_link == "fixed": - return self.payment_mode_id.fixed_journal_id.bank_account_id - else: - return self.payment_mode_id.variable_journal_ids.mapped( - "bank_account_id" - ) - if ( - self.payment_mode_id.payment_method_id.code == "sepa_direct_debit" - ): # pragma: no cover - return ( - self.mandate_id.partner_bank_id - or self.partner_id.valid_mandate_id.partner_bank_id - ) - # Return this as empty recordset - return self.partner_bank_id diff --git a/account_payment_partner/models/account_move.py b/account_payment_partner/models/account_move.py new file mode 100644 index 000000000..1f606e835 --- /dev/null +++ b/account_payment_partner/models/account_move.py @@ -0,0 +1,136 @@ +# Copyright 2014-16 Akretion - Alexis de Lattre +# Copyright 2014 Serv. Tecnol. Avanzados - Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class AccountMove(models.Model): + _inherit = "account.move" + + payment_mode_filter_type_domain = fields.Char( + compute="_compute_payment_mode_filter_type_domain" + ) + partner_bank_filter_type_domain = fields.Many2one( + comodel_name="res.partner", compute="_compute_partner_bank_filter_type_domain" + ) + payment_mode_id = fields.Many2one( + comodel_name="account.payment.mode", + compute="_compute_payment_mode", + store=True, + ondelete="restrict", + states={"draft": [("readonly", False)]}, + ) + bank_account_required = fields.Boolean( + related="payment_mode_id.payment_method_id.bank_account_required", readonly=True + ) + invoice_partner_bank_id = fields.Many2one( + compute="_compute_invoice_partner_bank", + store=True, + ondelete="restrict", + states={"draft": [("readonly", False)]}, + ) + + @api.depends("type") + def _compute_payment_mode_filter_type_domain(self): + for move in self: + if move.type in ("out_invoice", "in_refund"): + move.payment_mode_filter_type_domain = "inbound" + elif move.type in ("in_invoice", "out_refund"): + move.payment_mode_filter_type_domain = "outbound" + else: + move.payment_mode_filter_type_domain = False + + @api.depends("partner_id", "type") + def _compute_partner_bank_filter_type_domain(self): + for move in self: + if move.type in ("out_invoice", "in_refund"): + move.partner_bank_filter_type_domain = move.bank_partner_id + elif move.type in ("in_invoice", "out_refund"): + move.partner_bank_filter_type_domain = move.commercial_partner_id + else: + move.partner_bank_filter_type_domain = False + + @api.depends("partner_id", "company_id") + def _compute_payment_mode(self): + for move in self: + if move.partner_id: + if move.type == "in_invoice": + move.payment_mode_id = move.with_context( + force_company=move.company_id.id + ).partner_id.supplier_payment_mode_id + elif move.type == "out_invoice": + move.payment_mode_id = move.with_context( + force_company=move.company_id.id + ).partner_id.customer_payment_mode_id + else: + move.payment_mode_id = False + + @api.depends("partner_id", "payment_mode_id") + def _compute_invoice_partner_bank(self): + for move in self: + # No bank account assignation is done for out_invoice as this is only + # needed for printing purposes and it can conflict with + # SEPA direct debit payments. Current report prints it. + def get_bank_id(): + return move.commercial_partner_id.bank_ids.filtered( + lambda b: b.company_id == move.company_id or not b.company_id + )[:1] + + bank_id = False + if move.partner_id: + pay_mode = move.payment_mode_id + if move.type == "in_invoice": + if ( + pay_mode + and pay_mode.payment_type == "outbound" + and pay_mode.payment_method_id.bank_account_required + and move.commercial_partner_id.bank_ids + ): + bank_id = get_bank_id() + move.invoice_partner_bank_id = bank_id + + # I think copying payment mode from invoice to refund by default + # is a good idea because the most common way of "paying" a refund is to + # deduct it on the payment of the next invoice (and OCA/bank-payment + # allows to have negative payment lines since March 2016) + def _reverse_move_vals(self, default_values, cancel=True): + move_vals = super()._reverse_move_vals(default_values, cancel=cancel) + move_vals["payment_mode_id"] = self.payment_mode_id.id + if self.type == "in_invoice": + move_vals["invoice_partner_bank_id"] = self.invoice_partner_bank_id.id + return move_vals + + @api.constrains("company_id", "payment_mode_id") + def _check_payment_mode_company_constrains(self): + for rec in self.sudo(): + if rec.payment_mode_id and rec.company_id != rec.payment_mode_id.company_id: + raise ValidationError( + _( + "The company of the invoice %s does not match " + "with that of the payment mode" + ) + % rec.name + ) + + def partner_banks_to_show(self): + self.ensure_one() + if self.invoice_partner_bank_id: + return self.invoice_partner_bank_id + if self.payment_mode_id.show_bank_account_from_journal: + if self.payment_mode_id.bank_account_link == "fixed": + return self.payment_mode_id.fixed_journal_id.bank_account_id + else: + return self.payment_mode_id.variable_journal_ids.mapped( + "bank_account_id" + ) + if ( + self.payment_mode_id.payment_method_id.code == "sepa_direct_debit" + ): # pragma: no cover + return ( + self.mandate_id.partner_bank_id + or self.partner_id.valid_mandate_id.partner_bank_id + ) + # Return this as empty recordset + return self.invoice_partner_bank_id diff --git a/account_payment_partner/models/account_move_line.py b/account_payment_partner/models/account_move_line.py index 65e393741..0664d80f5 100644 --- a/account_payment_partner/models/account_move_line.py +++ b/account_payment_partner/models/account_move_line.py @@ -1,16 +1,27 @@ # Copyright 2016 Akretion (http://www.akretion.com/) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import fields, models +from odoo import api, fields, models class AccountMoveLine(models.Model): _inherit = "account.move.line" payment_mode_id = fields.Many2one( - "account.payment.mode", - string="Payment Mode", - domain="[('company_id', '=', company_id)]", + comodel_name="account.payment.mode", + compute="_compute_payment_mode", + store=True, ondelete="restrict", index=True, ) + + @api.depends("move_id.payment_mode_id") + def _compute_payment_mode(self): + for line in self: + if line.move_id.is_invoice() and line.account_internal_type in ( + "receivable", + "payable", + ): + line.payment_mode_id = line.move_id.payment_mode_id + else: + line.payment_mode_id = False diff --git a/account_payment_partner/models/account_payment_mode.py b/account_payment_partner/models/account_payment_mode.py index c80f39e17..8f12621f9 100644 --- a/account_payment_partner/models/account_payment_mode.py +++ b/account_payment_partner/models/account_payment_mode.py @@ -29,7 +29,7 @@ class AccountPaymentMode(models.Model): def account_invoice_company_constrains(self): for mode in self: if ( - self.env["account.invoice"] + self.env["account.move"] .sudo() .search( [ @@ -42,7 +42,7 @@ class AccountPaymentMode(models.Model): raise ValidationError( _( "You cannot change the Company. There exists " - "at least one Invoice with this Payment Mode, " + "at least one Journal Entry with this Payment Mode, " "already assigned to another Company." ) ) diff --git a/account_payment_partner/models/res_partner.py b/account_payment_partner/models/res_partner.py index f64e21e92..28af57018 100644 --- a/account_payment_partner/models/res_partner.py +++ b/account_payment_partner/models/res_partner.py @@ -8,17 +8,14 @@ from odoo import api, fields, models class ResPartner(models.Model): _inherit = "res.partner" - # v8 fields : same without the _id suffix supplier_payment_mode_id = fields.Many2one( "account.payment.mode", - string="Supplier Payment Mode", company_dependent=True, domain="[('payment_type', '=', 'outbound')]", help="Select the default payment mode for this supplier.", ) customer_payment_mode_id = fields.Many2one( "account.payment.mode", - string="Customer Payment Mode", company_dependent=True, domain="[('payment_type', '=', 'inbound')]", help="Select the default payment mode for this customer.", @@ -26,6 +23,6 @@ class ResPartner(models.Model): @api.model def _commercial_fields(self): - res = super(ResPartner, self)._commercial_fields() + res = super()._commercial_fields() res += ["supplier_payment_mode_id", "customer_payment_mode_id"] return res diff --git a/account_payment_partner/readme/CONFIGURE.rst b/account_payment_partner/readme/CONFIGURE.rst deleted file mode 100644 index 10f006c4a..000000000 --- a/account_payment_partner/readme/CONFIGURE.rst +++ /dev/null @@ -1 +0,0 @@ -There is nothing to configure. diff --git a/account_payment_partner/static/description/index.html b/account_payment_partner/static/description/index.html index 1f15d881a..b1344a267 100644 --- a/account_payment_partner/static/description/index.html +++ b/account_payment_partner/static/description/index.html @@ -3,7 +3,7 @@ - + Account Payment Partner