set_auxiliary_name.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from odoo import models, fields, api
  4. from odoo.exceptions import ValidationError
  5. class SetAuxiliaryName(models.TransientModel):
  6. _name = 'archives.set_auxiliary_name'
  7. _module = 'archives'
  8. _description = u'自定义设置'
  9. user_id = fields.Many2one('res.users', string=u'用户', readonly=True, index=True, default=lambda self: self.env.user)
  10. company_id = fields.Many2one('res.company', string=u'公司', readonly=True,
  11. default=lambda self: self.env['res.company']._company_default_get())
  12. table = fields.Char(string=u'表名', index=True)
  13. table_name = fields.Char(string=u'表名', readonly=True)
  14. year = fields.Boolean(string=u'年')
  15. month = fields.Boolean(string=u'月')
  16. day = fields.Boolean(string=u'日')
  17. serial_number_digits = fields.Integer(string=u'流水号位数', default=5)
  18. # 打开设置对话框
  19. def send_and_open(self, table, table_name):
  20. context = {
  21. 'default_user_id': self.env.user.id,
  22. 'default_company_id': self.env.company.id,
  23. 'default_table': table,
  24. 'default_table_name': table_name,
  25. }
  26. setting = self.env["archives.auxiliary_name"].search(
  27. [('user_id', '=', self.env.user.id), ('table', '=', table)])
  28. if setting:
  29. context['default_year'] = setting.year
  30. context['default_month'] = setting.month
  31. context['default_day'] = setting.day
  32. context['default_serial_number_digits'] = setting.serial_number_digits
  33. return {
  34. 'type': 'ir.actions.act_window',
  35. 'res_model': 'archives.set_auxiliary_name',
  36. 'view_type': 'form',
  37. 'view_mode': 'form',
  38. 'context': context
  39. }
  40. def subscribe(self):
  41. setting = self.env["archives.auxiliary_name"].search(
  42. [('user_id', '=', self.user_id.id), ('table', '=', self.table)])
  43. if setting:
  44. setting.unlink()
  45. self.create_setting()
  46. return {}
  47. def create_setting(self):
  48. values = {
  49. 'user_id': self.user_id.id,
  50. 'company_id': self.company_id.id,
  51. 'table': self.table,
  52. 'table_name': self.table_name,
  53. 'year': self.year,
  54. 'month': self.month,
  55. 'day': self.day,
  56. 'serial_number_digits': self.serial_number_digits,
  57. }
  58. self.env['archives.auxiliary_name'].create(values)