You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.4 KiB

  1. # Copyright 2019 Tecnativa - Ernesto Tejeda
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import fields, models, api, _
  4. from odoo.exceptions import UserError
  5. class SaleOrderLine(models.Model):
  6. _inherit = 'sale.order.line'
  7. @api.multi
  8. def expand_pack_line(self, write=False):
  9. self.ensure_one()
  10. # if we are using update_pricelist or checking out on ecommerce we
  11. # only want to update prices
  12. do_not_expand = self._context.get('update_prices') or \
  13. self._context.get('update_pricelist', False)
  14. if (
  15. self.state == 'draft' and
  16. self.product_id.pack_ok and
  17. self.pack_type == 'detailed'):
  18. for subline in self.product_id.get_pack_lines():
  19. vals = subline.get_sale_order_line_vals(self, self.order_id)
  20. vals['sequence'] = self.sequence
  21. vals['active'] = False
  22. if write:
  23. existing_subline = self.search([
  24. ('product_id', '=', subline.product_id.id),
  25. ('pack_parent_line_id', '=', self.id),
  26. ], limit=1)
  27. # if subline already exists we update, if not we create
  28. if existing_subline:
  29. if do_not_expand:
  30. vals.pop('product_uom_qty')
  31. existing_subline.write(vals)
  32. elif not do_not_expand:
  33. self.create(vals)
  34. else:
  35. self.create(vals)
  36. @api.model
  37. def create(self, vals):
  38. record = super().create(vals)
  39. record.expand_pack_line()
  40. return record
  41. @api.multi
  42. def write(self, vals):
  43. super().write(vals)
  44. if 'product_id' in vals or 'product_uom_qty' in vals:
  45. for record in self:
  46. record.expand_pack_line(write=True)
  47. def _get_real_price_currency(
  48. self, product, rule_id, qty, uom, pricelist_id):
  49. new_list_price, currency_id = super()._get_real_price_currency(
  50. product, rule_id, qty, uom, pricelist_id)
  51. pack_types = {'totalized', 'ignored'}
  52. parent_line = self.pack_parent_line_id
  53. if parent_line and parent_line.pack_type == 'details' \
  54. and parent_line.pack_component_price in pack_types:
  55. new_list_price = 0.0
  56. return new_list_price, currency_id
  57. @api.onchange('product_id', 'product_uom_qty', 'product_uom', 'price_unit',
  58. 'discount', 'name', 'tax_id')
  59. def check_pack_line_modify(self):
  60. """ Do not let to edit a sale order line if this one belongs to pack
  61. """
  62. if self._origin.pack_parent_line_id and \
  63. not self._origin.pack_parent_line_id.product_id.pack_modifiable:
  64. raise UserError(_(
  65. 'You can not change this line because is part of a pack'
  66. ' included in this order'))
  67. @api.multi
  68. def _get_display_price(self, product):
  69. # We do this to clean the price if the parent of the
  70. # component it's that type
  71. pack_types = {'totalized', 'ignored'}
  72. parent_line = self.pack_parent_line_id
  73. if parent_line.pack_type == 'detailed' \
  74. and parent_line.pack_component_price in pack_types:
  75. return 0.0
  76. return super()._get_display_price(product)