[IMP] account_payment_mode: black, isort

This commit is contained in:
Raf Ven
2019-11-08 12:12:03 +01:00
committed by Thomas Binsfeld
parent 297ff807fd
commit 6288e032e6
9 changed files with 275 additions and 202 deletions

View File

@@ -1,2 +1 @@
from . import models

View File

@@ -2,22 +2,22 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
'name': 'Account Payment Mode',
'version': '13.0.1.0.0',
'license': 'AGPL-3',
'author': "Akretion,Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/bank-payment',
'category': 'Banking addons',
'depends': ['account'],
'data': [
'security/account_payment_mode.xml',
'security/ir.model.access.csv',
'views/account_payment_method.xml',
'views/account_payment_mode.xml',
'views/res_partner_bank.xml',
'views/res_partner.xml',
'views/account_journal.xml',
"name": "Account Payment Mode",
"version": "13.0.1.0.0",
"license": "AGPL-3",
"author": "Akretion,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/bank-payment",
"category": "Banking addons",
"depends": ["account"],
"data": [
"security/account_payment_mode.xml",
"security/ir.model.access.csv",
"views/account_payment_method.xml",
"views/account_payment_mode.xml",
"views/res_partner_bank.xml",
"views/res_partner.xml",
"views/account_journal.xml",
],
'demo': ['demo/payment_demo.xml'],
'installable': True,
"demo": ["demo/payment_demo.xml"],
"installable": True,
}

View File

@@ -1,4 +1,3 @@
from . import account_payment_method
from . import account_payment_mode
from . import account_journal

View File

@@ -1,49 +1,67 @@
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models, _
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class AccountJournal(models.Model):
_inherit = 'account.journal'
_inherit = "account.journal"
def _default_outbound_payment_methods(self):
all_out = self.env['account.payment.method'].search([
('payment_type', '=', 'outbound')])
all_out = self.env["account.payment.method"].search(
[("payment_type", "=", "outbound")]
)
return all_out
def _default_inbound_payment_methods(self):
all_in = self.env['account.payment.method'].search([
('payment_type', '=', 'inbound')])
all_in = self.env["account.payment.method"].search(
[("payment_type", "=", "inbound")]
)
return all_in
outbound_payment_method_ids = fields.Many2many(
default=_default_outbound_payment_methods)
default=_default_outbound_payment_methods
)
inbound_payment_method_ids = fields.Many2many(
default=_default_inbound_payment_methods)
default=_default_inbound_payment_methods
)
company_partner_id = fields.Many2one(
'res.partner', related='company_id.partner_id',
readonly=True) # Used in domain of field bank_account_id
"res.partner", related="company_id.partner_id", readonly=True
) # Used in domain of field bank_account_id
@api.constrains('company_id')
@api.constrains("company_id")
def company_id_account_payment_mode_constrains(self):
for journal in self:
mode = self.env['account.payment.mode'].search([
('fixed_journal_id', '=', journal.id),
('company_id', '!=', journal.company_id.id)], limit=1)
mode = self.env["account.payment.mode"].search(
[
("fixed_journal_id", "=", journal.id),
("company_id", "!=", journal.company_id.id),
],
limit=1,
)
if mode:
raise ValidationError(_(
"The company of the journal '%s' does not match "
"with the company of the payment mode '%s' where it is "
"being used as Fixed Bank Journal.") % (
journal.name, mode.name))
mode = self.env['account.payment.mode'].search([
('variable_journal_ids', 'in', [journal.id]),
('company_id', '!=', journal.company_id.id)], limit=1)
raise ValidationError(
_(
"The company of the journal '%s' does not match "
"with the company of the payment mode '%s' where it is "
"being used as Fixed Bank Journal."
)
% (journal.name, mode.name)
)
mode = self.env["account.payment.mode"].search(
[
("variable_journal_ids", "in", [journal.id]),
("company_id", "!=", journal.company_id.id),
],
limit=1,
)
if mode:
raise ValidationError(_(
"The company of the journal '%s' does not match "
"with the company of the payment mode '%s' where it is "
"being used in the Allowed Bank Journals.") % (
journal.name, mode.name))
raise ValidationError(
_(
"The company of the journal '%s' does not match "
"with the company of the payment mode '%s' where it is "
"being used in the Allowed Bank Journals."
)
% (journal.name, mode.name)
)

