Sélectionner une révision Git
Bifurcation depuis
Le Filament / Confédération Générale des SCOP / cgscop_partner
Le projet source a une visibilité limitée.
scop_cotisation_cg_wizard.py 4,33 Kio
# © 2021 Le Filament (<http://www.le-filament.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging
from odoo import api, fields, models, exceptions
_logger = logging.getLogger(__name__)
class ScopCotisationWizard(models.TransientModel):
_name = 'scop.cotisation.cg.wizard'
_description = "Wizard: Génération des cotisations nouveaux adhérents"
year = fields.Char(
string='Année',
required=True)
cotisation_cg_id = fields.Many2one(
comodel_name='scop.cotisation.cg',
string='Base de cotisation')
nb_quarter = fields.Selection(
string='Nombre de trimestres de cotisation',
selection=[(1, '1'),
(2, '2'),
(3, '3'),
(4, '4')],
default='4',
required=True)
type = fields.Selection([
('all', 'Toutes les coop nouvelles adhérentes'),
('selected', 'Sélectionner des coop')],
string='Type de création',
default='all'
)
partner_ids = fields.Many2many(
comodel_name='res.partner',
string='Coopératives'
)
date = fields.Date('Date de cotisation', default=fields.Date.today())
# ------------------------------------------------------
# Button function
# ------------------------------------------------------
@api.onchange('type')
def onchange_domain_partner_ids(self):
if self.type == 'selected':
id_cotisation_cg = self.env.context['id_cotisation_cg']
invoiced_members = self.cotisation_cg_id.browse(
id_cotisation_cg).invoice_ids.mapped('partner_id')
res = {'domain': {
'partner_ids': [
('is_company', '=', True),
('id', 'not in', invoiced_members.mapped('id'))]
}}
return res
# ------------------------------------------------------
# Button function
# ------------------------------------------------------
@api.multi
def cotiz_generate_new_adherents(self):
if not self.env.user.company_id.is_contribution_cg:
raise exceptions.UserError(
"La gestion des cotisations CGScop n'est pas configurée.")
if self.type == 'selected':
members = self.partner_ids
else:
members = self.cotisation_cg_id.get_new_members(self.date)
invoiced_members = self.cotisation_cg_id. \
invoice_ids.mapped('partner_id')
members_to_invoice = members - invoiced_members
if len(members_to_invoice) > 0:
message = (
"<h3>Bordereaux " + str(self.year) +
"</h3> <hr/>" +
"<br/>Bordereaux nouveaux adhérents à générer : " +
str(len(members_to_invoice)) +
"<p>Les appels de cotisation sont en cours de "
"création...</p> "
)
message_id = self.env['message.wizard'].create(
{'message': message})
# Job queue
batch_name = (fields.Datetime.to_string(fields.Datetime.now()) +
" Génération des bordereaux " +
str(self.cotisation_cg_id.year))
batch = self.env['queue.job.batch'].get_new_batch(batch_name)
for member in members_to_invoice:
liasse_id = self.cotisation_cg_id.get_liasse(member)
self.cotisation_cg_id.with_context(
job_batch=batch
).with_delay().create_bordereau(
member=member, liasse=liasse_id,
nb_quarter=self.nb_quarter, date=self.date)
batch.enqueue()
else:
message = ("<p class='text-center'>Tous les bordereaux pour les "
"coops qui ont adhéré cette année avant le %s ont "
"déjà été créés !</p>" % self.date.strftime("%d/%m"))
message_id = self.env['message.wizard'].create(
{'message': message})
return {
'name': 'Génération des appels de cotisation nouveaux adhérents',
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'message.wizard',
'res_id': message_id.id,
'target': 'new'
}