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.

225 lines
7.8 KiB

  1. # Copyright 2019 Coop IT Easy SCRL fs
  2. # Houssine Bakkali <houssine@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, fields, models
  5. class ResPartner(models.Model):
  6. _inherit = "res.partner"
  7. @api.multi
  8. def _get_report_base_filename(self):
  9. self.ensure_one()
  10. if self.member:
  11. return "Cooperator Certificate - %s" % self.name
  12. else:
  13. return "unknown"
  14. @api.multi
  15. def _invoice_total(self):
  16. account_invoice_report = self.env["account.invoice.report"]
  17. if not self.ids:
  18. self.total_invoiced = 0.0
  19. return True
  20. all_partners_and_children = {}
  21. all_partner_ids = []
  22. for partner in self:
  23. # price_total is in the company currency
  24. all_partners_and_children[partner] = self.search(
  25. [("id", "child_of", partner.id)]
  26. ).ids
  27. all_partner_ids += all_partners_and_children[partner]
  28. # searching account.invoice.report via the orm is comparatively
  29. # expensive (generates queries "id in []" forcing to build the
  30. # full table).
  31. # In simple cases where all invoices are in the same currency than
  32. # the user's company access directly these elements
  33. # generate where clause to include multicompany rules
  34. where_query = account_invoice_report._where_calc(
  35. [
  36. ("partner_id", "in", all_partner_ids),
  37. ("state", "not in", ["draft", "cancel"]),
  38. ("company_id", "=", self.env.user.company_id.id),
  39. ("type", "in", ("out_invoice", "out_refund")),
  40. ("release_capital_request", "=", False),
  41. ]
  42. )
  43. account_invoice_report._apply_ir_rules(where_query, "read")
  44. from_clause, where_clause, where_clause_params = where_query.get_sql()
  45. # fixme while you're here, please fix the query
  46. # to pass pylint sql-injection
  47. # Note de Houssine: note que c'est la
  48. # surcharge d'une fonction standard de la facturation Odoo. Elle
  49. # date de la 9 voir si la v12 a été adaptée où est équivalente à la 12
  50. # price_total is in the company currency pylint:
  51. # pylint: disable=sql-injection
  52. query = (
  53. """
  54. SELECT SUM(price_total) as total, partner_id
  55. FROM account_invoice_report account_invoice_report
  56. WHERE %s
  57. GROUP BY partner_id
  58. """
  59. % where_clause
  60. )
  61. self.env.cr.execute(query, where_clause_params)
  62. price_totals = self.env.cr.dictfetchall()
  63. for partner, child_ids in all_partners_and_children.items():
  64. partner.total_invoiced = sum(
  65. price["total"]
  66. for price in price_totals
  67. if price["partner_id"] in child_ids
  68. )
  69. @api.multi
  70. @api.depends("share_ids")
  71. def _compute_effective_date(self):
  72. # TODO change it to compute it from the share register
  73. for partner in self:
  74. if partner.share_ids:
  75. partner.effective_date = partner.share_ids[0].effective_date
  76. @api.multi
  77. def _get_share_type(self):
  78. shares = self.env["product.product"].search([("is_share", "=", True)])
  79. share_types = [(s.default_code, s.short_name) for s in shares]
  80. return [("", "")] + share_types
  81. @api.multi
  82. @api.depends(
  83. "share_ids",
  84. "share_ids.share_product_id",
  85. "share_ids.share_product_id.default_code",
  86. "share_ids.share_number",
  87. )
  88. def _compute_cooperator_type(self):
  89. for partner in self:
  90. share_type = ""
  91. for line in partner.share_ids:
  92. if line.share_number > 0:
  93. share_type = line.share_product_id.default_code
  94. break
  95. partner.cooperator_type = share_type
  96. @api.multi
  97. @api.depends("share_ids")
  98. def _compute_share_info(self):
  99. for partner in self:
  100. number_of_share = 0
  101. total_value = 0.0
  102. for line in partner.share_ids:
  103. number_of_share += line.share_number
  104. total_value += line.share_unit_price * line.share_number
  105. partner.number_of_share = number_of_share
  106. partner.total_value = total_value
  107. cooperator = fields.Boolean(
  108. string="Cooperator",
  109. help="Check this box if this contact is a"
  110. " cooperator (effective or not).",
  111. )
  112. member = fields.Boolean(
  113. string="Effective cooperator",
  114. help="Check this box if this cooperator" " is an effective member.",
  115. )
  116. coop_candidate = fields.Boolean(
  117. string="Cooperator candidate",
  118. compute="_compute_coop_candidate",
  119. store=True,
  120. readonly=True,
  121. )
  122. old_member = fields.Boolean(
  123. string="Old cooperator",
  124. help="Check this box if this cooperator is"
  125. " no more an effective member.",
  126. )
  127. # todo use oca partner_contact_gender
  128. gender = fields.Selection(
  129. [("male", "Male"), ("female", "Female"), ("other", "Other")],
  130. string="Gender",
  131. )
  132. share_ids = fields.One2many(
  133. "share.line", "partner_id", string="Share Lines"
  134. )
  135. cooperator_register_number = fields.Integer(string="Cooperator Number")
  136. number_of_share = fields.Integer(
  137. compute="_compute_share_info",
  138. multi="share",
  139. string="Number of share",
  140. readonly=True,
  141. )
  142. total_value = fields.Float(
  143. compute="_compute_share_info",
  144. multi="share",
  145. string="Total value of shares",
  146. readonly=True,
  147. )
  148. company_register_number = fields.Char(string="Company Register Number")
  149. cooperator_type = fields.Selection(
  150. selection="_get_share_type",
  151. compute=_compute_cooperator_type,
  152. string="Cooperator Type",
  153. store=True,
  154. )
  155. effective_date = fields.Date(
  156. sting="Effective Date", compute=_compute_effective_date, store=True
  157. )
  158. representative = fields.Boolean(string="Legal Representative")
  159. subscription_request_ids = fields.One2many(
  160. "subscription.request", "partner_id", string="Subscription request"
  161. )
  162. legal_form = fields.Selection([("", "")], string="Legal form")
  163. data_policy_approved = fields.Boolean(string="Approved Data Policy")
  164. internal_rules_approved = fields.Boolean(string="Approved Internal Rules")
  165. financial_risk_approved = fields.Boolean(string="Approved Financial Risk")
  166. @api.multi
  167. @api.depends("subscription_request_ids.state")
  168. def _compute_coop_candidate(self):
  169. for partner in self:
  170. if partner.member:
  171. is_candidate = False
  172. else:
  173. sub_requests = partner.subscription_request_ids.filtered(
  174. lambda record: record.state == "done"
  175. )
  176. is_candidate = bool(sub_requests)
  177. partner.coop_candidate = is_candidate
  178. @api.multi
  179. def has_representative(self):
  180. self.ensure_one()
  181. if self.child_ids.filtered("representative"):
  182. return True
  183. return False
  184. @api.multi
  185. def get_representative(self):
  186. self.ensure_one()
  187. return self.child_ids.filtered("representative")
  188. def get_cooperator_from_email(self, email):
  189. return self.env["res.partner"].search(
  190. [("cooperator", "=", True), ("email", "=", email)]
  191. )
  192. def get_cooperator_from_crn(self, company_register_number):
  193. company_register_number = company_register_number.strip()
  194. if company_register_number:
  195. partner = self.env["res.partner"].search(
  196. [
  197. ("cooperator", "=", True),
  198. ("company_register_number", "=", company_register_number),
  199. ]
  200. )
  201. else:
  202. partner = None
  203. return partner