<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-line-chart"></i> Journal Report <?php echo $this->lang->line('reports'); ?> <small></small>
        </h1>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-md-12">
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h3 class="box-title"><i class="fa fa-search"></i>
                                <?php echo $this->lang->line('select_criteria'); ?></h3>
                        </div>

                        <form role="form" action="<?php echo site_url('jornal/journalsearch') ?>" method="post"
                            class="">
                            <div class="box-body row">

                                <?php echo $this->customlib->getCSRF(); ?>

                                <div class="col-sm-8 col-md-4">
                                    <div class="form-group">
                                        <label for="journal_accounts">Journal Account</label><small class="req">
                                            *</small>
                                        <select id="journal_accounts" name="journal_accounts" placeholder="" type="text"
                                            class="form-control">
                                            <option value="select"><?php echo $this->lang->line('select') ?></option>
                                            <?php 
												foreach ($journal->result() as $grup) {
												?>
                                            <option value="<?php echo $grup->journal_accounts; ?>">
                                                <?php echo $grup->journal_accounts; ?></option>
                                            <?php }
											?>
                                            <option value="all">All</option>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('journal_accounts'); ?></span>
                                    </div>
                                </div>
                                <div class="col-sm-8 col-md-4">
                                    <div class="form-group">
                                        <label for="colour">From Date</label><small class="req"> *</small>
                                        <input autofocus="" id="from" name="from" placeholder="" type="date"
                                            class="form-control" value="<?php echo date("Y-m-d")?>" />
                                        <span class="text-danger"><?php echo form_error('from'); ?></span>
                                    </div>
                                </div>
                                <div class="col-sm-8 col-md-4">
                                    <div class="form-group">
                                        <label for="colour">To Date</label><small class="req"> *</small>
                                        <input autofocus="" id="to" name="to" placeholder="" type="date"
                                            class="form-control" value="<?php echo date("Y-m-d")?>" />
                                        <span class="text-danger"><?php echo form_error('to'); ?></span>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <button type="submit" name="search" value="search_filter"
                                            class="btn btn-primary btn-sm checkbox-toggle pull-right"><i
                                                class="fa fa-search"></i>
                                            <?php echo $this->lang->line('search'); ?></button>
                                    </div>
                                </div>

                        </form>
                    </div>
                </div>
                <?php
            if (isset($cashresultlist)) {
                ?>
                <div class="box box-info" style="padding:5px;">
                    <div class="box-header ptbnull">
                        <h3 class="box-title titlefix"><i class="fa fa-users"></i> Journal Report </h3>
                    </div>
                    <div class="box-body table-responsive">
                        <div class="download_label">Report </div>
                        <table class="table table-striped table-bordered table-hover example">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Reference</th>
                                    <th>Journal</th>
                                    <th>Description</th>
                                    <th>Deposits</th>
                                    <th>Payments</th>
                                    <th>Balance</th>

                                </tr>
                            </thead>
                            <tbody>
                                <?php  
													$balance=0;
													$buffer =$balance;		
										 foreach ($cashresultlist->result() as $row)  
										 {   
													
													$this->db->select_sum('debit');
													$this->db->where('cashbook =',$journal_accounts);
													$this->db->where('date >= ', $from);
													$this->db->where('date <= ', $to);
													$result = $this->db->get('journal')->row();  
													$total_deposits = $result->debit;		

													$this->db->select_sum('credit');
													$this->db->where('cashbook =',$journal_accounts);
													$this->db->where('date >= ', $from);
													$this->db->where('date <= ', $to);													
													$result2 = $this->db->get('journal')->row();  
													$total_payments = $result2->credit;	
													
													$total_balance = $total_deposits - $total_payments;
													
													$balance=($balance + $row->debit)-( $row->credit);
													$buffer =$balance;
													if($balance<0)
													{
													 $balance=$balance - ($balance*2);

													?>

                                <tr>
                                    <td class="mailbox-name"><?php echo $row->date_captured;?></td>
                                    <td class="mailbox-name"><?php echo $row->receipt;?></td>
                                    <td class="mailbox-name"><?php echo $row->cashbook;?></td>
                                    <td class="mailbox-name"><?php echo $row->description;?></td>
                                    <td class="mailbox-name"><?php echo $row->debit;?></td>
                                    <td class="mailbox-name"><?php echo $row->credit;?></td>
                                    <td class="mailbox-name"><?php echo "(".$balance.")";?></td>
                                </tr>


                                <?php
												$balance=$buffer;
												   }
												   else{
														?>


                                <tr>
                                    <td class="mailbox-name"><?php echo $row->date_captured;?></td>
                                    <td class="mailbox-name"><?php echo $row->receipt;?></td>
                                    <td class="mailbox-name"><?php echo $row->cashbook;?></td>
                                    <td class="mailbox-name"><?php echo $row->description;?></td>
                                    <td class="mailbox-name"><?php echo $row->debit;?></td>
                                    <td class="mailbox-name"><?php echo $row->credit;?></td>
                                    <td class="mailbox-name"><?php echo $balance;?></td>
                                </tr>


                                <?php
												  // cashbook_account
												   }												
											} ?>
                                <tr>
                                    <td class="mailbox-name"></td>
                                    <td class="mailbox-name"></td>
                                    <td class="mailbox-name"></td>
                                    <td class="mailbox-name">Totals</td>
                                    <td class="mailbox-name"><?php echo $total_deposits;?></td>
                                    <td class="mailbox-name"><?php echo $total_payments?></td>
                                    <td class="mailbox-name"><?php echo $total_balance;?></td>
                                </tr>
                            </tbody>

                            <tr>
                                <td>
                                    <div class="btn-group pull-right">
                                        <button class="btn btn-xs btn-info printInv" data-from="<?php echo $from; ?>"
                                            data-to="<?php echo $to; ?>"
                                            data-cashbook_account="<?php echo $cashbook_account; ?>"
                                            title="<?php echo $this->lang->line('print'); ?>"><i
                                                class="fa fa-print"></i> Print Statement</button>
                                    </div>
                                </td>

                            </tr>
                        </table><!-- /.table -->
                    </div>
                </div>
                <?php
            }
            elseif (isset($cashresultlist_all)) {
                ?>
                <div class="box box-info" style="padding:5px;">
                    <div class="box-header ptbnull">
                        <h3 class="box-title titlefix"><i class="fa fa-users"></i> Journal Report </h3>
                    </div>
                    <div class="box-body table-responsive">
                        <div class="download_label">Report </div>
                        <table class="table table-striped table-bordered table-hover example">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Reference</th>
                                    <th>Journal</th>
                                    <th>Description</th>
                                    <th>Deposits</th>
                                    <th>Payments</th>
                                    <th>Balance</th>

                                </tr>
                            </thead>
                            <tbody>
                                <?php  
													$balance=0;
													$buffer =$balance;		
										 foreach ($cashresultlist_all->result() as $row)  
										 {   
													
													$this->db->select_sum('debit');
													$this->db->where('date >= ', $from);
													$this->db->where('date <= ', $to);
													$result = $this->db->get('journal')->row();  
													$total_deposits = $result->debit;		

													$this->db->select_sum('credit');
													$this->db->where('date >= ', $from);
													$this->db->where('date <= ', $to);													
													$result2 = $this->db->get('journal')->row();  
													$total_payments = $result2->credit;	
													
													$total_balance = $total_deposits - $total_payments;
													
													$balance=($balance + $row->debit)-( $row->credit);
													$buffer =$balance;
													if($balance<0)
													{
													 $balance=$balance - ($balance*2);

													?>

                                <tr>
                                    <td class="mailbox-name"><?php echo $row->date_captured;?></td>
                                    <td class="mailbox-name"><?php echo $row->receipt;?></td>
                                    <td class="mailbox-name"><?php echo $row->cashbook;?></td>
                                    <td class="mailbox-name"><?php echo $row->description;?></td>
                                    <td class="mailbox-name"><?php echo $row->debit;?></td>
                                    <td class="mailbox-name"><?php echo $row->credit;?></td>
                                    <td class="mailbox-name"><?php echo "(".$balance.")";?></td>
                                </tr>


                                <?php
												$balance=$buffer;
												   }
												   else{
														?>


                                <tr>
                                    <td class="mailbox-name"><?php echo $row->date_captured;?></td>
                                    <td class="mailbox-name"><?php echo $row->receipt;?></td>
                                    <td class="mailbox-name"><?php echo $row->cashbook;?></td>
                                    <td class="mailbox-name"><?php echo $row->description;?></td>
                                    <td class="mailbox-name"><?php echo $row->debit;?></td>
                                    <td class="mailbox-name"><?php echo $row->credit;?></td>
                                    <td class="mailbox-name"><?php echo $balance;?></td>
                                </tr>


                                <?php
												  // cashbook_account
												   }												
											} ?>
                                <tr>
                                    <td class="mailbox-name"></td>
                                    <td class="mailbox-name"></td>
                                    <td class="mailbox-name"></td>
                                    <td class="mailbox-name">Totals</td>
                                    <td class="mailbox-name"><?php echo $total_deposits;?></td>
                                    <td class="mailbox-name"><?php echo $total_payments?></td>
                                    <td class="mailbox-name"><?php echo $total_balance;?></td>
                                </tr>
                            </tbody>

                            <tr>
                                <td>
                                    <div class="btn-group pull-right">
                                        <button class="btn btn-xs btn-info printInv" data-from="<?php echo $from; ?>"
                                            data-to="<?php echo $to; ?>"
                                            data-cashbook_account="<?php echo $cashbook_account; ?>"
                                            title="<?php echo $this->lang->line('print'); ?>"><i
                                                class="fa fa-print"></i> Print Statement</button>
                                    </div>
                                </td>

                            </tr>
                        </table><!-- /.table -->
                    </div>
                </div>
                <?php
            }else{

            }
            ?>
            </div>
