Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions purchase_deposit_currency/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright 2026 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import models
20 changes: 20 additions & 0 deletions purchase_deposit_currency/__manifest__.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat as per our standard and I think summary should be one line brief description.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2026 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Purchase Deposit Multi-Currency",
"version": "16.0.2.2.0",
"author": "Quartile Limited",
"website": "https://www.quartile.co",
"category": "Purchase",
"license": "AGPL-3",
"summary": "Let each vendor-bill line carry a manually-entered "
"company-currency amount so foreign-currency deposits and invoices "
"post the exact JPY (company-currency) value the user actually paid, "
"bypassing Odoo's exchange-rate conversion. The deposit value is "
"carried over to the deposit-offset line of the final invoice.",
"depends": ["purchase_deposit"],
"data": [
"views/account_move_views.xml",
],
"installable": True,
}
5 changes: 5 additions & 0 deletions purchase_deposit_currency/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2026 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import account_move
from . import account_move_line
from . import purchase_order_line
114 changes: 114 additions & 0 deletions purchase_deposit_currency/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright 2026 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, models


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

def _moves_needing_deposit_company_adjustment(self):
return self.filtered(
lambda m: m.move_type in ("in_invoice", "in_refund")
and m.currency_id != m.company_id.currency_id
and m.line_ids.filtered(
lambda l: l.display_type == "product"
and l.purchase_line_id.is_deposit
and l.purchase_line_id.deposit_company_amount
and l.quantity < 0
)
)

def _apply_deposit_company_adjustment(self):
"""Push the deposit's exchange-rate difference onto the product
line(s) of the final invoice.

The deposit-offset line's company-currency balance is pinned to the
JPY actually paid (``deposit_company_amount``), while the product
lines are booked at the current rate. The gap between the deposit
valued at the current rate and the JPY actually paid is the
rate-difference; it belongs in the goods' acquisition cost. We absorb
it into the product lines' ``company_amount`` so the goods value (and
SVL) reflects the true cost and the auto-balanced payable equals the
remaining foreign amount at the current rate.
"""
for move in self:
company_currency = move.company_id.currency_id
offset_lines = move.line_ids.filtered(
lambda l: l.display_type == "product"
and l.purchase_line_id.is_deposit
and l.purchase_line_id.deposit_company_amount
and l.quantity < 0
)
# delta = deposit-at-current-rate - JPY actually paid (pinned
# balance). Signed, so it works for in_invoice and in_refund.
total_delta = sum(
offset._deposit_natural_balance() - offset.balance
for offset in offset_lines
)
if company_currency.is_zero(total_delta):
continue
product_lines = move.line_ids.filtered(
lambda l: l.display_type == "product"
and l.purchase_line_id
and not l.purchase_line_id.is_deposit
# Auto-fill empty lines and lines we filled before (rate may
# have changed); never clobber a manual override.
and (not l.company_amount or l.deposit_amount_adjusted)
)
if not product_lines:
continue
naturals = {l.id: l._deposit_natural_balance() for l in product_lines}
total_weight = sum(abs(v) for v in naturals.values())
line_count = len(product_lines)
remaining = total_delta
for idx, line in enumerate(product_lines):
if idx < line_count - 1:
if total_weight:
weight = abs(naturals[line.id]) / total_weight
raw_share = total_delta * weight
else:
raw_share = total_delta / line_count
share = company_currency.round(raw_share)
remaining -= share
else:
# last line absorbs the rounding remainder
share = company_currency.round(remaining)
target = company_currency.round(naturals[line.id] + share)
line.with_context(skip_deposit_company_adjustment=True).write(
{"company_amount": target, "deposit_amount_adjusted": True}
)

@api.model_create_multi
def create(self, vals_list):
moves = super().create(vals_list)
target = moves._moves_needing_deposit_company_adjustment()
target._apply_deposit_company_adjustment()
return moves

def write(self, vals):
res = super().write(vals)
if not self.env.context.get("skip_deposit_company_adjustment"):
target = self._moves_needing_deposit_company_adjustment()
target._apply_deposit_company_adjustment()
return res

