Skip to content
Extraits de code Groupes Projets
account_payment_order.py 5,04 ko
Newer Older
  • Learn to ignore specific revisions
  • # Copyright 2020 Le Filament
    # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    
    
    from odoo import fields, models
    
    from odoo.exceptions import ValidationError
    
    class AccountPaymentOrder(models.Model):
    
        _inherit = "account.payment.order"
    
            string="Total Opérations", compute="_compute_payment_line_amount"
    
            string="Total Lignes de paiement", compute="_compute_bank_line_amount"
    
        attachment_ids = fields.One2many(
    
            comodel_name="ir.attachment", compute="_compute_attachment_ids"
    
        mandate_validity = fields.Boolean("Mandats valides", compute="_compute_mandate_validity")
    
        # ------------------------------------------------------
        # Compute fields
        # ------------------------------------------------------
        def _compute_payment_line_amount(self):
            for po in self:
    
                po.payment_line_amount = sum(po.payment_line_ids.mapped("amount_currency"))
    
                po.bank_line_amount = sum(po.bank_line_ids.mapped("amount_currency"))
    
        def _compute_attachment_ids(self):
    
            Attachment = self.env["ir.attachment"]
    
                po.attachment_ids = Attachment.search(
                    [
                        ("res_model", "=", "account.payment.order"),
                        ("res_id", "=", po.id),
                    ]
                )
    
        def _compute_mandate_validity(self):
            for o in self:
                validity = o.mapped("payment_line_ids.mandate_id").filtered(
                    lambda m: m.state != "valid")
                if validity:
                    o.mandate_validity = False
                else:
                    o.mandate_validity = True
    
    
        # ------------------------------------------------------
        # Button function
        # ------------------------------------------------------
        def view_payment_line(self):
    
            tree_id = self.env.ref("cgscop_cotisation.scop_account_payment_line_tree").id
    
                "cgscop_cotisation.scop_account_payment_line_search"
            ).id
    
                "type": "ir.actions.act_window",
                "name": "Lignes d'opérations",
                "res_model": "account.payment.line",
                "views": [[tree_id, "tree"]],
                "search_view_id": [search_id, "search"],
                "domain": [["order_id", "=", self.id]],
    
            }
    
        def view_account_move(self):
    
            tree_id = self.env.ref("cgscop_cotisation.scop_account_move_tree").id
            search_id = self.env.ref("cgscop_cotisation.scop_account_move_search").id
    
                "type": "ir.actions.act_window",
                "name": "Pièces comptables de l'ordre de prélèvement",
                "res_model": "account.move",
                "views": [[tree_id, "tree"], [False, "form"]],
                "search_view_id": [search_id, "search"],
                "domain": [["payment_order_id", "=", self.id]],
    
        def view_wrong_iban(self):
            self.ensure_one()
            bank_ids = self.mapped("payment_line_ids.partner_bank_id").filtered(
                lambda b: b.acc_type != 'iban')
            return {
                'type': 'ir.actions.act_window',
                'name': "Comptes bancaires",
                'res_model': 'res.partner.bank',
                'views': [[False, 'tree'], [False, 'form']],
                'domain': [['id', 'in', bank_ids.ids]],
            }
    
        def view_wrong_mandate(self):
            self.ensure_one()
            mandate_ids = self.mapped("payment_line_ids.mandate_id").filtered(
                lambda m: m.state != "valid")
            return {
                'type': 'ir.actions.act_window',
                'name': "Mandats non valides",
                'res_model': 'account.banking.mandate',
                'views': [[False, 'tree'], [False, 'form']],
                'domain': [['id', 'in', mandate_ids.ids]],
            }
    
    
        # ------------------------------------------------------
        # Common function
        # ------------------------------------------------------
        def check_sepa_order(self):
            for order in self:
                if not order.sepa:
    
                    msg = (
                        "Les comptes bancaires des coopératives "
                        "suivantes ne sont pas corrects : \n"
                    )
                    payment_line_ids = order.payment_line_ids.mapped("partner_bank_id")
                    account_ids = payment_line_ids.filtered(lambda a: a.acc_type != "iban")
    
                    for acc in account_ids:
    
                        msg += " - " + acc.partner_id.name + " - " + acc.acc_number + "\n"
    
                    msg += (
                        "\nVeuillez corriger ces comptes bancaires pour "
                        "pouvoir valider l'ordre de prélèvement."
                    )
    
                    raise ValidationError(msg)
                else:
                    return True
    
        # ------------------------------------------------------
        # Override Parent
        # ------------------------------------------------------
        def open2generated(self):
            self.check_sepa_order()
    
            return super(AccountPaymentOrder, self).open2generated()