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.

185 lines
6.7 KiB

  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields, api
  3. from odoo.osv.expression import OR
  4. from .ffck_structure_type import SCALES
  5. class ResPartner(models.Model):
  6. _inherit = "res.partner"
  7. # Partner fields
  8. ffck_network = fields.Boolean(string="FFCK network")
  9. first_membership_date = fields.Date("Structure 1st membership")
  10. # Structure typing
  11. partner_scale = fields.Selection(
  12. selection=SCALES,
  13. string="Scale",
  14. compute="_get_partner_scale",
  15. store=True,
  16. )
  17. ffck_structure_type_id = fields.Many2one(
  18. comodel_name="ffck.structure.type",
  19. string="Structure type",
  20. )
  21. partner_code = fields.Char(string="FFCK code", size=6, index=True)
  22. # partner_code_editable = fields.Boolean(string="FFCK code editable", compute="_can_edit_partner_code")
  23. # FFCK
  24. ffck_partner_id = fields.Many2one(
  25. comodel_name="res.partner",
  26. string="FFCK partner",
  27. ondelete="restrict",
  28. compute="_get_ffck_partner",
  29. store=False,
  30. )
  31. ffck_partner_code = fields.Char(string="FFCK", default="0", readonly=True)
  32. # CRCK
  33. crck_partner_id = fields.Many2one(
  34. comodel_name="res.partner",
  35. string="CRCK partner",
  36. index=True,
  37. ondelete="restrict",
  38. compute="_get_crck_partner",
  39. store=True,
  40. )
  41. crck_partner_code = fields.Char(related="crck_partner_id.partner_code", store=True)
  42. # CDCK
  43. cdck_partner_id = fields.Many2one(
  44. comodel_name="res.partner",
  45. string="CDCK partner",
  46. index=True,
  47. ondelete="restrict",
  48. compute="_get_cdck_partner",
  49. store=True,
  50. )
  51. cdck_partner_code = fields.Char(related="cdck_partner_id.partner_code", store=True)
  52. local_partner_id = fields.Many2one(
  53. comodel_name="res.partner",
  54. string="Local partner",
  55. index=True,
  56. ondelete="restrict",
  57. domain=[("ffck_network", "=", True), ("partner_scale", "=", 4)],
  58. )
  59. local_partner_code = fields.Char(
  60. related="local_partner_id.partner_code", store=True
  61. )
  62. @api.model
  63. def name_search(self, name="", args=None, operator="ilike", limit=100):
  64. """Allow searching by sequence code by default."""
  65. # Do not add any domain when user just clicked on search widget
  66. if not (name == "" and operator == "ilike"):
  67. # The dangling | is needed to combine with the domain added by super()
  68. args = OR(
  69. [
  70. [
  71. "&",
  72. ("ffck_network", "=", True),
  73. ("partner_code", "=like", name + "%"),
  74. ],
  75. args or [],
  76. ]
  77. )
  78. return super().name_search(name, args, operator, limit)
  79. def name_get(self):
  80. result = []
  81. # ffck_partners = self.filtered('ffck_network')
  82. # others = self - ffck_partners
  83. for record in self:
  84. if record.ffck_network:
  85. code = record.partner_code
  86. ref = record.ref
  87. result.append(
  88. (
  89. record.id,
  90. "{}{}{}".format(
  91. code and code + " - " or "",
  92. ref and record.is_company and ref + " - " or "",
  93. super(ResPartner, record).name_get()[0][1],
  94. ),
  95. )
  96. )
  97. else:
  98. result += super(ResPartner, record).name_get()
  99. return result
  100. # COMPUTES
  101. @api.depends("company_type", "ffck_structure_type_id")
  102. def _get_partner_scale(self):
  103. for partner in self:
  104. if partner.company_type == "individual":
  105. partner.partner_scale = "5"
  106. else:
  107. partner.partner_scale = partner.ffck_structure_type_id.scale
  108. def _get_ffck_partner(self):
  109. ffck = self.env.ref("ffck_commons.res_partner_ffck", raise_if_not_found=False)
  110. main = self.env.ref("base.main_partner")
  111. for partner in self:
  112. if main.ref == "FFCK":
  113. if ffck and ffck.active:
  114. ffck.write({"active": False})
  115. partner.ffck_partner_id = main
  116. else:
  117. partner.ffck_partner_id = ffck
  118. @api.depends("state_id", "ffck_network", "partner_scale")
  119. def _get_crck_partner(self):
  120. states = self.mapped("state_id")
  121. crck_ok = self.search(
  122. [
  123. ("ffck_network", "=", True),
  124. ("partner_scale", "=", "2"),
  125. ("state_id", "in", states.ids),
  126. ]
  127. )
  128. crck_by_state = {crck.state_id: crck for crck in crck_ok}
  129. print(crck_by_state)
  130. states_ok = states & crck_ok.mapped("state_id")
  131. concerned = self.filtered(
  132. lambda rp: rp.ffck_network
  133. and int(rp.partner_scale) >= 3
  134. and rp.state_id in states_ok
  135. )
  136. for partner in concerned:
  137. state = partner.state_id
  138. partner.crck_partner_id = crck_by_state[state]
  139. # treat unconcerned
  140. for partner in self - concerned:
  141. partner.crck_partner_id = False
  142. @api.depends("country_department_id", "ffck_network", "partner_scale")
  143. def _get_cdck_partner(self):
  144. depts = self.mapped("country_department_id")
  145. cdck_ok = self.search(
  146. [
  147. ("ffck_network", "=", True),
  148. ("partner_scale", "=", "4"),
  149. ("country_department_id", "in", depts.ids),
  150. ]
  151. )
  152. cdck_by_dept = {cdck.country_department_id: cdck for cdck in cdck_ok}
  153. depts_ok = depts & cdck_ok.mapped("country_department_id")
  154. concerned = self.filtered(
  155. lambda rp: rp.ffck_network
  156. and int(rp.partner_scale) >= 4
  157. and rp.country_department_id in depts_ok
  158. )
  159. for partner in concerned:
  160. dept = partner.country_department_id
  161. partner.cdck_partner_id = cdck_by_dept[dept]
  162. # treat unconcerned
  163. for partner in self - concerned:
  164. partner.cdck_partner_id = False
  165. # ONCHANGES
  166. @api.onchange("partner_code", "company_type", "ffck_network")
  167. def onchange_partner_code(self):
  168. if self.ffck_network and self.is_company == False:
  169. code = self.partner_code
  170. if len(code) < 6:
  171. self.update({"partner_code": code.zfill(6)})
  172. elif len(code) > 6:
  173. self.update({"partner_code": code[:-6]})