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.

101 lines
5.1 KiB

  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models, _
  3. class ResPartner(models.Model):
  4. _inherit = 'res.partner'
  5. @api.multi
  6. def _invoice_total(self):
  7. account_invoice_report = self.env['account.invoice.report']
  8. if not self.ids:
  9. self.total_invoiced = 0.0
  10. return True
  11. user_currency_id = self.env.user.company_id.currency_id.id
  12. all_partners_and_children = {}
  13. all_partner_ids = []
  14. for partner in self:
  15. # price_total is in the company currency
  16. all_partners_and_children[partner] = self.search([('id', 'child_of', partner.id)]).ids
  17. all_partner_ids += all_partners_and_children[partner]
  18. # searching account.invoice.report via the orm is comparatively expensive
  19. # (generates queries "id in []" forcing to build the full table).
  20. # In simple cases where all invoices are in the same currency than the user's company
  21. # access directly these elements
  22. # generate where clause to include multicompany rules
  23. where_query = account_invoice_report._where_calc([
  24. ('partner_id', 'in', all_partner_ids), ('state', 'not in', ['draft', 'cancel']), ('company_id', '=', self.env.user.company_id.id),
  25. ('type', 'in', ('out_invoice', 'out_refund')),
  26. ('release_capital_request','=',False),
  27. ])
  28. account_invoice_report._apply_ir_rules(where_query, 'read')
  29. from_clause, where_clause, where_clause_params = where_query.get_sql()
  30. # price_total is in the company currency
  31. query = """
  32. SELECT SUM(price_total) as total, partner_id
  33. FROM account_invoice_report account_invoice_report
  34. WHERE %s
  35. GROUP BY partner_id
  36. """ % where_clause
  37. self.env.cr.execute(query, where_clause_params)
  38. price_totals = self.env.cr.dictfetchall()
  39. for partner, child_ids in all_partners_and_children.items():
  40. partner.total_invoiced = sum(price['total'] for price in price_totals if price['partner_id'] in child_ids)
  41. @api.multi
  42. def _get_share_type(self):
  43. share_type_list = [('','')]
  44. for share_type in self.env['product.template'].search([('is_share','=',True)]):
  45. share_type_list.append((str(share_type.id),share_type.short_name))
  46. return share_type_list
  47. @api.multi
  48. @api.depends('share_ids')
  49. def _compute_effective_date(self):
  50. for partner in self:
  51. if partner.share_ids:
  52. partner.effective_date = partner.share_ids[0].effective_date
  53. @api.multi
  54. @api.depends('share_ids')
  55. def _compute_cooperator_type(self):
  56. for partner in self:
  57. share_type = ''
  58. for line in partner.share_ids:
  59. share_type = str(line.share_product_id.id)
  60. if share_type != '':
  61. partner.cooperator_type = share_type
  62. @api.multi
  63. @api.depends('share_ids')
  64. def _compute_share_info(self):
  65. for partner in self:
  66. number_of_share = 0
  67. total_value = 0.0
  68. for line in partner.share_ids:
  69. number_of_share += line.share_number
  70. total_value += line.share_unit_price * line.share_number
  71. partner.number_of_share = number_of_share
  72. partner.total_value = total_value
  73. cooperator = fields.Boolean(string='Cooperator', help="Check this box if this contact is a cooperator(effective or not).")
  74. member = fields.Boolean(string='Effective cooperator', help="Check this box if this cooperator is an effective member.")
  75. old_member = fields.Boolean(string='Old cooperator', help="Check this box if this cooperator is no more an effective member.")
  76. gender = fields.Selection([('male', 'Male'), ('female', 'Female'), ('other', 'Other')], string='Gender')
  77. national_register_number = fields.Char(string='National Register Number')
  78. share_ids = fields.One2many('share.line','partner_id',string='Share Lines')
  79. cooperator_register_number = fields.Integer(string='Cooperator Number')
  80. birthdate = fields.Date(string="Birthdate")
  81. number_of_share = fields.Integer(compute="_compute_share_info", multi='share', string='Number of share', readonly=True)
  82. total_value = fields.Float(compute="_compute_share_info", multi='share', string='Total value of shares', readonly=True)
  83. company_register_number = fields.Char(string='National Register Number')
  84. cooperator_type = fields.Selection(selection='_get_share_type', compute='_compute_cooperator_type', string='Cooperator Type', store=True)
  85. effective_date = fields.Date(sting="Effective Date", compute='_compute_effective_date', store=True)
  86. def get_cooperator_from_nin(self, national_id_number):
  87. return self.search([('cooperator','=',True),('national_register_number','=',national_id_number)])
  88. def get_cooperator_from_crn(self, company_register_number):
  89. return self.search([('cooperator','=',True),('company_register_number','=',company_register_number)])