def action_post(self):
"""When a deposit vendor bill is posted, capture the company-currency
value of its deposit line and store it on the linked PO deposit
line. The standard purchase_deposit offset on the final invoice then
re-applies that value (see
``purchase.order.line._prepare_account_move_line``).
"""
deposit_writes = []
for move in self:
for line in move.line_ids:
po_line = line.purchase_line_id
if not po_line or not po_line.is_deposit:
continue
amount = line.company_amount or abs(line.balance)
if amount:
deposit_writes.append((po_line, amount))
Comment on lines +109 to +110

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it consider both deposit bill and normal vendor bill?

res = super().action_post()
for po_line, amount in deposit_writes:
po_line.deposit_company_amount = amount
return res
94 changes: 94 additions & 0 deletions purchase_deposit_currency/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright 2026 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models
from odoo.tools.float_utils import float_is_zero


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

company_amount = fields.Monetary(
string="Company Currency Amount",
currency_field="company_currency_id",
help="Manually-entered company-currency value of this line. "
"When set (non-zero), the line's balance is forced to this value, "
"bypassing the standard amount_currency × exchange_rate "
"calculation. Useful for foreign-currency vendor bills where you "
"actually paid an exact JPY amount that doesn't match today's "
"exchange rate.",
)
deposit_amount_adjusted = fields.Boolean(
copy=False,
help="Set automatically when this product line's company_amount was "
"filled by the deposit rate-difference adjustment on the final "
"invoice. Lets the value be recomputed when the rate changes without "
"mistaking it for a manual override.",
)

def _deposit_natural_balance(self):
"""Rate-based company-currency value of this line, i.e. what
``balance`` would be without any ``company_amount`` override. Used by
the deposit rate-difference adjustment so its computation stays
idempotent regardless of overrides already applied.
"""
self.ensure_one()
if not self.currency_rate:
return self.balance
return self.amount_currency / self.currency_rate

@api.onchange("company_amount")
def _onchange_company_amount(self):
for line in self:
if not line.company_amount:
continue
line._apply_company_amount_override()

def _apply_company_amount_override(self):
"""Force the line's balance to ``company_amount`` with the sign that
matches the line's intended direction. The companion AP / receivable
line is auto-balanced by Odoo from the sum of the other lines.
"""
self.ensure_one()
if not self.company_amount:
return
rounding = self.company_currency_id.rounding
amount_currency_positive = self.amount_currency >= 0
target = abs(self.company_amount)
signed_balance = target if amount_currency_positive else -target
if float_is_zero(self.balance - signed_balance, precision_rounding=rounding):
return
self.balance = signed_balance

def write(self, vals):
res = super().write(vals)
if (
"company_amount" in vals
or "amount_currency" in vals
or "price_unit" in vals
or "quantity" in vals
):
for line in self.filtered("company_amount"):
line._apply_company_amount_override()
return res

@api.model_create_multi
def create(self, vals_list):
lines = super().create(vals_list)
for line in lines.filtered("company_amount"):
line._apply_company_amount_override()
return lines

def _get_gross_unit_price(self):
# Make purchase_stock's price-diff logic (which divides by currency_rate)
# see the company_amount override, so SVL/AML adjustments are generated.
res = super()._get_gross_unit_price()
if (
self.company_amount
and self.quantity
and self.currency_rate
and self.currency_id != self.company_currency_id
and self.move_id.move_type in ("in_invoice", "in_refund")
):
sign = -1 if self.move_id.move_type == "in_refund" else 1
return abs(self.company_amount) / self.quantity * self.currency_rate * sign
return res
32 changes: 32 additions & 0 deletions purchase_deposit_currency/models/purchase_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2026 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models


class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

company_currency_id = fields.Many2one(
"res.currency",
related="company_id.currency_id",
string="Company Currency",
readonly=True,
)
deposit_company_amount = fields.Monetary(
string="Deposit Company-Currency Amount",
currency_field="company_currency_id",
copy=False,
help="Company-currency value carried over from the deposit vendor "
"bill. Used to populate ``company_amount`` on the negative "
"deposit-offset line of the final invoice so the JPY amount "
"matches what was actually paid.",
)

def _prepare_account_move_line(self, move=False):
res = super()._prepare_account_move_line(move=move)
if self.is_deposit and self.deposit_company_amount:
# purchase_deposit flips quantity to -1 for the offset line.
# The corresponding company-currency value must also flip
# so balance lands at -<deposit JPY> on the final invoice.
res["company_amount"] = -self.deposit_company_amount
return res
Loading
Loading