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.

140 lines
6.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models, _
  3. from datetime import date
  4. class ResPartner(models.Model):
  5. _inherit = 'res.partner'
  6. # def _auto_init(self, cr, context=None):
  7. # """
  8. # Convert the column birthdate into date if it's not the case to avoid warning and data loss with orm conversion
  9. # """
  10. # cr.execute("select data_type from information_schema.columns where table_name = 'res_partner' and column_name= 'birthdate';")
  11. # res = cr.fetchone()
  12. # if not 'date' in res:
  13. # cr.execute("ALTER TABLE res_partner ALTER COLUMN birthdate TYPE date USING birthdate::date;")
  14. #
  15. # return super(ResPartner, self)._auto_init(cr, context=context)
  16. @api.multi
  17. def _invoice_total(self):
  18. account_invoice_report = self.env['account.invoice.report']
  19. if not self.ids:
  20. self.total_invoiced = 0.0
  21. return True
  22. user_currency_id = self.env.user.company_id.currency_id.id
  23. all_partners_and_children = {}
  24. all_partner_ids = []
  25. for partner in self:
  26. # price_total is in the company currency
  27. all_partners_and_children[partner] = self.search([('id', 'child_of', partner.id)]).ids
  28. all_partner_ids += all_partners_and_children[partner]
  29. # searching account.invoice.report via the orm is comparatively expensive
  30. # (generates queries "id in []" forcing to build the full table).
  31. # In simple cases where all invoices are in the same currency than the user's company
  32. # access directly these elements
  33. # generate where clause to include multicompany rules
  34. where_query = account_invoice_report._where_calc([
  35. ('partner_id', 'in', all_partner_ids), ('state', 'not in', ['draft', 'cancel']), ('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. share_type_list = [('','')]
  55. for share_type in self.env['product.product'].search([('is_share','=',True)]):
  56. share_type_list.append((str(share_type.id),share_type.short_name))
  57. return share_type_list
  58. @api.multi
  59. @api.depends('share_ids')
  60. def _compute_effective_date(self):
  61. #TODO change it to compute it from the share register
  62. for partner in self:
  63. if partner.share_ids:
  64. partner.effective_date = partner.share_ids[0].effective_date
  65. @api.multi
  66. @api.depends('share_ids')
  67. def _compute_cooperator_type(self):
  68. for partner in self:
  69. share_type = ''
  70. for line in partner.share_ids:
  71. share_type = str(line.share_product_id.id)
  72. if share_type != '':
  73. partner.cooperator_type = share_type
  74. @api.multi
  75. @api.depends('share_ids')
  76. def _compute_share_info(self):
  77. for partner in self:
  78. number_of_share = 0
  79. total_value = 0.0
  80. for line in partner.share_ids:
  81. number_of_share += line.share_number
  82. total_value += line.share_unit_price * line.share_number
  83. partner.number_of_share = number_of_share
  84. partner.total_value = total_value
  85. cooperator = fields.Boolean(string='Cooperator', help="Check this box if this contact is a cooperator(effective or not).")
  86. member = fields.Boolean(string='Effective cooperator', help="Check this box if this cooperator is an effective member.")
  87. coop_candidate = fields.Boolean(string="Cooperator candidate", compute="_compute_coop_candidate", store=True, readonly=True)
  88. old_member = fields.Boolean(string='Old cooperator', help="Check this box if this cooperator is no more an effective member.")
  89. gender = fields.Selection([('male', 'Male'), ('female', 'Female'), ('other', 'Other')], string='Gender')
  90. national_register_number = fields.Char(string='National Register Number')
  91. share_ids = fields.One2many('share.line','partner_id',string='Share Lines')
  92. cooperator_register_number = fields.Integer(string='Cooperator Number')
  93. #birthdate = fields.Date(string="Birthdate")
  94. number_of_share = fields.Integer(compute="_compute_share_info", multi='share', string='Number of share', readonly=True)
  95. total_value = fields.Float(compute="_compute_share_info", multi='share', string='Total value of shares', readonly=True)
  96. company_register_number = fields.Char(string='Company Register Number')
  97. cooperator_type = fields.Selection(selection='_get_share_type', compute='_compute_cooperator_type', string='Cooperator Type', store=True)
  98. effective_date = fields.Date(sting="Effective Date", compute='_compute_effective_date', store=True)
  99. representative = fields.Boolean(string="Legal Representative")
  100. subscription_request_ids = fields.One2many('subscription.request', 'partner_id', string="Subscription request")
  101. @api.multi
  102. @api.depends('subscription_request_ids.state')
  103. def _compute_coop_candidate(self):
  104. for partner in self:
  105. paid_sub_req = partner.subscription_request_ids.filtered(lambda record: record.state == 'paid')
  106. if paid_sub_req:
  107. is_candidate = False
  108. else:
  109. if len(partner.subscription_request_ids.filtered(lambda record: record.state != 'cancelled')) > 0:
  110. is_candidate = True
  111. else :
  112. is_candidate = False
  113. partner.coop_candidate = is_candidate
  114. def has_representative(self):
  115. if self.child_ids.filtered('representative'):
  116. return True
  117. return False
  118. def get_representative(self):
  119. return self.child_ids.filtered('representative')
  120. def get_cooperator_from_nin(self, national_id_number):
  121. return self.search([('cooperator','=',True),('national_register_number','=',national_id_number)])
  122. def get_cooperator_from_crn(self, company_register_number):
  123. return self.search([('cooperator','=',True),('company_register_number','=',company_register_number)])