Skip to content

Commit

Permalink
Merge 4d3ab73 into 5b1bd99
Browse files Browse the repository at this point in the history
  • Loading branch information
TDu committed May 14, 2019
2 parents 5b1bd99 + 4d3ab73 commit c550961
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions crm_lead_currency/__init__.py
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions crm_lead_currency/__manifest__.py
@@ -0,0 +1,19 @@
# Copyright 2019 Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'CRM Lead Currency',
'summary': """
On leads/opportunities, add the amount in the customer's currency.""",
'version': '12.0.1.0.0',
'license': 'AGPL-3',
'author': 'Camptocamp SA,Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/crm',
'depends': [
'crm',
],
'data': [
'views/crm_lead_opportunity_currency_views.xml',
],
'installable': True,
}
1 change: 1 addition & 0 deletions crm_lead_currency/models/__init__.py
@@ -0,0 +1 @@
from . import crm_lead
54 changes: 54 additions & 0 deletions crm_lead_currency/models/crm_lead.py
@@ -0,0 +1,54 @@
# Copyright 2018 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import api, fields, models


class CrmLead(models.Model):
_inherit = 'crm.lead'

customer_currency_id = fields.Many2one(
string='Customer currency',
comodel_name='res.currency',
default=lambda self: self.env.user.company_id.currency_id,
)
amount_customer_currency = fields.Monetary(
string='Customer amount',
currency_field='customer_currency_id',
)
is_same_currency = fields.Boolean(
string='Same currency',
compute='_compute_is_same_currency',
)

@api.onchange('customer_currency_id', 'amount_customer_currency')
def _onchange_currency(self):
self.planned_revenue = self.get_revenue_in_company_currency()

@api.multi
def get_revenue_in_company_currency(self):
"""Compute the planned revenue in the company currency.
If the customer currency is different than the company currency,
the planned revenue is computed in the company currency.
"""
self.ensure_one()
if self.is_same_currency:
return self.planned_revenue
return self.customer_currency_id._convert(
self.amount_customer_currency or 0,
self.company_currency,
self.env.user.company_id,
fields.Datetime.now(),
)

@api.multi
@api.depends('customer_currency_id', 'company_id.currency_id')
def _compute_is_same_currency(self):
for lead in self:
lead.is_same_currency = (
lead.customer_currency_id == (
lead.company_currency or
self.env.user.company_id.currency_id
)
)
3 changes: 3 additions & 0 deletions crm_lead_currency/readme/CONFIGURE.rst
@@ -0,0 +1,3 @@
No specific configuration is needed but multi-currency should be enabled for the module
to make sense.

1 change: 1 addition & 0 deletions crm_lead_currency/readme/CONTRIBUTORS.rst
@@ -0,0 +1 @@
* Thierry Ducrest <thierry.ducrest@camptocamp.com>
5 changes: 5 additions & 0 deletions crm_lead_currency/readme/DESCRIPTION.rst
@@ -0,0 +1,5 @@
This module allows to select a specific currency for a lead or an opportunity.
If the selected currency is different to the company currency, an amount in the customer
currency can be set and the expected revenue of the opportunity will automatically be
computed in the company currency. The default rate used for the computation is the rate
of the day.
2 changes: 2 additions & 0 deletions crm_lead_currency/readme/INSTALL.rst
@@ -0,0 +1,2 @@
Just install the module.

3 changes: 3 additions & 0 deletions crm_lead_currency/readme/USAGE.rst
@@ -0,0 +1,3 @@
To use this module, you need to:

#. Created a new opportunity in the CRM and set a customer currency on it.
Binary file added crm_lead_currency/static/description/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions crm_lead_currency/tests/__init__.py
@@ -0,0 +1 @@
from . import test_crm_opportunity_currency
34 changes: 34 additions & 0 deletions crm_lead_currency/tests/test_crm_opportunity_currency.py
@@ -0,0 +1,34 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo.tests.common import SavepointCase


class TestCrmOpportunityCurrency(SavepointCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.lead = cls.env['crm.lead'].create({
'name': 'test lead'
})

def test_is_same_currency(self):
self.lead.customer_currency_id = self.lead.company_id.currency_id
self.assertTrue(self.lead.is_same_currency)
self.lead.customer_currency_id = self.ref('base.CHF')
self.assertFalse(self.lead.is_same_currency)

def test_same_currency_planned_revenue_not_updated(self):
self.lead.customer_currency_id = self.lead.company_id.currency_id
self.lead.planned_revenue = 100
self.lead.amount_customer_currency = 124
self.lead._onchange_currency()
self.assertEqual(self.lead.planned_revenue, 100)

def test_different_currency_planned_revenue_updated(self):
self.lead.planned_revenue = 100
self.lead.customer_currency_id = self.ref('base.CHF')
self.lead.amount_customer_currency = 124
self.lead._onchange_currency()
self.assertNotEqual(self.lead.planned_revenue, 100)
47 changes: 47 additions & 0 deletions crm_lead_currency/views/crm_lead_opportunity_currency_views.xml
@@ -0,0 +1,47 @@
<?xml version="1.0"?>

<odoo>

<record id="crm_case_form_view_oppor" model="ir.ui.view">
<field name="name">crm.lead.form.oppor</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
<field name="arch" type="xml">
<field name="partner_id" position="before">
<field name="customer_currency_id"/>
<field name="is_same_currency" invisible="1"/>
<field name="amount_customer_currency" attrs="{'invisible': [('is_same_currency', '=', True)]}" />
</field>
<field name="planned_revenue" position="attributes">
<attribute name="attrs">{'readonly': [('is_same_currency', '=', False)]}</attribute>
<attribute name="force_save">1</attribute>
</field>
</field>
</record>

<!-- In kanban display the amount in the customer currency as suitable -->
<record id="crm_case_kanban_view_leads" model="ir.ui.view">
<field name="name">crm.lead.kanban.lead</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_kanban_view_leads"/>
<field name="arch" type="xml">

<field name="planned_revenue" position="after">
<field name="is_same_currency"/>
<field name="amount_customer_currency"/>
<field name="customer_currency_id"/>
</field>

<xpath expr="//templates//field[@name='planned_revenue']" position="replace">
<t t-if="record.is_same_currency.raw_value">
<field name="planned_revenue" widget="monetary" options="{'currency_field': 'company_currency'}"/>
</t>
<t t-else="">
<field name="amount_customer_currency" widget="monetary" options="{'currency_field': 'customer_currency_id'}"/>
</t>
</xpath>

</field>
</record>

</odoo>

0 comments on commit c550961

Please sign in to comment.