GoodERP
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

176 lines
6.1KB

  1. from .utils import safe_division
  2. from odoo import models, fields, api
  3. from odoo.exceptions import UserError
  4. class WhMoveMatching(models.Model):
  5. _name = 'wh.move.matching'
  6. _description = '匹配记录'
  7. line_in_id = fields.Many2one(
  8. 'wh.move.line', '入库',
  9. ondelete='set null', index=True,
  10. help='入库单行')
  11. line_out_id = fields.Many2one(
  12. 'wh.move.line', '出库',
  13. ondelete='set null', index=True,
  14. help='出库单行')
  15. goods_id = fields.Many2one('goods',string='商品', related='line_in_id.goods_id', store=True)
  16. attribute_id = fields.Many2one('attribute',string='属性', related='line_in_id.attribute_id', store=True)
  17. lot = fields.Char(string='批号', related='line_in_id.lot', store=True)
  18. qty = fields.Float(
  19. '出库数量',
  20. digits='Quantity', required=True,
  21. help='出库单行商品的数量')
  22. uos_qty = fields.Float(
  23. '辅助数量',
  24. digits='Quantity', required=True,
  25. help='出库单行商品的辅助数量')
  26. cost_unit = fields.Float(
  27. '单位成本',
  28. digits='price',
  29. related='line_in_id.cost_unit',
  30. store=True)
  31. expiration_date = fields.Date(
  32. '过保日',
  33. related='line_in_id.expiration_date',
  34. store=True,
  35. help='商品保质期截止日期')
  36. in_name = fields.Char(
  37. '入库单号',
  38. related='line_in_id.move_id.name',
  39. help='入库单号')
  40. in_date = fields.Date(
  41. '入库日期',
  42. related='line_in_id.date',
  43. help='入库日期')
  44. out_name = fields.Char(
  45. '出库单号',
  46. related='line_out_id.move_id.name',
  47. help='出库单号')
  48. out_date = fields.Date(
  49. '出库日期',
  50. related='line_out_id.date',
  51. help='出库日期')
  52. in_partner_id = fields.Many2one(
  53. 'partner',
  54. string='供应商',
  55. related='line_in_id.move_id.partner_id'
  56. )
  57. out_partner_id = fields.Many2one(
  58. 'partner',
  59. string='客户',
  60. related='line_out_id.move_id.partner_id'
  61. )
  62. warehouse_id = fields.Many2one(
  63. 'warehouse',
  64. string='仓库',
  65. related='line_out_id.warehouse_id'
  66. )
  67. warehouse_id = fields.Many2one(
  68. 'warehouse',
  69. string='仓库',
  70. related='line_out_id.warehouse_id'
  71. )
  72. company_id = fields.Many2one(
  73. 'res.company',
  74. string='公司',
  75. change_default=True,
  76. default=lambda self: self.env.company)
  77. def create_matching(self, line_in_id, line_out_id, qty, uos_qty, expiration_date):
  78. res = {
  79. 'line_out_id': line_out_id,
  80. 'line_in_id': line_in_id,
  81. 'qty': qty,
  82. 'uos_qty': uos_qty,
  83. 'expiration_date': expiration_date,
  84. }
  85. return self.create(res)
  86. class WhMoveLine(models.Model):
  87. _inherit = 'wh.move.line'
  88. qty_remaining = fields.Float(
  89. compute='_get_qty_remaining',
  90. string='剩余数量',
  91. digits='Quantity',
  92. index=True, store=True, readonly=True,
  93. help='商品的剩余数量')
  94. uos_qty_remaining = fields.Float(
  95. compute='_get_qty_remaining', string='剩余辅助数量',
  96. digits='Quantity',
  97. index=True, store=True, readonly=True,
  98. help='商品的剩余辅助数量')
  99. matching_in_ids = fields.One2many(
  100. 'wh.move.matching', 'line_in_id', string='关联的出库',
  101. help='关联的入库单行')
  102. matching_out_ids = fields.One2many(
  103. 'wh.move.matching', 'line_out_id', string='关联的入库',
  104. help='关联的出库单行')
  105. @api.depends('goods_qty', 'matching_in_ids.qty', 'matching_in_ids.uos_qty')
  106. def _get_qty_remaining(self):
  107. for wml in self:
  108. wml.qty_remaining = wml.goods_qty - \
  109. sum(match.qty for match in wml.matching_in_ids)
  110. wml.uos_qty_remaining = wml.goods_uos_qty - \
  111. sum(match.uos_qty for match in wml.matching_in_ids)
  112. def create_matching_obj(self, line, matching):
  113. matching_obj = self.env['wh.move.matching']
  114. matching_obj.create_matching(
  115. matching.get('line_in_id'),
  116. line.id, matching.get('qty'),
  117. matching.get('uos_qty'),
  118. matching.get('expiration_date'),
  119. )
  120. def prev_action_done(self):
  121. """
  122. 发货 matching
  123. """
  124. for line in self:
  125. if line.warehouse_id.type == 'stock' and \
  126. line.goods_id.is_using_matching():
  127. if line.goods_id.is_using_batch() and line.lot_id:
  128. matching_records, cost = \
  129. line.goods_id.get_matching_records_by_lot(
  130. line.lot_id, line.goods_qty, line.goods_uos_qty)
  131. for matching in matching_records:
  132. self.create_matching_obj(line, matching)
  133. else:
  134. matching_records, cost = line.goods_id \
  135. .get_matching_records(
  136. line.warehouse_id, line.goods_qty,
  137. uos_qty = line.goods_uos_qty,
  138. attribute = line.attribute_id,
  139. move_line = line)
  140. for matching in matching_records:
  141. self.create_matching_obj(line, matching)
  142. line.cost_unit = safe_division(cost, line.goods_qty)
  143. line.cost = cost
  144. # 将过保日填充到出库明细行
  145. line.expiration_date = matching_records and matching_records[0].get(
  146. 'expiration_date')
  147. return super(WhMoveLine, self).prev_action_done()
  148. def prev_action_draft(self):
  149. for line in self:
  150. if line.qty_remaining != line.goods_qty:
  151. raise UserError(
  152. '当前的入库已经被其他出库匹配,请先取消相关的出库\n %s' %
  153. '\n'.join([m.out_name for m in line.matching_in_ids])
  154. )
  155. line.matching_in_ids.unlink()
  156. line.matching_out_ids.unlink()
  157. line.expiration_date = False
  158. return super(WhMoveLine, self).prev_action_draft()
上海开阖软件有限公司 沪ICP备12045867号-1