Skip to content

Commit

Permalink
[ADD] product_catalog: new module
Browse files Browse the repository at this point in the history
Backport from the v17 functionality

TT50477
  • Loading branch information
chienandalu committed Sep 12, 2024
1 parent 71f6fcf commit 16a23a6
Show file tree
Hide file tree
Showing 29 changed files with 1,558 additions and 0 deletions.
104 changes: 104 additions & 0 deletions product_catalog/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
===============
Product Catalog
===============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:3d34fa62aed534517dd4da5bbb4a76ef626aa74cd47b3bd7a0c726c8d7de853e
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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%2Fproduct--attribute-lightgray.png?logo=github
:target: https://github.com/OCA/product-attribute/tree/16.0/product_catalog
:alt: OCA/product-attribute
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/product-attribute-16-0/product-attribute-16-0-product_catalog
: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/product-attribute&target_branch=16.0
:alt: Try me on Runboat

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

A backport of Odoo's v17 Product Catalog.

Changes over mainstream:

::

- Price is an optional value now so we can use it in other models.

**Table of contents**

.. contents::
:local:

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

The product catalog is a nice addon the version 17 features that allows
users to use a simple product picker to quickly add products to a
document (sale or purchase order).

Usage
=====

To use this module, you need to:

1. Go to ...

Known issues / Roadmap
======================

- Be able to filter just the added products.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/product-attribute/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/product-attribute/issues/new?body=module:%20product_catalog%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
-------

* Odoo SA
* Tecnativa

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

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

- David Vidal

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/product-attribute <https://github.com/OCA/product-attribute/tree/16.0/product_catalog>`_ 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 product_catalog/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
23 changes: 23 additions & 0 deletions product_catalog/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Product Catalog",
"summary": "Backport of Odoos v17 product catalog",
"version": "16.0.1.0.0",
"author": "Odoo SA, Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/product-attribute",
"license": "AGPL-3",
"category": "Product",
"depends": ["product"],
"data": [
"views/product_views.xml",
],
"demo": [],
"assets": {
"web.assets_backend": [
"product_catalog/static/src/product_catalog/**/*.js",
"product_catalog/static/src/product_catalog/**/*.xml",
"product_catalog/static/src/product_catalog/**/*.scss",
],
},
}
1 change: 1 addition & 0 deletions product_catalog/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import catalog
54 changes: 54 additions & 0 deletions product_catalog/controllers/catalog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo.http import Controller, request, route


class ProductCatalogController(Controller):
@route("/product/catalog/order_lines_info", auth="user", type="json")
def product_catalog_get_order_lines_info(
self, res_model, order_id, product_ids, **kwargs
):
"""Returns products information to be shown in the catalog.
:param string res_model: The order model.
:param int order_id: The order id.
:param list product_ids: The products currently displayed in the product
catalog, as a list of `product.product` ids.
:rtype: dict
:return: A dict with the following structure:
{
product.id: {
'productId': int
'quantity': float (optional)
'price': float
'readOnly': bool (optional)
}
}
"""
order = request.env[res_model].browse(order_id)
return order.with_company(

Check warning on line 29 in product_catalog/controllers/catalog.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/controllers/catalog.py#L28-L29

Added lines #L28 - L29 were not covered by tests
order.company_id
)._get_product_catalog_order_line_info(
product_ids,
**kwargs,
)

@route("/product/catalog/update_order_line_info", auth="user", type="json")
def product_catalog_update_order_line_info(
self, res_model, order_id, product_id, quantity=0, **kwargs
):
"""Update order line information on a given order for a given product.
:param string res_model: The order model.
:param int order_id: The order id.
:param int product_id: The product, as a `product.product` id.
:return: The unit price price of the product, based on the pricelist of the order and
the quantity selected.
:rtype: float
"""
order = request.env[res_model].browse(order_id)
return order.with_company(order.company_id)._update_order_line_info(

Check warning on line 50 in product_catalog/controllers/catalog.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/controllers/catalog.py#L49-L50

Added lines #L49 - L50 were not covered by tests
product_id,
quantity,
**kwargs,
)
1 change: 1 addition & 0 deletions product_catalog/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import product_catalog_mixin
146 changes: 146 additions & 0 deletions product_catalog/models/product_catalog_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import _, fields, models


class ProductCatalogMixin(models.AbstractModel):
"""This mixin should be inherited when the model should be able to work
with the product catalog.
It assumes the model using this mixin has a O2M field where the products are
added/removed and this field's co-related model should has a method named
`_get_product_catalog_lines_data`.
"""

_name = "product.catalog.mixin"
_description = "Product Catalog Mixin"

catalog_button_text = fields.Text(compute="_compute_catalog_button_text")

def _compute_catalog_button_text(self):
quotations = self.filtered(lambda x: x.state in {"draft", "sent"})
quotations.catalog_button_text = _("Back to Quotation")
(self - quotations).catalog_button_text = _("Back to Order")

Check warning on line 22 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L21-L22

Added lines #L21 - L22 were not covered by tests

