golem/golem_member/wizard/golem_member_analysis.py

100 lines
4.4 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 odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError
from dateutil.relativedelta import relativedelta
_LOGGER = logging.getLogger(__name__)
class GolemMemberAnalysisPivot(models.TransientModel):
""" GOLEM Members Analysis Pivot Management """
_name = 'golem.member.analysis'
_description = 'GOLEM Members Analysis Management'
season_id = fields.Many2one('golem.season', required=True)
member_id = fields.Many2one('golem.member', 'Season')
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 end')
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')
@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.depends('member_id')
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.age_end_season = age_end_season.years
rec.age_start_season = age_start_season.years
@api.multi
@api.depends('age_start_season', 'age_end_season')
def _compute_age_range(self):
""" Compute age range """
for rec in self:
if rec.age_start_season is not None:
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 is not None:
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