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

[16.0][ADD] - sale_order_line_cancel #2357

Open
wants to merge 10 commits into
base: 16.0
Choose a base branch
from
96 changes: 96 additions & 0 deletions sale_order_line_cancel/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
======================
Sale Order Line Cancel
======================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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%2Fsale--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/sale-workflow/tree/16.0/sale_order_line_cancel
:alt: OCA/sale-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-workflow-16-0/sale-workflow-16-0-sale_order_line_cancel
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/sale-workflow&target_branch=16.0
:alt: Try me on Runboat

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

This module allows you to cancel the remaining quantity on sale order by adding
a dedicated action to sale lines. It also add two new fields to track canceled
and remaining to deliver quantities.

This module differs from the original odoo behavior in the following way:

* In odoo, if the update of the quantity ordered is allowed on the sale order at
the confirmed state, odoo will recompute the required stock operations
according to the new quantity. This change is possible
even the stock operations are started for this sale order line.
* In this module, the quantity ordered is not updated on the sale order line to
keep track of the original ordered by the customer. At the same time, we
cancel only the stock moves for the remaining qty to deliver. This is only
possible if no operation is started for this sale order line.


.. warning::

It's not recommended to use this module if the update of the quantity ordered
on the sale order line is allowed the confirmed state. This could lead to
unpredictable behavior.

**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-workflow/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_order_line_cancel%0Aversion:%2016.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
~~~~~~~

* Camptocamp
* ACSONE SA/NV

Contributors
~~~~~~~~~~~~

* Sylvain Van Hoof <[email protected]>
* Jacques-Etienne Baudoux (BCIM) <[email protected]>
* Souheil Bejaoui <[email protected]>
sbejaoui marked this conversation as resolved.
Show resolved Hide resolved

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.

