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.

181 lines
7.6 KiB

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