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.

119 lines
5.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 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. 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. for partner in self:
  61. if partner.share_ids:
  62. partner.effective_date = partner.share_ids[0].effective_date
  63. @api.multi
  64. @api.depends('share_ids')
  65. def _compute_cooperator_type(self):
  66. for partner in self:
  67. share_type = ''
  68. for line in partner.share_ids:
  69. share_type = str(line.share_product_id.id)
  70. if share_type != '':
  71. partner.cooperator_type = share_type
  72. @api.multi
  73. @api.depends('share_ids')
  74. def _compute_share_info(self):
  75. for partner in self:
  76. number_of_share = 0
  77. total_value = 0.0
  78. for line in partner.share_ids:
  79. number_of_share += line.share_number
  80. total_value += line.share_unit_price * line.share_number
  81. partner.number_of_share = number_of_share
  82. partner.total_value = total_value
  83. cooperator = fields.Boolean(string='Cooperator', help="Check this box if this contact is a cooperator(effective or not).")
  84. member = fields.Boolean(string='Effective cooperator', help="Check this box if this cooperator is an effective member.")
  85. old_member = fields.Boolean(string='Old cooperator', help="Check this box if this cooperator is no more an effective member.")
  86. gender = fields.Selection([('male', 'Male'), ('female', 'Female'), ('other', 'Other')], string='Gender')
  87. national_register_number = fields.Char(string='National Register Number')
  88. share_ids = fields.One2many('share.line','partner_id',string='Share Lines')
  89. cooperator_register_number = fields.Integer(string='Cooperator Number')
  90. birthdate = fields.Date(string="Birthdate")
  91. number_of_share = fields.Integer(compute="_compute_share_info", multi='share', string='Number of share', readonly=True)
  92. total_value = fields.Float(compute="_compute_share_info", multi='share', string='Total value of shares', readonly=True)
  93. company_register_number = fields.Char(string='Company Register Number')
  94. cooperator_type = fields.Selection(selection='_get_share_type', compute='_compute_cooperator_type', string='Cooperator Type', store=True)
  95. effective_date = fields.Date(sting="Effective Date", compute='_compute_effective_date', store=True)
  96. representative = fields.Boolean(string="Legal Representative")
  97. def has_representative(self):
  98. if self.child_ids.filtered('representative'):
  99. return True
  100. return False
  101. def get_cooperator_from_nin(self, national_id_number):
  102. return self.search([('cooperator','=',True),('national_register_number','=',national_id_number)])
  103. def get_cooperator_from_crn(self, company_register_number):
  104. return self.search([('cooperator','=',True),('company_register_number','=',company_register_number)])