Skip to content

Commit

Permalink
[17.0][ADD] sale_product_approval
Browse files Browse the repository at this point in the history
This PR adds the sale_product_approval module to v17 - Note that this module was introduced in earlier versions but PR's were not merged.
  • Loading branch information
Patrick Wilson committed May 16, 2024
1 parent 0ad838f commit 530e79e
Show file tree
Hide file tree
Showing 23 changed files with 1,059 additions and 0 deletions.
117 changes: 117 additions & 0 deletions sale_product_approval/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
=====================
Sale Product Approval
=====================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:5290a42368d5045c781cfd4477d2cb3a5310ed2dc8a0d5168a0f6109c3ca9ba9
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |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/17.0/sale_product_approval
: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-17-0/sale-workflow-17-0-sale_product_approval
: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/sale-workflow&target_branch=17.0
:alt: Try me on Runboat

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

This module links the 'Can be sold' field to the states. The states then
control whether or not the product can be sold or not in the particular
state.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Configuration
=============

- Create product states.
- Check the 'Approved to be added to SO' box on the states where the
product is allowed to be added to an SO when it's in that state.
- Check the 'Approved to be sold' box on the states where the product
is allowed to be sold when it's in that state.

Usage
=====

- When a product is moved to a state where it's approved, the 'Can be
sold' box should be checked and the product is selectable on sales
quotes.
- If the product is not in a state where it's approved to be sold, then
the 'Can be sold' check box is not checked and the product can not be
added to sales quotes.
- If it's not approved, any sales quote that already contained the
product will show a warning and have an exception activity.

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 to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_product_approval%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
-------

* Open Source Integrators

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

- `Open Source Integrators <https://opensourceintegrators.com>`__.

- Chandresh Thakkar <[email protected]>
- Daniel Reis <[email protected]>
- Patrick Wilson <[email protected]>
- Hardik Suthar <[email protected]>

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-dreispt| image:: https://github.com/dreispt.png?size=40px
:target: https://github.com/dreispt
:alt: dreispt

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

|maintainer-dreispt|

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
5 changes: 5 additions & 0 deletions sale_product_approval/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2021 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
from .hooks import _set_candidate_sale
26 changes: 26 additions & 0 deletions sale_product_approval/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2021 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Sale Product Approval",
"summary": """Control whether or not the product can be sold or not
in the particular state""",
"version": "17.0.1.0.0",
"website": "https://github.com/OCA/sale-workflow",
"category": "Products",
"author": "Open Source Integrators, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"maintainers": ["dreispt"],
"development_status": "Alpha",
"depends": ["product_state", "sale_management", "stock"],
"data": [
"security/res_groups.xml",
"views/menu_items.xml",
"views/product_exception.xml",
"views/product_state.xml",
"views/product_template.xml",
"views/sale_order.xml",
],
"post_init_hook": "_set_candidate_sale",
}
13 changes: 13 additions & 0 deletions sale_product_approval/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2019 ForgeFlow S.L.
# (http://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


def _set_candidate_sale(env):
"""
This post-init-hook will update last price information for all products
"""
product_obj = env["product.template"]
products = product_obj.search([("sale_ok", "=", True)])
products.write({"candidate_sale": True, "sale_ok": True})
products.write({"candidate_sale_confirm": True, "sale_ok_confirm": True})
7 changes: 7 additions & 0 deletions sale_product_approval/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2021 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import product_product
from . import product_state
from . import sale_order
from . import sale_order_line
85 changes: 85 additions & 0 deletions sale_product_approval/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2021 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class ProductTemplate(models.Model):
_inherit = "product.template"

sale_ok = fields.Boolean(
string="Can be added to Quote",
copy=False,
readonly=True,
)
sale_ok_confirm = fields.Boolean(
string="Can be Sold",
copy=False,
readonly=True,
)
candidate_sale = fields.Boolean(
string="Candidate to add to Quote",
)
candidate_sale_confirm = fields.Boolean(
string="Candidate to be Sold",
)
can_edit_candidate = fields.Boolean(compute="_compute_can_edit_candidate")

def _compute_can_edit_candidate(self):
for product in self:
product.can_edit_candidate = self.env.user.has_group(
"sale_product_approval.group_product_administrator"
)

@api.onchange("candidate_sale_confirm")
def _onchange_candidate_sale_confirm(self):
for product in self:
if product.candidate_sale_confirm and not product.candidate_sale:
product.candidate_sale = True

