GoodERP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

278 行
11KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo.exceptions import UserError
  4. from odoo import models, fields, api
  5. from reportlab.graphics.transform import translate
  6. class Country(models.Model):
  7. _name = 'country'
  8. _description = '国家/地区'
  9. name = fields.Char('国家/地区名称', translate=True)
  10. state_ids = fields.One2many('country.state', 'country_id', '省/直辖市/自治区')
  11. company_id = fields.Many2one(
  12. 'res.company',
  13. string='公司',
  14. change_default=True,
  15. default=lambda self: self.env.company)
  16. _sql_constraints = [
  17. ('code_uniq', 'unique(name)', '国家/地区名称必须唯一')
  18. ]
  19. class CountryState(models.Model):
  20. _name = 'country.state'
  21. _description = '省/直辖市/自治区'
  22. country_id = fields.Many2one('country', '国家/地区', required=True)
  23. name = fields.Char('名称', required=True,translate=True)
  24. code = fields.Char('编号', required=True)
  25. city_ids = fields.One2many('all.city', 'province_id', '下辖市/区')
  26. company_id = fields.Many2one(
  27. 'res.company',
  28. string='公司',
  29. change_default=True,
  30. default=lambda self: self.env.company)
  31. class AllCity(models.Model):
  32. _name = 'all.city'
  33. _rec_name = 'city_name'
  34. _description = '地级市'
  35. city_name = fields.Char('名称', required=True, translate=True)
  36. country_id = fields.Many2one('country', '国家/地区', related='province_id.country_id', readonly=True, store=True)
  37. county_ids = fields.One2many('all.county', 'city_id', '下辖县/市')
  38. province_id = fields.Many2one('country.state', '省/直辖市/自治区', required=True)
  39. company_id = fields.Many2one(
  40. 'res.company',
  41. string='公司',
  42. change_default=True,
  43. default=lambda self: self.env.company)
  44. class AllCounty(models.Model):
  45. _name = 'all.county'
  46. _rec_name = 'county_name'
  47. _description = '县/市/区'
  48. country_id = fields.Many2one('country', '国家/地区', related='city_id.province_id.country_id', readonly=True, store=True)
  49. province_id = fields.Many2one('country.state', '省', related='city_id.province_id', readonly=True, store=True)
  50. city_id = fields.Many2one('all.city', '地级市', required=True)
  51. county_name = fields.Char('名称', required=True, translate=True)
  52. description = fields.Char('描述')
  53. company_id = fields.Many2one(
  54. 'res.company',
  55. string='公司',
  56. change_default=True,
  57. default=lambda self: self.env.company)
  58. class StateCityCounty(models.Model):
  59. _name = 'state.city.county'
  60. _description = '县'
  61. country_id = fields.Many2one('country', '国家')
  62. province_id = fields.Many2one('country.state', '省')
  63. city_id = fields.Many2one('all.city', '市')
  64. county_id = fields.Many2one('all.county', '区县')
  65. company_id = fields.Many2one(
  66. 'res.company',
  67. string='公司',
  68. change_default=True,
  69. default=lambda self: self.env.company)
  70. @api.onchange('country_id')
  71. def onchange_country(self):
  72. # 为地址填写时方便,当选定国家时 ,省的列表里面只有所选国家的
  73. if self.country_id:
  74. domain_dict = {'province_id': [
  75. ('country_id', '=', self.country_id.id)]}
  76. if self.province_id:
  77. if self.province_id.country_id == self.country_id:
  78. if self.city_id:
  79. if self.city_id.province_id == self.province_id:
  80. domain_dict = {}
  81. else:
  82. self.city_id = False
  83. else:
  84. self.province_id = False
  85. else:
  86. self.province_id = False
  87. self.city_id = False
  88. self.county_id = False
  89. domain_dict = {'province_id': [], 'city_id': []}
  90. return {'domain': domain_dict}
  91. @api.onchange('province_id')
  92. def onchange_province(self):
  93. # 为地址填写时方便,当选定省时 ,市区的列表里面只有所选省的
  94. if self.province_id:
  95. domain_dict = {'city_id': [
  96. ('province_id', '=', self.province_id.id)]}
  97. if self.city_id:
  98. if self.city_id.province_id == self.province_id:
  99. if self.county_id:
  100. if self.county_id.city_id == self.city_id:
  101. domain_dict = {}
  102. else:
  103. self.county_id = False
  104. else:
  105. self.city_id = False
  106. self.country_id = self.province_id.country_id
  107. else:
  108. self.city_id = False
  109. self.county_id = False
  110. domain_dict = {'city_id': [], 'county_id': []}
  111. return {'domain': domain_dict}
  112. @api.onchange('city_id')
  113. def onchange_city(self):
  114. # 为地址填写时方便,当选定市时 ,县区的列表里面只有所选市的
  115. if self.city_id:
  116. domain_dict = {'county_id': [('city_id', '=', self.city_id.id)]}
  117. province = self.city_id.province_id
  118. if not self.province_id:
  119. if self.county_id:
  120. if self.county_id.city_id != self.city_id:
  121. self.county_id = False
  122. self.province_id = province
  123. else:
  124. self.province_id = province
  125. else:
  126. domain_dict.update(
  127. {'city_id': [('province_id', '=', province.id)]})
  128. if self.county_id:
  129. if self.county_id.city_id == self.city_id:
  130. if province != self.province_id:
  131. self.province_id = province
  132. else:
  133. if province != self.province_id:
  134. self.province_id = province
  135. self.county_id = False
  136. else:
  137. self.county_id = False
  138. else:
  139. if province != self.province_id:
  140. self.province_id = province
  141. else:
  142. self.county_id = False
  143. domain_dict = {'county_id': []}
  144. return {'domain': domain_dict}
  145. @api.onchange('county_id')
  146. def onchange_county(self):
  147. # 选定了一个区县,自动填充其所属的省和市
  148. if self.county_id \
  149. and self.county_id.city_id \
  150. and self.county_id.city_id.province_id:
  151. city_obj = self.county_id.city_id
  152. self.city_id = city_obj
  153. self.province_id = city_obj.province_id
  154. return {'domain': {'county_id': [('city_id', '=', city_obj.id)]}}
  155. class PartnerAddress(models.Model):
  156. _name = 'partner.address'
  157. _inherit = "state.city.county"
  158. _description = '联系人地址'
  159. _order = 'address_func'
  160. _rec_name = 'contact'
  161. partner_id = fields.Many2one('partner', '业务伙伴')
  162. contact = fields.Char('联系人', required=True)
  163. mobile = fields.Char('手机')
  164. phone = fields.Char('座机')
  165. qq = fields.Char('QQ')
  166. email = fields.Char('邮箱')
  167. town = fields.Char('乡镇')
  168. detail_address = fields.Char('详细地址')
  169. is_default_add = fields.Boolean('默认收货人')
  170. department = fields.Char('部门')
  171. job = fields.Char('职务')
  172. supervisor_id = fields.Many2one(
  173. 'partner.address', '上级', domain="[('partner_id','=',partner_id)]")
  174. gone = fields.Boolean('已离职')
  175. note = fields.Text('备注')
  176. address_func = fields.Many2one('core.value', '联系人类型',
  177. ondelete='restrict',
  178. domain=[('type', '=', 'address_func')])
  179. display_name = fields.Char(compute='_compute_display_name', store=True)
  180. @api.depends('province_id', 'province_id.name',
  181. 'city_id', 'city_id.city_name',
  182. 'county_id', 'county_id.county_name',
  183. 'town',
  184. 'detail_address')
  185. def _compute_display_name(self):
  186. ''' 计算完整地址 显示, 注意原来的 name_get 被name_search使用, 不能删除'''
  187. for adds in self:
  188. adds.display_name = '%s%s%s%s%s' % (
  189. adds.province_id and adds.province_id.name or '',
  190. adds.city_id and adds.city_id.city_name or '',
  191. adds.county_id and adds.county_id.county_name or '',
  192. adds.town or '',
  193. adds.detail_address or '')
  194. def name_get(self):
  195. res = []
  196. for adds in self:
  197. add_str = '%s%s%s%s%s' % (
  198. adds.province_id and adds.province_id.name or '',
  199. adds.city_id and adds.city_id.city_name or '',
  200. adds.county_id and adds.county_id.county_name or '',
  201. adds.town or '',
  202. adds.detail_address or '')
  203. res.append((adds.id, add_str))
  204. return res
  205. class Partner(models.Model):
  206. _inherit = 'partner'
  207. def _put_info_to_partner(self, child):
  208. self.contact = child.contact
  209. self.mobile = child.mobile
  210. self.phone = child.phone
  211. self.qq = child.qq
  212. address = '%s%s%s%s%s' % (
  213. child.province_id and child.province_id.name or '',
  214. child.city_id and child.city_id.city_name or '',
  215. child.county_id and child.county_id.county_name or '',
  216. child.town or '',
  217. child.detail_address or '')
  218. self.address = address
  219. @api.depends('child_ids.is_default_add', 'child_ids.province_id',
  220. 'child_ids.city_id', 'child_ids.county_id', 'child_ids.town',
  221. 'child_ids.detail_address')
  222. def _compute_partner_address(self):
  223. '''如果业务伙伴地址中有默认地址,则显示在业务伙伴列表上'''
  224. for s in self:
  225. s.contact = s.mobile = ""
  226. s.phone = s.qq = s.address = ""
  227. for child in s.child_ids:
  228. if child.is_default_add: # 如果有默认地址取默认地址
  229. s._put_info_to_partner(child)
  230. # 如果没有默认地址取第一个联系人的
  231. if not any([child.is_default_add for child in s.child_ids]):
  232. partners_add = s.env['partner.address'].search(
  233. [('partner_id', '=', s.id)], order='id')
  234. child = partners_add and partners_add[0] or False
  235. if child:
  236. s._put_info_to_partner(child)
  237. child_ids = fields.One2many('partner.address', 'partner_id', '业务伙伴地址')
  238. contact = fields.Char(
  239. '联系人', compute='_compute_partner_address', store=True)
  240. mobile = fields.Char('手机', compute='_compute_partner_address', store=True)
  241. phone = fields.Char('座机', compute='_compute_partner_address', store=True)
  242. qq = fields.Char('QQ', compute='_compute_partner_address', store=True)
  243. address = fields.Char(
  244. '送货地址', compute='_compute_partner_address', store=True)
上海开阖软件有限公司 沪ICP备12045867号-1