<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-line-chart"></i> Motor Vehicle 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>           
    </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('MV_expenses/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">MV Expense</label><small class="req"> ( Optional )</small>
										<select id="mv_expense" name="mv_expense" placeholder="" type="text" class="form-control" >
											<option>Select Motor Vehicle</option>
											<?php 
												foreach ($vehicle_list->result() as $grup) {
												?>
									                <option value="<?php echo $grup->vehicle_no; ?>" >  <?php echo $grup->vehicle_model.' - '.$grup->manufacture_year; ?></option>
											<?php  }
											?>
										</select>
                                    <span class="text-danger"><?php echo form_error('mv_expense'); ?></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($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>Cashbook</th>
                                        <th>Account</th>
                                        <th>Module</th>
                                        <th>Description</th>
                                        <th>Vehicle</th>
                                        <th>Reference (Receipt)</th>
                                        <th>Currency</th>
                                        <th>Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php  	            $amount=0;
													$buffer =$amount;
                                                    $amount1=0;
                                                    $buffer1 =$amount1;
                                                    $module = "GL";

                                                    $this->db->select('*');
                                                    $this->db->from('supplier_statement');
                                                    //$this->db->where('module = ', $module);
                                                    $this->db->where('mv_expense = ', $vehicle_no);
                                                    $this->db->where('date >= ', $from);
                                                    $this->db->where('date <= ', $to);
                                                    $supplierStatement_results = $this->db->get();

										 foreach ($results->result() as $row)  										 
										 {  
													$amount=($amount + $row->credit);
													$buffer =$amount;
													        $account_name = $row->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;
																	
															$vehicle_no = $row->mv_expense;
															$this->db->select('*');
															$this->db->from('vehicles');
															$this->db->where('vehicle_no = ', $vehicle_no);
															$query_vehicle = $this->db->get();
															$v_row = $query_vehicle->row();
														    $vehicle_no = $v_row->vehicle_no;
														    $vehicle_model = $v_row->vehicle_model;
														    $manufacture_year = $v_row->manufacture_year;
																	  											
										 ?>
													<tr>
														<td class="mailbox-name"><?php echo $glsub_account_type.' > '.$glsub_code; ?></td>
														<td class="mailbox-name"><?php echo $account_name;?></td>
														<td class="mailbox-name"><?php echo $row->date;?></td>
														<td class="mailbox-name"><?php echo $row->cashbook;?></td>
														<td class="mailbox-name"><?php echo $row->account;?></td>
														<td class="mailbox-name"><?php echo $row->module;?></td>
														<td class="mailbox-name"><?php echo $row->description;?></td>
														<td class="mailbox-name"><?php echo $vehicle_no.' > '.$vehicle_model.' > '.$manufacture_year;?></td>
														<td class="mailbox-name"><?php echo $row->receipt;?></td>
														<td class="mailbox-name"><?php 
														
															$currency= $row->cashbook;

															$this->db->select('*');
															$this->db->from('cashbook_accounts');
															$this->db->where('cashbook_accounts', $currency);
															$query = $this->db->get();
															$result = $query->row();
															
															echo $result->currency;
														?></td>   
														 <td class="text text-right"><?php echo $row->credit;?></td>														
													</tr>																																											
													<?php
														$amount=$buffer;
												} 
                                                foreach ($supplierStatement_results->result() as $row2)  
													 {			
                                                        $amount1=($amount1 + $row2->credit);
                                                        $buffer =$amount1;			
                                                        
                                                        $vehicle = $row2->mv_expense;
                                                        $this->db->select('*');
                                                        $this->db->from('vehicles');
                                                        $this->db->where('vehicle_no = ', $vehicle);
                                                        $query_vehicle1 = $this->db->get();
                                                        $v_row1 = $query_vehicle1->row();
                                                        $vehicle_num = $v_row1->vehicle_no;
                                                        $vehicle_model1 = $v_row1->vehicle_model;
                                                        $manufacture_year1 = $v_row1->manufacture_year;

														 ?>
																		<tr>
																			<td class="mailbox-name"></td>
                                                                            <td class="mailbox-name"><?php echo $row2->gl_account;?></td>
																			<td class="mailbox-name"><?php echo $row2->date;?></td>
																			<td class="mailbox-name">-----</td>
																			<td class="mailbox-name"><?php echo $row2->supplier;?></td>
																			<td class="mailbox-name">GL</td>    
																			<td class="mailbox-name"><?php echo $row2->description;?></td>
                                                                            <td class="mailbox-name"><?php echo $vehicle_num.' > '.$vehicle_model1.' > '.$manufacture_year1;?></td>
                                                                            <td class="mailbox-name"><?php echo $row2->reference;?>/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>