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.

170 lines
7.5 KiB

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