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

WIP: [NEW] purchase_allowed_product_matrix used with purchase_product_matrix #2502

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions purchase_allowed_product_matrix/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions purchase_allowed_product_matrix/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# © 2016 Chafique DELLI @ Akretion
# © 2017 Today Mourad EL HADJ MIMOUNE @ Akretion
# 2020 Manuel Calero - Tecnativa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Purchase and Invoice Allowed Product with product matrix",
"summary": "This module allows to select only products that can be "
"supplied by the vendor",
"version": "17.0.1.0.0",
"category": "Accounting & Finance",
"website": "https://github.com/OCA/purchase-workflow",
"author": "Nicolas JEUDY",
"license": "AGPL-3",
"depends": ["purchase", "purchase_allowed_product"],
"data": [
"views/purchase_order_views.xml",
],
"installable": True,
}
1 change: 1 addition & 0 deletions purchase_allowed_product_matrix/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import product_template
42 changes: 42 additions & 0 deletions purchase_allowed_product_matrix/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2017 Today Mourad EL HADJ MIMOUNE @ Akretion
# Copyright 2020 Tecnativa - Manuel Calero
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models


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

@api.model
def _search(
self,
domain,
offset=0,
limit=None,
order=None,
access_rights_uid=None,
):
if self.env.context.get("use_only_supplied_product"):
restrict_supplier_id = self.env.context.get("restrict_supplier_id")
seller = (
self.env["res.partner"]
.browse(restrict_supplier_id)
.commercial_partner_id
)
supplierinfos = self.env["product.supplierinfo"].search(
[("partner_id", "=", seller.id)]
)
domain += [
"|",
("id", "in", supplierinfos.product_tmpl_id.ids),
("id", "in", supplierinfos.product_id.ids),
]
return super()._search(
domain,
offset=offset,
limit=limit,
order=order,
access_rights_uid=access_rights_uid,
)
3 changes: 3 additions & 0 deletions purchase_allowed_product_matrix/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
1 change: 1 addition & 0 deletions purchase_allowed_product_matrix/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Nicolas JEUDY "https://github.com/njeudy"
10 changes: 10 additions & 0 deletions purchase_allowed_product_matrix/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This module adds a restriction in purchase and supplier invoices that
has the mark "Order and invoice only supplied products" checked for
allowing to select only products that can be supplied by the defined
vendor.

The restriction can be set by default for each partner, and it's
propagated to their supplier invoices and purchases, but it can be
changed for each invoice and purchase order if you want.

This is for the case og purchase_product_matrix module use
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions purchase_allowed_product_matrix/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_purchase_allowed_product
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2017 Today Mourad EL HADJ MIMOUNE @ Akretion
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from odoo.tests.common import Form, TransactionCase


class TestPurchaseAllowedProduct(TransactionCase):
def setUp(self):
super().setUp()
self.supplierinfo_model = self.env["product.supplierinfo"]
self.product_template_model = self.env["product.template"]
self.partner_4 = self.env.ref("base.res_partner_4")
self.supplierinfo = self.supplierinfo_model.search(
[("partner_id", "=", self.partner_4.id)]
)
self.partner_4_supplied_products = self.product_template_model.search(
[
(
"id",
"in",
[x.product_tmpl_id.id for x in self.supplierinfo],
)
]
)

def test_purchase_onchange(self):
"""A user creates a purchase from the form."""
self.partner_4.use_only_supplied_product = True
with Form(
self.env["purchase.order"], view="purchase.purchase_order_form"
) as purchase_form:
purchase_form.partner_id = self.partner_4

# Ensure the use_only_supplied_product is set
self.assertEqual(
purchase_form.use_only_supplied_product,
self.partner_4.use_only_supplied_product,
)

self.assertEqual(purchase_form.use_only_supplied_product, True)
context = {
"restrict_supplier_id": purchase_form.partner_id.id,
"use_only_supplied_product": purchase_form.use_only_supplied_product,
}
supplied_product = self.product_template_model.with_context(**context)._search([])
self.assertEqual(
set(supplied_product), set(self.partner_4_supplied_products.ids)
)
18 changes: 18 additions & 0 deletions purchase_allowed_product_matrix/views/purchase_order_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record model="ir.ui.view" id="purchase_order_form_supplied_product">
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase_product_matrix.purchase_order_form_matrix" />
<field name="arch" type="xml">
<xpath
expr="//field[@name='order_line']/tree/field[@name='product_template_id']"
position="attributes"
>
<attribute name="context" operation="update">
{'restrict_supplier_id': parent.partner_id,
'use_only_supplied_product': parent.use_only_supplied_product}
</attribute>
</xpath>
</field>
</record>
</odoo>
Loading