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.

139 lines
6.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 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. user_currency_id = self.env.user.company_id.currency_id.id
  22. all_partners_and_children = {}
  23. all_partner_ids = []
  24. for partner in self:
  25. # price_total is in the company currency
  26. all_partners_and_children[partner] = self.search([('id', 'child_of', partner.id)]).ids
  27. all_partner_ids += all_partners_and_children[partner]
  28. # searching account.invoice.report via the orm is comparatively expensive
  29. # (generates queries "id in []" forcing to build the full table).
  30. # In simple cases where all invoices are in the same currency than the user's company
  31. # 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), ('state', 'not in', ['draft', 'cancel']), ('company_id', '=', self.env.user.company_id.id),
  35. ('type', 'in', ('out_invoice', 'out_refund')),
  36. ('release_capital_request','=',False),
  37. ])
  38. account_invoice_report._apply_ir_rules(where_query, 'read')
  39. from_clause, where_clause, where_clause_params = where_query.get_sql()
  40. # price_total is in the company currency
  41. query = """
  42. SELECT SUM(price_total) as total, partner_id
  43. FROM account_invoice_report account_invoice_report
  44. WHERE %s
  45. GROUP BY partner_id
  46. """ % where_clause
  47. self.env.cr.execute(query, where_clause_params)
  48. price_totals = self.env.cr.dictfetchall()
  49. for partner, child_ids in all_partners_and_children.items():
  50. partner.total_invoiced = sum(price['total'] for price in price_totals if price['partner_id'] in child_ids)
  51. @api.multi
  52. def _get_share_type(self):
  53. share_type_list = [('','')]
  54. for share_type in self.env['product.product'].search([('is_share','=',True)]):
  55. share_type_list.append((str(share_type.id),share_type.short_name))
  56. return share_type_list
  57. @api.multi
  58. @api.depends('share_ids')
  59. def _compute_effective_date(self):
  60. #TODO change it to compute it from the share register
  61. for partner in self:
  62. if partner.share_ids:
  63. partner.effective_date = partner.share_ids[0].effective_date
  64. @api.multi
  65. @api.depends('share_ids')
  66. def _compute_cooperator_type(self):
  67. for partner in self:
  68. share_type = ''
  69. for line in partner.share_ids:
  70. share_type = str(line.share_product_id.id)
  71. if share_type != '':
  72. partner.cooperator_type = share_type
  73. @api.multi
  74. @api.depends('share_ids')
  75. def _compute_share_info(self):
  76. for partner in self:
  77. number_of_share = 0
  78. total_value = 0.0
  79. for line in partner.share_ids:
  80. number_of_share += line.share_number
  81. total_value += line.share_unit_price * line.share_number
  82. partner.number_of_share = number_of_share
  83. partner.total_value = total_value
  84. cooperator = fields.Boolean(string='Cooperator', help="Check this box if this contact is a cooperator(effective or not).")
  85. member = fields.Boolean(string='Effective cooperator', help="Check this box if this cooperator is an effective member.")
  86. coop_candidate = fields.Boolean(string="Cooperator candidate", compute="_compute_coop_candidate", store=True, readonly=True)
  87. old_member = fields.Boolean(string='Old cooperator', help="Check this box if this cooperator is no more an effective member.")
  88. gender = fields.Selection([('male', 'Male'), ('female', 'Female'), ('other', 'Other')], string='Gender')
  89. national_register_number = fields.Char(string='National Register Number')
  90. share_ids = fields.One2many('share.line','partner_id',string='Share Lines')
  91. cooperator_register_number = fields.Integer(string='Cooperator Number')
  92. #birthdate = fields.Date(string="Birthdate")
  93. number_of_share = fields.Integer(compute="_compute_share_info", multi='share', string='Number of share', readonly=True)
  94. total_value = fields.Float(compute="_compute_share_info", multi='share', string='Total value of shares', readonly=True)
  95. company_register_number = fields.Char(string='Company Register Number')
  96. cooperator_type = fields.Selection(selection='_get_share_type', compute='_compute_cooperator_type', string='Cooperator Type', store=True)
  97. effective_date = fields.Date(sting="Effective Date", compute='_compute_effective_date', store=True)
  98. representative = fields.Boolean(string="Legal Representative")
  99. subscription_request_ids = fields.One2many('subscription.request', 'partner_id', string="Subscription request")
  100. @api.multi
  101. @api.depends('subscription_request_ids.state')
  102. def _compute_coop_candidate(self):
  103. for partner in self:
  104. if partner.member:
  105. is_candidate = False
  106. else:
  107. if len(partner.subscription_request_ids.filtered(lambda record: record.state == 'done')) > 0:
  108. is_candidate = True
  109. else :
  110. is_candidate = False
  111. partner.coop_candidate = is_candidate
  112. def has_representative(self):
  113. if self.child_ids.filtered('representative'):
  114. return True
  115. return False
  116. def get_representative(self):
  117. return self.child_ids.filtered('representative')
  118. def get_cooperator_from_nin(self, national_id_number):
  119. return self.search([('cooperator','=',True),('national_register_number','=',national_id_number)])
  120. def get_cooperator_from_crn(self, company_register_number):
  121. return self.search([('cooperator','=',True),('company_register_number','=',company_register_number)])