@api.model
def create(self, vals):
new = super().create(vals)
new._set_sale_ok_product()
return new

def write(self, vals):
res = super().write(vals)
if (
"product_state_id" in vals
or "candidate_sale" in vals
or "candidate_sale_confirm" in vals
):
self._set_sale_ok_product()
return res

def _set_sale_ok_product(self):
order_ids = (
self.env["sale.order.line"]
.search(
[
("product_id", "in", self.product_variant_ids.ids),
("state", "in", ["draft", "sent"]),
("approved_sale_confirm", "=", True),
]
)
.mapped("order_id")
)
for product in self:
if product.product_state_id:
product.sale_ok = (
product.candidate_sale and product.product_state_id.approved_sale
)
product.sale_ok_confirm = (
product.candidate_sale_confirm
and product.product_state_id.approved_sale_confirm
)

if not product.sale_ok_confirm:
order_ids._log_exception_activity_sale(product)

@api.model
def _get_default_product_state_id(self):
return self.env.ref(
"product_state.product_state_draft", raise_if_not_found=False
)
11 changes: 11 additions & 0 deletions sale_product_approval/models/product_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2021 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ProductState(models.Model):
_inherit = "product.state"

approved_sale = fields.Boolean(string="Approved to be added to SO", default=True)
approved_sale_confirm = fields.Boolean(string="Approved to be Sold", default=True)
53 changes: 53 additions & 0 deletions sale_product_approval/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2021 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import date

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


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

exceptions_sale_approval_confirm = fields.Boolean(
compute="_compute_exceptions", string="Exception", default=False
)
override_exception = fields.Boolean(default=False)

@api.depends("order_line.approved_sale_confirm")
def _compute_exceptions(self):
for so in self:
so.exceptions_sale_approval_confirm = any(
not line.approved_sale_confirm
for line in so.order_line.filtered(lambda x: not x.display_type)
)

def _log_exception_activity_sale(self, product_id):
for order in self:
note = self._render_product_state_excep(order, product_id)
order.activity_schedule(
"mail.mail_activity_data_warning",
date.today(),
note=note,
user_id=order.user_id.id or SUPERUSER_ID,
)

def _render_product_state_excep(self, order, product_id):
values = {"sale_order_ref": order, "product_ref": product_id}
return self.env["ir.ui.view"]._render_template(
template=self.env.ref("sale_product_approval.exception_on_product").id,
values=values,
)

def action_confirm(self):
res = super().action_confirm()
for sale in self:
if sale.exceptions_sale_approval_confirm and not sale.override_exception:
raise UserError(
_(
"You can not confirm this sale order "
"because some products are not sellable in this order."
)
)
return res
15 changes: 15 additions & 0 deletions sale_product_approval/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2021 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


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

approved_sale_confirm = fields.Boolean(
related="product_id.sale_ok_confirm",
string="Approved for Sale",
store=True,
default=True,
)
3 changes: 3 additions & 0 deletions sale_product_approval/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
5 changes: 5 additions & 0 deletions sale_product_approval/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- Create product states.
- Check the 'Approved to be added to SO' box on the states where the
product is allowed to be added to an SO when it's in that state.
- Check the 'Approved to be sold' box on the states where the product is
allowed to be sold when it's in that state.
5 changes: 5 additions & 0 deletions sale_product_approval/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- [Open Source Integrators](https://opensourceintegrators.com).
- Chandresh Thakkar \<<[email protected]>\>
- Daniel Reis \<<[email protected]>\>
- Patrick Wilson \<<[email protected]>\>
- Hardik Suthar \<<[email protected]>\>
3 changes: 3 additions & 0 deletions sale_product_approval/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module links the 'Can be sold' field to the states. The states then
control whether or not the product can be sold or not in the particular
state.
8 changes: 8 additions & 0 deletions sale_product_approval/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- When a product is moved to a state where it's approved, the 'Can be
sold' box should be checked and the product is selectable on sales
quotes.
- If the product is not in a state where it's approved to be sold, then
the 'Can be sold' check box is not checked and the product can not be
added to sales quotes.
- If it's not approved, any sales quote that already contained the
product will show a warning and have an exception activity.
8 changes: 8 additions & 0 deletions sale_product_approval/security/res_groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<odoo>

<record id="group_product_administrator" model="res.groups">
<field name="name">Product Administrator</field>
<field name="category_id" ref="base.module_category_usability" />
</record>

</odoo>
Loading

0 comments on commit 530e79e

Please sign in to comment.