pack.py 3.7 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. 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. default=0.0,
  41. )
  42. @api.onchange('product_id')
  43. def get_price(self):
  44. self.price = self.product_id.standard_price
  45. self.subtotal = self.price * self.quantity
  46. @api.onchange('quantity')
  47. def get_subtotal(self):
  48. self.subtotal = self.price * self.quantity
  49. @api.multi
  50. def get_sale_order_line_vals(self, line, order):
  51. self.ensure_one()
  52. # pack_price = 0.0
  53. subproduct = self.product_id
  54. quantity = self.quantity * line.product_uom_qty
  55. taxes = order.fiscal_position.map_tax(
  56. subproduct.taxes_id)
  57. tax_id = [(6, 0, taxes.ids)]
  58. if subproduct.uos_id:
  59. uos_id = subproduct.uos_id.id
  60. uos_qty = quantity * subproduct.uos_coeff
  61. else:
  62. uos_id = False
  63. uos_qty = quantity
  64. # if pack is fixed price or totlice price we don want amount on
  65. # pack lines
  66. if line.product_id.pack_price_type in [
  67. 'fixed_price', 'totalice_price']:
  68. price = 0.0
  69. discount = 0.0
  70. else:
  71. pricelist = order.pricelist_id.id
  72. price = self.env['product.pricelist'].price_get(
  73. subproduct.id, quantity,
  74. order.partner_id.id, context={
  75. 'uom': subproduct.uom_id.id,
  76. 'date': order.date_order})[pricelist]
  77. discount = self.discount
  78. # Obtain product name in partner's language
  79. if order.partner_id.lang:
  80. subproduct = subproduct.with_context(
  81. lang=order.partner_id.lang)
  82. subproduct_name = subproduct.name
  83. vals = {
  84. 'order_id': order.id,
  85. 'name': '%s%s' % (
  86. '> ' * (line.pack_depth + 1), subproduct_name
  87. ),
  88. # 'delay': subproduct.sale_delay or 0.0,
  89. 'product_id': subproduct.id,
  90. # 'procurement_ids': (
  91. # [(4, x.id) for x in line.procurement_ids]
  92. # ),
  93. 'price_unit': price,
  94. 'tax_id': tax_id,
  95. 'address_allotment_id': False,
  96. 'product_uom_qty': quantity,
  97. 'product_uom': subproduct.uom_id.id,
  98. 'product_uos_qty': uos_qty,
  99. 'product_uos': uos_id,
  100. 'product_packaging': False,
  101. 'discount': discount,
  102. 'number_packages': False,
  103. 'th_weight': False,
  104. 'state': 'draft',
  105. 'pack_parent_line_id': line.id,
  106. 'pack_depth': line.pack_depth + 1,
  107. }
  108. return vals
  109. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: