Search code examples
python-3.xodooodoo-11

How to add two float fields with different currency sign?


We have already Amount field on invoice lines which shows the amount in the currency selected on invoice. I want to convert the same amount to base currency and also show the base currency amount on invoice lines with base currency symbol.


Solution

  • For this purpose I have added new many2one field for base currency as show below:

    base_currency_id = fields.Many2one('res.currency', default=lambda self: self.invoice_id.company_id.currency_id.id)
    

    Then I added new float field for computing the amount in base currency like this:

    @api.onchange('price_subtotal', 'invoice_id.currency_id')
    def compute_amount_in_base_currency(self):
        company_currency = self.invoice_id.company_id.currency_id
        for l in self:
            amount_in_base = l.currency_id.compute(l.price_subtotal, company_currency)
            l.amount_in_base = amount_in_base
    
    amount_in_base = fields.Float('Base Amount', readonly=True, compute='compute_amount_in_base_currency')
    

    In xml file I have added base_currency_id field and made it invisible. Then added amount_in_base field to the view with widget='monetary' and options="{'currency_field': 'base_currency_id'}". My xml file looks like this:

    <xpath expr="//field[@name='invoice_line_ids']/tree/field[@name='price_subtotal']" position="after">
        <field name="base_currency_id" invisible="True"/>
        <field name="amount_in_base" widget="monetary" options="{'currency_field': 'base_currency_id'}"/>
    </xpath>