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.

84 lines
3.1 KiB

  1. from odoo import api, fields, models
  2. class PartnerUpdateInfo(models.TransientModel):
  3. _name = "partner.update.info"
  4. _description = "Update Partner Info"
  5. @api.model
  6. def _get_partner(self):
  7. active_id = self.env.context.get("active_id")
  8. return self.env["res.partner"].browse(active_id)
  9. @api.model
  10. def _get_register_number(self):
  11. partner = self._get_partner()
  12. if partner.is_company:
  13. return partner.company_register_number
  14. @api.model
  15. def _get_is_company(self):
  16. return self._get_partner().is_company
  17. is_company = fields.Boolean(string="Is company", default=_get_is_company)
  18. register_number = fields.Char(
  19. string="Register Company Number", default=_get_register_number
  20. )
  21. cooperator = fields.Many2one(
  22. "res.partner", string="Cooperator", default=_get_partner
  23. )
  24. all = fields.Boolean(string="Update from subscription request")
  25. birthdate = fields.Boolean(string="Set missing birth date")
  26. legal_form = fields.Boolean(string="Set legal form")
  27. representative_function = fields.Boolean(string="Set function")
  28. @api.multi
  29. def update(self):
  30. partner_obj = self.env["res.partner"]
  31. cooperator = self.cooperator
  32. coop_vals = {}
  33. if self.all:
  34. if self.legal_form or self.representative_function:
  35. coops = partner_obj.search(
  36. [("cooperator", "=", True), ("is_company", "=", True)]
  37. )
  38. for coop in coops:
  39. coop_vals = {}
  40. sub_reqs = coop.subscription_request_ids.filtered(
  41. lambda r: r.state in ["done", "paid"]
  42. )
  43. if sub_reqs:
  44. sub_req = sub_reqs[0]
  45. if self.legal_form:
  46. coop_vals["legal_form"] = sub_req.company_type
  47. coop.write(coop_vals)
  48. if self.representative_function:
  49. contact = coop.get_representative()
  50. contact.function = sub_req.contact_person_function
  51. else:
  52. coops = partner_obj.search(
  53. [
  54. ("cooperator", "=", True),
  55. ("birthdate_date", "=", False),
  56. ("is_company", "=", False),
  57. ]
  58. )
  59. for coop in coops:
  60. coop_vals = {}
  61. sub_reqs = coop.subscription_request_ids.filtered(
  62. lambda r: r.state in ["done", "paid"]
  63. )
  64. if sub_reqs:
  65. sub_req = sub_reqs[0]
  66. if self.birthdate:
  67. coop_vals["birthdate_date"] = sub_req.birthdate
  68. coop.write(coop_vals)
  69. else:
  70. if cooperator:
  71. if cooperator.is_company:
  72. coop_vals["company_register_number"] = self.register_number
  73. if coop_vals:
  74. cooperator.write(coop_vals)
  75. return True