Skip to content

Commit

Permalink
[MIG] account_manual_currency: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiagoMForgeFlow committed Jan 20, 2025
1 parent 15afc23 commit c09f480
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 107 deletions.
8 changes: 6 additions & 2 deletions account_manual_currency/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@

{
"name": "Account - Manual Currency",
"version": "15.0.1.0.0",
"version": "17.0.1.0.0",
"category": "Accounting & Finance",
"summary": "Allows to manual currency of Accounting",
"author": "Ecosoft, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
"license": "AGPL-3",
"depends": ["account"],
"data": ["data/decimal_data.xml", "views/account_move_view.xml"],
"data": [
"data/decimal_data.xml",
"views/account_move_view.xml",
"wizard/account_payment_register_views.xml",
],
"installable": True,
"maintainer": ["Saran440"],
}
83 changes: 26 additions & 57 deletions account_manual_currency/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,15 @@
class AccountMove(models.Model):
_inherit = "account.move"

manual_currency = fields.Boolean(
readonly=True,
states={"draft": [("readonly", False)]},
)
manual_currency = fields.Boolean()
is_manual = fields.Boolean(compute="_compute_currency")
type_currency = fields.Selection(
selection=lambda self: self._get_label_currency_name(),
default=lambda self: self._get_label_currency_name()[0][0],
readonly=True,
states={"draft": [("readonly", False)]},
)
manual_currency_rate = fields.Float(
digits="Manual Currency",
tracking=True,
readonly=True,
states={"draft": [("readonly", False)]},
help="Set new currency rate to apply on the invoice\n."
"This rate will be taken in order to convert amounts between the "
"currency on the purchase order and last currency",
Expand Down Expand Up @@ -97,11 +90,19 @@ def _onchange_currency_change_rate(self):
if self.type_currency == "inverse_company_rate":
amount_currency = 1.0 / amount_currency
self.manual_currency_rate = amount_currency
self.line_ids._onchange_amount_currency()

@api.onchange("manual_currency_rate")
def _onchange_manual_currency_rate(self):
self.line_ids._onchange_amount_currency()
last_rate = self.env["res.currency.rate"]._get_last_rates_for_companies(
self.company_id | self.env.company
)
for move in self:
if move.manual_currency and not move.manual_currency_rate:
move.manual_currency_rate = (
last_rate[self.company_id]
if move.type_currency == "inverse_company_rate"
else (1.0 / last_rate[self.company_id])
)

@api.depends("currency_id")
def _compute_currency(self):
Expand All @@ -116,13 +117,9 @@ def action_refresh_currency(self):
return True

@api.model
def _fields_view_get(
self, view_id=None, view_type="form", toolbar=False, submenu=False
):
def get_view(self, view_id=None, view_type="form", **options):
"""Change string name to company currency"""
result = super()._fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu
)
result = super().get_view(view_id=view_id, view_type=view_type, **options)
if view_type == "form":
company_currency_name = (
self.env["res.company"].browse(self._context.get("company_id"))
Expand All @@ -140,47 +137,19 @@ def _fields_view_get(
class AccountMoveLine(models.Model):
_inherit = "account.move.line"

@api.onchange("amount_currency")
def _onchange_amount_currency(self):
@api.depends(
"currency_id", "company_id", "move_id.date", "move_id.manual_currency_rate"
)
def _compute_currency_rate(self):
res = super()._compute_currency_rate()
for line in self:
if line.move_id.manual_currency:
rate = (
line.move_id.manual_currency_rate
if line.move_id.type_currency == "inverse_company_rate"
else (1.0 / line.move_id.manual_currency_rate)
)
balance = line.amount_currency * rate
line.debit = balance if balance > 0.0 else 0.0
line.credit = -balance if balance < 0.0 else 0.0

if not line.move_id.is_invoice(include_receipts=True):
continue

line.update(line._get_fields_onchange_balance())
line.update(line._get_price_total_and_subtotal())
else:
super(AccountMoveLine, line)._onchange_amount_currency()
return

@api.model
def _get_fields_onchange_subtotal_model(
self, price_subtotal, move_type, currency, company, date
):
if self.move_id.manual_currency:
sign = -1 if move_type in self.move_id.get_inbound_types() else 1
amount_currency = price_subtotal * sign
if not line.move_id.manual_currency:
continue
# Currency Rate on move line use 'company_rate'
rate = (
self.move_id.manual_currency_rate
if self.move_id.type_currency == "inverse_company_rate"
else (1.0 / self.move_id.manual_currency_rate)
line.move_id._origin.manual_currency_rate
if line.move_id.type_currency == "company_rate"
else (1.0 / line.move_id._origin.manual_currency_rate)
)
balance = price_subtotal * rate
return {
"amount_currency": amount_currency,
"currency_id": currency.id,
"debit": balance > 0.0 and balance or 0.0,
"credit": balance < 0.0 and -balance or 0.0,
}
return super()._get_fields_onchange_subtotal_model(
price_subtotal, move_type, currency, company, date
)
line.currency_rate = rate
return res
6 changes: 3 additions & 3 deletions account_manual_currency/tests/test_account_manual_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_01_account_move_inverse_currency(self):
10,
"inverse_company_rate",
)
invoice1._fields_view_get()
invoice1.get_view()
self.assertTrue(invoice1.is_manual)
# Currency will recompute to default
self.assertEqual(invoice1.manual_currency_rate, 10)
Expand All @@ -78,7 +78,7 @@ def test_01_account_move_inverse_currency(self):
self.assertAlmostEqual(
total_currency_not_manual,
sum(
invoice1.line_ids.filtered(lambda line: line.product_id).mapped(
invoice1.line_ids.filtered(lambda line: line.balance > 0).mapped(
"balance"
)
),
Expand All @@ -94,7 +94,7 @@ def test_01_account_move_inverse_currency(self):
self.assertAlmostEqual(
invoice1.total_company_currency,
sum(
invoice1.line_ids.filtered(lambda line: line.product_id).mapped(
invoice1.line_ids.filtered(lambda line: line.balance > 0).mapped(
"balance"
)
),
Expand Down
31 changes: 13 additions & 18 deletions account_manual_currency/views/account_move_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,35 @@
<label
for="manual_currency"
groups="base.group_multi_currency"
attrs="{'invisible': [('is_manual', '=', False)]}"
invisible="not is_manual"
/>
<div
groups="base.group_multi_currency"
attrs="{'invisible': [('is_manual', '=', False)]}"
>
<field name="manual_currency" />
<div groups="base.group_multi_currency" invisible="not is_manual">
<field name="manual_currency" readonly="state != 'draft'" />
<label for="manual_currency_rate" invisible='1' />
<div
class="o_row"
attrs="{'invisible': [('manual_currency', '=', False)]}"
>
<field name="manual_currency_rate" />
<div class="o_row" invisible="not manual_currency">
<field
name="manual_currency_rate"
readonly="state != 'draft'"
/>
<field
name="type_currency"
attrs="{'required': [('manual_currency', '=', True)]}"
required="manual_currency"
readonly="state != 'draft'"
/>
<button
aria-label="Opened"
type="object"
name="action_refresh_currency"
class="btn-link"
states="draft"
invisible="state != 'draft'"
>
<i title="Update Currency" class="fa fa-fw fa-refresh" />
</button>
</div>
</div>
</xpath>
<xpath expr="//field[@name='tax_totals_json']" position="before">
<field
name="total_company_currency"
attrs="{'invisible': [('currency_diff', '=', False)]}"
/>
<xpath expr="//field[@name='tax_totals']" position="before">
<field name="total_company_currency" invisible="not currency_diff" />
<field name="company_currency_id" invisible="1" />
<field name="currency_diff" invisible="1" />
</xpath>
Expand Down
67 changes: 40 additions & 27 deletions account_manual_currency/wizard/account_payment_register.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError


class AccountPaymentRegister(models.TransientModel):
_inherit = "account.payment.register"

currency_diff = fields.Boolean(
compute="_compute_currency_diff",
store=True,
)
manual_currency = fields.Boolean()
type_currency = fields.Selection(
selection=lambda self: self.line_ids.move_id._get_label_currency_name(),
)
manual_currency_rate = fields.Float(
digits="Manual Currency",
help="Set new currency rate to apply on the invoice\n."
"This rate will be taken in order to convert amounts between the "
"currency on the purchase order and last currency",
)

@api.depends("currency_id")
def _compute_currency_diff(self):
for rec in self:
rec.currency_diff = rec.company_currency_id != rec.currency_id

@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
Expand All @@ -27,36 +47,29 @@ def default_get(self, fields_list):
"with the same manual currency."
)
)
res["manual_currency"] = moves.mapped("manual_currency")[0]
if len(list(set(moves.mapped("manual_currency_rate")))) == 1:
res["manual_currency_rate"] = moves.mapped("manual_currency_rate")[0]
if len(list(set(moves.mapped("type_currency")))) == 1:
res["type_currency"] = moves.mapped("type_currency")[0]
return res

def _init_payments(self, to_process, edit_mode=False):
"""Update currency rate on move line payment"""
payments = super()._init_payments(to_process, edit_mode)
if self._context.get("active_model") == "account.move":
if self._context.get("active_model") == "account.move" and self.manual_currency:
for vals in to_process:
lines = vals["to_reconcile"]
payment = vals["payment"]
origin_move = lines.mapped("move_id")
# Not allow group payments for case manual currency
if (
self.group_payment
and len(origin_move) != 1
and len({move.manual_currency_rate for move in origin_move}) != 1
):
raise UserError(
_(
"You can't register a payment for invoices "
"belong to multiple manual currency rate."
)
)
if all(move.manual_currency for move in origin_move):
payment.move_id.write(
{
"manual_currency": origin_move[0].manual_currency,
"type_currency": origin_move[0].type_currency,
"manual_currency_rate": origin_move[0].manual_currency_rate,
}
)
payment.move_id.with_context(
check_move_validity=False
).line_ids._onchange_amount_currency()
payment.move_id.write(
{
"manual_currency": self.manual_currency,
"type_currency": self.type_currency,
"manual_currency_rate": self.manual_currency_rate,
}
)
payment_ml = payment.move_id.line_ids
payment_ml._compute_currency_rate()
payment_ml.with_context(
check_move_validity=False
)._inverse_amount_currency()
return payments
26 changes: 26 additions & 0 deletions account_manual_currency/wizard/account_payment_register_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_account_payment_register_form" model="ir.ui.view">
<field name="name">account.payment.register.form</field>
<field name="model">account.payment.register</field>
<field name="inherit_id" ref="account.view_account_payment_register_form" />
<field name="arch" type="xml">
<xpath expr="//group[@name='group2']" position="inside">
<field name="currency_diff" invisible="1" />
<label
for="manual_currency"
groups="base.group_multi_currency"
invisible="not currency_diff"
/>
<div groups="base.group_multi_currency" invisible="not currency_diff">
<field name="manual_currency" />
<label for="manual_currency_rate" invisible='1' />
<div class="o_row" invisible="not manual_currency">
<field name="manual_currency_rate" />
<field name="type_currency" required="manual_currency" />
</div>
</div>
</xpath>
</field>
</record>
</odoo>

0 comments on commit c09f480

Please sign in to comment.