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.

184 lines
7.8 KiB

5 years ago
  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([('id', 'child_of', partner.id)]).ids
  25. all_partner_ids += all_partners_and_children[partner]
  26. # searching account.invoice.report via the orm is comparatively
  27. # expensive (generates queries "id in []" forcing to build the
  28. # full table).
  29. # In simple cases where all invoices are in the same currency than
  30. # the user's company access directly these elements
  31. # generate where clause to include multicompany rules
  32. where_query = account_invoice_report._where_calc([
  33. ('partner_id', 'in', all_partner_ids),
  34. ('state', 'not in', ['draft', 'cancel']),
  35. ('company_id', '=', self.env.user.company_id.id),
  36. ('type', 'in', ('out_invoice', 'out_refund')),
  37. ('release_capital_request', '=', False),
  38. ])
  39. account_invoice_report._apply_ir_rules(where_query, 'read')
  40. from_clause, where_clause, where_clause_params = where_query.get_sql()
  41. # price_total is in the company currency
  42. query = """
  43. SELECT SUM(price_total) as total, partner_id
  44. FROM account_invoice_report account_invoice_report
  45. WHERE %s
  46. GROUP BY partner_id
  47. """ % where_clause
  48. self.env.cr.execute(query, where_clause_params)
  49. price_totals = self.env.cr.dictfetchall()
  50. for partner, child_ids in all_partners_and_children.items():
  51. partner.total_invoiced = sum(price['total'] for price in price_totals if price['partner_id'] in child_ids)
  52. @api.multi
  53. @api.depends('share_ids')
  54. def _compute_effective_date(self):
  55. # TODO change it to compute it from the share register
  56. for partner in self:
  57. if partner.share_ids:
  58. partner.effective_date = partner.share_ids[0].effective_date
  59. @api.multi
  60. def _get_share_type(self):
  61. shares = (
  62. self.env['product.product']
  63. .search([('is_share', '=', True)])
  64. )
  65. share_types = [
  66. (share.default_code, share.short_name) for share in shares
  67. ]
  68. return [('', '')] + share_types
  69. @api.multi
  70. @api.depends('share_ids', 'share_ids.share_product_id',
  71. 'share_ids.share_product_id.default_code',
  72. 'share_ids.share_number')
  73. def _compute_cooperator_type(self):
  74. for partner in self:
  75. if partner.share_ids and partner.share_ids[0].share_number > 0:
  76. share = partner.share_ids[0]
  77. partner.cooperator_type = share.share_product_id.default_code
  78. else:
  79. partner.cooperator_type = ''
  80. @api.multi
  81. @api.depends('share_ids')
  82. def _compute_share_info(self):
  83. for partner in self:
  84. number_of_share = 0
  85. total_value = 0.0
  86. for line in partner.share_ids:
  87. number_of_share += line.share_number
  88. total_value += line.share_unit_price * line.share_number
  89. partner.number_of_share = number_of_share
  90. partner.total_value = total_value
  91. cooperator = fields.Boolean(string='Cooperator',
  92. help="Check this box if this contact is a"
  93. " cooperator (effective or not).")
  94. member = fields.Boolean(string='Effective cooperator',
  95. help="Check this box if this cooperator"
  96. " is an effective member.")
  97. coop_candidate = fields.Boolean(string="Cooperator candidate",
  98. compute="_compute_coop_candidate",
  99. store=True,
  100. readonly=True)
  101. old_member = fields.Boolean(string='Old cooperator',
  102. help="Check this box if this cooperator is"
  103. " no more an effective member.")
  104. # todo use oca partner_contact_gender
  105. gender = fields.Selection([('male', 'Male'),
  106. ('female', 'Female'),
  107. ('other', 'Other')],
  108. string='Gender')
  109. share_ids = fields.One2many('share.line',
  110. 'partner_id',
  111. string='Share Lines')
  112. cooperator_register_number = fields.Integer(string='Cooperator Number')
  113. number_of_share = fields.Integer(compute="_compute_share_info",
  114. multi='share',
  115. string='Number of share',
  116. readonly=True)
  117. total_value = fields.Float(compute="_compute_share_info",
  118. multi='share',
  119. string='Total value of shares',
  120. readonly=True)
  121. company_register_number = fields.Char(string='Company Register Number')
  122. cooperator_type = fields.Selection(selection='_get_share_type',
  123. compute=_compute_cooperator_type,
  124. string='Cooperator Type',
  125. store=True)
  126. effective_date = fields.Date(sting="Effective Date",
  127. compute=_compute_effective_date,
  128. store=True)
  129. representative = fields.Boolean(string="Legal Representative")
  130. subscription_request_ids = fields.One2many('subscription.request',
  131. 'partner_id',
  132. string="Subscription request")
  133. legal_form = fields.Selection([('', '')],
  134. string="Legal form")
  135. data_policy_approved = fields.Boolean(string="Approved Data Policy")
  136. internal_rules_approved = fields.Boolean(string="Approved Internal Rules")
  137. @api.multi
  138. @api.depends('subscription_request_ids.state')
  139. def _compute_coop_candidate(self):
  140. for partner in self:
  141. if partner.member:
  142. is_candidate = False
  143. else:
  144. sub_requests = partner.subscription_request_ids.filtered(
  145. lambda record: record.state == 'done')
  146. is_candidate = bool(sub_requests)
  147. partner.coop_candidate = is_candidate
  148. @api.multi
  149. def has_representative(self):
  150. self.ensure_one()
  151. if self.child_ids.filtered('representative'):
  152. return True
  153. return False
  154. @api.multi
  155. def get_representative(self):
  156. self.ensure_one()
  157. return self.child_ids.filtered('representative')
  158. def get_cooperator_from_email(self, email):
  159. return self.env['res.partner'].search([('cooperator', '=', True),
  160. ('email', '=', email)])
  161. def get_cooperator_from_crn(self, company_register_number):
  162. return self.env['res.partner'].search([('cooperator', '=', True),
  163. ('company_register_number', '=', company_register_number)])