Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • a9791466dd2117319e3e5b0a23ea5c93e8792a04
  • 14.0 par défaut
  • 12.0 protégée
  • 13.0
  • 12.0-lm-00 protégée
5 résultats

scop_deces_wizard.py

Blame
  • scop_cotisation_cg_wizard.py 4,95 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, exceptions, fields, models
    
    _logger = logging.getLogger(__name__)
    
    
    class ScopCotisationWizard(models.TransientModel):
        _name = "scop.cotisation.cg.wizard"
        _description = "Wizard: Génération des cotisations nouveaux adhérents"
    
        @api.model
        def _default_cotisation_cg_id(self):
            return self.env.context.get("active_id")
    
        @api.model
        def _default_year(self):
            return self.env["scop.cotisation.cg"].browse(
                self.env.context.get("active_id")).year
    
        year = fields.Char(string="Année", required=True, default=_default_year)
        cotisation_cg_id = fields.Many2one(
            comodel_name="scop.cotisation.cg",
            string="Base de cotisation",
            default=_default_cotisation_cg_id,
        )
        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 bordereau", default=fields.Date.today())
        date_start = fields.Date("Adhésion après le")
        date_limit = fields.Date("Adhésion avant le")
    
        # ------------------------------------------------------
        # Button function
        # ------------------------------------------------------
        @api.onchange("type", "date_start", "date_limit")
        def onchange_domain_partner_ids(self):
            if self.type == "selected":
                invoiced_members = self.cotisation_cg_id.bordereau_ids.mapped('partner_id')
                domain = [
                    ("is_cooperative", "=", True),
                    ("type", "!=", "facility"),
                    ("id", "not in", invoiced_members.mapped("id")),
                ]
                if self.date_limit:
                    domain.append(("member_start", "<=", self.date_limit))
                if self.date_start:
                    domain.append(("member_start", ">=", self.date_start))
    
                res = {
                    "domain": {
                        "partner_ids": domain
                    }
                }
                return res
    
        # ------------------------------------------------------
        # Button function
        # ------------------------------------------------------
        def cotiz_generate_new_adherents(self):
            if not self.env.company.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> "
                )
    
                # 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")
                )
    
            return {
                "type": "ir.actions.act_window.message",
                "title": _("Génération des appels de cotisation nouveaux adhérents"),
                "is_html_message": True,
                "message": _(message),
                "close_button_title": False,
                "buttons": [
                    {
                        "type": "ir.actions.act_window_close",
                        "name": "Fermer",
                    },
                ],
            }