res_state.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (c) 2012 Cubic ERP - Teradata SAC. (http://cubicerp.com).
  6. #
  7. # WARNING: This program as such is intended to be used by professional
  8. # programmers who take the whole responsability of assessing all potential
  9. # consequences resulting from its eventual inadequacies and bugs
  10. # End users who are looking for a ready-to-use solution with commercial
  11. # garantees and support are strongly adviced to contract a Free Software
  12. # Service Company
  13. #
  14. # This program is Free Software; you can redistribute it and/or
  15. # modify it under the terms of the GNU General Public License
  16. # as published by the Free Software Foundation; either version 2
  17. # of the License, or (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program; if not, write to the Free Software
  26. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. #
  28. ##############################################################################
  29. from openerp.osv import fields, osv
  30. class res_state(osv.osv):
  31. def name_get(self, cr, uid, ids, context=None):
  32. if not len(ids):
  33. return []
  34. res = []
  35. for state in self.browse(cr, uid, ids, context=context):
  36. data = []
  37. acc = state
  38. while acc:
  39. data.insert(0, acc.name)
  40. acc = acc.parent_id
  41. data = ' / '.join(data)
  42. res.append((state.id, (state.code and '[' + state.code + '] ' or '') + data))
  43. return res
  44. def complete_name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
  45. if not args:
  46. args = []
  47. args = args[:]
  48. ids = []
  49. if name:
  50. ids = self.search(cr, user, [('name', operator, name)]+ args, limit=limit)
  51. if not ids and len(name.split()) >= 2:
  52. #Separating code and name of account for searching
  53. operand1,operand2 = name.split(': ',1) #name can contain spaces e.g. OpenERP S.A.
  54. ids = self.search(cr, user, [('name', operator, operand2)]+ args, limit=limit)
  55. else:
  56. ids = self.search(cr, user, args, context=context, limit=limit)
  57. return self.name_get(cr, user, ids, context=context)
  58. def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
  59. if not ids:
  60. return []
  61. res = []
  62. for state in self.browse(cr, uid, ids, context=context):
  63. data = []
  64. acc = state
  65. while acc:
  66. data.insert(0, acc.name)
  67. acc = acc.parent_id
  68. data = ' / '.join(data)
  69. res.append((state.id, data))
  70. return dict(res)
  71. _name = 'res.country.state'
  72. _inherit = 'res.country.state'
  73. _columns = {
  74. 'code': fields.char('State Code', size=32,help='The state code.\n', required=True),
  75. 'complete_name': fields.function(_name_get_fnc, method=True, type="char", string='Complete Name', fnct_search=complete_name_search),
  76. 'parent_id': fields.many2one('res.country.state','Parent State', select=True, domain=[('type','=','view')]),
  77. 'child_ids': fields.one2many('res.country.state', 'parent_id', string='Child States'),
  78. 'type': fields.selection([('view','View'), ('normal','Normal')], 'Type'),
  79. }
  80. _defaults = {
  81. 'type': 'normal',
  82. }
  83. res_state()
  84. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: