-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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] [IMP] Make packaging required on orders for configured products. #3528
base: 16.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,8 @@ | |||||||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) | ||||||||||||
|
||||||||||||
|
||||||||||||
from odoo import api, fields, models | ||||||||||||
from odoo import _, api, fields, models | ||||||||||||
from odoo.exceptions import UserError | ||||||||||||
|
||||||||||||
|
||||||||||||
class SaleOrderRecommendationLine(models.TransientModel): | ||||||||||||
|
@@ -26,6 +27,7 @@ | |||||||||||
digits="Product Unit of Measure", | ||||||||||||
help="Quantity of packagings to sell.", | ||||||||||||
) | ||||||||||||
require_packaging = fields.Boolean(compute="_compute_require_packaging") | ||||||||||||
|
||||||||||||
@api.depends("product_id", "units_included", "sale_uom_id") | ||||||||||||
def _compute_product_packaging(self): | ||||||||||||
|
@@ -109,3 +111,22 @@ | |||||||||||
# Nothing to create | ||||||||||||
return result | ||||||||||||
return self._prepare_packaging_line_vals(result) | ||||||||||||
|
||||||||||||
@api.depends("product_id") | ||||||||||||
def _compute_require_packaging(self): | ||||||||||||
for line in self: | ||||||||||||
line.require_packaging = line.product_id.require_packaging | ||||||||||||
Check warning on line 118 in sale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py Codecov / codecov/patchsale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py#L118
|
||||||||||||
Comment on lines
+114
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: if using a related field, you don't really need to add a compute method.
Suggested change
|
||||||||||||
|
||||||||||||
@api.depends("product_id", "product_uom_qty", "product_uom") | ||||||||||||
def _warning_product_packaging_required(self): | ||||||||||||
Comment on lines
+120
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a constraint instead:
Suggested change
|
||||||||||||
"""Notify the user when a packaging is required.""" | ||||||||||||
result = {} | ||||||||||||
Check warning on line 123 in sale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py Codecov / codecov/patchsale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py#L123
|
||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: no need for a result while constraining.
Suggested change
|
||||||||||||
for line in self: | ||||||||||||
if line.require_packaging and not line.product_packaging_id: | ||||||||||||
raise UserError( | ||||||||||||
Check warning on line 126 in sale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py Codecov / codecov/patchsale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py#L126
|
||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: within constrains, you should raise
Suggested change
|
||||||||||||
_( | ||||||||||||
"The product %s requires a packaging to be sold" | ||||||||||||
% line.product_id.name | ||||||||||||
Comment on lines
+128
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: you must render the message before composing it for translations to work fine:
Suggested change
|
||||||||||||
) | ||||||||||||
) | ||||||||||||
return result | ||||||||||||
Check warning on line 132 in sale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py Codecov / codecov/patchsale_order_product_recommendation_packaging_default/wizards/sale_order_recommendation.py#L132
|
||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuing from above...
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,14 @@ | |
ref="sale_order_product_recommendation.sale_order_recommendation_view_form" | ||
/> | ||
<field name="arch" type="xml"> | ||
|
||
<xpath | ||
expr="//field[@name='line_ids']/form//field[@name='units_included']" | ||
position="before" | ||
> | ||
<field name="require_packaging" invisible="1" /> | ||
</xpath> | ||
|
||
<xpath | ||
expr="//field[@name='line_ids']/tree/field[@name='units_included']" | ||
position="before" | ||
|
@@ -36,12 +44,13 @@ | |
name="product_packaging_id" | ||
groups="product.group_stock_packaging" | ||
context="{'default_product_id': product_id}" | ||
attrs="{'required': [('require_packaging', '=', True)]}" | ||
/> | ||
<field | ||
name="product_packaging_qty" | ||
groups="product.group_stock_packaging" | ||
widget="numeric_step" | ||
attrs="{'readonly': [('product_packaging_id', '=', False)]}" | ||
attrs="{'readonly': ['|', ('product_packaging_id', '=', False), ('require_packaging', '=', True)]}" | ||
/> | ||
Comment on lines
49
to
54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: just like below, this field should be writable always when there's a packaging. But instead, do the opposite and make the |
||
</xpath> | ||
</field> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from . import product_packaging | ||
from . import product_template | ||
from . import sale_order_line |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,9 @@ | ||||||||||||||||||
# Copyright 2024 Moduon Team S.L. | ||||||||||||||||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) | ||||||||||||||||||
from odoo import fields, models | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class ProductTemplate(models.Model): | ||||||||||||||||||
_inherit = "product.template" | ||||||||||||||||||
|
||||||||||||||||||
require_packaging = fields.Boolean(default=False) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: add some help and make sure you cannot require packaging without packaging:
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,12 +2,15 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from contextlib import suppress | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from odoo import api, fields, models | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from odoo import _, api, fields, models | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from odoo.exceptions import UserError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class SaleOrderLine(models.Model): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_inherit = "sale.order.line" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_packaging = fields.Boolean(compute="_compute_require_packaging") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use related instead
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def onchange(self, values, field_name, field_onchange): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"""Record which field was being changed.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if isinstance(field_name, list): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -43,6 +46,23 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
and line.product_uom_qty % line.product_packaging_id.qty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
line.product_packaging_id = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res = line._warning_product_packaging_required() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if res: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return res | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@api.depends("product_id", "product_uom_qty", "product_uom") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def _warning_product_packaging_required(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"""Notify the user when a packaging is required.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for line in self: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if line.require_packaging and not line.product_packaging_id: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise UserError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"The product %s requires a packaging to be sold" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% line.product_id.name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+49
to
66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: use a constrain instead, and I summarize all the comments from above here too:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@api.model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -84,3 +104,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_self = self.with_context(keep_product_packaging=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result = super(SaleOrderLine, _self)._compute_product_uom_qty() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@api.depends("product_id") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def _compute_require_packaging(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for line in self: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
line.require_packaging = line.product_id.require_packaging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+107
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use related instead
Suggested change
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||
<?xml version="1.0" encoding="utf-8" ?> | ||||
<!-- Copyright 2024 Moduon Team S.L. <[email protected]> | ||||
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). --> | ||||
<data> | ||||
|
||||
<record id="product_template_form_view_inherit" model="ir.ui.view"> | ||||
<field name="name">Simplify packaging fields</field> | ||||
<field name="model">product.template</field> | ||||
<field name="inherit_id" ref="product.product_template_form_view" /> | ||||
<field name="arch" type="xml"> | ||||
<xpath expr="//page[@name='inventory']" position="inside"> | ||||
<group string="Packaging Division"> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
<field | ||||
name="require_packaging" | ||||
placeholder="Indicates if the product can be sold individually or if it requires keeping the original packaging." | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: a placeholder doesn't make sense if the input is a checkbox. I already commented above about adding help to the field, so this is unnecessary.
Suggested change
|
||||
/> | ||||
</group> | ||||
</xpath> | ||||
</field> | ||||
</record> | ||||
|
||||
</data> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,33 @@ | |
position="move" | ||
/> | ||
</xpath> | ||
|
||
<xpath | ||
expr="//field[@name='order_line']/tree/field[@name='product_packaging_id']" | ||
position="before" | ||
> | ||
<field name="require_packaging" invisible="1" /> | ||
</xpath> | ||
|
||
<!-- Make packaging mandatory when the product is not allow to divide the packaging --> | ||
<xpath | ||
expr="//field[@name='order_line']/tree/field[@name='product_packaging_id']" | ||
position="attributes" | ||
> | ||
<attribute | ||
name="attrs" | ||
>{'required': [('require_packaging', '=', True)]}</attribute> | ||
</xpath> | ||
|
||
<!-- Make packaging qty readonly when the product is not allow to divide the packaging --> | ||
<xpath | ||
expr="//field[@name='order_line']/tree/field[@name='product_packaging_qty']" | ||
position="attributes" | ||
> | ||
<attribute | ||
name="attrs" | ||
>{'readonly': [('require_packaging', '=', True)]}</attribute> | ||
</xpath> | ||
Comment on lines
+57
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: so, we require packaging, but we don't allow to set how many? It doesn't make sense: Instead, please do the opposite thing. The field that you should make readonly is |
||
</field> | ||
</record> | ||
</data> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: you can use a readonly related field instead, which is essentially the same: