Skip to content

Commit

Permalink
Merge PR #806 into 11.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Dec 3, 2019
2 parents 3afbd23 + 05832e3 commit 19c40e1
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions purchase_order_product_recommendation_brand/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import wizards
20 changes: 20 additions & 0 deletions purchase_order_product_recommendation_brand/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2019 David Vidal <david.vidal@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Purchase Order Product Recommendation Brand Filter",
"summary": "Allow to filter recommendations by brand",
"version": "11.0.1.0.0",
"category": "Purchases",
"website": "https://github.com/OCA/purchase-workflow",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"purchase_order_product_recommendation",
"product_brand",
],
"data": [
"wizards/purchase_order_recommendation_view.xml",
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to filter product recommendations by brand.
6 changes: 6 additions & 0 deletions purchase_order_product_recommendation_brand/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To use this module, you need to:

#. Create a new purchase order.
#. Assign its supplier.
#. Press *Recommended Products* button.
#. Now you can also filter by product brands.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_recommendation
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2019 Tecnativa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.addons.purchase_order_product_recommendation.tests import (
test_recommendation)


class BrandRecommendationCase(test_recommendation.RecommendationCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.brand_obj = cls.env['product.brand']
cls.brand_1 = cls.brand_obj.create({
'name': 'OCA Cola',
})
cls.brand_2 = cls.brand_obj.create({
'name': 'Play-Odoo',
})
cls.prod_1.product_brand_id = cls.brand_1
cls.prod_2.product_brand_id = cls.brand_2
cls.prod_3.product_brand_id = cls.brand_2

def test_recommendations_by_brand(self):
"""We can filter by brand"""
wizard = self.wizard()
wizard.date_begin = wizard.date_end = '2019-02-01'
# Just delivered from brand 1
wizard.product_brand_ids = self.brand_1
wizard.show_all_partner_products = True
wizard._generate_recommendations()
# Just one line with products from brand 1
self.assertEqual(wizard.line_ids.product_id, self.prod_1)
# Just delivered from brand 2
wizard.product_brand_ids = self.brand_2
wizard._generate_recommendations()
self.assertEqual(len(wizard.line_ids), 2)
# All brands
wizard.product_brand_ids += self.brand_1
wizard._generate_recommendations()
self.assertEqual(len(wizard.line_ids), 3)
# No brand set
wizard.product_brand_ids = False
wizard._generate_recommendations()
self.assertEqual(len(wizard.line_ids), 3)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_order_recommendation
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2019 Tecnativa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models


class PurchaseOrderRecommendation(models.TransientModel):
_inherit = 'purchase.order.recommendation'

product_brand_ids = fields.Many2many(
comodel_name='product.brand',
string="Brands",
)

def _get_products(self):
"""Filter products of the given brands"""
products = super()._get_products()
# Filter products by brand if set.
# It will apply to show_all_partner_products as well
if self.product_brand_ids:
products = products.filtered(
lambda x: x.product_brand_id in self.product_brand_ids)
return products

def _get_all_products_domain(self):
"""Filter products of the given brands"""
domain = super()._get_all_products_domain()
if self.product_brand_ids and not self.env.context.get(
'no_brands_filter'):
domain += [('product_brand_id', 'in', self.product_brand_ids.ids)]
return domain

@api.onchange('product_brand_ids')
def _generate_recommendations(self):
"""Just to add field to the onchange method"""
return super()._generate_recommendations()

@api.onchange('show_all_partner_products', 'show_all_products')
def _onchange_products(self):
"""Restrict available brands domain"""
products = self._get_supplier_products()
# Gets all products avoiding to filter them by brand again
if self.show_all_products:
products += self.with_context(
no_brands_filter=True).env['product.product'].search(
self._get_all_products_domain())
brands = products.mapped('product_brand_id')
return {'domain': {'product_brand_ids': [('id', 'in', brands.ids)]}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="purchase_order_recommendation_view_form" model="ir.ui.view">
<field name="model">purchase.order.recommendation</field>
<field name="inherit_id" ref="purchase_order_product_recommendation.purchase_order_recommendation_view_form"/>
<field name="arch" type="xml">
<field name="warehouse_ids" position="before">
<field name="product_brand_ids"
widget="many2many_tags"
options="{'no_open': True, 'no_create': True}"/>
</field>
</field>
</record>

</odoo>

0 comments on commit 19c40e1

Please sign in to comment.