GoodERP
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

104 lignes
4.7KB

  1. # -*- coding: utf-8 -*-
  2. from odoo import _
  3. from odoo import models, fields, api
  4. from odoo.exceptions import UserError
  5. class Location(models.Model):
  6. _name = 'location'
  7. _description = '库位'
  8. _order = 'name'
  9. def _get_current_qty(self):
  10. # 获取当前 库位 的商品数量
  11. for Location in self:
  12. lines = self.env['wh.move.line'].search([('goods_id', '=', Location.goods_id.id),
  13. ('attribute_id', '=',
  14. Location.attribute_id.id),
  15. ('warehouse_dest_id', '=',
  16. Location.warehouse_id.id),
  17. ('location_id',
  18. '=', Location.id),
  19. ('state', '=', 'done')])
  20. Location.current_qty = sum([line.qty_remaining for line in lines])
  21. Location.write({'save_qty': Location.current_qty})
  22. if Location.current_qty == 0:
  23. Location.write({'goods_id': False, 'attribute_id': False})
  24. name = fields.Char('库位号',
  25. required=True)
  26. warehouse_id = fields.Many2one('warehouse',
  27. string='仓库',
  28. required=True)
  29. goods_id = fields.Many2one('goods',
  30. '商品')
  31. attribute_id = fields.Many2one('attribute', '属性', ondelete='restrict',
  32. help='商品的属性')
  33. current_qty = fields.Integer('数量',
  34. compute='_get_current_qty'
  35. )
  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