pack.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import openerp.addons.decimal_precision as dp
  7. class product_pack(models.Model):
  8. _name = 'product.pack.line'
  9. _rec_name = 'product_id'
  10. parent_product_id = fields.Many2one(
  11. 'product.product',
  12. 'Parent Product',
  13. ondelete='cascade',
  14. required=True
  15. )
  16. quantity = fields.Float(
  17. 'Quantity',
  18. required=True,
  19. default=1.0,
  20. digits=dp.get_precision('Product UoS'),
  21. )
  22. product_id = fields.Many2one(
  23. 'product.product',
  24. 'Product',
  25. ondelete='cascade',
  26. required=True,
  27. )
  28. discount = fields.Float(
  29. 'Discount (%)',
  30. digits=dp.get_precision('Discount'),
  31. )
  32. price = fields.Float(
  33. 'Precio',
  34. required=True,
  35. default=0.0,
  36. )
  37. subtotal = fields.Float(
  38. 'Subtotal',
  39. required=True,
  40. compute="_product_price_subtotal"
  41. )
  42. @api.one
  43. @api.depends('product_id')
  44. def _product_price_subtotal(self):
  45. if(self.price > 0):
  46. self.subtotal = self.price * self.quantity
  47. else:
  48. self.price = self.product_id.lst_price
  49. self.subtotal = self.price * self.quantity
  50. @api.onchange('price')
  51. def _product_change_price_subtotal(self):
  52. if(self.price > 0):
  53. self.subtotal = self.price * self.quantity
  54. else:
  55. self.price = self.product_id.lst_price
  56. self.subtotal = self.price * self.quantity
  57. @api.onchange('quantity')
  58. def _product_change_quantity_subtotal(self):
  59. if(self.price > 0):
  60. self.subtotal = self.price * self.quantity
  61. else:
  62. self.price = self.product_id.lst_price
  63. self.subtotal = self.price * self.quantity
  64. @api.multi
  65. def get_sale_order_line_vals(self, line, order):
  66. self.ensure_one()
  67. # pack_price = 0.0
  68. subproduct = self.product_id
  69. quantity = self.quantity * line.product_uom_qty
  70. taxes = order.fiscal_position.map_tax(
  71. subproduct.taxes_id)
  72. tax_id = [(6, 0, taxes.ids)]
  73. if subproduct.uos_id:
  74. uos_id = subproduct.uos_id.id
  75. uos_qty = quantity * subproduct.uos_coeff
  76. else:
  77. uos_id = False
  78. uos_qty = quantity
  79. # if pack is fixed price or totlice price we don want amount on
  80. # pack lines
  81. if line.product_id.pack_price_type in [
  82. 'fixed_price', 'totalice_price']:
  83. price = 0.0
  84. discount = 0.0
  85. else:
  86. pricelist = order.pricelist_id.id
  87. price = self.env['product.pricelist'].price_get(
  88. subproduct.id, quantity,
  89. order.partner_id.id, context={
  90. 'uom': subproduct.uom_id.id,
  91. 'date': order.date_order})[pricelist]
  92. discount = self.discount
  93. # Obtain product name in partner's language
  94. if order.partner_id.lang:
  95. subproduct = subproduct.with_context(
  96. lang=order.partner_id.lang)
  97. subproduct_name = subproduct.name
  98. vals = {
  99. 'order_id': order.id,
  100. 'name': '%s%s' % (
  101. '> ' * (line.pack_depth + 1), subproduct_name
  102. ),
  103. # 'delay': subproduct.sale_delay or 0.0,
  104. 'product_id': subproduct.id,
  105. # 'procurement_ids': (
  106. # [(4, x.id) for x in line.procurement_ids]
  107. # ),
  108. 'price_unit': price,
  109. 'tax_id': tax_id,
  110. 'address_allotment_id': False,
  111. 'product_uom_qty': quantity,
  112. 'product_uom': subproduct.uom_id.id,
  113. 'product_uos_qty': uos_qty,
  114. 'product_uos': uos_id,
  115. 'product_packaging': False,
  116. 'discount': discount,
  117. 'number_packages': False,
  118. 'th_weight': False,
  119. 'state': 'draft',
  120. 'pack_parent_line_id': line.id,
  121. 'pack_depth': line.pack_depth + 1,
  122. }
  123. return vals
  124. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: