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.

103 lines
3.0KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, models
  4. from odoo.exceptions import UserError
  5. '''
  6. odoo 按日期分组的显示格式不符合中国用户习惯
  7. 按月分组显示为【十月 2022】 需改成 【2022-10】
  8. 按日期分组显示为 【15 十月 2022】需改成 【2022-10-15】
  9. '''
  10. models.READ_GROUP_DISPLAY_FORMAT = {
  11. 'hour': 'hh:00 dd MM', # 哪里有按小时分组的?
  12. 'day': 'YYYY-MM-dd', # 年月日
  13. 'week': "YYYY-'第'w'周'", # w YYYY = ISO week-year
  14. 'month': 'YYYY-MM',
  15. 'quarter': 'YYYY-QQQ',
  16. 'year': 'YYYY',
  17. }
  18. '''
  19. 单据自动编号,避免在所有单据对象上重载create方法
  20. 只需在ir.sequence里新增一条code与当前model相同的记录即可实现自动编号
  21. '''
  22. create_original = models.BaseModel.create
  23. @api.model_create_multi
  24. @api.returns('self', lambda value: value.id)
  25. def create(self, vals_list):
  26. if not self._name.split('.')[0] in ['mail', 'ir', 'res']:
  27. for vals in vals_list:
  28. if not vals.get('name'):
  29. next_name = self.env['ir.sequence'].next_by_code(self._name)
  30. if next_name:
  31. vals.update({'name': next_name})
  32. record_ids = create_original(self, vals_list)
  33. return record_ids
  34. models.BaseModel.create = create
  35. '''
  36. 不能删除已确认的单据
  37. 避免在所有单据对象上重载unlink
  38. 删除记录要在ir.logging表里记录
  39. '''
  40. # 部分模型也是用了done这个状态,需要放行
  41. BYPASS_MODELS = [
  42. 'survey.user_input',
  43. 'survey.user_input_line'
  44. ]
  45. # 部分模型不需要记录删除时的 log
  46. _NOLOG_MODELS = [
  47. 'ir.logging',
  48. 'website.page',
  49. 'mail.message',
  50. 'ir.model.data',
  51. ]
  52. unlink_original = models.BaseModel.unlink
  53. def unlink(self):
  54. IrLogging = self.env['ir.logging']
  55. field_list = [item[0] for item in self._fields.items()]
  56. for record in self:
  57. if record.is_transient():
  58. continue
  59. if 'state' in field_list and self._name not in BYPASS_MODELS:
  60. if record.state == 'done':
  61. raise UserError('不能删除已确认的 %s !' % record._description)
  62. if self._name in _NOLOG_MODELS or not record.display_name:
  63. continue
  64. IrLogging.sudo().create({
  65. 'name': record.display_name,
  66. 'type': 'client',
  67. 'dbname': self.env.cr.dbname,
  68. 'level': 'WARN',
  69. 'message': '%s被删除' % self._name,
  70. 'path': self.env.user.name,
  71. 'func': 'delete',
  72. 'line': 1})
  73. return unlink_original(self)
  74. models.BaseModel.unlink = unlink
  75. '''
  76. 在所有模型上增加了作废方法
  77. '''
  78. def action_cancel(self):
  79. for record in self:
  80. if 'state' in [item[0] for item in self._fields.items()]:
  81. record.state = 'cancel'
  82. return True
  83. models.BaseModel.action_cancel = action_cancel
上海开阖软件有限公司 沪ICP备12045867号-1