sale_order_line.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in root directory
  4. ##############################################################################
  5. from openerp import fields, models, api, _
  6. class sale_order_line(models.Model):
  7. _inherit = 'sale.order.line'
  8. # Fields for sale order pack
  9. pack_total = fields.Float(
  10. string='Pack total',
  11. compute='_get_pack_total'
  12. )
  13. pack_line_ids = fields.One2many(
  14. 'sale.order.line.pack.line',
  15. 'order_line_id',
  16. 'Pack Lines'
  17. )
  18. pack_type = fields.Selection(
  19. related='product_id.pack_price_type',
  20. readonly=True
  21. )
  22. # Fields for common packs
  23. pack_depth = fields.Integer(
  24. 'Depth',
  25. help='Depth of the product if it is part of a pack.'
  26. )
  27. pack_parent_line_id = fields.Many2one(
  28. 'sale.order.line',
  29. 'Pack',
  30. help='The pack that contains this product.',
  31. ondelete="cascade",
  32. # copy=False,
  33. )
  34. pack_child_line_ids = fields.One2many(
  35. 'sale.order.line',
  36. 'pack_parent_line_id',
  37. 'Lines in pack'
  38. )
  39. @api.one
  40. @api.constrains('product_id', 'price_unit', 'product_uom_qty')
  41. def expand_pack_line(self):
  42. detailed_packs = ['components_price', 'totalice_price', 'fixed_price']
  43. if (
  44. self.state == 'draft' and
  45. self.product_id.pack and
  46. self.pack_type in detailed_packs):
  47. for subline in self.product_id.pack_line_ids:
  48. vals = subline.get_sale_order_line_vals(
  49. self, self.order_id)
  50. vals['sequence'] = self.sequence
  51. existing_subline = self.search([
  52. ('product_id', '=', subline.product_id.id),
  53. ('pack_parent_line_id', '=', self.id),
  54. ], limit=1)
  55. # if subline already exists we update, if not we create
  56. if existing_subline:
  57. existing_subline.write(vals)
  58. else:
  59. self.create(vals)
  60. @api.multi
  61. def button_save_data(self):
  62. return True
  63. @api.multi
  64. def action_pack_detail(self):
  65. view_id = self.env['ir.model.data'].xmlid_to_res_id(
  66. 'product_pack.view_order_line_form2')
  67. view = {
  68. 'name': _('Details'),
  69. 'view_type': 'form',
  70. 'view_mode': 'form',
  71. 'res_model': 'sale.order.line',
  72. 'view_id': view_id,
  73. 'type': 'ir.actions.act_window',
  74. 'target': 'new',
  75. 'readonly': True,
  76. 'res_id': self.id,
  77. 'context': self.env.context
  78. }
  79. return view
  80. @api.one
  81. @api.depends(
  82. 'pack_line_ids',
  83. 'pack_line_ids.price_subtotal',
  84. )
  85. def _get_pack_total(self):
  86. pack_total = 0.0
  87. if self.pack_line_ids:
  88. pack_total = sum(x.price_subtotal for x in self.pack_line_ids)
  89. self.pack_total = pack_total
  90. @api.one
  91. @api.onchange('pack_total')
  92. def _onchange_pack_line_ids(self):
  93. self.price_unit = self.pack_total
  94. @api.constrains('product_id')
  95. def expand_none_detailed_pack(self):
  96. if self.product_id.pack_price_type == 'none_detailed_assited_price':
  97. # remove previus existing lines
  98. self.pack_line_ids.unlink()
  99. # create a sale pack line for each product pack line
  100. for pack_line in self.product_id.pack_line_ids:
  101. price_unit = pack_line.product_id.lst_price
  102. quantity = pack_line.quantity
  103. vals = {
  104. 'order_line_id': self.id,
  105. 'product_id': pack_line.product_id.id,
  106. 'product_uom_qty': quantity,
  107. 'price_unit': price_unit,
  108. 'discount': pack_line.discount,
  109. 'price_subtotal': price_unit * quantity,
  110. }
  111. self.pack_line_ids.create(vals)