-
Notifications
You must be signed in to change notification settings - Fork 1
[6131][ADD] purchase_deposit_currency #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kanda999
wants to merge
3
commits into
16.0
Choose a base branch
from
16.0-add-purchase_deposit_currency
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.