View File

@@ -1,37 +1,47 @@
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api
from odoo import api, fields, models
class AccountPaymentMethod(models.Model):
_inherit = 'account.payment.method'
_inherit = "account.payment.method"
code = fields.Char(
string='Code (Do Not Modify)',
string="Code (Do Not Modify)",
help="This code is used in the code of the Odoo module that handles "
"this payment method. Therefore, if you change it, "
"the generation of the payment file may fail.")
"the generation of the payment file may fail.",
)
active = fields.Boolean(default=True)
bank_account_required = fields.Boolean(
help="Activate this option if this payment method requires you to "
"know the bank account number of your customer or supplier.")
"know the bank account number of your customer or supplier."
)
payment_mode_ids = fields.One2many(
comodel_name='account.payment.mode', inverse_name='payment_method_id',
string='Payment modes')
comodel_name="account.payment.mode",
inverse_name="payment_method_id",
string="Payment modes",
)
@api.depends('code', 'name', 'payment_type')
@api.depends("code", "name", "payment_type")
def name_get(self):
result = []
for method in self:
result.append((
method.id, u'[%s] %s (%s)' % (
method.code, method.name, method.payment_type)
))
result.append(
(
method.id,
u"[{}] {} ({})".format(
method.code, method.name, method.payment_type
),
)
)
return result
_sql_constraints = [(
'code_payment_type_unique',
'unique(code, payment_type)',
'A payment method of the same type already exists with this code'
)]
_sql_constraints = [
(
"code_payment_type_unique",
"unique(code, payment_type)",
"A payment method of the same type already exists with this code",
)
]

View File

