GoodERP
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

309 行
11KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import models, fields, api
  4. from odoo.exceptions import UserError
  5. class Goods(models.Model):
  6. """
  7. 继承了core里面定义的goods 模块,并定义了视图和添加字段。
  8. """
  9. _inherit = 'goods'
  10. specification = fields.Char('规格型号', help='规格型号,如果商品有产品图纸,此处填写产品图号', translate=True)
  11. def get_parent_tax_rate(self, parent_id):
  12. # 逐级取商品分类上的税率
  13. tax_rate = parent_id.tax_rate
  14. if not tax_rate and parent_id.parent_id:
  15. tax_rate = self.get_parent_tax_rate(parent_id.parent_id)
  16. return tax_rate
  17. def get_tax_rate(self, goods, partner, type):
  18. """
  19. 获得税率
  20. 如果商品上没有税率,则逐级取商品分类上的税率;
  21. 商品税率和业务伙伴税率做比较:如果都存在,取小的;其中一个存在取该值;都不存在取公司上的进/销项税
  22. """
  23. if not goods:
  24. return
  25. goods_tax_rate, partner_tax_rate = False, False
  26. # 如果商品上没有税率,则取商品分类上的税率
  27. if goods.tax_rate:
  28. goods_tax_rate = goods.tax_rate
  29. elif goods.goods_class_id.tax_rate:
  30. goods_tax_rate = goods.goods_class_id.tax_rate
  31. elif goods.goods_class_id.parent_id: # 逐级取商品分类上的税率
  32. goods_tax_rate = self.get_parent_tax_rate(
  33. goods.goods_class_id.parent_id)
  34. # 取业务伙伴税率
  35. if partner:
  36. partner_tax_rate = partner.tax_rate
  37. # 商品税率和业务伙伴税率做比较,并根据情况返回
  38. if goods_tax_rate and partner_tax_rate:
  39. if goods_tax_rate >= partner_tax_rate:
  40. return partner_tax_rate
  41. else:
  42. return goods_tax_rate
  43. elif goods_tax_rate and not partner_tax_rate:
  44. return goods_tax_rate
  45. elif not goods_tax_rate and partner_tax_rate:
  46. return partner_tax_rate
  47. else:
  48. if type == 'buy':
  49. return self.env.user.company_id.import_tax_rate
  50. elif type == 'sell':
  51. return self.env.user.company_id.output_tax_rate
  52. no_stock = fields.Boolean('虚拟商品')
  53. using_batch = fields.Boolean('管理批号')
  54. force_batch_one = fields.Boolean('管理序列号')
  55. attribute_ids = fields.One2many('attribute', 'goods_id', string='属性')
  56. image = fields.Binary('图片', attachment=True)
  57. supplier_id = fields.Many2one('partner',
  58. '默认供应商',
  59. ondelete='restrict',
  60. domain=[('s_category_id', '!=', False)])
  61. get_way = fields.Selection([
  62. ('self', '自制'),
  63. ('ous', '委外'),
  64. ('po', '采购'),
  65. ], default='po', string='获取方式')
  66. price = fields.Float('售价(含税)')
  67. gpm = fields.Float('毛利率')
  68. barcode = fields.Char('条形码')
  69. note = fields.Text('备注')
  70. goods_class_id = fields.Many2one(
  71. 'goods.class', string='商品分类',
  72. domain=[('type', '=', 'normal')])
  73. display_name = fields.Char(compute='_compute_display_name', store=True)
  74. # 产品显示品牌、名称、内部编号、规格型号。
  75. @api.depends('name','code','specification')
  76. def _compute_display_name(self):
  77. for goods in self:
  78. if goods.name:
  79. name_display = goods.name
  80. else:
  81. name_display = ""
  82. if goods.code:
  83. code_display = "[" + goods.code + "]"
  84. else:
  85. code_display = ""
  86. if goods.specification:
  87. specification_display = "[" + goods.specification + "]"
  88. else:
  89. specification_display = ""
  90. goods.display_name = code_display + name_display + specification_display
  91. def name_get(self):
  92. '''在many2one字段里显示 编号_名称_规格型号'''
  93. res = []
  94. for goods in self:
  95. if goods.name:
  96. name_display = goods.name
  97. else:
  98. name_display = ""
  99. if goods.code:
  100. code_display = "[" + goods.code + "]"
  101. else:
  102. code_display = ""
  103. if goods.specification:
  104. specification_display = "[" + goods.specification + "]"
  105. else:
  106. specification_display = ""
  107. display_name = code_display + name_display + specification_display
  108. res.append((goods.id, display_name))
  109. return res
  110. @api.model
  111. def name_search(self, name='', args=None, operator='ilike', limit=100):
  112. """在many2one字段中支持按编号、图号、规格型号、品牌搜索"""
  113. args = args or []
  114. code_search_goods = []
  115. barcode_search_goods = []
  116. specification_search_goods = []
  117. if name:
  118. goods_id = self.search([('code', '=', name)])
  119. if goods_id:
  120. return goods_id.name_get()
  121. args.append(('code', 'ilike', name))
  122. goods_ids = self.search(args)
  123. if goods_ids:
  124. code_search_goods = goods_ids.name_get()
  125. args.remove(('code', 'ilike', name))
  126. """在many2one字段中支持按规格型号搜索"""
  127. args.append(('specification', 'ilike', name))
  128. my_goods_ids = self.search(args)
  129. if my_goods_ids:
  130. specification_search_goods = my_goods_ids.name_get()
  131. args.remove(('specification', 'ilike', name))
  132. """在many2one字段中支持按条形码搜索"""
  133. args.append(('barcode', 'ilike', name))
  134. my_goods_ids = self.search(args)
  135. if my_goods_ids:
  136. barcode_search_goods = my_goods_ids.name_get()
  137. args.remove(('barcode', 'ilike', name))
  138. search_goods = super(Goods, self).name_search(
  139. name=name, args=args,
  140. operator=operator, limit=limit
  141. )
  142. for good_tup in code_search_goods: # 去除重复产品
  143. if good_tup not in search_goods:
  144. search_goods.append(good_tup)
  145. for good_tup in barcode_search_goods: # 去除重复产品
  146. if good_tup not in search_goods:
  147. search_goods.append(good_tup)
  148. for good_tup in specification_search_goods: # 去除重复产品
  149. if good_tup not in search_goods:
  150. search_goods.append(good_tup)
  151. return search_goods
  152. _sql_constraints = [
  153. ('barcode_uniq', 'unique(barcode)', '条形码不能重复'),
  154. ]
  155. @api.onchange('uom_id')
  156. def onchange_uom(self):
  157. """
  158. :return: 当选取单位时辅助单位默认和 单位相等。
  159. """
  160. self.uos_id = self.uom_id
  161. @api.onchange('gpm', 'cost')
  162. def onchange_gpm(self):
  163. """
  164. 根据毛利率计算零售价
  165. """
  166. if self.gpm >= 1 or self.gpm < 0:
  167. raise UserError('毛利率请输入0%-100%之间的数值.')
  168. self.price = self.cost / (1 - self.gpm)
  169. @api.onchange('price', 'cost')
  170. def onchange_price(self):
  171. """
  172. 根据零售价计算毛利率
  173. """
  174. if self.price > 0 and self.cost > 0:
  175. self.gpm = 1 - self.cost / self.price
  176. @api.onchange('using_batch')
  177. def onchange_using_batch(self):
  178. """
  179. :return: 当将管理批号的勾去掉后,自动将管理序列号的勾去掉
  180. """
  181. if not self.using_batch:
  182. self.force_batch_one = False
  183. def conversion_unit(self, qty):
  184. """ 数量 × 转化率 = 辅助数量
  185. :param qty: 传进来数量计算出辅助数量
  186. :return: 返回辅助数量
  187. """
  188. self.ensure_one()
  189. return self.conversion * qty
  190. def anti_conversion_unit(self, qty):
  191. """ 数量 = 辅助数量 / 转化率
  192. :param qty: 传入值为辅助数量
  193. :return: 数量
  194. """
  195. self.ensure_one()
  196. return self.conversion and qty / self.conversion or 0
  197. class Attribute(models.Model):
  198. _name = 'attribute'
  199. _description = '属性'
  200. @api.depends('value_ids')
  201. def _compute_name(self):
  202. for a in self:
  203. a.name = ' '.join(
  204. [value.category_id.name + ':' + value.value_id.name for value in a.value_ids])
  205. @api.model
  206. def name_search(self, name='', args=None, operator='ilike', limit=100):
  207. '''在many2one字段中支持按条形码搜索'''
  208. args = args or []
  209. if name:
  210. attribute_ids = self.search([('ean', '=', name)])
  211. if attribute_ids:
  212. return attribute_ids.name_get()
  213. return super(Attribute, self).name_search(
  214. name=name, args=args, operator=operator, limit=limit)
  215. ean = fields.Char('条码')
  216. name = fields.Char('属性', compute='_compute_name',
  217. store=True, readonly=True)
  218. goods_id = fields.Many2one('goods', '商品', ondelete='cascade')
  219. value_ids = fields.One2many(
  220. 'attribute.value', 'attribute_id', string='属性值')
  221. company_id = fields.Many2one(
  222. 'res.company',
  223. string='公司',
  224. change_default=True,
  225. default=lambda self: self.env.company)
  226. _sql_constraints = [
  227. ('ean_uniq', 'unique (ean)', '该条码已存在'),
  228. ('goods_attribute_uniq', 'unique (goods_id, name)', '该SKU已存在'),
  229. ]
  230. @api.constrains('value_ids')
  231. def check_value_ids(self):
  232. for a in self:
  233. att_dict = {}
  234. for line in a.value_ids:
  235. if line.category_id not in att_dict:
  236. att_dict[line.category_id] = line.category_id
  237. else:
  238. raise UserError('属性值的类别不能相同')
  239. class AttributeValue(models.Model):
  240. _name = 'attribute.value'
  241. _rec_name = 'value_id'
  242. _description = '属性明细'
  243. attribute_id = fields.Many2one('attribute', '商品属性', ondelete='cascade')
  244. category_id = fields.Many2one('core.category', '属性',
  245. ondelete='cascade',
  246. domain=[('type', '=', 'attribute')],
  247. required=True)
  248. value_id = fields.Many2one('attribute.value.value', '值',
  249. ondelete='restrict',
  250. domain="[('category_id','=',category_id)]",
  251. default=lambda self: self.env.context.get(
  252. 'default_category_id'),
  253. required=True)
  254. company_id = fields.Many2one(
  255. 'res.company',
  256. string='公司',
  257. change_default=True,
  258. default=lambda self: self.env.company)
  259. class AttributeValueValue(models.Model):
  260. _name = 'attribute.value.value'
  261. _description = '属性值'
  262. category_id = fields.Many2one('core.category', '属性',
  263. ondelete='cascade',
  264. domain=[('type', '=', 'attribute')],
  265. required=True)
  266. name = fields.Char('值', required=True)
  267. company_id = fields.Many2one(
  268. 'res.company',
  269. string='公司',
  270. change_default=True,
  271. default=lambda self: self.env.company)
  272. _sql_constraints = [
  273. ('name_category_uniq', 'unique(category_id,name)', '同一属性的值不能重复')
  274. ]
上海开阖软件有限公司 沪ICP备12045867号-1