GoodERP
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

89 lines
3.3KB

  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. display_name = fields.Char(compute='_compute_display_name', store=True)
  16. name = fields.Char('名称', required=True)
  17. code = fields.Char('编号')
  18. type = fields.Selection(WAREHOUSE_TYPE, '类型', default='stock')
  19. active = fields.Boolean('启用', default=True)
  20. user_ids = fields.Many2many('res.users', string='库管')
  21. address = fields.Char('地址')
  22. phone = fields.Char('电话')
  23. contact = fields.Char('联系人')
  24. company_id = fields.Many2one(
  25. 'res.company',
  26. string='公司',
  27. change_default=True,
  28. default=lambda self: self.env.company)
  29. _sql_constraints = [
  30. ('name_uniq', 'unique(name)', '仓库不能重名')
  31. ]
  32. @api.model
  33. def name_search(self, name='', args=None, operator='ilike', limit=100):
  34. ''' 让warehouse支持使用code来搜索'''
  35. args = args or []
  36. # 将name当成code搜
  37. if name and not [_type for _type in args if _type[0] == 'code']:
  38. warehouses = self.search(
  39. [('type', '=', 'stock'), ('code', 'ilike', name)])
  40. if warehouses:
  41. return warehouses.name_get()
  42. # 下拉列表只显示stock类型的仓库
  43. if not [_type for _type in args if _type[0] == 'type']:
  44. args = [['type', '=', 'stock']] + args
  45. return super(Warehouse, self).name_search(
  46. name=name, args=args,
  47. operator=operator, limit=limit)
  48. def name_get(self):
  49. '''将仓库显示为 [编号]名字 的形式'''
  50. res = []
  51. for Warehouse in self:
  52. res.append((Warehouse.id, '[%s]%s' %
  53. (Warehouse.code, Warehouse.name)))
  54. return res
  55. @api.depends('code', 'name')
  56. def _compute_display_name(self):
  57. '''将仓库显示为 [编号]名字 的形式, 注意原来的 name_get 被name_search使用, 不能删除'''
  58. for record in self:
  59. record.display_name= record.code and (record.code + '_' + record.name) or record.name
  60. def get_warehouse_by_type(self, _type, need_user_ids=True):
  61. '''返回指定类型的第一个仓库'''
  62. if not _type or _type not in [
  63. _type[0] for _type in self.WAREHOUSE_TYPE]:
  64. raise UserError('仓库类型" % s"不在预先定义的type之中,请联系管理员' % _type)
  65. domain = [('type', '=', _type)]
  66. # 仓库管理员带出有权限的仓库作为默认值
  67. if _type == 'stock' and self.env.user.has_group('warehouse.group_warehouse') and need_user_ids:
  68. # 由于权限组在仓库模块,这里core模块测试用例测不到
  69. domain += ['|', ('user_ids', '=', False),
  70. ('user_ids', 'in', self._uid)]
  71. warehouses = self.search(domain, limit=1, order='id asc')
  72. if not warehouses:
  73. raise UserError('不存在类型为%s的仓库,请检查基础数据是否全部导入' % _type)
  74. return warehouses[0]
上海开阖软件有限公司 沪ICP备12045867号-1