golem/golem_member/wizard/golem_member_analysis.py

106 lines
5.0 KiB
Python
Raw Normal View History

# -*- 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__)
def get_root_category(category_id):
""" Get Root Category """
if not category_id.parent_id:
return category_id
return get_root_category(category_id.parent_id)
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', required=True)
analyse_type = fields.Selection([('age_start_gender', 'By age at season start and gender'),
('age_end_gender', 'By age at season end and gender'),
('age_start_area', 'By age at season start and area'),
('age_end_area', 'By age at season end and area'),
('age_start_tag', 'By age at season start and tag'),
('age_end_tag', 'By age at season end and tag')],
required=True)
area_id = fields.Many2one(related='member_id.area_id', store=True)
gender = fields.Selection(related='member_id.gender', store=True)
category_id = fields.Many2many('res.partner.category')
age_start_season = fields.Integer(compute='_compute_age', string='Age at season start')
age_end_season = fields.Integer(compute='_compute_age', string='Age at season end')
age_range_start_season = fields.Many2one('golem.member.age.range', 'Age range at season start',
compute='_compute_age_range', store=True)
age_range_end_season = fields.Many2one('golem.member.age.range', 'Age range at season end',
compute='_compute_age_range', store=True)
@api.constrains('member_id')
def compute_fields(self):
for rec in self:
print '_-_-_-_____________-______________-_______________-___________-'
rec.category_id = get_root_category(rec.member_id.category_id)
print rec.category_id
@api.depends('member_id')
def _compute_category_id(self):
""" Compute category_id """
for rec in self:
rec.category_id = get_root_category(rec.member_id.category_id)
@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