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.

209 lines
9.2 KiB

6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models
  3. class ResPartner(models.Model):
  4. _inherit = 'res.partner'
  5. # def _auto_init(self, cr, context=None):
  6. # """
  7. # Convert the column birthdate into date if it's not the case to avoid warning and data loss with orm conversion
  8. # """
  9. # cr.execute("select data_type from information_schema.columns where table_name = 'res_partner' and column_name= 'birthdate';")
  10. # res = cr.fetchone()
  11. # if not 'date' in res:
  12. # cr.execute("ALTER TABLE res_partner ALTER COLUMN birthdate TYPE date USING birthdate::date;")
  13. #
  14. # return super(ResPartner, self)._auto_init(cr, context=context)
  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.model
  54. def _get_share_type(self):
  55. product_obj = self.env['product.product']
  56. share_type_list = [('', '')]
  57. for share_type in product_obj.search([('is_share', '=', True)]):
  58. share_type_list.append([share_type.default_code,
  59. share_type.short_name])
  60. return share_type_list
  61. @api.multi
  62. @api.depends('share_ids')
  63. def _compute_effective_date(self):
  64. # TODO change it to compute it from the share register
  65. for partner in self:
  66. if partner.share_ids:
  67. partner.effective_date = partner.share_ids[0].effective_date
  68. @api.multi
  69. @api.depends('share_ids', 'share_ids.share_product_id',
  70. 'share_ids.share_product_id.default_code',
  71. 'share_ids.share_number')
  72. def _compute_cooperator_type(self):
  73. for partner in self:
  74. share_type = ''
  75. for line in partner.share_ids:
  76. code = line.share_product_id.default_code
  77. if code:
  78. share_type = str(code)
  79. if share_type != '':
  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='Easy My Coop',
  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. gender = fields.Selection([('male', 'Male'),
  106. ('female', 'Female'),
  107. ('other', 'Other')],
  108. string='Gender')
  109. national_register_number = fields.Char(string='National Register Number')
  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. representative_of_member_company = fields.Boolean(
  132. string="Legal Representative of Member Company",
  133. store=True,
  134. compute="_compute_representative_of_member_company",
  135. )
  136. subscription_request_ids = fields.One2many('subscription.request',
  137. 'partner_id',
  138. string="Subscription request")
  139. legal_form = fields.Selection([('', '')],
  140. string="Legal form")
  141. internal_rules_approved = fields.Boolean(string="Internal Rules Approved ")
  142. data_policy_approved = fields.Boolean(string="Data Policy Approved ")
  143. financial_risk_approved = fields.Boolean(string="Financial Risk Approved ")
  144. @api.multi
  145. def touch_cooperator_type(self):
  146. self.ensure_one()
  147. for line in self.share_ids:
  148. line.share_product_id = line.share_product_id.id
  149. break
  150. return True
  151. @api.multi
  152. @api.depends('subscription_request_ids.state')
  153. def _compute_coop_candidate(self):
  154. for partner in self:
  155. if partner.member:
  156. is_candidate = False
  157. else:
  158. if len(partner.subscription_request_ids.filtered(lambda record: record.state == 'done')) > 0:
  159. is_candidate = True
  160. else:
  161. is_candidate = False
  162. partner.coop_candidate = is_candidate
  163. @api.multi
  164. @api.depends("parent_id", "representative")
  165. def _compute_representative_of_member_company(self):
  166. for partner in self:
  167. member_companies = self.env["res.partner"].search(
  168. [("is_company", "=", True), ("member", "=", True)]
  169. )
  170. partner.representative_of_member_company = (
  171. partner in member_companies.mapped(
  172. "child_ids"
  173. ).filtered(
  174. "representative"
  175. )
  176. )
  177. def has_representative(self):
  178. if self.child_ids.filtered('representative'):
  179. return True
  180. return False
  181. def get_representative(self):
  182. return self.child_ids.filtered('representative')
  183. def get_cooperator_from_email(self, email):
  184. return self.search([('cooperator', '=', True),
  185. ('email', '=', email)])
  186. def get_cooperator_from_crn(self, comp_reg_number):
  187. return self.search([('cooperator', '=', True),
  188. ('company_register_number', '=', comp_reg_number)])