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.

163 lines
7.3 KiB

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