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.7 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. # def _auto_init(self, cr, context=None):
  5. # """
  6. # Convert the column birthdate into date if it's not the case to avoid warning and data loss with orm conversion
  7. # """
  8. # cr.execute("select data_type from information_schema.columns where table_name = 'res_partner' and column_name= 'birthdate';")
  9. # res = cr.fetchone()
  10. # if not 'date' in res:
  11. # cr.execute("ALTER TABLE res_partner ALTER COLUMN birthdate TYPE date USING birthdate::date;")
  12. #
  13. # return super(ResPartner, self)._auto_init(cr, context=context)
  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. def _get_share_type(self):
  54. product_obj = self.env['product.product']
  55. share_type_list = [('', '')]
  56. for share_type in product_obj.search([('is_share', '=', True)]):
  57. share_type_list.append((str(share_type.id), share_type.short_name))
  58. return share_type_list
  59. @api.multi
  60. @api.depends('share_ids')
  61. def _compute_effective_date(self):
  62. # TODO change it to compute it from the share register
  63. for partner in self:
  64. if partner.share_ids:
  65. partner.effective_date = partner.share_ids[0].effective_date
  66. @api.multi
  67. @api.depends('share_ids')
  68. def _compute_cooperator_type(self):
  69. for partner in self:
  70. share_type = ''
  71. for line in partner.share_ids:
  72. share_type = str(line.share_product_id.id)
  73. if share_type != '':
  74. partner.cooperator_type = share_type
  75. @api.multi
  76. @api.depends('share_ids')
  77. def _compute_share_info(self):
  78. for partner in self:
  79. number_of_share = 0
  80. total_value = 0.0
  81. for line in partner.share_ids:
  82. number_of_share += line.share_number
  83. total_value += line.share_unit_price * line.share_number
  84. partner.number_of_share = number_of_share
  85. partner.total_value = total_value
  86. cooperator = fields.Boolean(string='Cooperator',
  87. help="Check this box if this contact is a"
  88. " cooperator(effective or not).")
  89. member = fields.Boolean(string='Effective cooperator',
  90. help="Check this box if this cooperator"
  91. " is an effective member.")
  92. coop_candidate = fields.Boolean(string="Cooperator candidate",
  93. compute="_compute_coop_candidate",
  94. store=True,
  95. readonly=True)
  96. old_member = fields.Boolean(string='Old cooperator',
  97. help="Check this box if this cooperator is"
  98. " no more an effective member.")
  99. gender = fields.Selection([('male', 'Male'),
  100. ('female', 'Female'),
  101. ('other', 'Other')],
  102. string='Gender')
  103. national_register_number = fields.Char(string='National Register Number')
  104. share_ids = fields.One2many('share.line',
  105. 'partner_id',
  106. string='Share Lines')
  107. cooperator_register_number = fields.Integer(string='Cooperator Number')
  108. number_of_share = fields.Integer(compute="_compute_share_info",
  109. multi='share',
  110. string='Number of share',
  111. readonly=True)
  112. total_value = fields.Float(compute="_compute_share_info",
  113. multi='share',
  114. string='Total value of shares',
  115. readonly=True)
  116. company_register_number = fields.Char(string='Company Register Number')
  117. cooperator_type = fields.Selection(selection='_get_share_type',
  118. compute=_compute_cooperator_type,
  119. string='Cooperator Type',
  120. store=True)
  121. effective_date = fields.Date(sting="Effective Date",
  122. compute=_compute_effective_date,
  123. store=True)
  124. representative = fields.Boolean(string="Legal Representative")
  125. subscription_request_ids = fields.One2many('subscription.request',
  126. 'partner_id',
  127. string="Subscription request")
  128. @api.multi
  129. @api.depends('subscription_request_ids.state')
  130. def _compute_coop_candidate(self):
  131. for partner in self:
  132. if partner.member:
  133. is_candidate = False
  134. else:
  135. if len(partner.subscription_request_ids.filtered(lambda record: record.state == 'done')) > 0:
  136. is_candidate = True
  137. else:
  138. is_candidate = False
  139. partner.coop_candidate = is_candidate
  140. def has_representative(self):
  141. if self.child_ids.filtered('representative'):
  142. return True
  143. return False
  144. def get_representative(self):
  145. return self.child_ids.filtered('representative')
  146. def get_cooperator_from_nin(self, national_id_number):
  147. return self.search([('cooperator', '=', True),
  148. ('national_register_number', '=', national_id_number)])
  149. def get_cooperator_from_crn(self, company_register_number):
  150. return self.search([('cooperator', '=', True),
  151. ('company_register_number', '=', company_register_number)])