[IMP] contract_payment_mode: black, isort, prettier

This commit is contained in:
Guille
2020-10-21 09:21:58 +02:00
parent a16f8ba6ba
commit b30f04cfa4
7 changed files with 132 additions and 115 deletions

View File

@@ -6,23 +6,16 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'Contract Payment Mode',
'summary': 'Payment mode in contracts and their invoices',
'version': '12.0.1.1.0',
'author': 'Domatix, '
'Tecnativa, '
'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/contract',
'depends': [
'contract',
'account_payment_partner'
],
'category': 'Sales Management',
'license': 'AGPL-3',
'data': [
'views/contract_view.xml',
],
'post_init_hook': 'post_init_hook',
'installable': True,
'auto_install': True,
"name": "Contract Payment Mode",
"summary": "Payment mode in contracts and their invoices",
"version": "12.0.1.1.0",
"author": "Domatix, " "Tecnativa, " "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/contract",
"depends": ["contract", "account_payment_partner"],
"category": "Sales Management",
"license": "AGPL-3",
"data": ["views/contract_view.xml",],
"post_init_hook": "post_init_hook",
"installable": True,
"auto_install": True,
}

View File

@@ -1,9 +1,10 @@
# Copyright 2016 Antiun Ingenieria S.L. - Antonio Espinosa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, SUPERUSER_ID
import logging
from odoo import SUPERUSER_ID, api
_logger = logging.getLogger(__name__)
@@ -11,15 +12,12 @@ def post_init_hook(cr, registry):
"""Copy payment mode from partner to the new field at contract."""
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
m_contract = env['contract.contract']
contracts = m_contract.search([
('payment_mode_id', '=', False),
])
m_contract = env["contract.contract"]
contracts = m_contract.search([("payment_mode_id", "=", False),])
if contracts:
_logger.info('Setting payment mode: %d contracts' %
len(contracts))
_logger.info("Setting payment mode: %d contracts" % len(contracts))
for contract in contracts:
payment_mode = contract.partner_id.customer_payment_mode_id
if payment_mode:
contract.payment_mode_id = payment_mode.id
_logger.info('Setting payment mode: Done')
_logger.info("Setting payment mode: Done")

View File

@@ -2,26 +2,27 @@ from odoo import api, fields, models
class ContractContract(models.Model):
_inherit = 'contract.contract'
_inherit = "contract.contract"
payment_mode_id = fields.Many2one(
comodel_name='account.payment.mode',
string='Payment Mode',
domain=[('payment_type', '=', 'inbound')],
comodel_name="account.payment.mode",
string="Payment Mode",
domain=[("payment_type", "=", "inbound")],
index=True,
)
@api.onchange('partner_id')
@api.onchange("partner_id")
def on_change_partner_id(self):
self.payment_mode_id = self.partner_id.customer_payment_mode_id.id
@api.multi
def _prepare_invoice(self, date_invoice, journal=None):
invoice_vals = super(ContractContract, self)._prepare_invoice(
date_invoice=date_invoice, journal=journal)
date_invoice=date_invoice, journal=journal
)
if self.payment_mode_id:
invoice_vals['payment_mode_id'] = self.payment_mode_id.id
invoice = self.env['account.invoice'].new(invoice_vals)
invoice_vals["payment_mode_id"] = self.payment_mode_id.id
invoice = self.env["account.invoice"].new(invoice_vals)
invoice._onchange_payment_mode_id()
invoice_vals = invoice._convert_to_write(invoice._cache)
return invoice_vals
@@ -35,8 +36,7 @@ class ContractContract(models.Model):
payment_modes_by_invoice = {}
for invoice in invoices:
payment_modes_by_invoice[invoice] = invoice.payment_mode_id
res = super(ContractContract, self)._finalize_invoice_creation(
invoices)
res = super(ContractContract, self)._finalize_invoice_creation(invoices)
for invoice in invoices:
invoice.payment_mode_id = payment_modes_by_invoice.get(invoice)
return res

View File

@@ -4,91 +4,105 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import odoo.tests
from ..hooks import post_init_hook
@odoo.tests.post_install(True)
@odoo.tests.at_install(False)
class TestContractPaymentInit(odoo.tests.HttpCase):
def setUp(self):
super(TestContractPaymentInit, self).setUp()
self.payment_method = self.env['account.payment.method'].create({
'name': 'Test Payment Method',
'code': 'Test',
'payment_type': 'inbound',
})
self.payment_mode = self.env['account.payment.mode'].create({
'name': 'Test payment mode',
'active': True,
'payment_method_id': self.payment_method.id,
'bank_account_link': 'variable',
})
self.partner = self.env['res.partner'].create({
'name': 'Test contract partner',
'customer_payment_mode_id': self.payment_mode,
})
self.product = self.env['product.product'].create({
'name': 'Custom Service',
'type': 'service',
'uom_id': self.env.ref('uom.product_uom_hour').id,
'uom_po_id': self.env.ref('uom.product_uom_hour').id,
'sale_ok': True,
})
self.contract = self.env['contract.contract'].create({
'name': 'Maintenance of Servers',
'partner_id': self.partner.id,
})
company = self.env.ref('base.main_company')
self.journal = self.env['account.journal'].create({
'name': 'Sale Journal - Test',
'code': 'HRTSJ',
'type': 'sale',
'company_id': company.id})
self.payment_method = self.env["account.payment.method"].create(
{"name": "Test Payment Method", "code": "Test", "payment_type": "inbound",}
)
self.payment_mode = self.env["account.payment.mode"].create(
{
"name": "Test payment mode",
"active": True,
"payment_method_id": self.payment_method.id,
"bank_account_link": "variable",
}
)
self.partner = self.env["res.partner"].create(
{
"name": "Test contract partner",
"customer_payment_mode_id": self.payment_mode,
}
)
self.product = self.env["product.product"].create(
{
"name": "Custom Service",
"type": "service",
"uom_id": self.env.ref("uom.product_uom_hour").id,
"uom_po_id": self.env.ref("uom.product_uom_hour").id,
"sale_ok": True,
}
)
self.contract = self.env["contract.contract"].create(
{"name": "Maintenance of Servers", "partner_id": self.partner.id,}
)
company = self.env.ref("base.main_company")
self.journal = self.env["account.journal"].create(
{
"name": "Sale Journal - Test",
"code": "HRTSJ",
"type": "sale",
"company_id": company.id,
}
)
def test_post_init_hook(self):
contract = self.env['contract.contract'].create({
'name': 'Test contract',
'partner_id': self.partner.id,
'payment_mode_id': self.payment_mode.id,
})
self.assertEqual(contract.payment_mode_id,
self.payment_mode)
contract = self.env["contract.contract"].create(
{
"name": "Test contract",
"partner_id": self.partner.id,
"payment_mode_id": self.payment_mode.id,
}
)
self.assertEqual(contract.payment_mode_id, self.payment_mode)
contract.payment_mode_id = False
self.assertEqual(contract.payment_mode_id.id, False)
post_init_hook(self.cr, self.env)
self.assertEqual(contract.payment_mode_id,
self.payment_mode)
self.assertEqual(contract.payment_mode_id, self.payment_mode)
def test_contract_and_invoices(self):
self.contract.write({'partner_id': self.partner.id})
self.contract.write({"partner_id": self.partner.id})
self.contract.on_change_partner_id()
self.assertEqual(self.contract.payment_mode_id,
self.contract.partner_id.customer_payment_mode_id)
self.contract.write({
'contract_line_ids': [(0, 0, {
'product_id': self.product.id,
'name': 'Database Administration 25',
'quantity': 2.0,
'uom_id': self.product.uom_id.id,
'price_unit': 200.0,
'recurring_rule_type': 'monthly',
'recurring_interval': 1,
'date_start': '2018-01-01',
'recurring_next_date': '2018-01-15',
'is_auto_renew': False,
})]
})
self.assertEqual(
self.contract.payment_mode_id,
self.contract.partner_id.customer_payment_mode_id,
)
self.contract.write(
{
"contract_line_ids": [
(
0,
0,
{
"product_id": self.product.id,
"name": "Database Administration 25",
"quantity": 2.0,
"uom_id": self.product.uom_id.id,
"price_unit": 200.0,
"recurring_rule_type": "monthly",
"recurring_interval": 1,
"date_start": "2018-01-01",
"recurring_next_date": "2018-01-15",
"is_auto_renew": False,
},
)
]
}
)
self.contract.recurring_create_invoice()
new_invoice = self.contract._get_related_invoices()
self.assertTrue(new_invoice)
self.assertEqual(new_invoice.partner_id, self.contract.partner_id)
self.assertEqual(new_invoice.payment_mode_id,
self.contract.payment_mode_id)
self.assertEqual(new_invoice.payment_mode_id, self.contract.payment_mode_id)
self.assertEqual(len(new_invoice.ids), 1)
self.contract.recurring_create_invoice()
self.assertEqual(self.contract.payment_mode_id,
new_invoice.payment_mode_id)
self.assertEqual(self.contract.payment_mode_id, new_invoice.payment_mode_id)

View File

@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<!--FORM view-->
<record id="contract_contract_form_view" model="ir.ui.view">
<field name="name">contract.contract form view (in contract_payment_mode)</field>
<field
name="name"
>contract.contract form view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="inherit_id" ref="contract.contract_contract_form_view" />
<field name="arch" type="xml">
@@ -12,10 +13,11 @@
</field>
</field>
</record>
<!--TREE view-->
<record id="contract_contract_tree_view" model="ir.ui.view">
<field name="name">contract.contract tree view (in contract_payment_mode)</field>
<field
name="name"
>contract.contract tree view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="inherit_id" ref="contract.contract_contract_tree_view" />
<field name="arch" type="xml">
@@ -24,10 +26,11 @@
</field>
</field>
</record>
<!--FORM view-->
<record id="contract_contract_search_view" model="ir.ui.view">
<field name="name">contract.contract search view (in contract_payment_mode)</field>
<field
name="name"
>contract.contract search view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="inherit_id" ref="contract.contract_contract_search_view" />
<field name="arch" type="xml">
@@ -36,19 +39,21 @@
</field>
</field>
</record>
<!--Supplier FORM view-->
<record id="contract_contract_supplier_form_view" model="ir.ui.view">
<field name="name">contract.contract supplier form view (in contract_payment_mode)</field>
<field
name="name"
>contract.contract supplier form view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="priority">18</field>
<field name="inherit_id" ref="contract.contract_contract_supplier_form_view" />
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="payment_mode_id" domain="[('payment_type', '=', 'outbound')]"/>
<field
name="payment_mode_id"
domain="[('payment_type', '=', 'outbound')]"
/>
</field>
</field>
</record>
</odoo>

View File

@@ -0,0 +1 @@
../../../../contract_payment_mode

View File

@@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)