<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-line-chart"></i> Income History <small></small></h1>
								 <a href="<?php  echo base_url(); ?>income_history" class="col-md pull-right">Income History</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>	
								 <a href="<?php  echo base_url(); ?>income_report" class="col-md pull-right">Income Statement</a>
    </section>
    <!-- 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('income_history/index') ?>" 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="currency">GL Account</label><small class="req"> *</small>
										<select id="gl_account" name="gl_account" placeholder="" type="text" class="form-control" >
											<option>Select GL Account</option>
											<?php 
												foreach ($glledger_accounts->result() as $grup11) {
												?>
									                <option value="<?php echo $grup11->name; ?>" >
														<?php echo $grup11->gl_code.' > '.$grup11->name; ?></option>
													
													<?php
														$account_type = $grup11->gl_code;
											
														$this->db->select('*');
														$this->db->from('glsub_account');
														$this->db->where('account_type = ', $account_type);
														$this->db->order_by('glsub_account.name','ASC');
														$query1 = $this->db->get();
														
														foreach ($query1->result() as $grup12) {
													?>
													      <option value="<?php echo $grup12->name; ?>" > 
														  <?php echo $grup11->gl_code.' > '.$grup12->glsub_code.' > '.$grup12->name; ?></option>
													
														<?php }
											}
											?>
											<option>All</option>
										</select>
                                    <span class="text-danger"><?php echo form_error('gl_account'); ?></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 set_value('from'); ?>" />
									<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 set_value('to'); ?>" />
									<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($customer_results)) {
                ?>
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header ptbnull">
                        <h3 class="box-title titlefix"><?php echo $title_list; ?></h3>
                        <div class="box-tools pull-right">
						 <!--<a href="supplier/supplier_statement" class="btn btn-primary btn-sm pull-right">
								Return Back</a> -->
                        </div><!-- /.box-tools -->
                    </div><!-- /.box-header -->
                    <div class="box-body">
                        <div class="table-responsive mailbox-messages">
                            <h4  class="box-title titlefix"></h4 >
							<table class="table table-striped table-bordered table-hover example">
                                <thead>
                                    <tr>
                                        <th colspan='2'>GL Account</th> 
                                        <th>Date</th> 
                                        <th>Supplier</th> 
                                        <th>Currency</th> 
                                        <th>Reference</th>
                                        <th>Description</th>
                                        <th>Qnty.</th>
                                        <th>Price.</th>
                                        <th>Dscnt (%).</th>
                                        <th>Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php  	
								                    $amount=0;
                                                    $amount1=0;
													$buffer =$amount;
                                                    $buffer1 =$amount1;
                                                    $module = "GL";

                                                    $this->db->select('*');
                                                    $this->db->from('cashbook');
                                                    //$this->db->where('module = ', $module);
                                                    $this->db->where('account = ', $gl_name);
                                                    $this->db->where('date >= ', $from);
                                                    $this->db->where('date <= ', $to);
                                                    $cashbook_results = $this->db->get();
													
										 foreach ($customer_results->result() as $row)  										 
										 {  
													$amount=($amount + $row->total_amount);
													$buffer =$amount;
													        $account_name = $row->gl_account;
															
															$this->db->select('*');
															$this->db->from('glledger_account');
															$this->db->where('name = ', $account_name);
															$query11 = $this->db->get();
															$queryrow11 = $query11->row();
														    $gl_code = $queryrow11->gl_code;																												
														    $gl_name = $queryrow11->name;																												
														    $gl_account_type = $queryrow11->account_type;																												
														  
																$this->db->select('*');
																$this->db->from('glsub_account');
																$this->db->where('name = ', $account_name);
																$query2 = $this->db->get();
																$sub_row = $query2->row();
																
																$glsub_name = $sub_row->name;
																$glsub_code = $sub_row->glsub_code;
																$glsub_account_type= $sub_row->account_type;
																	
																	  											
										 ?>
													<tr>
														<td class="mailbox-name"><?php if($glsub_name == $row->gl_account){ echo $gl_code.' > '.$glsub_code;}
														else { echo $gl_code;} ?></td>
														<td class="text text-left"><?php echo $row->gl_account;?></td>
														<td class="mailbox-name"><?php echo $row->inv_date;?></td>
														<td class="mailbox-name"><?php echo $row->name;?></td>
														<td class="mailbox-name"><?php echo $row->currency;?></td>
														<td class="mailbox-name"><?php echo $row->inv_number;?></td>
														<td class="mailbox-name"><?php echo $row->description;?></td>
														<td class="mailbox-name"><?php echo $row->quantity;?></td>
														<td class="mailbox-name"><?php echo $row->price;?></td>
														<td class="mailbox-name"><?php echo $row->discount;?></td>
														<td class="text text-right"><?php echo $row->total_amount;?></td>                                      
													</tr>																																											
													<?php
														$amount=$buffer;
												} 
                                                foreach ($cashbook_results->result() as $row2)  
													 {			
                                                        $amount1=($amount1 + $row2->credit);
                                                        $buffer =$amount1;												
														 ?>
																		<tr>
																			<td class="mailbox-name"></td>
                                                                            <td class="mailbox-name"><?php echo $row2->account;?></td>
																			<td class="mailbox-name"><?php echo $row2->date;?></td>
																			<td class="mailbox-name"><?php echo $row2->cashbook;?></td>
																			<td class="mailbox-name">-----</td>
																			<td class="mailbox-name"><?php echo $row2->receipt;?></td>    
																			<td class="mailbox-name"><?php echo $row2->description.'( Vehicle Model: '.$row2->mv_expense.')';?></td>
                                                                            <td class="mailbox-name">-----</td>
                                                                            <td class="mailbox-name">-----</td>
                                                                            <td class="mailbox-name">-----</td>
                                                                            <td class="text text-right"><?php echo $row2->credit;?></td> 																		
																		</tr>														 
														 <?php 
													$amount1=$buffer;															
													 }
                                                ?>
														<tr>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name"></td>
														<td class="mailbox-name">Grand Total</td>
														<td class="text text-right"><b><?php echo $amount + $amount1;?></b></td>                                     
													</tr>	
                                </tbody>
                            </table><!-- /.table -->



                        </div><!-- /.mail-box-messages -->
                    </div><!-- /.box-body -->
                </div>
            </div><!--/.col (left) -->
                <?php
            }
            ?>
        </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>