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
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. share_type = ''
  76. for line in partner.share_ids:
  77. if line.share_number > 0:
  78. share_type = line.share_product_id.default_code
  79. break
  80. partner.cooperator_type = share_type
  81. @api.multi
  82. @api.depends('share_ids')
  83. def _compute_share_info(self):
  84. for partner in self:
  85. number_of_share = 0
  86. total_value = 0.0
  87. for line in partner.share_ids:
  88. number_of_share += line.share_number
  89. total_value += line.share_unit_price * line.share_number
  90. partner.number_of_share = number_of_share
  91. partner.total_value = total_value
  92. cooperator = fields.Boolean(string='Cooperator',
  93. help="Check this box if this contact is a"
  94. " cooperator (effective or not).")
  95. member = fields.Boolean(string='Effective cooperator',
  96. help="Check this box if this cooperator"
  97. " is an effective member.")
  98. coop_candidate = fields.Boolean(string="Cooperator candidate",
  99. compute="_compute_coop_candidate",
  100. store=True,
  101. readonly=True)
  102. old_member = fields.Boolean(string='Old cooperator',
  103. help="Check this box if this cooperator is"
  104. " no more an effective member.")
  105. # todo use oca partner_contact_gender
  106. gender = fields.Selection([('male', 'Male'),
  107. ('female', 'Female'),
  108. ('other', 'Other')],
  109. string='Gender')
  110. share_ids = fields.One2many('share.line',
  111. 'partner_id',
  112. string='Share Lines')
  113. cooperator_register_number = fields.Integer(string='Cooperator Number')
  114. number_of_share = fields.Integer(compute="_compute_share_info",
  115. multi='share',
  116. string='Number of share',
  117. readonly=True)
  118. total_value = fields.Float(compute="_compute_share_info",
  119. multi='share',
  120. string='Total value of shares',
  121. readonly=True)
  122. company_register_number = fields.Char(string='Company Register Number')
  123. cooperator_type = fields.Selection(selection='_get_share_type',
  124. compute=_compute_cooperator_type,
  125. string='Cooperator Type',
  126. store=True)
  127. effective_date = fields.Date(sting="Effective Date",
  128. compute=_compute_effective_date,
  129. store=True)
  130. representative = fields.Boolean(string="Legal Representative")
  131. subscription_request_ids = fields.One2many('subscription.request',
  132. 'partner_id',
  133. string="Subscription request")
  134. legal_form = fields.Selection([('', '')],
  135. string="Legal form")
  136. data_policy_approved = fields.Boolean(string="Approved Data Policy")
  137. internal_rules_approved = fields.Boolean(string="Approved Internal Rules")
  138. @api.multi
  139. @api.depends('subscription_request_ids.state')
  140. def _compute_coop_candidate(self):
  141. for partner in self:
  142. if partner.member:
  143. is_candidate = False
  144. else:
  145. sub_requests = partner.subscription_request_ids.filtered(
  146. lambda record: record.state == 'done')
  147. is_candidate = bool(sub_requests)
  148. partner.coop_candidate = is_candidate
  149. @api.multi
  150. def has_representative(self):
  151. self.ensure_one()
  152. if self.child_ids.filtered('representative'):
  153. return True
  154. return False
  155. @api.multi
  156. def get_representative(self):
  157. self.ensure_one()
  158. return self.child_ids.filtered('representative')
  159. def get_cooperator_from_email(self, email):
  160. return self.env['res.partner'].search([('cooperator', '=', True),
  161. ('email', '=', email)])
  162. def get_cooperator_from_crn(self, company_register_number):
  163. return self.env['res.partner'].search([('cooperator', '=', True),
  164. ('company_register_number', '=', company_register_number)])