<div class="box box-primary">
<div class="box-header with-border">
    <h3 class="box-title"><i class="fa fa-th-large"></i> <?php echo $_translator->_getTranslation(ucfirst($_reporte->getTituloReporte()));?></h3>
    <div class="box-tools pull-right"></div><!-- /.box-tools -->
</div><!-- /.box-header -->
<div class="box-body">
<?php
$reportResult = $_reporte->getReporte();
if($reportResult == null){
    echo "There is no information";
    exit;
}

$data = $reportResult['sales_transactions'];
$summary = $reportResult['summary']['data'];
$formasPago = $reportResult['summary']['formas_pago'];
$translationPaymentMethod = array("Efectivo"=>'Cash','Tarjeta de credito'=>'Credit card','Tarjeta de debito'=>'Debit card','Cheque'=>'Check','EBT'=>'EBT');                        

$total_sales = 0;
$count_sales = 0;
$average_sales = 0;

foreach($data as $sale){
    $total_sales += round($sale['total_sales'],2);
    $count_sales++;
}
?>
    <div class='col-md-4'>
    <table class="table table-bordered">
        <tbody>
        <caption><?php echo $_translator->_getTranslation('Summary sales') ?></caption>
        <tr>
            <td class="text-center"><h3>$ <?php echo number_format($total_sales,2); ?></h3></td>
            <td class="text-center"><h3>$ <?php echo number_format($total_sales/$count_sales,2); ?></h3></td>
            <td class="text-center"><h3><?php echo number_format($count_sales,0); ?></h3></td>
        </tr>
        <tr>
            <th class="text-center label-success"><?php echo $_translator->_getTranslation('Gross sales') ?></th>
            <th class="text-center label-primary"><?php echo $_translator->_getTranslation('Average sales'); ?></th>
            <th class="text-center label-info"><?php echo $_translator->_getTranslation('Transactions'); ?></th>
        </tr>
        </tbody>
    </table>    
    </div>
    <div class='col-md-4'>
        <table class='table'>
            <caption><?php echo $_translator->_getTranslation('Tax sales') ?></caption>
            <tbody>               
                <tr>
                    <td><?php echo 'Net sales' ?></td>
                    <td class="text-right"><?php echo '$'.number_format($summary['importe'],2) ?></td>
                </tr>
                <tr>
                    <td><?php echo '- Discounts' ?></td>
                    <td class="text-right"><?php echo '$'.number_format($summary['descuentos'],2) ?></td>
                </tr>
                <tr>
                    <td><?php echo '= Subtotal' ?></td>
                    <td class="text-right"><?php echo '$'.number_format($summary['subtotal'],2) ?></td>
                </tr>
                <tr>
                    <td><?php echo '+ Taxes' ?></td>
                    <td class="text-right"><?php echo '$'.number_format($summary['impuestos'],2) ?></td>
                </tr>
                 <tr>
                    <th><?php echo '= Gross sales' ?></th>
                    <th class="text-right"><?php echo '$'.number_format($summary['total_venta'],2) ?></th>
                </tr>
            </tbody>
        </table>
    </div>
    <div class='col-md-4'>
        <table class='table'>
            <caption><?php echo $_translator->_getTranslation('Sales by payment method') ?></caption>
            <tbody>
                <?php 
                $totalPayments = 0;                
                foreach($formasPago as $payment){
                    $totalPayments += (float)$payment['monto'];
                    $paymentMethodName = $payment['metodo_pago_'];
                    if(key_exists($payment['metodo_pago_'], $translationPaymentMethod)){$paymentMethodName = $translationPaymentMethod[$payment['metodo_pago_']];}?>
                    
                    <tr>
                        <td><?php echo $paymentMethodName ?></td>
                        <td class="text-right"><?php echo '$'.number_format($payment['monto'],2) ?></td>
                    </tr><?php               
                }
                ?>
                <tr>
                    <th><?php echo 'Total' ?></th>
                    <th class="text-right"><?php echo '$'.number_format($totalPayments,2) ?></th>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="clear"></div>
    <form><?php /* Lo puse para que no se autollenen campos con usuario*/?>
    <div class="card">
        <ul class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active"><a href="#sales_list" aria-controls="home" role="tab" data-toggle="tab"><?php echo $_translator->_getTranslation('Transactions') ?></a></li>            
        </ul>
        <!-- Tab panes -->
        <div class="tab-content" style="margin-top: 20px">
            <div role="tabpanel" class="tab-pane active" id="sales_list">
                <table id="tblSales" class="table table-striped table-bordered table-hover table-condensed datatable_whit_filter_column _hideSearch">
                    <thead class="table-header">
                    <tr>
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Store');?></th>
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Sale #');?></th>
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Date');?></th>                                   
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Amount');?></th>
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Discount');?></th>
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Subtotal');?></th>
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Taxes');?></th>
                        <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Total');?></th>
                        <th class="col-md-2 text-center"><?php echo $_translator->_getTranslation('Payment method');?></th>
                        <th class="col-md-2 text-center"><?php echo $_translator->_getTranslation('Sale time');?></th>
                    </tr>
                    </thead>
                    <tfoot>                        
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Sucursal');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Venta');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Fecha');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Monto');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Descuento');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Subtotal');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Imppuestos');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Total');?></th>
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Metodo de Pago');?></th>                        
                        <th class="filter text-center"><?php echo $_translator->_getTranslation('Sale time');?></th>
                   </tfoot>
                    <tbody>
                    <?php 
                    foreach($data as $row){
                        $paymentMethodName = $row['payment_method'];
                        if(key_exists($row['payment_method'], $translationPaymentMethod)){$paymentMethodName = $translationPaymentMethod[$row['payment_method']];}?>
                        <tr>
                        <td class="text-center"><?php echo $row['store_name'];?></td>
                        <td class="text-center"><?php echo $row['sale_number'];?></td>
                        <td class="text-center"><?php echo $row['date'];?></td>      
                        <td class="text-center">$<?php echo number_format($row['amount'],2);?></td> 
                        <td class="text-center">$<?php echo number_format($row['discounts'],2);?></td>
                        <td class="text-center">$<?php echo number_format($row['subtotal'],2);?></td>
                        <td class="text-center">$<?php echo number_format($row['taxes'],2);?></td>
                        <td class="text-center">$<?php echo number_format($row['total_sales'],2);?></td>
                        <td class="text-center"><?php echo $paymentMethodName;?></td>      
                        <td class="text-center"><?php echo $row['sale_time'];?></td>      
                        </tr><?php  
                    }?>                 
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    </form>
</div>
</div>
<style>
    tfoot {
        display: table-header-group;
    }
</style>
<script type="text/javascript" language="javascript">
    $('#tblSales').DataTable({
        paginate:false,
        lengthChange: false,
        filter:true,
        bFilter:true,
        aaSorting:[],
        dom: 'Bfrtip',
        buttons: [{ extend: 'excel', text: 'Export to excel'}]
    });
    
    $('#tblSales_filter').hide();
    
</script>