GoodERP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 line
4.5KB

  1. from odoo import models, fields, api
  2. from odoo.exceptions import UserError
  3. class Location(models.Model):
  4. _name = 'location'
  5. _description = '库位'
  6. _order = 'name'
  7. def _get_current_qty(self):
  8. # 获取当前 库位 的商品数量
  9. for location in self:
  10. domain = [
  11. ('warehouse_dest_id', '=', location.warehouse_id.id),
  12. ('location_id', '=', location.id),
  13. ('state', '=', 'done')
  14. ]
  15. if location.attribute_id:
  16. domain.append(('attribute_id', '=', location.attribute_id.id))
  17. if location.goods_id:
  18. lines = self.env['wh.move.line'].search(domain)
  19. current_qty = sum(line.qty_remaining for line in lines)
  20. if not current_qty:
  21. location.write({'goods_id': False, 'attribute_id': False})
  22. else:
  23. current_qty = 0
  24. location.current_qty = current_qty
  25. location.write({'save_qty': current_qty})
  26. name = fields.Char('库位号',
  27. required=True)
  28. warehouse_id = fields.Many2one('warehouse',
  29. string='仓库',
  30. required=True)
  31. goods_id = fields.Many2one('goods',
  32. '商品')
  33. attribute_id = fields.Many2one('attribute', '属性', ondelete='restrict',
  34. help='商品的属性')
  35. current_qty = fields.Float('数量', compute='_get_current_qty')
  36. save_qty = fields.Float('在手数量')
  37. _sql_constraints = [
  38. ('wh_loc_uniq', 'unique(warehouse_id, name)', '同仓库库位不能重名')
  39. ]
  40. def change_location(self):
  41. for location in self:
  42. view = self.env.ref('warehouse.change_location_form')
  43. return {
  44. 'name': '库位',
  45. 'view_mode': 'form',
  46. 'res_model': 'change.location',
  47. 'view_id': False,
  48. 'views': [(view.id, 'form')],
  49. 'type': 'ir.actions.act_window',
  50. 'target': 'new',
  51. 'context': {'default_from_location': location.id},
  52. }
  53. class ChangeLocation(models.TransientModel):
  54. _name = 'change.location'
  55. _description = '库位转移'
  56. from_location = fields.Many2one('location', string='源库位', required=True)
  57. to_location = fields.Many2one('location', string='目的库位', required=True)
  58. change_qty = fields.Float('转出数量')
  59. def confirm_change(self):
  60. for change in self:
  61. if change.change_qty <= 0.0:
  62. raise UserError('转出数量必须大于零')
  63. if change.from_location.id == change.to_location.id:
  64. raise UserError('转出库位 %s 与转入库位不能相同' %
  65. change.from_location.name)
  66. if change.from_location.current_qty < change.change_qty:
  67. raise UserError('转出数量不能大于库位现有数量,库位 %s 现有数量 %s'
  68. % (change.from_location.name, change.from_location.current_qty))
  69. # 转出库位与转入库位的 产品、产品属性要相同
  70. if (change.from_location.goods_id.id != change.to_location.goods_id.id and change.to_location.goods_id.id) or \
  71. (change.from_location.attribute_id.id != change.to_location.attribute_id.id and change.to_location.attribute_id.id):
  72. raise UserError('请检查转出库位与转入库位的产品、产品属性是否都相同!')
  73. # 创建 内部移库单
  74. wh_internal = self.env['wh.internal'].with_context({'location': change.from_location.id}).create({
  75. 'warehouse_id': change.from_location.warehouse_id.id,
  76. 'warehouse_dest_id': change.to_location.warehouse_id.id,
  77. })
  78. self.env['wh.move.line'].with_context({'type': 'internal'}).create({
  79. 'move_id': wh_internal.move_id.id,
  80. 'goods_id': change.from_location.goods_id.id,
  81. 'attribute_id': change.from_location.attribute_id.id,
  82. 'goods_qty': change.change_qty,
  83. 'location_id': change.to_location.id,
  84. })
  85. # 自动审核 内部移库单
  86. wh_internal.approve_order()
  87. # 返回 更新产品数量后的 库位列表
  88. return {
  89. 'type': 'ir.actions.client',
  90. 'tag': 'reload',
  91. }
上海开阖软件有限公司 沪ICP备12045867号-1