GOLEM/golem_member/wizard/golem_member_analysis_wizard.py

129 lines
5.5 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright 2019-2020 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 Management """
import logging
from odoo import models, fields, api, _
_LOGGER = logging.getLogger(__name__)
class GolemMemberAnalysis(models.TransientModel):
""" GOLEM Members Analysis Management """
_name = 'golem.member.analysis.wizard'
_description = 'GOLEM Members Analysis Wizard Management'
def default_season(self):
""" Get default season """
domain = [('is_default', '=', True)]
return self.env['golem.season'].search(domain, limit=1)
name = fields.Char(default='/')
season_id = fields.Many2one('golem.season', 'Season', required=True,
default=default_season, ondelete='cascade')
season_start = fields.Date(related='season_id.date_start')
season_end = fields.Date(related='season_id.date_end')
analyse_type = fields.Selection([('gender', 'By gender'), ('area', 'By area'),
('tag', 'By tag and area')], required=True,
default='gender')
age_computing = fields.Selection([('season_start', 'At season start'),
('season_end', 'At season stop'),
('now', 'Now')], required=True,
default='season_start', string='Age')
category_id = fields.Many2one('res.partner.category', ondelete='cascade')
service_user = fields.Selection([('user', 'Service user'),
('member', 'Member')], default='user',
string='Service users or members')
@api.multi
def get_pivot_view(self):
""" Get pivot view """
self.ensure_one()
rec = self[0]
ref = u''
if rec.age_computing == 'season_start':
if rec.analyse_type == 'gender':
ref = u'golem_member.golem_member_analysis_pivot_age_start_gender'
elif rec.analyse_type == 'area':
ref = u'golem_member.golem_member_analysis_pivot_age_start_area'
elif rec.analyse_type == 'tag':
ref = u'golem_member.golem_member_analysis_pivot_age_start_tag'
elif rec.age_computing == 'season_end':
if rec.analyse_type == 'gender':
ref = u'golem_member.golem_member_analysis_pivot_age_end_gender'
elif rec.analyse_type == 'area':
ref = u'golem_member.golem_member_analysis_pivot_age_end_area'
elif rec.analyse_type == 'tag':
ref = u'golem_member.golem_member_analysis_pivot_age_end_tag'
else: # now
if rec.analyse_type == 'gender':
ref = u'golem_member.golem_member_analysis_pivot_age_now_gender'
elif rec.analyse_type == 'area':
ref = u'golem_member.golem_member_analysis_pivot_age_now_area'
elif rec.analyse_type == 'tag':
ref = u'golem_member.golem_member_analysis_pivot_age_now_tag'
return self.env.ref(ref, False)
@api.multi
def get_members(self):
""" Returns members """
self.ensure_one()
if self[0].service_user == 'user':
return self.env['golem.member'].search([]).filtered(
lambda m: self[0].season_id.id in m.season_ids.ids)
membership_ids = self[0].season_id.membership_ids.mapped('product_variant_id')
membership_set = set(membership_ids.ids)
return self.env['golem.member'].search([]).filtered(
lambda m: (self[0].season_id.id in m.season_ids.ids and
set(m.member_lines.mapped('membership_id.id')) & membership_set)
)
@api.multi
def members_analyse(self):
""" members analyse """
self.ensure_one()
analysis_rec = self[0]
# Remove existing analysis lines for current analysis
self.env['golem.member.analysis'].search(
[('member_analysis_id', '=', analysis_rec.id)]
).unlink()
members = analysis_rec.get_members()
if members:
for member in members:
self.env['golem.member.analysis'].create({
'member_id': member.id,
'season_id': analysis_rec.season_id.id,
'member_analysis_id': analysis_rec.id
})
analyse_pivot = analysis_rec.get_pivot_view()
return {
'name': _('Member Analysis'),
'type': 'ir.actions.act_window',
'res_model': 'golem.member.analysis',
'domain': [('member_analysis_id', '=', analysis_rec.id)],
'view_type': 'pivot',
'view_mode': 'pivot',
'view_id': analyse_pivot.id,
'target': 'current'
}