membership.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. STATE = [
  5. ('none', 'Non Member'),
  6. ('canceled', 'Cancelled Member'),
  7. ('old', 'Old Member'),
  8. ('waiting', 'Waiting Member'),
  9. ('invoiced', 'Invoiced Member'),
  10. ('free', 'Free Member'),
  11. ('paid', 'Paid Member'),
  12. ]
  13. class MembershipLine(models.Model):
  14. _name = 'membership.membership_line'
  15. _rec_name = 'partner'
  16. _order = 'id desc'
  17. _description = 'Membership Line'
  18. partner = fields.Many2one('res.partner', string='Partner', ondelete='cascade', index=True)
  19. membership_id = fields.Many2one('product.product', string="Membership", required=True)
  20. date_from = fields.Date(string='From', readonly=True)
  21. date_to = fields.Date(string='To', readonly=True)
  22. date_cancel = fields.Date(string='Cancel date')
  23. date = fields.Date(string='Join Date',
  24. help="Date on which member has joined the membership")
  25. member_price = fields.Float(string='Membership Fee',
  26. digits='Product Price', required=True,
  27. help='Amount for the membership')
  28. account_invoice_line = fields.Many2one('account.move.line', string='Account Invoice line', readonly=True, ondelete='cascade')
  29. account_invoice_id = fields.Many2one('account.move', related='account_invoice_line.move_id', string='Invoice', readonly=True)
  30. company_id = fields.Many2one('res.company', related='account_invoice_line.move_id.company_id', string="Company", readonly=True, store=True)
  31. state = fields.Selection(STATE, compute='_compute_state', string='Membership Status', store=True,
  32. help="It indicates the membership status.\n"
  33. "-Non Member: A member who has not applied for any membership.\n"
  34. "-Cancelled Member: A member who has cancelled his membership.\n"
  35. "-Old Member: A member whose membership date has expired.\n"
  36. "-Waiting Member: A member who has applied for the membership and whose invoice is going to be created.\n"
  37. "-Invoiced Member: A member whose invoice has been created.\n"
  38. "-Paid Member: A member who has paid the membership amount.")
  39. @api.depends('account_invoice_id.state',
  40. 'account_invoice_id.amount_residual',
  41. 'account_invoice_id.payment_state')
  42. def _compute_state(self):
  43. """Compute the state lines """
  44. if not self:
  45. return
  46. self._cr.execute('''
  47. SELECT reversed_entry_id, COUNT(id)
  48. FROM account_move
  49. WHERE reversed_entry_id IN %s
  50. GROUP BY reversed_entry_id
  51. ''', [tuple(self.mapped('account_invoice_id.id'))])
  52. reverse_map = dict(self._cr.fetchall())
  53. for line in self:
  54. move_state = line.account_invoice_id.state
  55. payment_state = line.account_invoice_id.payment_state
  56. line.state = 'none'
  57. if move_state == 'draft':
  58. line.state = 'waiting'
  59. elif move_state == 'posted':
  60. if payment_state == 'paid':
  61. if reverse_map.get(line.account_invoice_id.id):
  62. line.state = 'canceled'
  63. else:
  64. line.state = 'paid'
  65. elif payment_state == 'in_payment':
  66. line.state = 'paid'
  67. elif payment_state in ('not_paid', 'partial'):
  68. line.state = 'invoiced'
  69. elif move_state == 'cancel':
  70. line.state = 'canceled'