mrp.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2015 ERP|OPEN (www.erpopen.nl).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp.osv import osv, fields
  23. from openerp.tools.translate import _
  24. from datetime import datetime
  25. import openerp.addons.decimal_precision as dp
  26. class mrp_bom(osv.osv):
  27. _inherit = "mrp.bom"
  28. def _get_bom_standard_price(self, cr, uid, bom_id, context=None):
  29. """
  30. :rtype : float
  31. """
  32. if isinstance(bom_id,(long,int)):
  33. bom_id = [bom_id]
  34. cost_price = 0.0
  35. for mainbom in self.browse(cr, uid, bom_id, context=context):
  36. if mainbom.bom_line_ids:
  37. cost_price = 0.0
  38. for line in mainbom.bom_line_ids:
  39. cost_price += (line.product_id and line.product_id.standard_price)*(line.product_qty or 0.0) or 0.0
  40. else:
  41. cost_price = (mainbom.product_id and mainbom.product_id.standard_price)*\
  42. (mainbom.product_qty or 0.0) or 0.0
  43. return cost_price
  44. def _get_bom_list_price(self, cr, uid, bom_id, context=None):
  45. """
  46. :rtype : float
  47. """
  48. if isinstance(bom_id,(long,int)):
  49. bom_id = [bom_id]
  50. list_price = 0.0
  51. for mainbom in self.browse(cr, uid, bom_id, context=context):
  52. if mainbom.bom_line_ids:
  53. list_price = 0.0
  54. for line in mainbom.bom_line_ids:
  55. list_price += (line.product_id and line.product_id.list_price)*(line.product_qty or 0.0) or 0.0
  56. else:
  57. list_price = (mainbom.product_id and mainbom.product_id.list_price)*\
  58. (mainbom.product_qty or 0.0) or 0.0
  59. return list_price
  60. def _get_price(self, cr, uid, ids, field_name, arg, context=None):
  61. if isinstance(ids,(long, int)):
  62. ids = [ids]
  63. if not context:
  64. context={}
  65. res={}
  66. for mainbom in self.browse(cr, uid, ids, context=context):
  67. res[mainbom.id] = {'bom_cost_price': 0.0, 'bom_base_price': 0.0, 'bom_list_price': 0.0}
  68. res[mainbom.id]['bom_cost_price'] = self._get_bom_standard_price(cr, uid, mainbom.id, context=context) or 0.0
  69. res[mainbom.id]['bom_base_price'] = self._get_bom_list_price(cr, uid, mainbom.id, context=context) or 0.0
  70. if mainbom.list_price_factor:
  71. res[mainbom.id]['bom_list_price'] = (self._get_bom_list_price(cr, uid, mainbom.id, context=context) or 0.0) * mainbom.list_price_factor
  72. else:
  73. res[mainbom.id]['bom_list_price'] = res[mainbom.id]['bom_base_price']
  74. return res
  75. def _get_product(self, cr, uid, ids, context=None):
  76. bom_ids = self.pool.get('mrp.bom').search(cr, uid, [('product_id','in',ids)])
  77. return bom_ids
  78. _columns = {
  79. 'bom_cost_price': fields.function(_get_price, string='BOM Cost Price', type='float', readonly=True,
  80. digits_compute= dp.get_precision('Product Price'), multi='price', store={
  81. 'product.product': (_get_product, ['standard_price','list_price'], 20),
  82. 'mrp.bom': (lambda self, cr, uid, ids, c={}: ids, ['bom_line_ids', 'product_id', 'product_qty'], 20),
  83. }),
  84. 'bom_base_price': fields.function(_get_price, string='BOM Base Price', type='float', readonly=True,
  85. digits_compute= dp.get_precision('Product Price'), multi='price', store={
  86. 'product.product': (_get_product, ['standard_price','list_price'], 20),
  87. 'mrp.bom': (lambda self, cr, uid, ids, c={}: ids, ['bom_line_ids', 'product_id', 'product_qty'], 20),
  88. }),
  89. 'auto_update_costprice': fields.boolean('Automatically update the cost price of the product'),
  90. 'auto_update_listprice': fields.boolean('Automatically update the sale price of the product'),
  91. 'list_price_factor': fields.float('Sale Price Factor'),
  92. 'bom_list_price': fields.function(_get_price, string='BOM Sale Price', type='float', readonly=True,
  93. digits_compute= dp.get_precision('Product Price'), multi='price', store={
  94. 'product.product': (_get_product, ['standard_price','list_price'], 20),
  95. 'mrp.bom': (lambda self, cr, uid, ids, c={}: ids, ['bom_line_ids', 'product_id', 'product_qty'], 20),
  96. }),
  97. }
  98. _defaults = {
  99. 'auto_update_costprice': lambda *a: 0,
  100. 'auto_update_listprice': lambda *a: 0,
  101. 'list_price_factor': lambda *a: 1,
  102. }
  103. def write_cost_price(self, cr, uid, ids, context=None):
  104. product_obj = self.pool.get('product.product')
  105. if isinstance(ids,(long, int)):
  106. ids = [ids]
  107. for bom in self.browse(cr, uid, ids, context=context):
  108. # If bom change product cost price
  109. if bom.bom_line_ids and bom.product_id and bom.auto_update_costprice:
  110. cost_price = self._get_bom_standard_price(cr, uid, [bom.id],context=context) or 0.0
  111. product_obj.write(cr, uid, [bom.product_id.id],{'standard_price': cost_price})
  112. def write_list_price(self, cr, uid, ids, context=None):
  113. product_obj = self.pool.get('product.product')
  114. if isinstance(ids,(long, int)):
  115. ids = [ids]
  116. for bom in self.browse(cr, uid, ids, context=context):
  117. # If bom change product list price update
  118. if bom.bom_line_ids and bom.product_id and bom.auto_update_listprice:
  119. list_price = self._get_bom_list_price(cr, uid, [bom.id],context=context) or 0.0
  120. if bom.list_price_factor:
  121. list_price = list_price * bom.list_price_factor
  122. product_obj.write(cr, uid, [bom.product_id.id],{'list_price': list_price})
  123. # def create(self, cr, uid, vals, context=None):
  124. # bom_id = super(mrp_bom, self).create(cr, uid, vals, context=context)
  125. # if bom_id:
  126. # for bom in self.browse(cr, uid, [bom_id], context=context):
  127. # if bom.bom_id:
  128. # self.write_cost_price(cr, uid, [bom.bom_id.id])
  129. # self.write_list_price(cr, uid, [bom.bom_id.id])
  130. #
  131. # return bom_id
  132. #
  133. # def write(self, cr, uid, ids, vals, context=None):
  134. # return_value = super(mrp_bom, self).write(cr, uid, ids, vals, context=context)
  135. # if return_value:
  136. # self.write_cost_price(cr, uid, ids, context=context)
  137. # self.write_list_price(cr, uid, ids, context=context)
  138. #
  139. # return return_value
  140. class mrp_bom_line(osv.osv):
  141. _inherit = 'mrp.bom.line'
  142. _columns = {
  143. 'product_standard_price': fields.related(
  144. 'product_id',
  145. 'standard_price',
  146. type="float",
  147. string="Cost Price",
  148. store=False
  149. ),
  150. 'product_list_price': fields.related(
  151. 'product_id',
  152. 'list_price',
  153. type="float",
  154. string="Sale Price",
  155. store=False
  156. )
  157. }
  158. def create(self, cr, uid, vals, context=None):
  159. bom_line_id = super(mrp_bom_line, self).create(
  160. cr, uid, vals, context=context
  161. )
  162. if bom_line_id:
  163. for bom_line in self.browse(
  164. cr, uid, [bom_line_id], context=context
  165. ):
  166. if bom_line.bom_id:
  167. self.pool.get('mrp.bom').write_cost_price(
  168. cr, uid, [bom_line.bom_id.id]
  169. )
  170. self.pool.get('mrp.bom').write_list_price(
  171. cr, uid, [bom_line.bom_id.id]
  172. )
  173. return bom_line_id
  174. def write(self, cr, uid, ids, vals, context=None):
  175. return_value = super(mrp_bom_line, self).write(
  176. cr, uid, ids, vals, context=context
  177. )
  178. if return_value:
  179. for bom_line in self.browse(cr, uid, ids, context=context):
  180. if bom_line.bom_id:
  181. self.pool.get('mrp.bom').write_cost_price(
  182. cr, uid, [bom_line.bom_id.id]
  183. )
  184. self.pool.get('mrp.bom').write_list_price(
  185. cr, uid, [bom_line.bom_id.id]
  186. )
  187. return return_value
  188. class product_product(osv.osv):
  189. _inherit = "product.product"
  190. def write(self, cr, uid, ids, vals, context=None):
  191. return_value = super(product_product, self).write(cr, uid, ids, vals, context=context)
  192. bom_pool = self.pool.get('mrp.bom')
  193. bom_line_pool = self.pool.get('mrp.bom.line')
  194. if isinstance(ids,(long, int)):
  195. ids = [ids]
  196. for product in self.browse(cr, uid, ids, context=context):
  197. bom_line_ids = bom_line_pool.search(cr, uid, [('product_id','=',product.id)])
  198. if isinstance(bom_line_ids, (long,int)):
  199. bom_line_ids = [bom_line_ids]
  200. for line in bom_line_pool.browse(cr, uid, bom_line_ids, context=context):
  201. if line.bom_id.product_id and line.bom_id.auto_update_costprice:
  202. # update cost price
  203. standard_price = bom_pool._get_bom_standard_price(cr, uid, line.bom_id.id, context=context) or 0.0
  204. self.write(cr, uid, line.bom_id.product_id.id,{'standard_price': standard_price})
  205. if line.bom_id.product_id and line.bom_id.auto_update_listprice:
  206. # update list price
  207. list_price = bom_pool._get_bom_list_price(cr, uid, line.bom_id.id, context=context) or 0.0
  208. if line.bom_id.list_price_factor:
  209. list_price = list_price * line.bom_id.list_price_factor
  210. self.write(cr, uid, line.bom_id.product_id.id,{'list_price': list_price})
  211. return return_value