From 8e7af0b9117b6034a66dc830b2e8e7bd03a8af84 Mon Sep 17 00:00:00 2001
From: benjamin <benjamin@le-filament.com>
Date: Wed, 26 Jan 2022 11:49:19 +0100
Subject: [PATCH] [update] compute is_loss & review query

---
 report/scop_contribution_report.py | 45 ++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/report/scop_contribution_report.py b/report/scop_contribution_report.py
index 23dedb4..544325a 100644
--- a/report/scop_contribution_report.py
+++ b/report/scop_contribution_report.py
@@ -21,7 +21,7 @@ class ScopContributionReport(models.Model):
     amount_called = fields.Float('Montant Appelé')
     amount_paid = fields.Float('Montant Payé')
     amount_due = fields.Float('Montant Restant')
-    is_loss = fields.Boolean('Exonération/Perte')
+    is_loss = fields.Boolean('Exonération/Perte', compute="_compute_is_loss")
     payments = fields.Html('Paiements', compute="_compute_payments")
 
     _depends = {
@@ -43,8 +43,7 @@ class ScopContributionReport(models.Model):
                 i.partner_id,
                 SUM(i.amount_total_signed) AS amount_called,
                 SUM(i.amount_total_signed - i.residual_company_signed) AS amount_paid,
-                SUM(i.residual_company_signed) AS amount_due,
-                (CASE WHEN max(refund.id) IS NOT NULL THEN true ELSE FALSE END) AS is_loss
+                SUM(i.residual_company_signed) AS amount_due
         """
         return select_str
 
@@ -52,16 +51,13 @@ class ScopContributionReport(models.Model):
         from_str = """
                     FROM
                         account_invoice i
-                    LEFT JOIN
-                        account_invoice refund ON refund.refund_invoice_id = i.id
                 """
         return from_str
 
     def _where_invoice(self):
         where_str = """
             WHERE
-                i.type = 'out_invoice' AND
-                i.state in ('open', 'paid') AND
+                i.state in ('open', 'paid', 'in_payment') AND
                 i.is_contribution = true
         """
         return where_str
@@ -96,8 +92,7 @@ class ScopContributionReport(models.Model):
                 c.partner_id,
                 SUM(c.amount_called) AS amount_called,
                 SUM(c.amount_paid) AS amount_paid,
-                SUM(c.amount_due) AS amount_due,
-                BOOL_OR(c.is_loss) AS is_loss
+                SUM(c.amount_due) AS amount_due
             FROM (
         """
         return select_str
@@ -131,6 +126,11 @@ class ScopContributionReport(models.Model):
     # ------------------------------------------------------
     # Computed fields
     # ------------------------------------------------------
+    @api.multi
+    def _compute_is_loss(self):
+        for contribution in self:
+            contribution.is_loss = contribution._get_is_loss()
+
     @api.multi
     def _compute_name(self):
         for contribution in self:
@@ -146,13 +146,26 @@ class ScopContributionReport(models.Model):
     # ------------------------------------------------------
     # Business functions
     # ------------------------------------------------------
+    def _get_is_loss(self):
+        invoice_ids = self.env['account.invoice'].search([
+            ('year', '=', int(self.year)),
+            ('partner_id', '=', self.partner_id.id),
+            ('type_contribution_id', '=', self.type_contribution_id.id),
+        ])
+        refund_ids = invoice_ids.filtered(lambda i: i.type == 'out_refund')
+        if refund_ids:
+            return True
+        else:
+            return False
+
     def _get_payment(self):
         self.ensure_one()
         invoice_ids = self.env['account.invoice'].search([
             ('year', '=', int(self.year)),
             ('partner_id', '=', self.partner_id.id),
             ('type_contribution_id', '=', self.type_contribution_id.id),
-            ('type', '=', 'out_invoice')
+            ('type', '=', 'out_invoice'),
+            ('bordereau_id.state', 'not in', ('cancel',))
         ])
         payment_ids = invoice_ids.mapped('payment_move_line_ids')
         if payment_ids:
@@ -161,6 +174,13 @@ class ScopContributionReport(models.Model):
                 'name': p.name,
                 'ref': p.ref,
                 'credit': p.credit,
+                'class': ''
+            } if not p.invoice_id else {
+                'date': p.date,
+                'name': p.name,
+                'ref': 'Avoir',
+                'credit': p.credit,
+                'class': 'text-danger'
             })
             payments_html = self._get_html_table(payments)
         else:
@@ -185,13 +205,14 @@ class ScopContributionReport(models.Model):
         content_html = ""
         for payment in payments:
             content_html += """
-                <tr>
+                <tr class='%s'>
                     <td>%s</td>
                     <td>%s</td>
                     <td>%s</td>
                     <td class='text-right'>%.2f €</td>
                 </tr>
-            """ % (payment.get('date', '').strftime('%d/%m/%Y'),
+            """ % (payment.get('class', ''),
+                   payment.get('date', '').strftime('%d/%m/%Y'),
                    payment.get('name', ''), payment.get('ref', ''),
                    payment.get('credit', 0.0),)
 
-- 
GitLab