product_pricelist.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # -*- coding: utf-8 -*-
  2. # Copyright© 2016 ICTSTUDIO <http://www.ictstudio.eu>
  3. # License: AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
  4. import logging
  5. from openerp import models, fields, api, _
  6. _logger = logging.getLogger(__name__)
  7. class product_pricelist(models.Model):
  8. _inherit = 'product.pricelist'
  9. show_on_products = fields.Boolean(string="Display on product page")
  10. product_price = fields.Float(
  11. compute="_get_product_price",
  12. inverse="_set_product_price",
  13. string="Price"
  14. )
  15. product_price_manual = fields.Boolean(
  16. compute="_get_price_manual",
  17. # inverse="_set_price_manual",
  18. string="Manual Price",
  19. )
  20. product_id = fields.Integer(
  21. #comodel_name="product.product",
  22. compute="_get_product_price",
  23. string="product_id"
  24. )
  25. @api.model
  26. def _get_product_id(self):
  27. product_id = False
  28. if self.env.context.get('product_template_id', False):
  29. product_tmpl = self.env['product.template'].browse(
  30. [self.env.context.get('product_template_id')]
  31. )
  32. product_id = False
  33. if product_tmpl:
  34. product_tmpl_id = product_tmpl[0]
  35. if product_tmpl_id and product_tmpl_id.product_variant_ids:
  36. product = product_tmpl_id.product_variant_ids[0]
  37. if product:
  38. product_id = product.id
  39. elif self.env.context.get('product_id', False):
  40. product_id = self.env.context.get('product_id')
  41. return product_id
  42. @api.one
  43. def _get_price_manual(self):
  44. product_id = self._get_product_id()
  45. version = self.get_active_pricelist_version()
  46. if version:
  47. if self._context.get('product_id') and \
  48. self.env['product.pricelist.item'].search(
  49. [
  50. ('price_version_id', '=', version.id),
  51. ('product_id', '=', self._context.get('product_id'))
  52. ]
  53. ):
  54. self.product_price_manual = True
  55. if self._context.get('product_template_id') and \
  56. self.env['product.pricelist.item'].search(
  57. [
  58. ('price_version_id', '=', version.id),
  59. ('product_tmpl_id', '=', self._context.get('product_template_id'))
  60. ]
  61. ):
  62. self.product_price_manual = True
  63. @api.one
  64. def _get_product_price(self):
  65. product_id = self._get_product_id()
  66. if product_id:
  67. self.product_price = self.price_get(
  68. product_id, 1).get(self.id, 0.0
  69. )
  70. self.product_id = product_id
  71. def _set_product_price(self):
  72. # Real change takes place in price_set after inverse
  73. # method of pricelists object on product_template
  74. _logger.debug("Set Price: %s", self.product_price)
  75. @api.one
  76. def remove_price_manual(self):
  77. # Real change takes place in price_set after inverse
  78. # method of pricelists object on product_template
  79. if self._context.get('product_template_id'):
  80. self.price_remove(self._context.get('product_template_id'))
  81. if self._context.get('product_id'):
  82. product = self.env['product.product'].browse(self._context.get('product_id'))
  83. self.price_remove(product.product_tmpl_id.id)
  84. def price_set(self, product_template, new_price):
  85. """
  86. Set the price of the product in current pricelist
  87. if different from price through pricerules
  88. :param product_template: product_template object
  89. :param new_price: new price
  90. :return:
  91. """
  92. if new_price:
  93. version = self.get_active_pricelist_version()
  94. if not version:
  95. return False
  96. items = self.env['product.pricelist.item'].search(
  97. [
  98. ('product_tmpl_id','=', product_template.id),
  99. ('price_version_id', '=', version.id)
  100. ]
  101. )
  102. product_price_type_ids = self.env['product.price.type'].search(
  103. [
  104. ('field', '=', 'list_price')
  105. ]
  106. )
  107. if not items:
  108. self.env['product.pricelist.item'].create(
  109. {
  110. 'base': product_price_type_ids and product_price_type_ids[0].id,
  111. 'sequence': 1,
  112. 'name': product_template.name,
  113. 'product_tmpl_id': product_template.id,
  114. 'price_version_id': version.id,
  115. 'price_surcharge': new_price,
  116. 'price_discount': -1
  117. }
  118. )
  119. else:
  120. for item in items:
  121. item.write(
  122. {
  123. 'base': product_price_type_ids and product_price_type_ids[0].id,
  124. 'sequence': 1,
  125. 'name': product_template.name,
  126. 'product_tmpl_id': product_template.id,
  127. 'price_version_id': version.id,
  128. 'price_surcharge': new_price,
  129. 'price_discount': -1
  130. }
  131. )
  132. return True
  133. def price_remove(self, product_template_id):
  134. version = self.get_active_pricelist_version()
  135. if not version:
  136. return False
  137. items = self.env['product.pricelist.item'].search(
  138. [
  139. ('product_tmpl_id', '=', product_template_id),
  140. ('price_version_id', '=', version.id)
  141. ]
  142. )
  143. _logger.debug("Items: %s", items)
  144. for item in items:
  145. _logger.debug("Remove Item: %s", item)
  146. item.unlink()
  147. return True
  148. def get_active_pricelist_version(self):
  149. date = self.env.context.get('date') or fields.Date.context_today(self)
  150. version = False
  151. for v in self.version_id:
  152. if ((v.date_start is False) or (v.date_start <= date)) and ((v.date_end is False) or (v.date_end >= date)):
  153. version = v
  154. break
  155. return version