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 d0e10c4
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 33 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
51 changes: 51 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,51 @@
# Copyright 2021 Tecnativa - Víctor Martínez
# 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 item.show_currency_rate_amount:
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()
rates = item.currency_id._get_rates(item.company_id, 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
)
35 changes: 25 additions & 10 deletions account_invoice_show_currency_rate/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,42 @@ class AccountMove(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.filtered("show_currency_rate_amount"):
for item in self:
item.currency_rate_amount = 1
if not item.show_currency_rate_amount:
continue
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")])
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")
@api.depends(
"currency_id",
"currency_id.rate_ids",
"company_id"
)
def _compute_show_currency_rate_amount(self):
for item in self:
invoice_id = self.env["account.invoice"].search(
[("move_id", "=", item.id)], limit=1
)
item.show_currency_rate_amount = (
item.currency_id and item.currency_id != item.company_id.currency_id
item.currency_id and item.currency_id != invoice_id.currency_id
)


Expand All @@ -70,11 +83,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 Down
35 changes: 15 additions & 20 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,31 @@ 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 = 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.currency_rate_amount, 1.0, 2)
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)
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(invoice.currency_rate_amount, 3.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate_amount, 3.0, 2)
move = invoice.move_id
self.assertAlmostEqual(move.currency_rate_amount, 2.0, 2)
self.assertAlmostEqual(move.line_ids[0].currency_rate_amount, 2.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,7 +8,7 @@ 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="journal_id" position="after">
<field name="show_currency_rate_amount" invisible="1" />
<field
name="currency_rate_amount"
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 d0e10c4

Please sign in to comment.