Skip to content

Commit

Permalink
[ADD] product_pack: Compatibility 11.0 and multiples improves. (OCA#185)
Browse files Browse the repository at this point in the history
* [ADD] product_pack: Add compatibility with website_sale.

* [ADD] product_pack: Allow product pack modify

* [ADD] product_pack: In sale order do not let to delete/modify a line that
belongs to a pack

* [ADD] product_pack: Be able to edit pack lines inf allow_modify_pack is set in
product.

* [ADD] product_pack: Do not compute price in cart for components_price packs

[FIX] product_pack: Pack line price fixed (take into account pricelist) (OCA#187)

* [FIX] product_pack: Pack line price fixed (take into account pricelist)

Proper compute pack line price using price instead of list_price

* [FIX] product_pack: Allow Modidy Pack only showed for Components price packs

* [FIX] product_pack: When changing pack_price_type clean allow_modify_pack if
needed.
  • Loading branch information
zaoral authored and ALopez-Adhoc committed Nov 20, 2024
1 parent 492d4fe commit 45f0b37
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion product_pack/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##############################################################################
{
'name': 'Product Pack',
'version': '11.0.1.1.0',
'version': '11.0.1.3.0',
'category': 'Product',
'sequence': 14,
'summary': '',
Expand Down
38 changes: 34 additions & 4 deletions product_pack/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,52 @@ def check_recursion(self):
pack_lines = pack_lines.mapped('product_id.pack_line_ids')

@api.multi
def price_compute(self, price_type, uom=False, currency=False,
company=False):
def separete_pack_products(self):
""" Divide the products and the pack products into two separate
recordsets.
:return: [packs, no_packs]
"""
packs = self.filtered(lambda p: p.pack and p.pack_price_type in [
'totalice_price',
'none_detailed_assited_price',
'none_detailed_totaliced_price',
])

# for compatibility with website_sale
if self._context.get('website_id', False) and \
not self._context.get('from_cart', False):
packs |= self.filtered(
lambda p: p.pack and p.pack_price_type == 'components_price')

no_packs = (self | self.mapped('pack_line_ids.product_id')) - packs
return packs, no_packs

@api.multi
def price_compute(self, price_type, uom=False, currency=False,
company=False):
packs, no_packs = self.separete_pack_products()
prices = super(ProductProduct, no_packs).price_compute(
price_type, uom, currency, company)
for product in packs:
pack_price = 0.0
for pack_line in product.pack_line_ids:
product_line_price = prices[
pack_line.product_id.id] * (
product_line_price = pack_line.product_id.price * (
1 - (pack_line.discount or 0.0) / 100.0)
pack_price += (product_line_price * pack_line.quantity)
prices[product.id] = pack_price
return prices

@api.depends('list_price', 'price_extra')
def _compute_product_lst_price(self):
packs, no_packs = self.separete_pack_products()
super(ProductProduct, no_packs)._compute_product_lst_price()

to_uom = None
if 'uom' in self._context:
to_uom = self.env['product.uom'].browse([self._context['uom']])
for product in packs:
list_price = product.price_compute('list_price').get(product.id)
if to_uom:
list_price = product.uom_id._compute_price(
list_price, to_uom)
product.lst_price = list_price + product.price_extra
10 changes: 10 additions & 0 deletions product_pack/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class ProductTemplate(models.Model):
related='product_variant_ids.used_pack_line_ids',
readonly=True,
)
allow_modify_pack = fields.Boolean(
)

@api.onchange('pack_price_type')
def onchange_allow_modify_pack(self):
products = self.filtered(
lambda x: x.allow_modify_pack and
x.pack_price_type and x.pack_price_type != 'components_price')
for rec in products:
rec.allow_modify_pack = False

@api.constrains(
'product_variant_ids', 'pack_price_type')
Expand Down
15 changes: 14 additions & 1 deletion product_pack/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import models, api
from odoo import models, api, _
from odoo.exceptions import UserError


class SaleOrder(models.Model):
Expand All @@ -16,3 +17,15 @@ def copy(self, default=None):
lambda l: l.pack_parent_line_id.order_id == self)
pack_copied_lines.unlink()
return sale_copy

@api.onchange('order_line')
def check_pack_line_unlink(self):
"""At least on embeded tree editable view odoo returns a recordset on
_origin.order_line only when lines are unlinked and this is exactly
what we need
"""
if self._origin.order_line.filtered('pack_parent_line_id'):
raise UserError(_(
'You can not delete this line because is part of a pack in'
' this sale order. In order to delete this line you need to'
' delete the pack itself'))
12 changes: 12 additions & 0 deletions product_pack/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# directory
##############################################################################
from odoo import fields, models, api, _
from odoo.exceptions import UserError


class SaleOrderLine(models.Model):
Expand Down Expand Up @@ -129,3 +130,14 @@ def _get_real_price_currency(
'fixed_price', 'totalice_price']:
new_list_price = 0.0
return new_list_price, currency_id

@api.onchange('product_id', 'product_uom_qty', 'product_uom', 'price_unit',
'discount', 'name', 'tax_id')
def check_pack_line_modify(self):
""" Do not let to edit a sale order line if this one belongs to pack
"""
if self._origin.pack_parent_line_id and \
not self._origin.pack_parent_line_id.product_id.allow_modify_pack:
raise UserError(_(
'You can not change this line because is part of a pack'
' included in this order'))
1 change: 1 addition & 0 deletions product_pack/views/product_template_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<field name="type" position="after">
<field name="pack"/>
<field name="pack_price_type" attrs="{'invisible':[('pack', '=', False)], 'required':[('pack','=',True)]}" context="{'pack_price_type': pack_price_type}"/>
<field name="allow_modify_pack" attrs="{'invisible':[('pack', '!=', False), ('pack_price_type', '!=', 'components_price')]}"/>
</field>
<notebook position="inside">
<page string="Pack" attrs="{'invisible': ['|', ('product_variant_count', '>', 1), ('pack', '=', False)]}">
Expand Down

0 comments on commit 45f0b37

Please sign in to comment.