Skip to content

Commit

Permalink
[FIX] product_pack: Take into account both pricelist and pack line di…
Browse files Browse the repository at this point in the history
…scount. (OCA#197)

Formerly was only taking into account the product packs discount, now take
into account both discounts.

Also we simplify the way that get_sale_order_line_vals works and now is
simplier and use Odoo's onchange methods to compute the lines values.

[FIX] product_pack: Pack component now have the tax of the company (OCA#198)

Before this fix the product's packs new sale order line have multiple taxes:
one por each company. Actually only need to add the taxes realated to the
sale order's company.

[FIX] product_pack: compute proper components prices (OCA#199)

In some cases the values were prefetched and it was throwing bad prices.
we add prefetch_fields to the context of the packs and that solve it.

[FIX/IMP] product_pack: Allow modify pack and do not reset after confirm website sale order (#201)

* [FIX] product_pack: Do not reset packs components when confirm sale order.

If a sale order is updated from the website whrn confirm the order we do
not expand the pack, we usit as it is.

* [ADD] product_pack: Allow modify pack from backend/website

Now we can choose if the product can be modified in the only int he backend or
if can be modified also in the frontend by the customers.

* [FIX] lint
  • Loading branch information
zaoral authored and docker-odoo committed Jan 17, 2025
1 parent 5c1f0e6 commit c141e31
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 57 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.3.0',
'version': '11.0.1.4.0',
'category': 'Product',
'sequence': 14,
'summary': '',
Expand Down
71 changes: 20 additions & 51 deletions product_pack/models/product_pack_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,60 +37,29 @@ class ProductPack(models.Model):
@api.multi
def get_sale_order_line_vals(self, line, order):
self.ensure_one()
# pack_price = 0.0
subproduct = self.product_id
quantity = self.quantity * line.product_uom_qty

taxes = order.fiscal_position_id.map_tax(
subproduct.taxes_id.filtered(
lambda r: r.company_id.id == order.company_id.id))
tax_id = [(6, 0, taxes.ids)]

# if pack is fixed price or totlice price we don want amount on
# pack lines
if line.product_id.pack_price_type in [
'fixed_price', 'totalice_price']:
price = 0.0
discount = 0.0
else:
pricelist = order.pricelist_id.id
price = self.env['product.pricelist'].with_context(
context={
'uom': subproduct.uom_id.id,
'date': order.date_order,
}
).price_get(
subproduct.id, quantity,
order.partner_id.id)[pricelist]
discount = self.discount

# Obtain product name in partner's language
if order.partner_id.lang:
subproduct = subproduct.with_context(
lang=order.partner_id.lang)
subproduct_name = subproduct.name

vals = {
line_vals = {
'order_id': order.id,
'name': '%s%s' % (
'> ' * (line.pack_depth + 1), subproduct_name
),
# 'delay': subproduct.sale_delay or 0.0,
'product_id': subproduct.id,
# 'procurement_ids': (
# [(4, x.id) for x in line.procurement_ids]
# ),
'price_unit': price,
'tax_id': tax_id,
'address_allotment_id': False,
'product_uom_qty': quantity,
'product_uom': subproduct.uom_id.id,
'product_packaging': False,
'discount': discount,
'number_packages': False,
'th_weight': False,
'state': 'draft',
'product_id': self.product_id.id or False,
'pack_parent_line_id': line.id,
'pack_depth': line.pack_depth + 1,
# 'sequence': sequence,
'company_id': order.company_id.id,
}
sol = line.new(line_vals)
sol.product_id_change()
sol.product_uom_qty = quantity
sol.product_uom_change()
sol._onchange_discount()
vals = sol._convert_to_write(sol._cache)

discount = 100.0 - (
(100.0 - sol.discount) * (100.0 - self.discount) / 100.0)

vals.update({
'discount': discount,
'name': '%s%s' % (
'> ' * (line.pack_depth + 1), sol.name
),
})
return vals
2 changes: 1 addition & 1 deletion product_pack/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def price_compute(self, price_type, uom=False, currency=False,
packs, no_packs = self.separete_pack_products()
prices = super(ProductProduct, no_packs).price_compute(
price_type, uom, currency, company)
for product in packs:
for product in packs.with_context(prefetch_fields=False):
pack_price = 0.0
for pack_line in product.pack_line_ids:
product_line_price = pack_line.product_id.price * (
Expand Down
6 changes: 4 additions & 2 deletions product_pack/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class ProductTemplate(models.Model):
related='product_variant_ids.used_pack_line_ids',
readonly=True,
)
allow_modify_pack = fields.Boolean(
)
allow_modify_pack = fields.Selection([
('only_backend', 'Only backend'),
('frontend_backend', 'E-commerce and Bankend'),
])

@api.onchange('pack_price_type')
def onchange_allow_modify_pack(self):
Expand Down
5 changes: 4 additions & 1 deletion product_pack/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def check_pack_line_unlink(self):
_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'):
if self._origin.order_line.filtered(
lambda x: x.pack_parent_line_id and
x.pack_parent_line_id.product_id.allow_modify_pack not in [
'only_backend', 'frontend_backend']):
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'
Expand Down
7 changes: 6 additions & 1 deletion product_pack/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class SaleOrderLine(models.Model):

@api.constrains('product_id', 'price_unit', 'product_uom_qty')
def expand_pack_line(self):
if self._context.get('update_pricelist', False):
return
detailed_packs = ['components_price', 'totalice_price', 'fixed_price']
if (
self.state == 'draft' and
Expand Down Expand Up @@ -102,6 +104,8 @@ def _onchange_pack_line_ids(self):

@api.constrains('product_id')
def expand_none_detailed_pack(self):
if self._context.get('update_pricelist', False):
return
if self.product_id.pack_price_type == 'none_detailed_assited_price':
# remove previus existing lines
self.pack_line_ids.unlink()
Expand Down Expand Up @@ -137,7 +141,8 @@ 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:
self._origin.pack_parent_line_id.product_id.allow_modify_pack \
not in ['only_backend', 'frontend_backend']:
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_product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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':[('pack', '=', False)]}">
Expand Down

0 comments on commit c141e31

Please sign in to comment.