[IMP] contract: Allow to set a manual currency

Done through an extra field + inverse in the existing compute. This way, we have
the flexibility of auto-remove custom value if it matches the computed one + we don't
need extra changes in the rest of the code.
This commit is contained in:
Pedro M. Baeza
2020-06-05 17:05:50 +02:00
committed by Francisco Ivan Anton Prieto
parent 392c27a161
commit 9f82dd2d1a
2 changed files with 78 additions and 15 deletions

View File

@@ -34,8 +34,12 @@ class ContractContract(models.Model):
) )
currency_id = fields.Many2one( currency_id = fields.Many2one(
compute="_compute_currency_id", compute="_compute_currency_id",
inverse="_inverse_currency_id",
comodel_name="res.currency", comodel_name="res.currency",
string="Currency", string="Currency",
)
manual_currency_id = fields.Many2one(
comodel_name="res.currency",
readonly=True, readonly=True,
) )
contract_template_id = fields.Many2one( contract_template_id = fields.Many2one(
@@ -149,24 +153,47 @@ class ContractContract(models.Model):
) )
return invoices return invoices
@api.depends("pricelist_id", "partner_id", "journal_id", "company_id") def _get_computed_currency(self):
def _compute_currency_id(self): """Helper method for returning the theoretical computed currency."""
for rec in self: self.ensure_one()
currency = self.env['res.currency'] currency = self.env['res.currency']
if any(rec.contract_line_ids.mapped('automatic_price')): if any(self.contract_line_ids.mapped('automatic_price')):
# Use pricelist currency # Use pricelist currency
currency = ( currency = (
rec.pricelist_id.currency_id or self.pricelist_id.currency_id or
rec.partner_id.with_context( self.partner_id.with_context(
force_company=rec.company_id.id, force_company=self.company_id.id,
).property_product_pricelist.currency_id ).property_product_pricelist.currency_id
) )
rec.currency_id = ( return (
currency.id or currency or self.journal_id.currency_id or
rec.journal_id.currency_id.id or self.company_id.currency_id
rec.company_id.currency_id.id
) )
@api.depends(
"manual_currency_id",
"pricelist_id",
"partner_id",
"journal_id",
"company_id",
)
def _compute_currency_id(self):
for rec in self:
if rec.manual_currency_id:
rec.currency_id = rec.manual_currency_id
else:
rec.currency_id = rec._get_computed_currency()
def _inverse_currency_id(self):
"""If the currency is different from the computed one, then save it
in the manual field.
"""
for rec in self:
if rec._get_computed_currency() != rec.currency_id:
rec.manual_currency_id = rec.currency_id
else:
rec.manual_currency_id = False
@api.multi @api.multi
def _compute_invoice_count(self): def _compute_invoice_count(self):
for rec in self: for rec in self:

View File

@@ -2512,3 +2512,39 @@ class TestContract(TestContractBase):
'terminate_comment', 'terminate_comment',
to_date('2018-02-13'), to_date('2018-02-13'),
) )
def test_currency(self):
currency_eur = self.env.ref("base.EUR")
currency_cad = self.env.ref("base.CAD")
# Get currency from company
self.contract2.journal_id = False
self.assertEqual(
self.contract2.currency_id, self.contract2.company_id.currency_id)
# Get currency from journal
journal = self.env["account.journal"].create({
"name": "Test journal CAD",
"code": "TCAD",
"type": "sale",
"currency_id": currency_cad.id,
})
self.contract2.journal_id = journal.id
self.assertEqual(self.contract2.currency_id, currency_cad)
# Get currency from contract pricelist
pricelist = self.env['product.pricelist'].create({
"name": "Test pricelist",
"currency_id": currency_eur.id,
})
self.contract2.pricelist_id = pricelist.id
self.contract2.contract_line_ids.automatic_price = True
self.assertEqual(self.contract2.currency_id, currency_eur)
# Get currency from partner pricelist
self.contract2.pricelist_id = False
self.contract2.partner_id.property_product_pricelist = pricelist.id
pricelist.currency_id = currency_cad.id
self.assertEqual(self.contract2.currency_id, currency_cad)
# Assign manual currency
self.contract2.manual_currency_id = currency_eur.id
self.assertEqual(self.contract2.currency_id, currency_eur)
# Assign same currency as computed one
self.contract2.currency_id = currency_cad.id
self.assertFalse(self.contract2.manual_currency_id)