This module is part of the `OCA/sale-workflow <https://github.com/OCA/sale-workflow/tree/16.0/sale_order_line_cancel>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions sale_order_line_cancel/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizards
20 changes: 20 additions & 0 deletions sale_order_line_cancel/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# © 2016 Sylvain Van Hoof
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Sale Order Line Cancel",
"version": "16.0.1.0.0",
"author": "Okia, BCIM, Camptocamp, ACSONE SA/NV, Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Sales",
"summary": """Sale cancel remaining""",
"depends": ["sale_stock"],
"data": [
"security/sale_order_line_cancel.xml",
"wizards/sale_order_line_cancel.xml",
"views/sale_order.xml",
"views/sale_order_line.xml",
],
"website": "https://github.com/OCA/sale-workflow",
}
4 changes: 4 additions & 0 deletions sale_order_line_cancel/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import sale_order_line
from . import stock_move
from . import sale_order
from . import stock_picking
34 changes: 34 additions & 0 deletions sale_order_line_cancel/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class SaleOrder(models.Model):

_inherit = "sale.order"

def action_draft(self):
res = super().action_draft()
orders = self.filtered(lambda s: s.state == "draft")
orders.order_line.write({"product_qty_canceled": 0})
return res

def _action_cancel(self):
orders = self.filtered(lambda s: s.state != "cancel")
orders_with_picking = orders.filtered("picking_ids")
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this is better as in sale_stock they use qty_delivered_method:

Suggested change
orders_with_picking = orders.filtered("picking_ids")
orders_with_picking = orders.filtered(lambda o: any(line.qty_delivered_method == 'stock_move' for line in o.order_line)):

res = None
if orders_with_picking:
orders_with_picking = orders_with_picking.with_context(
orders_cancel_by_running_procurements=orders_with_picking.ids
)
res = super(SaleOrder, orders_with_picking)._action_cancel()
remaining_orders = orders - orders_with_picking
if remaining_orders:
res = super(SaleOrder, remaining_orders)._action_cancel()

return res

def _cancel_by_running_procurements(self):
orders = self.filtered(lambda s: s.state != "cancel")
orders.order_line.cancel_remaining_qty()
114 changes: 114 additions & 0 deletions sale_order_line_cancel/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright 2018 Okia SPRL
# Copyright 2020 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models
from odoo.tools import float_compare, float_is_zero


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

product_qty_canceled = fields.Float(
"Qty canceled", readonly=True, copy=False, digits="Product Unit of Measure"
)
product_qty_remains_to_deliver = fields.Float(
string="Remains to deliver",
digits="Product Unit of Measure",
compute="_compute_product_qty_remains_to_deliver",
store=True,
)
can_cancel_remaining_qty = fields.Boolean(
compute="_compute_can_cancel_remaining_qty"
)

@api.depends("product_qty_remains_to_deliver", "state")
def _compute_can_cancel_remaining_qty(self):
precision = self.env["decimal.precision"].precision_get(
"Product Unit of Measure"
)
for rec in self:
rec.can_cancel_remaining_qty = (
float_compare(
rec.product_qty_remains_to_deliver, 0, precision_digits=precision
)
== 1
and rec.state in ("sale", "done")
and rec.move_ids
)

@api.depends(
"product_uom_qty",
"qty_delivered",
"product_qty_canceled",
)
def _compute_product_qty_remains_to_deliver(self):
for line in self:
remaining_to_deliver = (
line.product_uom_qty - line.qty_delivered - line.product_qty_canceled
)
line.product_qty_remains_to_deliver = remaining_to_deliver

def _get_moves_to_cancel(self):
return self.move_ids.filtered(lambda m: m.state not in ("done", "cancel"))

def _check_moves_to_cancel(self, moves):
"""override this method to add checks before cancel"""
self.ensure_one()

def cancel_remaining_qty(self):
lines = self.filtered(lambda l: l.can_cancel_remaining_qty)
for line in lines:
moves_to_cancel = line._get_moves_to_cancel()
line._check_moves_to_cancel(moves_to_cancel)
line._cancel_by_running_procurement()
line.order_id.message_post(
body=_(
"<b>%(product)s</b>: The order line has been canceled",
product=line.product_id.display_name,
)
)
return True

def _cancel_by_running_procurement(self):
"""Cancel the remaining quantity by running a new procurement

To cancel qty to deliver, we'll run the procurement to decrease the
quantity to deliver. This will be achieved by triggering the
`_action_launch_stock_rule` by pretending that we previously asked
for more quantity to deliver than we actually did. This way, the
system will create a new procurement to deliver a negative quantity.
As result, stock moves with negative quantity will be created and, if
possible, be merged with the existing ones.

When want to cancel the remaining quantity to deliver the method will
pretend that we previously asked for the sum of the ordered quantity
and the remaining quantity. This way, the system will at the end create
the rules to decrease of the remaining quantity.
(ordered qty + remaining qty - ordered qty = remaining qty)


:param only_remaining: if True, only the remaining quantity will be
canceled. If False, the full quantity will be canceled.

"""
qty_to_cancel = self.product_qty_remains_to_deliver
precision = self.env["decimal.precision"].precision_get(
"Product Unit of Measure"
)
if float_is_zero(qty_to_cancel, precision_digits=precision):
return
simulate_procured_qty = self.product_uom_qty + qty_to_cancel
self.ensure_one()
line = self.with_context(simulate_procured_qty=simulate_procured_qty)
previous_sate = line.state
line.state = "sale"
line._action_launch_stock_rule(simulate_procured_qty)
line.state = previous_sate
line.product_qty_canceled += qty_to_cancel

def _get_qty_procurement(self, previous_product_uom_qty=False):
return self.env.context.get(
"simulate_procured_qty",
super()._get_qty_procurement(previous_product_uom_qty),
)
20 changes: 20 additions & 0 deletions sale_order_line_cancel/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class StockMove(models.Model):

_inherit = "stock.move"

def _action_cancel(self):
sale_moves = self.filtered(
lambda m: m.sale_line_id and m.state not in ("done", "cancel")
)
res = super()._action_cancel()
for rec in sale_moves:
if rec.state != "cancel":
continue
rec.sale_line_id.product_qty_canceled = rec.product_uom_qty
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not correct for the kits (mrp)

Copy link
Contributor

@jbaudoux jbaudoux Feb 21, 2024

Choose a reason for hiding this comment

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

we should also ensure on sale.order.line that product_qty_canceled <= product_uom_qty - qty_delivered

return res
25 changes: 25 additions & 0 deletions sale_order_line_cancel/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class StockPicking(models.Model):

_inherit = "stock.picking"

def action_cancel(self):
# The action_cancel method is called from the '_action_cancel' method
# of the sale_order model in sale_stock. To avoid a hard cancel of the
# pickings whatever the state of the picking process, the current addon
# add into the context the 'orders_cancel_by_running_procurements' key to
# instruct that in place of cancelling the picking, we'll run the proper
# procurement rules to cancel the remaining qties to deliver.
sale_order_ids = self.env.context.get("orders_cancel_by_running_procurements")
if not sale_order_ids:
return super().action_cancel()
return (
self.env["sale.order"]
.browse(sale_order_ids)
._cancel_by_running_procurements()
)
3 changes: 3 additions & 0 deletions sale_order_line_cancel/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Sylvain Van Hoof <[email protected]>
* Jacques-Etienne Baudoux (BCIM) <[email protected]>
* Souheil Bejaoui <[email protected]>
21 changes: 21 additions & 0 deletions sale_order_line_cancel/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This module allows you to cancel the remaining quantity on sale order by adding
a dedicated action to sale lines. It also add two new fields to track canceled
and remaining to deliver quantities.

This module differs from the original odoo behavior in the following way:

* In odoo, if the update of the quantity ordered is allowed on the sale order at
the confirmed state, odoo will recompute the required stock operations
according to the new quantity. This change is possible
even the stock operations are started for this sale order line.
* In this module, the quantity ordered is not updated on the sale order line to
keep track of the original ordered by the customer. At the same time, we
cancel only the stock moves for the remaining qty to deliver. This is only
possible if no operation is started for this sale order line.


.. warning::

It's not recommended to use this module if the update of the quantity ordered
on the sale order line is allowed the confirmed state. This could lead to
unpredictable behavior.
14 changes: 14 additions & 0 deletions sale_order_line_cancel/security/sale_order_line_cancel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record model="ir.model.access" id="sale_order_line_cancel_access">
<field name="name">sale.order.line.cancel access</field>
<field name="model_id" ref="model_sale_order_line_cancel" />
<field name="group_id" ref="sales_team.group_sale_manager" />
<field name="perm_read" eval="1" />
<field name="perm_create" eval="1" />
<field name="perm_write" eval="1" />
<field name="perm_unlink" eval="1" />
</record>

Choose a reason for hiding this comment

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

is it possible to add entry for users in groups sales_team.group_sale_salesman ?

sale.order.line.cancel access own document

</odoo>
Loading
Loading