@@ -1,7 +1,7 @@
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api, _
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
@@ -9,19 +9,25 @@ class AccountPaymentMode(models.Model):
"""This corresponds to the object payment.mode of v8 with some
important changes. It also replaces the object payment.method
of the module sale_payment_method of OCA/e-commerce"""
_name = "account.payment.mode"
_description = 'Payment Modes'
_order = 'name'
_description = "Payment Modes"
_order = "name"
name = fields.Char(required=True, translate=True)
company_id = fields.Many2one(
'res.company', string='Company', required=True, ondelete='restrict',
default=lambda self: self.env['res.company']._company_default_get(
'account.payment.mode'))
bank_account_link = fields.Selection([
('fixed', 'Fixed'),
('variable', 'Variable'),
], string='Link to Bank Account', required=True,
"res.company",
string="Company",
required=True,
ondelete="restrict",
default=lambda self: self.env["res.company"]._company_default_get(
"account.payment.mode"
),
)
bank_account_link = fields.Selection(
[("fixed", "Fixed"), ("variable", "Variable")],
string="Link to Bank Account",
required=True,
help="For payment modes that are always attached to the same bank "
"account of your company (such as wire transfer from customers or "
"SEPA direct debit from suppliers), select "
@@ -29,24 +35,35 @@ class AccountPaymentMode(models.Model):
"bank account (such as SEPA Direct debit for customers, wire transfer "
"to suppliers), you should select 'Variable', which means that you "
"will select the bank account on the payment order. If your company "
"only has one bank account, you should always select 'Fixed'.")
"only has one bank account, you should always select 'Fixed'.",
)
fixed_journal_id = fields.Many2one(
'account.journal', string='Fixed Bank Journal',
domain=[('type', '=', 'bank')], ondelete='restrict')
"account.journal",
string="Fixed Bank Journal",
domain=[("type", "=", "bank")],
ondelete="restrict",
)
# I need to use the old definition, because I have 2 M2M fields
# pointing to account.journal
variable_journal_ids = fields.Many2many(
comodel_name='account.journal',
relation='account_payment_mode_variable_journal_rel',
column1='payment_mode_id', column2='journal_id',
string='Allowed Bank Journals')
comodel_name="account.journal",
relation="account_payment_mode_variable_journal_rel",
column1="payment_mode_id",
column2="journal_id",
string="Allowed Bank Journals",
)
payment_method_id = fields.Many2one(
'account.payment.method', string='Payment Method', required=True,
ondelete='restrict') # equivalent v8 field : type
"account.payment.method",
string="Payment Method",
required=True,
ondelete="restrict",
) # equivalent v8 field : type
payment_type = fields.Selection(
related='payment_method_id.payment_type', readonly=True, store=True)
related="payment_method_id.payment_type", readonly=True, store=True
)
payment_method_code = fields.Char(
related='payment_method_id.code', readonly=True, store=True)
related="payment_method_id.code", readonly=True, store=True
)
active = fields.Boolean(default=True)
# I dropped sale_ok and purchase_ok fields, because it is replaced by
# payment_type = 'inbound' or 'outbound'
@@ -55,64 +72,83 @@ class AccountPaymentMode(models.Model):
# and one for wire transfer to your suppliers (outbound)
note = fields.Text(translate=True)
@api.onchange('company_id')
@api.onchange("company_id")
def _onchange_company_id(self):
self.variable_journal_ids = False
self.fixed_journal_id = False
@api.constrains(
'bank_account_link', 'fixed_journal_id', 'payment_method_id')
@api.constrains("bank_account_link", "fixed_journal_id", "payment_method_id")
def bank_account_link_constrains(self):
for mode in self.filtered(lambda x: x.bank_account_link == 'fixed'):
for mode in self.filtered(lambda x: x.bank_account_link == "fixed"):
if not mode.fixed_journal_id:
raise ValidationError(_(
"On the payment mode '%s', the bank account link is "
"'Fixed' but the fixed bank journal is not set")
% mode.name)
raise ValidationError(
_(
"On the payment mode '%s', the bank account link is "
"'Fixed' but the fixed bank journal is not set"
)
% mode.name
)
else:
if mode.payment_method_id.payment_type == 'outbound':
if mode.payment_method_id.payment_type == "outbound":
if (
mode.payment_method_id.id not in
mode.fixed_journal_id.
outbound_payment_method_ids.ids):
raise ValidationError(_(
"On the payment mode '%s', the payment method "
"is '%s', but this payment method is not part "
"of the payment methods of the fixed bank "
"journal '%s'") % (
mode.payment_method_id.id
not in mode.fixed_journal_id.outbound_payment_method_ids.ids
):
raise ValidationError(
_(
"On the payment mode '%s', the payment method "
"is '%s', but this payment method is not part "
"of the payment methods of the fixed bank "
"journal '%s'"
)
% (
mode.name,
mode.payment_method_id.name,
mode.fixed_journal_id.name))
mode.fixed_journal_id.name,
)
)
else:
if (
mode.payment_method_id.id not in
mode.fixed_journal_id.
inbound_payment_method_ids.ids):
raise ValidationError(_(
"On the payment mode '%s', the payment method "
"is '%s' (it is in fact a debit method), "
"but this debit method is not part "
"of the debit methods of the fixed bank "
"journal '%s'") % (
mode.payment_method_id.id
not in mode.fixed_journal_id.inbound_payment_method_ids.ids
):
raise ValidationError(
_(
"On the payment mode '%s', the payment method "
"is '%s' (it is in fact a debit method), "
"but this debit method is not part "
"of the debit methods of the fixed bank "
"journal '%s'"
)
% (
mode.name,
mode.payment_method_id.name,
mode.fixed_journal_id.name))
mode.fixed_journal_id.name,
)
)
@api.constrains('company_id', 'fixed_journal_id')
@api.constrains("company_id", "fixed_journal_id")
def company_id_fixed_journal_id_constrains(self):
for mode in self.filtered(
lambda x: x.fixed_journal_id
and x.company_id != x.fixed_journal_id.company_id):
raise ValidationError(_(
"The company of the payment mode '%s', does not match "
"with the company of journal '%s'.") % (
mode.name, mode.fixed_journal_id.name))
lambda x: x.fixed_journal_id
and x.company_id != x.fixed_journal_id.company_id
):
raise ValidationError(
_(
"The company of the payment mode '%s', does not match "
"with the company of journal '%s'."
)
% (mode.name, mode.fixed_journal_id.name)
)
@api.constrains('company_id', 'variable_journal_ids')
@api.constrains("company_id", "variable_journal_ids")
def company_id_variable_journal_ids_constrains(self):
for mode in self:
if any(mode.company_id != j.company_id for j in
mode.variable_journal_ids):
raise ValidationError(_(
"The company of the payment mode '%s', does not match "
"with the one of the Allowed Bank Journals.") % mode.name)
if any(mode.company_id != j.company_id for j in mode.variable_journal_ids):
raise ValidationError(
_(
"The company of the payment mode '%s', does not match "
"with the one of the Allowed Bank Journals."
)
% mode.name
)

