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.

244 lines
8.6 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. representative_of_member_company = fields.Boolean(
  160. string="Legal Representative of Member Company",
  161. store=True,
  162. compute="_compute_representative_of_member_company",
  163. )
  164. subscription_request_ids = fields.One2many(
  165. "subscription.request", "partner_id", string="Subscription request"
  166. )
  167. legal_form = fields.Selection([("", "")], string="Legal form")
  168. data_policy_approved = fields.Boolean(string="Approved Data Policy")
  169. internal_rules_approved = fields.Boolean(string="Approved Internal Rules")
  170. financial_risk_approved = fields.Boolean(string="Approved Financial Risk")
  171. @api.multi
  172. @api.depends("subscription_request_ids.state")
  173. def _compute_coop_candidate(self):
  174. for partner in self:
  175. if partner.member:
  176. is_candidate = False
  177. else:
  178. sub_requests = partner.subscription_request_ids.filtered(
  179. lambda record: record.state == "done"
  180. )
  181. is_candidate = bool(sub_requests)
  182. partner.coop_candidate = is_candidate
  183. @api.multi
  184. @api.depends("parent_id", "representative")
  185. def _compute_representative_of_member_company(self):
  186. for partner in self:
  187. member_companies = self.env["res.partner"].search(
  188. [("is_company", "=", True), ("member", "=", True)]
  189. )
  190. representatives = member_companies.mapped("child_ids").filtered(
  191. "representative"
  192. )
  193. partner.representative_of_member_company = (
  194. partner in representatives
  195. )
  196. @api.multi
  197. def has_representative(self):
  198. self.ensure_one()
  199. if self.child_ids.filtered("representative"):
  200. return True
  201. return False
  202. @api.multi
  203. def get_representative(self):
  204. self.ensure_one()
  205. return self.child_ids.filtered("representative")
  206. def get_cooperator_from_email(self, email):
  207. return self.env["res.partner"].search(
  208. [("cooperator", "=", True), ("email", "=", email)]
  209. )
  210. def get_cooperator_from_crn(self, company_register_number):
  211. company_register_number = company_register_number.strip()
  212. if company_register_number:
  213. partner = self.env["res.partner"].search(
  214. [
  215. ("cooperator", "=", True),
  216. ("company_register_number", "=", company_register_number),
  217. ]
  218. )
  219. else:
  220. partner = None
  221. return partner