golem/golem_member/wizard/golem_member_analysis.py

114 lines
4.9 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright 2019 Fabien Bourgeois <fabien@yaltik.com>
# Copyright 2019 Youssef El Ouahby <youssef@yaltik.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" GOLEM Members Analysis Pivot Management """
import logging
from dateutil.relativedelta import relativedelta
from odoo import models, fields, api, _
_LOGGER = logging.getLogger(__name__)
class GolemMemberAnalysisPivot(models.TransientModel):
""" GOLEM Members Analysis Pivot Management """
_name = 'golem.member.analysis'
_description = 'GOLEM Members Analysis Management'
member_analysis_id = fields.Many2one('golem.member.analysis.wizard', index=True)
season_id = fields.Many2one('golem.season', 'Season', required=True,
ondelete='cascade')
member_id = fields.Many2one('golem.member', 'Service user')
area_id = fields.Many2one('golem.partner.area')
gender = fields.Selection([('male', _('Male')),
('female', _('Female')),
('not_disclosed', _('Not Disclosed'))])
category_id = fields.Many2one('res.partner.category')
age_start_season = fields.Integer(string='Age at season start')
age_end_season = fields.Integer(string='Age at season stop')
age_now = fields.Integer()
age_range_start_season = fields.Many2one('golem.member.age.range',
'Age range at season start')
age_range_end_season = fields.Many2one('golem.member.age.range',
'Age range at season end')
age_range_now = fields.Many2one('golem.member.age.range', 'Age range now')
@api.model
def get_root_category(self, category_ids):
""" Get Root Category """
categories = self.env['res.partner.category']
for category_id in category_ids:
if not category_id.parent_id:
return category_id
categories |= category_id.parent_id
return self.get_root_category(categories)
@api.constrains('member_id')
@api.multi
def compute_fields(self):
""" compute model fields """
for rec in self:
rec.area_id = rec.member_id.area_id
rec.gender = rec.member_id.gender
if rec.member_id.category_id:
rec.category_id = self.get_root_category(rec.member_id.category_id)
rec._compute_age()
rec._compute_age_range()
@api.multi
def _compute_age(self):
""" Compute age """
for rec in self:
if(rec.member_id.birthdate_date and rec.season_id.date_start
and rec.season_id.date_end):
start_season = fields.Date.from_string(rec.season_id.date_start)
end_season = fields.Date.from_string(rec.season_id.date_end)
age_start_season = relativedelta(
start_season,
fields.Date.from_string(rec.member_id.birthdate_date)
)
age_end_season = relativedelta(
end_season,
fields.Date.from_string(rec.member_id.birthdate_date)
)
rec.write({'age_end_season': age_end_season.years,
'age_start_season': age_start_season.years,
'age_now': rec.member_id.age})
@api.multi
def _compute_age_range(self):
""" Compute age range """
for rec in self:
if rec.age_start_season:
age_range = self.env['golem.member.age.range'].search(
[('lower_bound', '<=', rec.age_start_season),
('higher_bound', '>=', rec.age_start_season)],
limit=1)
rec.age_range_start_season = age_range
if rec.age_end_season:
age_range = self.env['golem.member.age.range'].search(
[('lower_bound', '<=', rec.age_end_season),
('higher_bound', '>=', rec.age_end_season)],
limit=1)
rec.age_range_end_season = age_range
if rec.age_now:
age_range = self.env['golem.member.age.range'].search(
[('lower_bound', '<=', rec.age_now),
('higher_bound', '>=', rec.age_now)],
limit=1)
rec.age_range_now = age_range