View File

@@ -1,3 +1,2 @@
This module adds a new object *account.payment.mode*, that is used to better
classify and route incoming/outgoing payment orders with the banks.

View File

@@ -1,2 +1 @@
This module doesn't add any feature, but it is used by several other modules.

View File

@@ -1,116 +1,129 @@
# © 2016 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo.tests.common import TransactionCase
from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase
class TestAccountPaymentMode(TransactionCase):
def setUp(self):
super(TestAccountPaymentMode, self).setUp()
self.res_users_model = self.env['res.users']
self.journal_model = self.env['account.journal']
self.payment_mode_model = self.env['account.payment.mode']
self.res_users_model = self.env["res.users"]
self.journal_model = self.env["account.journal"]
self.payment_mode_model = self.env["account.payment.mode"]
# refs
self.manual_out = self.env.ref(
'account.account_payment_method_manual_out')
self.manual_out = self.env.ref("account.account_payment_method_manual_out")
# Company
self.company = self.env.ref('base.main_company')
self.company = self.env.ref("base.main_company")
# Company 2
self.company_2 = self.env['res.company'].create({
'name': 'Company 2',
})
self.company_2 = self.env["res.company"].create({"name": "Company 2"})
self.journal_c1 = self._create_journal('J1', self.company)
self.journal_c2 = self._create_journal('J2', self.company_2)
self.journal_c3 = self._create_journal('J3', self.company)
self.journal_c1 = self._create_journal("J1", self.company)
self.journal_c2 = self._create_journal("J2", self.company_2)
self.journal_c3 = self._create_journal("J3", self.company)
self.payment_mode_c1 = self.payment_mode_model.create({
'name': 'Direct Debit of suppliers from Bank 1',
'bank_account_link': 'variable',
'payment_method_id': self.manual_out.id,
'company_id': self.company.id,
'fixed_journal_id': self.journal_c1.id,
'variable_journal_ids': [(6, 0, [self.journal_c1.id,
self.journal_c3.id])]
})
self.payment_mode_c1 = self.payment_mode_model.create(
{
"name": "Direct Debit of suppliers from Bank 1",
"bank_account_link": "variable",
"payment_method_id": self.manual_out.id,
"company_id": self.company.id,
"fixed_journal_id": self.journal_c1.id,
"variable_journal_ids": [
(6, 0, [self.journal_c1.id, self.journal_c3.id])
],
}
)
def _create_journal(self, name, company):
# Create a cash account
# Create a journal for cash account
journal = self.journal_model.create({
'name': name,
'code': name,
'type': 'bank',
'company_id': company.id,
})
journal = self.journal_model.create(
{"name": name, "code": name, "type": "bank", "company_id": company.id}
)
return journal
def test_payment_mode_company_consistency_change(self):
# Assertion on the constraints to ensure the consistency
# for company dependent fields
with self.assertRaises(ValidationError):
self.payment_mode_c1. \
write({'fixed_journal_id': self.journal_c2.id})
self.payment_mode_c1.write({"fixed_journal_id": self.journal_c2.id})
with self.assertRaises(ValidationError):
self.payment_mode_c1.write({
'variable_journal_ids': [
(6, 0, [
self.journal_c1.id,
self.journal_c2.id,
self.journal_c3.id
])
]
})
self.payment_mode_c1.write(
{
"variable_journal_ids": [
(
6,
0,
[
self.journal_c1.id,
self.journal_c2.id,
self.journal_c3.id,
],
)
]
}
)
with self.assertRaises(ValidationError):
self.journal_c1.write({'company_id': self.company_2.id})
self.journal_c1.write({"company_id": self.company_2.id})
def test_payment_mode_company_consistency_create(self):
# Assertion on the constraints to ensure the consistency
# for company dependent fields
with self.assertRaises(ValidationError):
self.payment_mode_model.create({
'name': 'Direct Debit of suppliers from Bank 2',
'bank_account_link': 'variable',
'payment_method_id': self.manual_out.id,
'company_id': self.company.id,
'fixed_journal_id': self.journal_c2.id
})
self.payment_mode_model.create(
{
"name": "Direct Debit of suppliers from Bank 2",
"bank_account_link": "variable",
"payment_method_id": self.manual_out.id,
"company_id": self.company.id,
"fixed_journal_id": self.journal_c2.id,
}
)
with self.assertRaises(ValidationError):
self.payment_mode_model.create({
'name': 'Direct Debit of suppliers from Bank 3',
'bank_account_link': 'variable',
'payment_method_id': self.manual_out.id,
'company_id': self.company.id,
'variable_journal_ids': [(6, 0, [self.journal_c2.id])]
})
self.payment_mode_model.create(
{
"name": "Direct Debit of suppliers from Bank 3",
"bank_account_link": "variable",
"payment_method_id": self.manual_out.id,
"company_id": self.company.id,
"variable_journal_ids": [(6, 0, [self.journal_c2.id])],
}
)
with self.assertRaises(ValidationError):
self.payment_mode_model.create({
'name': 'Direct Debit of suppliers from Bank 4',
'bank_account_link': 'fixed',
'payment_method_id': self.manual_out.id,
'company_id': self.company.id,
})
self.payment_mode_model.create(
{
"name": "Direct Debit of suppliers from Bank 4",
"bank_account_link": "fixed",
"payment_method_id": self.manual_out.id,
"company_id": self.company.id,
}
)
self.journal_c1.outbound_payment_method_ids = False
with self.assertRaises(ValidationError):
self.payment_mode_model.create({
'name': 'Direct Debit of suppliers from Bank 5',
'bank_account_link': 'fixed',
'payment_method_id': self.manual_out.id,
'company_id': self.company.id,
'fixed_journal_id': self.journal_c1.id
})
self.payment_mode_model.create(
{
"name": "Direct Debit of suppliers from Bank 5",
"bank_account_link": "fixed",
"payment_method_id": self.manual_out.id,
"company_id": self.company.id,
"fixed_journal_id": self.journal_c1.id,
}
)
self.journal_c1.inbound_payment_method_ids = False
with self.assertRaises(ValidationError):
self.payment_mode_model.create({
'name': 'Direct Debit of suppliers from Bank 5',
'bank_account_link': 'fixed',
'payment_method_id': self.env.ref(
'account.account_payment_method_manual_in').id,
'company_id': self.company.id,
'fixed_journal_id': self.journal_c1.id
})
self.payment_mode_model.create(
{
"name": "Direct Debit of suppliers from Bank 5",
"bank_account_link": "fixed",
"payment_method_id": self.env.ref(
"account.account_payment_method_manual_in"
).id,
"company_id": self.company.id,
"fixed_journal_id": self.journal_c1.id,
}
)