Skip to content
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

[17.0][mig]stock_account_move_reset_to_draft #1854

Open
wants to merge 5 commits into
base: 17.0
Choose a base branch
from
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
128 changes: 128 additions & 0 deletions stock_account_move_reset_to_draft/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
=================================
Stock account move reset to draft
=================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:906ac39377d605e253d0470fffc49aff72a82685b43306c243c46d1735ba3e1c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github
:target: https://github.com/OCA/account-invoicing/tree/17.0/stock_account_move_reset_to_draft
:alt: OCA/account-invoicing
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/account-invoicing-17-0/account-invoicing-17-0-stock_account_move_reset_to_draft
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/account-invoicing&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows a vendor bill to be reverted to draft status even
when an SVL (Stock Valuation Layer) record is created due to a price
difference between the receipt and the vendor bill, when inventory from
the receipt has yet to be consumed. When a vendor bill is reset to
draft, as applicable, a new SVL record is generated to offset the
valuation difference that was generated upon the bill's confirmation.

**Table of contents**

.. contents::
:local:

Use Cases / Context
===================

Typically, when a purchase invoice is created at a price different from
the price at which the SVLs of the incoming picking were initially
created and confirmed, SVLs are generated to account for the price
difference. Once this happens, the invoice cannot normally be reverted
to draft due to stock_account restrictions.

Imagine international business settings where exchange rates are updated
daily; this limitation can be problematic. For example, if goods are
received on April 17th and the vendor bill is processed on April 18th
with a different exchange rate, an SVL record is generated, locking the
vendor bill. If a user then realizes a mistake, such as processing the
bill for the incorrect purchase order, they are unable to cancel it.

Usage
=====

1. Create a product category with Costing Method: Average Cost (AVCO) or
First In First Out (FIFO).
2. Create a product linked to the category created before.
3. Create a purchase order and adds a product line with quantity 1 and
price 10.
4. Confirms the purchase order and validates the incoming picking.
5. Creates an invoice from the purchase order.
6. Changes the invoice line price to 12 and confirm the invoice.
7. It is possible to reset the invoice to draft (a new SVL record to
offset the existing SVL difference will be created).

When attempting to reset an invoice to draft for a product that has been
partially or fully consumed, the system will display the following error
message: "The inventory has already been (partially) consumed." In that
case, consider using landed costs to adjust the valuation of the product
as necessary.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-invoicing/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/account-invoicing/issues/new?body=module:%20stock_account_move_reset_to_draft%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Tecnativa

Contributors
------------

- `Tecnativa <https://www.tecnativa.com>`__:

- Víctor Martínez
- Pedro M. Baeza

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

.. |maintainer-victoralmau| image:: https://github.com/victoralmau.png?size=40px
:target: https://github.com/victoralmau
:alt: victoralmau

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-victoralmau|

