<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-line-chart"></i> Expenses History <small></small></h1>
											 <a href="<?php  echo base_url(); ?>MV_expenses" class="col-md pull-right">MV Expense History</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>mv_expense_report" class="col-md pull-right">MV Expense Report</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>expenses_history" class="col-md pull-right">Expense History</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>	
								 <a href="<?php  echo base_url(); ?>expenses_report" class="col-md pull-right">Expense Statement</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>	
								 <a href="<?php  echo base_url(); ?>supplier/supplier_statement" class="col-md pull-right">Statement</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>											 
								 <a href="<?php  echo base_url(); ?>supplier/supplier_receipt" class="col-md pull-right">Receipts</a>
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>supplier/supplier_invoice" class="col-md pull-right">Invoice</a> 
								 <b style="padding-left:5px;padding-right:5px;"class="pull-right"> | </b>
								 <a href="<?php  echo base_url(); ?>supplier" class="col-md pull-right">Add Supplier</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('expenses_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 $grup) {
												?>
									                <option value="<?php echo $grup->name; ?>" >  <?php echo $grup->name; ?></option>
													
													<?php
														$account_type = $grup->account_type;
											
														$this->db->select('*');
														$this->db->from('glsub_account');
														$this->db->where('account_type = ', $account_type);
														$query = $this->db->get();
														
														foreach ($query->result() as $grup1) {
													?>
													      <option value="<?php echo $grup1->name; ?>" >  <?php echo $grup1->name; ?></option>
													
											<?php } ?>
														<option>All</option>
										<?php	}
											?>
											
										</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($supplier_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;
													$buffer =$amount;
													
										 foreach ($supplier_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 $glsub_account_type.' > '.$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;
												} ?>
														<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;?></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>