<style type="text/css">
    #searchdiv{
        margin-bottom: 10px;
    }
    .searchfieldslist{
        margin-top: 4px;
    }
</style>
<!-- Page Content -->
<div class="page-content">
    <!-- Page Header -->
    <div class="page-header position-relative">
        <div class="header-title">
            <h1>
                Supplier Diary
            </h1>
        </div>
    </div>
    <!-- /Page Header -->
    <!-- Page Body -->
    <div class="page-body">
        
        <div class="row">
            <div class="col-xs-12 col-md-12">
                <div class="widget">
                    <div class="widget-body">
                        <div id="searchdiv">
                            <form name="frmsearch" id="frmsearch" method="post">
                                <div class='col-md-2 searchfieldslist'>
                                    <input type="text" name="searchkey" id="searchkey" class="form-control" placeholder="search key"
                                           value="<?php if(isset($_POST['searchkey'])) echo $_POST["searchkey"]; ?>" />
                                </div>
                                <div class='col-md-3 searchfieldslist'>
                                    <input type="text" name="suplrname" id="suplrname" class="form-control" placeholder="Supplier Name"
                                           value="<?php if(isset($_POST['suplrname'])) echo $_POST["suplrname"]; ?>" />
                                </div>
                                <div class='col-md-2 searchfieldslist'>
                                    <input type="text" name="fromdate" id="fromdate" class="form-control datepickertxt" readonly
                                           placeholder="fromdate" value="<?php if(isset($_POST['fromdate'])) echo $_POST["fromdate"]; else echo date("d-m-Y",strtotime('-1 months')); ?>" />
                                </div>
                                <div class='col-md-2 searchfieldslist'>
                                    <input type="text" name="todate" id="todate" class="form-control datepickertxt" readonly
                                           placeholder="to date" value="<?php if(isset($_POST['todate'])) echo $_POST["todate"]; else echo date("d-m-Y"); ?>" />
                                </div>
                                            <div class="clearfix"></div>
                                    <div class='col-md-3 searchfieldslist'>
                                        <button type="submit" class="btn btn-primary shiny" name="allsearch"><span class="fa fa-search"></span>Execute</button>
                                    </div>
                            </form>
                        </div>
                <form name="exportexcel" id="exportexcel" action="<?php echo base_url(); ?>reports/Suppliersdiary/exportexceldata" method="post">
                    <?php
                    if(isset($_POST["allsearch"])){
                    ?>
                    <div style="float: right;">
                        <input type="hidden" name="hiddensqlquery" id="hiddensqlquery" value="<?php echo $sqlquery; ?>" />
<input type="hidden" name="hiddenfromdate" id="hiddenfromdate" value="<?php if(isset($_POST['fromdate'])) echo $_POST["fromdate"]; ?>" />
                        <div class="btn-group">
                            <button type="submit" class="btn btn-success shiny" name="excelexport"><i class="fa fa-file-excel-o"></i> Excel</button>
                            <button type="submit" class="btn btn-danger shiny" name="pdfxport"><i class="fa fa-file-pdf-o"></i> PDF</button>
                        </div>
                    </div>
                    <?php
                    }
                    ?>
                </form>
                    <div id="tablecontent" class="table-scrollable">
                        <div id="tablecontent" class="table-scrollable">
                            <table class="table table-striped table-hover table-bordered" id="reportdatatable">
                                <thead>
                                    <tr role="row">
                                        <th style="text-align:left;width:50px">Date</th>
                                        <th style="text-align:left;width:70px">Invoice Ref.</th>
                                        <th style="text-align:left;width:70px">Invoice Type</th>
                                        <th style="text-align:right;width:70px">Amount</th>
                                        <th style="text-align:right;width:70px">Balance</th>
                                    </tr>
                                </thead>
                                <tfoot>
                                    
                                </tfoot>
                                <tbody>
    <?php
    if(isset($resultdata)) {
        if($resultdata) {
            $supplierid   =   '';
            foreach($resultdata as $result) {
                if($supplierid!=$result->SUPPLIERID) {
                    echo '<tr role="row"><td colspan="5"><b>'.$result->SUPPLIERNAME.'</b></td></tr>';
                        $supplierid   =   $result->SUPPLIERID;
                    $sql = "SELECT SUM(A.AMOUNT) AS TOTALAMOUNT FROM TRANSACTIONS AS A JOIN PURCHASEINVOICE_PAYMENTS AS PP ON PP.TRANSACTION_ID=A.ID
                        JOIN PURCHASEINVOICE AS P ON P.ID=PP.PURCHASEINVOICE_ID JOIN SUPPLIERS AS S ON S.ID=P.SUPPLIERID WHERE A.STATUS=1"
                        . " AND S.ID='".$result->SUPPLIERID."'";
                    if($_POST["fromdate"])
                        $sql = $sql." AND A.TRANSACTION_DATE<'".date("Y-m-d",strtotime($_POST["fromdate"]." 00:00:00"))."'";
                    $totalamount = $this->common->customquery($sql);
                    echo '<tr role="row"><td colspan="4"> <b>Opening Balance as on '.date("d-M-Y",strtotime($_POST["fromdate"]." -1 day")).'</b></td>'
                    . '<td style="text-align:right;"><b>'.
                        $this->formatslist->formatCurrencywithHash($totalamount[0]->TOTALAMOUNT).'</b></td></tr>';
                    $balancedeflect =   $totalamount[0]->TOTALAMOUNT;
                }
                $balancedeflect =   $balancedeflect+$result->AMOUNT;
            ?>
                <tr role="row">
                    <td> <?php echo $this->formatslist->dateFormat($result->TRANSACTION_DATE); ?></td>
                    <td> <?php echo $result->REFERENCE; ?></td>

                    <td> 
                        <?php
                            if($result->AMOUNT>0)
                                echo "Purchase Invoice";
                            else if($result->AMOUNT<0)
                                echo "Purchase Invoice Return";
                        ?>
                    </td>

                    <td> <?php echo $this->formatslist->formatCurrencywithHash($result->AMOUNT); ?> </td>
                    <td> <?php echo $this->formatslist->formatCurrencywithHash($balancedeflect); ?> </td>
                </tr>
                <?php
                        }
                    }
                }
                ?>
            </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</div>
<!-- /Page Body -->
    </div>
    <!-- /Page Content -->
</div>
<!-- /Page Container -->
<!-- Main Container -->
</div>
<script src="<?= site_url() ?>assets/js/datetime/bootstrap-datepicker.js"></script>
<script type="text/javascript">
    $("#fromdate").datepicker({format: 'dd-mm-yyyy', setDate: new Date()});
    $("#todate").datepicker({format: 'dd-mm-yyyy', setDate: new Date()});
    $('.datepickertxt').datepicker().on('changeDate', function (ev) { 
        $('.datepicker').hide();
    });
</script>