GoodERP
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

290 Zeilen
13KB

  1. from .utils import safe_division
  2. from odoo.exceptions import UserError
  3. from odoo import models, fields, api
  4. from odoo.tools import float_compare
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. class Goods(models.Model):
  8. _inherit = 'goods'
  9. net_weight = fields.Float('净重', digits='Weight')
  10. current_qty = fields.Float('当前数量', compute='compute_stock_qty', digits='Quantity')
  11. max_stock_qty = fields.Float('库存上限', digits='Quantity')
  12. min_stock_qty = fields.Float('库存下限', digits='Quantity')
  13. moq = fields.Float('最小订单量', digits='Quantity')
  14. sell_lead_time = fields.Char('销售备货周期')
  15. excess = fields.Boolean('允许订单超发')
  16. bom_count = fields.Integer('Bom个数', compute="_compute_count")
  17. bom_ids = fields.Many2many('wh.bom', string='Bom', compute="_compute_count")
  18. move_line_count = fields.Integer('调拨次数', compute="_compute_count")
  19. incoming_ids = fields.One2many(
  20. string='即将入库',
  21. comodel_name='wh.move.line',
  22. inverse_name='goods_id',
  23. domain=[('type','=','in'),('state','=','draft')],
  24. readonly=True,
  25. )
  26. outgoing_ids = fields.One2many(
  27. string='即将出库',
  28. comodel_name='wh.move.line',
  29. inverse_name='goods_id',
  30. domain=[('type','=','out'),('state','=','draft')],
  31. readonly=True,
  32. )
  33. available_qty = fields.Float('可用数量', compute='compute_stock_qty', digits='Quantity')
  34. # 使用SQL来取得指定商品情况下的库存数量
  35. def get_stock_qty(self):
  36. for Goods in self:
  37. self.env.cr.execute('''
  38. SELECT sum(line.qty_remaining) as qty,
  39. wh.name as warehouse
  40. FROM wh_move_line line
  41. LEFT JOIN warehouse wh ON line.warehouse_dest_id = wh.id
  42. WHERE line.qty_remaining != 0
  43. AND wh.type = 'stock'
  44. AND line.state = 'done'
  45. AND line.goods_id = %s
  46. GROUP BY wh.name
  47. ''' % (Goods.id,))
  48. return self.env.cr.dictfetchall()
  49. def compute_stock_qty(self):
  50. for g in self:
  51. g.current_qty = sum(line.get('qty') for line in g.get_stock_qty())
  52. g.available_qty = g.current_qty \
  53. + sum(l.goods_qty for l in g.incoming_ids) \
  54. - sum(l.goods_qty for l in g.outgoing_ids)
  55. def _get_cost(self, warehouse=None, ignore=None):
  56. # 如果没有历史的剩余数量,计算最后一条move的成本
  57. # 存在一种情况,计算一条line的成本的时候,先done掉该line,之后在通过该函数
  58. # 查询成本,此时百分百搜到当前的line,所以添加ignore参数来忽略掉指定的line
  59. self.ensure_one()
  60. if warehouse:
  61. domain = [
  62. ('state', '=', 'done'),
  63. ('goods_id', '=', self.id),
  64. ('warehouse_dest_id', '=', warehouse.id)
  65. ]
  66. if ignore:
  67. if isinstance(ignore, int):
  68. ignore = [ignore]
  69. domain.append(('id', 'not in', ignore))
  70. move = self.env['wh.move.line'].search(
  71. domain, limit=1, order='cost_time desc, id desc')
  72. if move:
  73. return move.cost_unit
  74. return self.cost
  75. def get_suggested_cost_by_warehouse(
  76. self, warehouse, qty, lot_id=None, attribute=None, ignore_move=None):
  77. # 存在一种情况,计算一条line的成本的时候,先done掉该line,之后在通过该函数
  78. # 查询成本,此时百分百搜到当前的line,所以添加ignore参数来忽略掉指定的line
  79. if lot_id:
  80. print(lot_id)
  81. records, cost = self.get_matching_records_by_lot( # 有批次:优先按批次去取成本;返回:匹配记录和成本
  82. lot_id, qty, suggested=True)
  83. else:
  84. records, cost = self.get_matching_records( # 没有批次:按 库位 就近、先到期先出、先进先出原则 ->查(wh.move.line)匹配记录去取成本
  85. warehouse, qty, attribute=attribute, ignore_stock=True, ignore=ignore_move)
  86. matching_qty = sum(record.get('qty') for record in records)
  87. if matching_qty:
  88. cost_unit = safe_division(cost, matching_qty)
  89. if matching_qty >= qty:
  90. return cost, cost_unit
  91. else:
  92. cost_unit = self._get_cost(warehouse, ignore=ignore_move)
  93. return cost_unit * qty, cost_unit
  94. def is_using_matching(self):
  95. """
  96. 是否需要获取匹配记录
  97. :return:
  98. """
  99. if self.no_stock:
  100. return False
  101. return True
  102. def is_using_batch(self):
  103. """
  104. 是否使用批号管理
  105. :return:
  106. """
  107. self.ensure_one()
  108. return self.using_batch
  109. def get_matching_records_by_lot(self, lot_id, qty, uos_qty=0, suggested=False):
  110. """
  111. 按批号来获取匹配记录
  112. :param lot_id: 明细中输入的批号
  113. :param qty: 明细中输入的数量
  114. :param uos_qty: 明细中输入的辅助数量
  115. :param suggested:
  116. :return: 匹配记录和成本
  117. """
  118. self.ensure_one()
  119. if not lot_id:
  120. raise UserError(u'批号没有被指定,无法获得成本')
  121. if not suggested and lot_id.state != 'done':
  122. raise UserError(u'正在确认ID为{}的移库行。批号%s还没有实际入库,请先确认该入库'
  123. .format(lot_id.id, lot_id.move_id.name))
  124. decimal_quantity = self.env.ref('core.decimal_quantity')
  125. _logger.info('{} / {} / {} / {}'.format(lot_id.lot, qty, lot_id.qty_remaining, decimal_quantity.digits))
  126. if (float_compare(qty, lot_id.qty_remaining, decimal_quantity.digits) > 0
  127. and not self.env.context.get('wh_in_line_ids')):
  128. raise UserError(u'商品%s %s 批次 %s 的库存数量 %s 不够本次出库 %s' % (
  129. self.code and '[%s]' % self.code or '', self.name, lot_id.lot, lot_id.qty_remaining, qty))
  130. return [{'line_in_id': lot_id.id, 'qty': qty, 'uos_qty': uos_qty,
  131. 'expiration_date': lot_id.expiration_date}], \
  132. lot_id.get_real_cost_unit() * qty
  133. def get_matching_records(self, warehouse, qty, uos_qty=0, attribute=None,
  134. ignore_stock=False, ignore=None, move_line=False):
  135. """
  136. 获取匹配记录,不考虑批号
  137. :param ignore_stock: 当参数指定为True的时候,此时忽略库存警告
  138. :param ignore: 一个move_line列表,指定查询成本的时候跳过这些move
  139. :return: 匹配记录和成本
  140. """
  141. matching_records = []
  142. for Goods in self:
  143. domain = [
  144. ('qty_remaining', '>', 0),
  145. ('state', '=', 'done'),
  146. ('warehouse_dest_id', '=', warehouse.id),
  147. ('goods_id', '=', Goods.id)
  148. ]
  149. if ignore:
  150. if isinstance(ignore, int):
  151. domain.append(('id', 'not in', [ignore]))
  152. if attribute:
  153. domain.append(('attribute_id', '=', attribute.id))
  154. # 内部移库,从源库位移到目的库位,匹配时从源库位取值; location.py confirm_change 方法
  155. if self.env.context.get('location'):
  156. domain.append(
  157. ('location_id', '=', self.env.context.get('location')))
  158. # 出库单行 填写了库位
  159. if not self.env.context.get('location') and move_line and move_line.location_id:
  160. domain.append(('location_id', '=', move_line.location_id.id))
  161. """@zzx需要在大量数据的情况下评估一下速度"""
  162. # 出库顺序按 库位 就近、先到期先出、先进先出
  163. lines = self.env['wh.move.line'].search(
  164. domain, order='location_id, expiration_date, cost_time, id')
  165. qty_to_go, uos_qty_to_go, cost = qty, uos_qty, 0 # 分别为待出库商品的数量、辅助数量和成本
  166. for line in lines:
  167. if qty_to_go <= 0 and uos_qty_to_go <= 0:
  168. break
  169. matching_qty = min(line.qty_remaining, qty_to_go)
  170. matching_uos_qty = matching_qty / Goods.conversion
  171. matching_records.append({'line_in_id': line.id, 'expiration_date': line.expiration_date,
  172. 'qty': matching_qty, 'uos_qty': matching_uos_qty})
  173. cost += matching_qty * line.get_real_cost_unit()
  174. qty_to_go -= matching_qty
  175. uos_qty_to_go -= matching_uos_qty
  176. else:
  177. decimal_quantity = self.env.ref('core.decimal_quantity')
  178. if not ignore_stock and float_compare(qty_to_go, 0, decimal_quantity.digits) > 0 and not self.env.context.get('wh_in_line_ids'):
  179. raise UserError(u'商品%s %s的库存数量不够本次出库' % (Goods.code and '[%s]' % Goods.code or '', Goods.name,))
  180. if self.env.context.get('wh_in_line_ids'):
  181. domain = [('id', 'in', self.env.context.get('wh_in_line_ids')),
  182. ('state', '=', 'done'),
  183. ('warehouse_dest_id', '=', warehouse.id),
  184. ('goods_id', '=', Goods.id)]
  185. if attribute:
  186. domain.append(('attribute_id', '=', attribute.id))
  187. line_in_id = self.env['wh.move.line'].search(
  188. domain, order='expiration_date, cost_time, id')
  189. if line_in_id:
  190. matching_records.append({'line_in_id': line_in_id.id, 'expiration_date': line_in_id.expiration_date,
  191. 'qty': qty_to_go, 'uos_qty': uos_qty_to_go})
  192. return matching_records, cost
  193. _used_not_allowed_modification = ({'uom_id','uos_id','conversion'},
  194. '商品已被使用, 不允许修改单位或转化率')
  195. def write(self, vals):
  196. used_fields, used_msg = self._used_not_allowed_modification
  197. if len(used_fields)>0 and ( set(vals.keys()).intersection(used_fields) ):
  198. # 所有用到了商品的字段
  199. self.env.cr.execute("select imf.name, imf.model from goods_reference_black_list grbl "+
  200. "left join ir_model_fields imf on imf.model_id = grbl.ref_model_id" +
  201. " where imf.relation=%s " +
  202. "and imf.ttype in ('many2one') and imf.store=true;",
  203. ('goods', ))
  204. relation_fields = self.env.cr.fetchall()
  205. for goods in self:
  206. been_used = False
  207. # 所有用到了当前商品的记录
  208. for field, model in relation_fields:
  209. if not been_used:
  210. sql = "select id from " + model.replace('.', '_') + \
  211. " where " + field + "=" + str(goods.id) + ";"
  212. self.env.cr.execute(sql)
  213. if self.env.cr.fetchall():
  214. been_used = True
  215. if been_used:
  216. raise UserError(used_msg)
  217. return super().write(vals)
  218. def _compute_count(self):
  219. ''' 此商品作为组合件的BOM个数 '''
  220. for s in self:
  221. bom_lines = self.env['wh.bom.line'].search(
  222. [('goods_id', '=', s.id),
  223. ('type', '=', 'parent')])
  224. s.bom_ids = [(6, 0, list(set([l.bom_id.id for l in bom_lines])))]
  225. s.bom_count = len(s.bom_ids)
  226. move_lines = self.env['wh.move.line'].search(
  227. [('goods_id', '=', s.id),
  228. ('state', '=', 'done')])
  229. s.move_line_count = len(move_lines)
  230. def button_list_bom(self):
  231. return {
  232. 'name': '%s 物料清单' % self.name,
  233. 'view_mode': 'list,form',
  234. 'res_model': 'wh.bom',
  235. 'type': 'ir.actions.act_window',
  236. 'domain': [('id', 'in', self.bom_ids.ids)],
  237. }
  238. def button_list_move(self):
  239. return {
  240. 'name': '%s 库存调拨' % self.name,
  241. 'view_mode': 'list',
  242. 'res_model': 'wh.move.line',
  243. 'type': 'ir.actions.act_window',
  244. 'domain': [('goods_id', '=', self.id)],
  245. 'context': {'search_default_done':1}
  246. }
  247. class GoodsReferenceBlackList(models.Model):
  248. _name = 'goods.reference.black.list'
  249. _description = '商品引用检测黑名单'
  250. ref_model_id = fields.Many2one('ir.model', '模型', help='指定的模型若有引用商品的,该商品对应的单位或转化率无法进行修改')
上海开阖软件有限公司 沪ICP备12045867号-1