<?php include("include/header.php"); ?>
<?php 
$provincia = $_GET['provincia'];
$data_inicia = $_GET['data_inicial'];
$data_final = $_GET['data_final'];
?>
<style>
    .cabecalho{
        text-align: center;
    }
    .cabecalho img{
        width:54px;
        height: 54px;
    }
    .info{
        text-align: center;
    }
    .head .form-group{
        display: flex;
        flex-direction: row;
        justify-content: center;
    }
    .head .form-group .p{
        margin-right: 30px;
    }
    .rel .tabela{
        width:90%;
        margin: 10px auto;
        text-align: center;
        font-size: 10pt;
    }
    .rel table tbody tr td{
        padding: 7px 0;
    }
    .rodape{
    
        text-align: center;
        width: 90%;
        margin: 70px auto;
    }
    .rodape table{
        width: 100%;
    }
    .rodape table .no-print{
        padding:5px 15px;
        background: #000;
        color: #fff;
    }
    hr{
        width: 100%;
        text-align: center;
        border:2px solid #666;
    }
    @media print
    {    
        .no-print, .no-print *
        {
            display: none !important;
        }
    }
</style>
<div class="cabecalho">
    <img src="emblema.jpg">
    <p>REPÚBLICA DE MOÇAMBIQUE</p>
    <p><b>Portugal Junior Microcredito E.I</b></p>
</div>
<div class="info">
    <h4>BALANCETE POR PROVINCIA DE <?php echo $data_inicia ?> A <?php echo $data_final ?></h4>
</div>
<div class="head">

                <div class="form-group">
                    <p class="p"><span>Provincia:</span><span style="margin-left: 20px;"><?php echo $provincia ?></span></p>
                </div>
                <br>

</div>
<div class="rel">
    <table class="tabela">
        <thead>
                <tr>
                  <th>Mutuario</th>
                  <th>Valor de Investimento</th>
                  <th>Valor a Reembolsar</th>
                  <th>Pagamentos Efetuados</th>
                  <th>Saldo</th>
                  <th>Taxa de Reembolso</th>
                 </tr>
                </thead>
                <tbody>
<?php
$select2 = mysqli_query($link, "SELECT (select concat(fname, ' ',lname) as mt FROM borrowers as b WHERE b.id = l.`borrower`) as Mutuario, sum(`amount`) as debito,sum(`amount`+`tot_juros`) as apagar, sum((`amount`+`tot_juros`) - `amount_topay`) as valorpago, sum(`amount_topay`) as saldo FROM `loan_info` as l WHERE borrower IN (SELECT id FROM borrowers WHERE state = '$provincia') AND `pay_date` BETWEEN '$data_inicia' AND '$data_final' GROUP BY Mutuario") or die (mysqli_error($link));
while($dados2 = mysqli_fetch_array($select2)){
    $mutuario = $dados2['Mutuario'];
    $saldo = $dados2['saldo'];
    $debito = $dados2['debito'];
    $valor_pagar = $dados2['apagar'];
    $pago = $dados2['valorpago'];
    $taxa = $pago/$valor_pagar;
    $taxa1 = $taxa * 100;
    $select1 = mysqli_query($link, "SELECT * FROM systemset") or die (mysqli_error($link));
    while($row1 = mysqli_fetch_array($select1))
    {
    $currency = $row1['currency']; 
    ?>
        <tr>
        <td><?php echo $mutuario ?></td>
        <td><?php echo $currency.number_format($debito,2,".",","); ?></td>
        <td><?php echo $currency.number_format($valor_pagar,2,".",","); ?></td>
        <td><?php echo $currency.number_format($pago,2,".",","); ?></td>
        <td><?php echo $currency.number_format($saldo,2,".",","); ?></td>
        <td><?php echo round($taxa1,2); ?> %</td>
        </tr>
    <?php
    }
}
?>
<?php
        $select01 = mysqli_query($link, "SELECT sum(`amount`) as debito,sum(`amount`+`tot_juros`) as apagar, sum((`amount`+`tot_juros`) - `amount_topay`) as valorpago, sum(`amount_topay`) as saldo FROM `loan_info` as l WHERE `pay_date` BETWEEN '$data_inicial' AND '$data_final'") or die (mysqli_error($link));
        while($dados01 = mysqli_fetch_array($select01)){
            $saldos = $dados01['saldo'];
            $debitos = $dados01['debito'];
            $valor_pagars = $dados01['apagar'];
            $pagos = $dados01['valorpago'];
            
            $select1 = mysqli_query($link, "SELECT * FROM systemset") or die (mysqli_error($link));
            while($row1 = mysqli_fetch_array($select1))
            {
            $currency = $row1['currency']; 
            ?>
                <tr>
                <td><b><?php echo "Totais" ?></b></td>
                <td><b><?php echo $currency.number_format($debito,2,".",","); ?></b></td>
                <td><b><?php echo $currency.number_format($valor_pagar,2,".",","); ?></b></td>
                <td><b><?php echo $currency.number_format($pago,2,".",","); ?></b></td>
                <td><b><?php echo $currency.number_format($saldo,2,".",","); ?></b></td>
                
                </tr>
            <?php
            }
        }
?>
    </tbody>
    </table>
                
</div>

<div class="rodape">
    <hr>
    <table>
        <tr>
            <td><img src="1622834_cards_credit_method_pay_payment_icon.png" width="80px" height="80px"></td>
            <td>Sede: Tete, Av. da Liberdade n<sup>o</sup> 067, Telef. 252-23328, Fax 25223330</td>
            <td><button class="no-print" onclick = "window.print();return false">Imprimir</button></td>
        </tr>
    </table>
</div>