From 8b2adbb5272de2f3091d6e04f8e358fb4dd3c6c4 Mon Sep 17 00:00:00 2001
From: Benjamin <benjamin@le-filament.com>
Date: Tue, 20 Apr 2021 15:11:08 +0200
Subject: [PATCH] [add] report with payment

---
 models/scop_bordereau_cg.py      | 44 +++++++++++++++++++++++-
 report/report_scop_bordereau.xml | 57 +++++++++++++++++++++++++++++++-
 2 files changed, 99 insertions(+), 2 deletions(-)

diff --git a/models/scop_bordereau_cg.py b/models/scop_bordereau_cg.py
index 58d1e4e..870846e 100644
--- a/models/scop_bordereau_cg.py
+++ b/models/scop_bordereau_cg.py
@@ -423,10 +423,52 @@ class Bordereau(models.Model):
                         amount += item.debit - item.credit
                 schedule_plan.append({
                     'date': date.strftime('%d/%m/%Y'),
-                    'amount': amount
+                    'amount': amount,
                 })
             return schedule_plan
 
+    @api.multi
+    def get_bordereau_move_line_with_payments(self):
+        """
+        Get payment dates for bordereau according to payment_term in bordereau
+        Use "compute" function from account.move
+        :return: dict with date as key and amount (float) as value
+        """
+        MoveLine = self.env['account.move.line']
+        for bordereau in self:
+            inv_ids = bordereau.invoice_ids.ids
+            move_lines = MoveLine.search([
+                ('invoice_id', 'in', inv_ids),
+                ('account_id.internal_type', '=', 'receivable')],
+                order='date_maturity',
+            )
+            schedule = list(dict.fromkeys(move_lines.mapped('date_maturity')))
+            schedule.sort()
+            schedule_plan = []
+            total_amount = 0
+            total_paid = 0
+            for date in schedule:
+                amount = 0
+                amount_paid = 0
+                for item in move_lines:
+                    if item.date_maturity == date:
+                        amount += item.debit - item.credit
+                        if item.full_reconcile_id.reconciled_line_ids:
+                            aml_payment = item.full_reconcile_id.reconciled_line_ids - move_lines
+                            amount_paid += sum(aml_payment.mapped('credit')) - sum(aml_payment.mapped('debit'))
+                total_amount += amount
+                total_paid += amount_paid
+                schedule_plan.append({
+                    'date': date.strftime('%d/%m/%Y'),
+                    'amount': amount,
+                    'amount_paid': amount_paid,
+                })
+            return {
+                'plan': schedule_plan,
+                'total_amount': total_amount,
+                'total_paid': total_paid
+            }
+
     def get_contribution_type(self):
         """
         Get contribution by type
diff --git a/report/report_scop_bordereau.xml b/report/report_scop_bordereau.xml
index 92d87b6..9dab839 100644
--- a/report/report_scop_bordereau.xml
+++ b/report/report_scop_bordereau.xml
@@ -74,7 +74,7 @@
                             <t t-else="">
                                 <h5 style="font-weight: 600;">Échéancier de paiement</h5>
                             </t>
-                            <p>
+                            <p id="schedule_table">
                                 <t t-set="schedule_line" t-value="o.get_bordereau_move_line()" />
                                 <table class="table table-sm table-striped" style="border: none;">
                                     <tr t-foreach="schedule_line" t-as="line" style="border-bottom: 1px solid #ccc;">
@@ -151,6 +151,44 @@
             </t>
         </template>
 
+        <template id="report_bordereau_document_with_payments" inherit_id="cgscop_cotisation_cg.report_bordereau_document" primary="True">
+            <xpath expr="//p[@id='schedule_table']" position="replace">
+               <p id="schedule_table_with_payments">
+                    <t t-set="schedule_line" t-value="o.get_bordereau_move_line_with_payments()" />
+                    <table class="table table-sm table-striped" style="border: none;">
+                        <tr>
+                            <th>Date d'échéance</th>
+                            <th class="text-right">Montant appelé</th>
+                            <th class="text-right">Montant réglé</th>
+                        </tr>
+                        <tr t-foreach="schedule_line.get('plan')" t-as="line" style="border-bottom: 1px solid #ccc;">
+                            <td style="border: none; background: inherit; color: inherit;"><t t-esc="line.get('date')"/></td>
+                            <td class="text-right" style="border: none; background: inherit; color: inherit;"><t t-esc="line.get('amount')" t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/></td>
+                            <td class="text-right" style="border: none; background: inherit; color: inherit;"><t t-esc="line.get('amount_paid')" t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/></td>
+                        </tr>
+                    </table>
+                   <table class="table table-sm table-striped" style="border: none;">
+                        <tr>
+                            <th>Total appelé</th>
+                            <th class="text-right">Total réglé</th>
+                            <th class="text-right">Montant dû</th>
+                        </tr>
+                        <tr style="border-bottom: 1px solid #ccc;">
+                            <td style="border: none; background: inherit; color: inherit;">
+                                <t t-esc="schedule_line.get('total_amount')" t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/>
+                            </td>
+                            <td class="text-right" style="border: none; background: inherit; color: inherit;">
+                                <t t-esc="schedule_line.get('total_paid')" t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/>
+                            </td>
+                            <td class="text-right" style="border: none; background: inherit; color: inherit;">
+                                <t t-esc="schedule_line.get('total_amount') - schedule_line.get('total_paid')" t-options="{'widget': 'monetary', 'display_currency': o.company_id.currency_id}"/>
+                            </td>
+                        </tr>
+                    </table>
+                </p>
+            </xpath>
+        </template>
+
         <template id="report_bordereau">
             <t t-call="web.html_container">
                 <t t-set="docs" t-value="docs.with_context(lang='fr')"/>
@@ -160,6 +198,15 @@
             </t>
         </template>
 
+        <template id="report_bordereau_with_payments">
+            <t t-call="web.html_container">
+                <t t-set="docs" t-value="docs.with_context(lang='fr')"/>
+                <t t-foreach="docs" t-as="o">
+                    <t t-call="cgscop_cotisation_cg.report_bordereau_document_with_payments" t-lang="fr"/>
+                </t>
+            </t>
+        </template>
+
         <!-- QWeb Reports -->
         <report
                 id="cgscop_bordereau_report"
@@ -169,6 +216,14 @@
                 name="cgscop_cotisation_cg.report_bordereau"
                 file="cgscop_cotisation_cg.report_bordereau"
         />
+        <report
+                id="cgscop_bordereau_report_with_payments"
+                model="scop.bordereau"
+                string="Bordereau de Cotisation CG avec paiement"
+                report_type="qweb-pdf"
+                name="cgscop_cotisation_cg.report_bordereau_with_payments"
+                file="cgscop_cotisation_cg.report_bordereau_with_payments"
+        />
 
     </data>
 </odoo>
-- 
GitLab