GoodERP
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

84 lines
3.0KB

  1. from odoo import api, fields, models
  2. from odoo.exceptions import UserError
  3. class Warehouse(models.Model):
  4. _name = 'warehouse'
  5. _description = '仓库'
  6. # 用户只能创建stock类型的仓库
  7. WAREHOUSE_TYPE = [
  8. ('stock', '库存'),
  9. ('supplier', '供应商'),
  10. ('customer', '客户'),
  11. ('inventory', '盘点'),
  12. ('production', '生产'),
  13. ('others', '其他'),
  14. ]
  15. name = fields.Char('名称', required=True)
  16. code = fields.Char('编号')
  17. type = fields.Selection(WAREHOUSE_TYPE, '类型', default='stock')
  18. active = fields.Boolean('启用', default=True)
  19. user_ids = fields.Many2many('res.users', string='库管')
  20. address = fields.Char('地址')
  21. phone = fields.Char('电话')
  22. contact = fields.Char('联系人')
  23. company_id = fields.Many2one(
  24. 'res.company',
  25. string='公司',
  26. change_default=True,
  27. default=lambda self: self.env.company)
  28. _sql_constraints = [
  29. ('name_uniq', 'unique(name)', '仓库不能重名')
  30. ]
  31. @api.model
  32. def name_search(self, name='', args=None, operator='ilike', limit=100):
  33. ''' 让warehouse支持使用code来搜索'''
  34. args = args or []
  35. # 将name当成code搜
  36. if name and not [_type for _type in args if _type[0] == 'code']:
  37. warehouses = self.search(
  38. [('type', '=', 'stock'), ('code', 'ilike', name)])
  39. if warehouses:
  40. return warehouses.name_get()
  41. # 下拉列表只显示stock类型的仓库
  42. if not [_type for _type in args if _type[0] == 'type']:
  43. args = [['type', '=', 'stock']] + args
  44. return super(Warehouse, self).name_search(
  45. name=name, args=args,
  46. operator=operator, limit=limit)
  47. def name_get(self):
  48. '''将仓库显示为 [编号]名字 的形式'''
  49. res = []
  50. for Warehouse in self:
  51. res.append((Warehouse.id, '[%s]%s' %
  52. (Warehouse.code, Warehouse.name)))
  53. return res
  54. def get_warehouse_by_type(self, _type):
  55. '''返回指定类型的第一个仓库'''
  56. if not _type or _type not in [
  57. _type[0] for _type in self.WAREHOUSE_TYPE]:
  58. raise UserError('仓库类型" % s"不在预先定义的type之中,请联系管理员' % _type)
  59. domain = [('type', '=', _type)]
  60. # 仓库管理员带出有权限的仓库作为默认值
  61. if _type == 'stock' and self.env.user.has_group(
  62. 'warehouse.group_warehouse'):
  63. # 由于权限组在仓库模块,这里core模块测试用例测不到
  64. domain += ['|', ('user_ids', '=', False),
  65. ('user_ids', 'in', self._uid)]
  66. warehouses = self.search(domain, limit=1, order='id asc')
  67. if not warehouses:
  68. raise UserError('不存在类型为%s的仓库,请检查基础数据是否全部导入' % _type)
  69. return warehouses[0]
上海开阖软件有限公司 沪ICP备12045867号-1