</div>
</section>
</div>

<script type="text/javascript">
function getSectionByClass(class_id, section_id) {
    if (class_id != "" && section_id != "") {
        $('#section_id').html("");
        var base_url = '<?php echo base_url() ?>';
        var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
        $.ajax({
            type: "GET",
            url: base_url + "sections/getByClass",
            data: {
                'class_id': class_id
            },
            dataType: "json",
            success: function(data) {
                $.each(data, function(i, obj) {
                    var sel = "";
                    if (section_id == obj.section_id) {
                        sel = "selected";
                    }
                    div_data += "<option value=" + obj.section_id + " " + sel + ">" + obj.section +
                        "</option>";
                });
                $('#section_id').append(div_data);
            }
        });
    }
}

$(document).ready(function() {
    var class_id = $('#class_id').val();
    var section_id = '<?php echo set_value('section_id') ?>';
    getSectionByClass(class_id, section_id);
    $(document).on('change', '#class_id', function(e) {
        $('#section_id').html("");
        var class_id = $(this).val();
        var base_url = '<?php echo base_url() ?>';
        var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
        $.ajax({
            type: "GET",
            url: base_url + "sections/getByClass",
            data: {
                'class_id': class_id
            },
            dataType: "json",
            success: function(data) {
                $.each(data, function(i, obj) {
                    div_data += "<option value=" + obj.section_id + ">" + obj
                        .section + "</option>";
                });
                $('#section_id').append(div_data);
            }
        });
    });
});
</script>