def action_add_from_catalog(self):
kanban_view_id = self.env.ref("product_catalog.product_view_kanban_catalog").id
search_view_id = self.env.ref("product_catalog.product_view_search_catalog").id
additional_context = self._get_action_add_from_catalog_extra_context()
return {

Check warning on line 28 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L25-L28

Added lines #L25 - L28 were not covered by tests
"type": "ir.actions.act_window",
"name": _("Products"),
"res_model": "product.product",
"views": [(kanban_view_id, "kanban"), (False, "form")],
"search_view_id": [search_view_id, "search"],
"domain": self._get_product_catalog_domain(),
"context": {**self.env.context, **additional_context},
}

def _default_order_line_values(self):
return {

Check warning on line 39 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L39

Added line #L39 was not covered by tests
"quantity": 0,
"readOnly": self._is_readonly() if self else False,
}

def _get_product_catalog_domain(self):
"""Get the domain to search for products in the catalog.
For a model that uses products that has to be hidden in the catalog, it
must override this method and extend the appropriate domain.
:returns: A list of tuples that represents a domain.
:rtype: list
"""
return [("company_id", "in", [self.company_id.id, False])]

Check warning on line 52 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L52

Added line #L52 was not covered by tests

def _get_product_catalog_record_lines(self, product_ids):
"""Returns the record's lines grouped by product.
Must be overrided by each model using this mixin.
:param list product_ids: The ids of the products currently displayed in the
product catalog.
:rtype: dict
"""
return {}

Check warning on line 62 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L62

Added line #L62 was not covered by tests

def _get_product_catalog_order_data(self, products, **kwargs):
"""Returns a dict containing the products' data. Those data are for products
who aren't in the record yet. For products already in the record, see
`_get_product_catalog_lines_data`.
For each product, its id is the key and the value is another dict with all
needed data. By default, the price is the only needed data but each model is
free to add more data. Must be overrided by each model using this mixin.
:param products: Recordset of `product.product`.
:param dict kwargs: additional values given for inherited models.
:rtype: dict
:return: A dict with the following structure:
{
'productId': int
'quantity': float (optional)
'productType': string
'price': float
'readOnly': bool (optional)
}
"""
res = {}

Check warning on line 85 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L85

Added line #L85 was not covered by tests
for product in products:
res[product.id] = {"productType": product.type}
return res

Check warning on line 88 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L87-L88

Added lines #L87 - L88 were not covered by tests

def _get_product_catalog_order_line_info(self, product_ids, **kwargs):
"""Returns products information to be shown in the catalog.
:param list product_ids: The products currently displayed in the product
catalog, as a list of `product.product` ids.
:param dict kwargs: additional values given for inherited models.
:rtype: dict
:return: A dict with the following structure:
{
'productId': int
'quantity': float (optional)
'productType': string
'price': float (optional)
'readOnly': bool (optional)
}
"""
order_line_info = {}
default_data = self._default_order_line_values()

Check warning on line 106 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L105-L106

Added lines #L105 - L106 were not covered by tests

for product, record_lines in self._get_product_catalog_record_lines(
product_ids
).items():
order_line_info[product.id] = {

Check warning on line 111 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L111

Added line #L111 was not covered by tests
**record_lines._get_product_catalog_lines_data(**kwargs),
"productType": product.type,
}
product_ids.remove(product.id)

Check warning on line 115 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L115

Added line #L115 was not covered by tests

products = self.env["product.product"].browse(product_ids)
product_data = self._get_product_catalog_order_data(products, **kwargs)

Check warning on line 118 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L117-L118

Added lines #L117 - L118 were not covered by tests
for product_id, data in product_data.items():
order_line_info[product_id] = {**default_data, **data}
return order_line_info

Check warning on line 121 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L120-L121

Added lines #L120 - L121 were not covered by tests

def _get_action_add_from_catalog_extra_context(self):
return {

Check warning on line 124 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L124

Added line #L124 was not covered by tests
"product_catalog_order_id": self.id,
"product_catalog_order_model": self._name,
}

def _is_readonly(self):
"""Must be overrided by each model using this mixin.
:return: Whether the record is read-only or not.
:rtype: bool
"""
return False

Check warning on line 134 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L134

Added line #L134 was not covered by tests

def _update_order_line_info(self, product_id, quantity, **kwargs):
"""Update the line information for a given product or create a new one if none
exists yet. Must be overrided by each model using this mixin.
:param int product_id: The product, as a `product.product` id.
:param int quantity: The product's quantity.
:param dict kwargs: additional values given for inherited models.
:return: The unit price of the product, based on the pricelist of the
purchase order and the quantity selected.
:rtype: float
"""
return 0

Check warning on line 146 in product_catalog/models/product_catalog_mixin.py

View check run for this annotation

Codecov / codecov/patch

product_catalog/models/product_catalog_mixin.py#L146

Added line #L146 was not covered by tests
2 changes: 2 additions & 0 deletions product_catalog/readme/CONTEXT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The product catalog is a nice addon the version 17 features that allows users to use
a simple product picker to quickly add products to a document (sale or purchase order).
2 changes: 2 additions & 0 deletions product_catalog/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [Tecnativa](https://tecnativa.com)
- David Vidal
5 changes: 5 additions & 0 deletions product_catalog/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A backport of Odoo's v17 Product Catalog.

Changes over mainstream:

- Price is an optional value now so we can use it in other models.
1 change: 1 addition & 0 deletions product_catalog/readme/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Be able to filter just the added products.
3 changes: 3 additions & 0 deletions product_catalog/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To use this module, you need to:

1. Go to ...
Loading

0 comments on commit 16a23a6

Please sign in to comment.