This module is part of the `OCA/account-invoicing <https://github.com/OCA/account-invoicing/tree/17.0/stock_account_move_reset_to_draft>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions stock_account_move_reset_to_draft/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
14 changes: 14 additions & 0 deletions stock_account_move_reset_to_draft/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Stock account move reset to draft",
"author": "Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
"version": "17.0.1.0.0",
# Real dependency is stock_account but we need purchase_stock in tests
"depends": ["purchase_stock"],
"license": "AGPL-3",
"category": "Warehouse Management",
"installable": True,
"maintainers": ["victoralmau"],
}
36 changes: 36 additions & 0 deletions stock_account_move_reset_to_draft/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * stock_account_move_reset_to_draft
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-11 15:02+0000\n"
"PO-Revision-Date: 2024-10-11 15:02+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: stock_account_move_reset_to_draft
#. odoo-python
#: code:addons/stock_account_move_reset_to_draft/models/account_move.py:0
#, python-format
msgid "Inventory valuation records are intertwined for %(line_name)s."
msgstr ""

#. module: stock_account_move_reset_to_draft
#: model:ir.model,name:stock_account_move_reset_to_draft.model_account_move
msgid "Journal Entry"
msgstr "Asiento contable"

#. module: stock_account_move_reset_to_draft
#. odoo-python
#: code:addons/stock_account_move_reset_to_draft/models/account_move.py:0
#, python-format
msgid "The inventory has already been (partially) consumed for %(line_name)s."
msgstr ""
37 changes: 37 additions & 0 deletions stock_account_move_reset_to_draft/i18n/it.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * stock_account_move_reset_to_draft
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"PO-Revision-Date: 2024-11-08 11:06+0000\n"
"Last-Translator: mymage <[email protected]>\n"
"Language-Team: none\n"
"Language: it\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.6.2\n"

#. module: stock_account_move_reset_to_draft
#. odoo-python
#: code:addons/stock_account_move_reset_to_draft/models/account_move.py:0
#, python-format
msgid "Inventory valuation records are intertwined for %(line_name)s."
msgstr ""
"I record di valutazione dell'inventario sono intrecciati per %(line_name)s."

#. module: stock_account_move_reset_to_draft
#: model:ir.model,name:stock_account_move_reset_to_draft.model_account_move
msgid "Journal Entry"
msgstr "Registrazione contabile"

#. module: stock_account_move_reset_to_draft
#. odoo-python
#: code:addons/stock_account_move_reset_to_draft/models/account_move.py:0
#, python-format
msgid "The inventory has already been (partially) consumed for %(line_name)s."
msgstr "L'inventario è già stato (parzialmente) consumato per %(line_name)s."
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * stock_account_move_reset_to_draft
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: stock_account_move_reset_to_draft
#. odoo-python
#: code:addons/stock_account_move_reset_to_draft/models/account_move.py:0
#, python-format
msgid "Inventory valuation records are intertwined for %(line_name)s."
msgstr ""

#. module: stock_account_move_reset_to_draft
#: model:ir.model,name:stock_account_move_reset_to_draft.model_account_move
msgid "Journal Entry"
msgstr ""

#. module: stock_account_move_reset_to_draft
#. odoo-python
#: code:addons/stock_account_move_reset_to_draft/models/account_move.py:0
#, python-format
msgid "The inventory has already been (partially) consumed for %(line_name)s."
msgstr ""
3 changes: 3 additions & 0 deletions stock_account_move_reset_to_draft/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import account_move
73 changes: 73 additions & 0 deletions stock_account_move_reset_to_draft/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2024 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_is_zero


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

def button_draft(self):
"""If it is a purchase invoice, we will create a new SVL for each line with
the sum of the value in opposite sign.
"""
for item in self.sudo().filtered(
lambda x: x.is_inbound
and any(line.stock_valuation_layer_ids for line in x.line_ids)
):
for line in item.line_ids.filtered("stock_valuation_layer_ids"):
origin_svls = line.stock_valuation_layer_ids.stock_valuation_layer_id
if (
len(
origin_svls.stock_valuation_layer_ids.account_move_line_id.filtered(
lambda x: x.parent_state == "posted"
)
)
> 1
):
raise UserError(
_(
"Inventory valuation records are intertwined for \
%(line_name)s.",
line_name=line.display_name,
)
)
for origin_svl in origin_svls:
if origin_svl.quantity != origin_svl.remaining_qty:
raise UserError(

Check warning on line 39 in stock_account_move_reset_to_draft/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

stock_account_move_reset_to_draft/models/account_move.py#L39

Added line #L39 was not covered by tests
_(
"The inventory has already been (partially) consumed "
"for %(line_name)s.",
line_name=line.display_name,
)
)
svls = origin_svl.stock_valuation_layer_ids
value = sum(svls.mapped("value"))
if not float_is_zero(
value, precision_rounding=line.currency_id.rounding
):
origin_svl.remaining_value -= value
revert_svl = svls[0].copy({"value": -value})
revert_svl._validate_accounting_entries()
product = line.product_id.with_company(item.company_id.id)
if product.cost_method == "average":
product.sudo().with_context(disable_auto_svl=True).write(
{"standard_price": product.value_svl / product.quantity_svl}
)
return super().button_draft()

def _compute_show_reset_to_draft_button(self):
"""Overwrite the value only if it is already posted and with SVLs.
We use the same fields for filtering that account uses for the
show_reset_to_draft_button field.
"""
_self = self.sudo().filtered(
lambda x: not x.restrict_mode_hash_table
and x.state in ("posted", "cancel")
and any(line.stock_valuation_layer_ids for line in x.line_ids)
)
for item in self:
item.show_reset_to_draft_button = True
return super(AccountMove, self - _self)._compute_show_reset_to_draft_button()
3 changes: 3 additions & 0 deletions stock_account_move_reset_to_draft/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
12 changes: 12 additions & 0 deletions stock_account_move_reset_to_draft/readme/CONTEXT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Typically, when a purchase invoice is created at a price different from
the price at which the SVLs of the incoming picking were initially
created and confirmed, SVLs are generated to account for the price
difference. Once this happens, the invoice cannot normally be reverted
to draft due to stock_account restrictions.

Imagine international business settings where exchange rates are updated
daily; this limitation can be problematic. For example, if goods are
received on April 17th and the vendor bill is processed on April 18th
with a different exchange rate, an SVL record is generated, locking the
vendor bill. If a user then realizes a mistake, such as processing the
bill for the incorrect purchase order, they are unable to cancel it.
3 changes: 3 additions & 0 deletions stock_account_move_reset_to_draft/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [Tecnativa](https://www.tecnativa.com):
- Víctor Martínez
- Pedro M. Baeza
6 changes: 6 additions & 0 deletions stock_account_move_reset_to_draft/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This module allows a vendor bill to be reverted to draft status even
when an SVL (Stock Valuation Layer) record is created due to a price
difference between the receipt and the vendor bill, when inventory from
the receipt has yet to be consumed. When a vendor bill is reset to
draft, as applicable, a new SVL record is generated to offset the
valuation difference that was generated upon the bill's confirmation.
16 changes: 16 additions & 0 deletions stock_account_move_reset_to_draft/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1. Create a product category with Costing Method: Average Cost (AVCO)
or First In First Out (FIFO).
2. Create a product linked to the category created before.
3. Create a purchase order and adds a product line with quantity 1 and
price 10.
4. Confirms the purchase order and validates the incoming picking.
5. Creates an invoice from the purchase order.
6. Changes the invoice line price to 12 and confirm the invoice.
7. It is possible to reset the invoice to draft (a new SVL record to
offset the existing SVL difference will be created).

When attempting to reset an invoice to draft for a product that has been
partially or fully consumed, the system will display the following error
message: "The inventory has already been (partially) consumed." In that
case, consider using landed costs to adjust the valuation of the product
as necessary.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading