Skip to content

Commit

Permalink
[BKP] account_invoice_show_currency_rate
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanSForgeFlow committed Nov 19, 2024
1 parent e25d560 commit 75ec7d1
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 80 deletions.
7 changes: 5 additions & 2 deletions account_invoice_show_currency_rate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
{
"name": "Account Invoice Show Currency Rate",
"summary": "Show currency rate in invoices.",
"version": "13.0.1.0.3",
"version": "12.0.1.0.0",
"category": "Accounting & Finance",
"website": "https://github.com/OCA/account-invoicing",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": ["account"],
"maintainers": ["victoralmau"],
"data": ["views/account_move_view.xml"],
"data": [
"views/account_move_view.xml",
"views/account_invoice_view.xml",
],
}
1 change: 1 addition & 0 deletions account_invoice_show_currency_rate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import account_move
from . import account_invoice
53 changes: 53 additions & 0 deletions account_invoice_show_currency_rate/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2021 Tecnativa - Víctor Martínez
# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models


class AccountInvoice(models.Model):
_inherit = "account.invoice"

currency_rate_amount = fields.Float(
string="Rate amount", compute="_compute_currency_rate_amount", digits=0,
)
show_currency_rate_amount = fields.Boolean(
compute="_compute_show_currency_rate_amount", readonly=True
)

@api.depends(
"state",
"date",
"amount_total_company_signed",
"amount_total_signed",
"company_id",
"currency_id",
"show_currency_rate_amount",
)
def _compute_currency_rate_amount(self):
""" It's necessary to define value according to some cases:
- Case A: Currency is equal to company currency (Value = 1)
- Case B: Invoice exist previously (confirmed) and get real rate
according to amount
- Case C: Get expected rate (according to date)
to show some value in creation.
"""
for item in self:
item.currency_rate_amount = 1
if not item.show_currency_rate_amount:
continue
if item.state == 'open':
item.currency_rate_amount = item.currency_id.round(
item.amount_total_signed / item.amount_total_company_signed
)
else:
date = item.date or fields.Date.today()
item.currency_rate_amount = item.currency_id.with_context(
date=date).rate

@api.depends("currency_id", "currency_id.rate_ids", "company_id")
def _compute_show_currency_rate_amount(self):
for item in self:
item.show_currency_rate_amount = (
item.currency_id and item.currency_id != item.company_id.currency_id
)
63 changes: 8 additions & 55 deletions account_invoice_show_currency_rate/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,6 @@
from odoo import api, fields, models


class AccountMove(models.Model):
_inherit = "account.move"

currency_rate_amount = fields.Float(
string="Rate amount", compute="_compute_currency_rate_amount", digits=0,
)
show_currency_rate_amount = fields.Boolean(
compute="_compute_show_currency_rate_amount", readonly=True
)

@api.depends(
"state",
"date",
"line_ids.amount_currency",
"line_ids.balance",
"company_id",
"currency_id",
"show_currency_rate_amount",
)
def _compute_currency_rate_amount(self):
""" It's necessary to define value according to some cases:
- Case A: Currency is equal to company currency (Value = 1)
- Case B: Move exist previously (posted) and get real rate according to lines
- Case C: Get expected rate (according to date) to show some value in creation.
"""
self.currency_rate_amount = 1
for item in self.filtered("show_currency_rate_amount"):
lines = item.line_ids.filtered(lambda x: abs(x.amount_currency) > 0)
if item.state == "posted" and lines:
amount_currency_positive = sum(
[abs(amc) for amc in lines.mapped("amount_currency")]
)
total_balance_positive = sum([abs(b) for b in lines.mapped("balance")])
item.currency_rate_amount = item.currency_id.round(
amount_currency_positive / total_balance_positive
)
else:
rates = item.currency_id._get_rates(item.company_id, item.date)
item.currency_rate_amount = rates.get(item.currency_id.id)

@api.depends("currency_id", "currency_id.rate_ids", "company_id")
def _compute_show_currency_rate_amount(self):
for item in self:
item.show_currency_rate_amount = (
item.currency_id and item.currency_id != item.company_id.currency_id
)


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

Expand All @@ -70,11 +22,13 @@ class AccountMoveLine(models.Model):
def _compute_currency_rate_amount(self):
""" It's necessary to define value according to some cases:
- Case A: Currency is equal to company currency (Value = 1)
- Case B: Move exist previously (posted) and get real rate according to lines
- Case C: Get expected rate (according to date) to show some value in creation.
- Case B: Move exist previously (posted)
and get real rate according to lines
- Case C: Get expected rate (according to date)
to show some value in creation.
"""
self.currency_rate_amount = 1
for item in self:
item.currency_rate_amount = 1
if (
not item.currency_id
or item.currency_id == item.move_id.company_id.currency_id
Expand All @@ -86,7 +40,6 @@ def _compute_currency_rate_amount(self):
amount_currency / abs(item.balance)
)
else:
rates = item.currency_id._get_rates(
item.move_id.company_id, item.move_id.date
)
item.currency_rate_amount = rates.get(item.currency_id.id)
date = item.move_id.date or fields.Date.today()
item.currency_rate_amount = item.currency_id.with_context(
date=date).rate
31 changes: 17 additions & 14 deletions account_invoice_show_currency_rate/tests/test_account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,39 @@ def _create_currency_rate(self, currency_id, name, rate):
)

def _create_invoice(self, currency_id):
move_form = Form(
self.env["account.move"].with_context(default_type="out_invoice")
invoice_form = Form(
self.env["account.invoice"].with_context(default_type="out_invoice")
)
move_form.partner_id = self.partner
move_form.journal_id = self.journal
move_form.invoice_date = fields.Date.from_string("2000-01-01")
move_form.currency_id = currency_id
with move_form.invoice_line_ids.new() as line_form:
invoice_form.partner_id = self.partner
invoice_form.journal_id = self.journal
invoice_form.date_invoice = fields.Date.from_string("2000-01-01")
invoice_form.currency_id = currency_id
with invoice_form.invoice_line_ids.new() as line_form:
line_form.product_id = self.product
invoice = move_form.save()
invoice.action_post()
invoice = invoice_form.save()
invoice.action_invoice_open()
return invoice

def test_01_invoice_currency(self):
self.partner.property_product_pricelist = self.pricelist_currency
invoice = self._create_invoice(self.currency)
self.assertAlmostEqual(invoice.currency_rate_amount, 1.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate_amount, 1.0, 2)
move = invoice.move_id
self.assertAlmostEqual(move.line_ids[0].currency_rate_amount, 1.0, 2)

def test_02_invoice_currency_extra(self):
self.partner.property_product_pricelist = self.pricelist_currency_extra
invoice = self._create_invoice(self.currency_extra)
self.assertAlmostEqual(invoice.currency_rate_amount, 2.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate_amount, 2.0, 2)
move = invoice.move_id
self.assertAlmostEqual(move.line_ids[0].currency_rate_amount, 2.0, 2)
rate_custom = self.currency_extra.rate_ids.filtered(
lambda x: x.name == fields.Date.from_string("2000-01-01")
)
rate_custom.rate = 3.0
self.assertAlmostEqual(invoice.currency_rate_amount, 2.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate_amount, 2.0, 2)
invoice.button_draft()
self.assertAlmostEqual(move.line_ids[0].currency_rate_amount, 2.0, 2)
self.journal.write({'update_posted': True})
invoice.action_invoice_cancel()
invoice.action_invoice_draft()
self.assertAlmostEqual(invoice.currency_rate_amount, 3.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate_amount, 3.0, 2)
22 changes: 22 additions & 0 deletions account_invoice_show_currency_rate/views/account_invoice_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
-->
<odoo>
<record id="account_invoice_form_inherit" model="ir.ui.view">
<field name="name">account.invoice.form.inherit</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form" />
<field name="arch" type="xml">
<xpath expr="//label[@for='currency_id']" position="before">
<field name="show_currency_rate_amount" invisible="1" />
<field
name="currency_rate_amount"
groups="base.group_multi_currency"
digits="[12,12]"
attrs="{'invisible':[('show_currency_rate_amount', '=', False)]}"
/>
</xpath>
</field>
</record>
</odoo>
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<field name="currency_id" position="before">
<field name="show_currency_rate_amount" invisible="1" />
<field
name="currency_rate_amount"
groups="base.group_multi_currency"
digits="[12,12]"
attrs="{'invisible':[('show_currency_rate_amount', '=', False)]}"
/>
</field>
<xpath
expr="//field[@name='line_ids']/tree/field[@name='currency_id']"
position="after"
Expand Down
6 changes: 6 additions & 0 deletions setup/account_invoice_show_currency_rate/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 75ec7d1

Please sign in to comment.