Simulating statement of account in custom print format

Hi all,

I am simulating the Statement of Account in custom print format.
I am aware that we can refer to General Ledger.
But we are trying our best to do it from the custom print.

as you can see from the above, I am halfway there. But I am facing few issues.

1st : counting row for total credit and total debit.

               {% for row in doc.customers %}
                     
                     {% set details = frappe.get_all('GL Entry',
                     filters={
                     'posting_date':['between',(doc.from_date,doc.to_date)],
                     'party':['=',row.get_formatted('customer', doc)],
                     'docstatus':['=', 1],
                     'is_cancelled':['=', 0],
                     'cost_center':['not like', '%Main - HQ%']},
                     fields=['*'], order_by='posting_date, voucher_no asc') %}
                      
                      {% set total_debit = details|sum(attribute='debit') %}
                      {% set total_credit = details|sum(attribute='credit') %}
                      
                      {% set credit = details|groupby('credit') %}
                      {% set noofcredit = credit|length %}
                      
                      {% set debit = details|groupby('debit') %}
                      {% set noofdebit = debit|length %} 
                     
                      
                      <tr>
                        <td>Total Debit ({{ noofdebit }})</td>
                        <td>{{ frappe.utils.fmt_money(total_debit,currency="")  }}</td>
                      </tr>
                      <tr>
                        <td>Total Credit ({{ noofcredit }}) </td>
                        <td>{{ frappe.utils.fmt_money(total_credit,currency="")  }}</td>
                      </tr>
                       <tr style="border-top: 1px solid black;">
                        <td >Closing Balance</td>
                        <td>{{ frappe.utils.fmt_money(total_debit-total_credit,currency="")  }}</td>
                      </tr>
                {% endfor %}
            </table>
            </div>
        </div>

For example, it should be 10 rows only for total debit but it is displaying 11 instead of 10. Same goes to total credit. Can you guys point me where should I do the improvement/amendment ?

2nd problem is I am able to sum up the credit for the same transaction id but unable to group and make it as 1 row.

Statement of Account
                <tr>
                    <td><small>Date</small></td>
                    <td><small>Reference</small></td>
                    <td><small>Transaction Description</small></td>
                    <td></td>
                    <td><small>Debit</small></td>
                    <td><small>Credit</small></td>
                    <td><small>Balance</small></td>
                </tr>
                
                {% for row in doc.customers %}
                     
                     {% set details = frappe.get_all('GL Entry',
                     filters={
                     'posting_date':['between',(doc.from_date,doc.to_date)],
                     'party':['=',row.get_formatted('customer', doc)],
                     'docstatus':['=', 1],
                     'is_cancelled':['=', 0]},
                     fields=['*'], order_by='posting_date, voucher_no asc') %}
                     
                      
                      {% set total_credit = details|sum(attribute='credit') %}
                      {% set pedetail = details|groupby('voucher_type','voucher_no') %}
                   
                      {% for detail in details %}
                
                   
                 <tr>
                    <td><b> {{ frappe.format_date((detail.posting_date), "dd-MMM-yyyy") }}</b></td>
                    <td><b>{{ detail.voucher_no | truncate(7, True, '', 0) }} <p></p> </b></td> 
                    <td><b>{% if detail.voucher_type == 'Sales Invoice' %}  {{ 'Sales' }}
                    {% elif detail.voucher_type == 'Payment Voucher' %} {{ pedetail }} 
                    {% else %} {{ detail.voucher_type }} {% endif %}</b></td>
                    <td></td>
                    <td><b>{% if detail.debit <= 0.00 %}  {{ '' }}
                    {% else %} {{ frappe.utils.fmt_money(detail.debit,currency="")  }} {% endif %}</b></td>
                    <td><b>{% if detail.voucher_type == 'Payment Entry' %} {{ frappe.utils.fmt_money(total_credit,currency="") }} 
                  {% else %} {{ '' }} {% endif %}
                    
                    </b></td>
                    <td><b> {{ frappe.utils.fmt_money(detail.debit-detail.credit,currency="") }} </b></td>
                </tr>
                
                          <!--<p>{{ loop.length }} <p/>-->
                        {% endfor %}
                    {% endfor %}
                </table>
        </div>

3rd, how am I able to get the opening balance and start the calculation for balance for each row ? for this one I am 100% have no clue.

Any advises/guides is highly appreciated. Thankyou very much.