<?php
$user_id=$this->session->userdata('user_id');
if(!$user_id){
  redirect('user/login_view', 'refresh');
}
$type=$this->db->query("select type from sale  
  ");
$data='';
foreach ($type->result() as $key => $type){
  $data.=$type->type;
}
                         $value='';
                         $limit=$this->db->query("select page_limit from page_limit");
                         foreach ($limit->result() as $key => $value)
                         $limit=$value->page_limit;
?>
<?php 
$b_type='';
 $bus_type=$this->db->query("select * from buisness_type  
    ");
  $data='';
  foreach ($bus_type->result() as $key => $bus_type){
    $b_type=$bus_type->type;

  }
?>
<!DOCTYPE html>
<html lang="en">
<?php
$this->load->view('head.php');
?>
<body>
  <div id="wrapper">
      <div class="admin-title">
    <div class="row">
     <?php $this->load->view('header.php'); ?>
      </div>
    </div>
      <div class="content-area">
        <div class="row">
          <div class="col-xs-12 col-sm-3 col-md-3">
            <div class="admin-menu">
              <?php $this->load->view('sidebar.php'); ?>
            </div>
          </div>
        <div class="col-xs-12 col-sm-9 col-md-9">
          <div class="main-content">
            <div class="content-menu">
              <ul class="nav nav-tabs">
                <li class="page-header active" ><a data-toggle="tab" href="#home">All Expense</a></li>
                <?php
                $type=$this->db->query("select * from expense_type where buisness_type='$b_type' 
                  ");
                $data='';
                foreach ($type->result() as $key => $type){
                  $data=$type->id;
                  $type=$type->type;
                  echo '<li class="page-header"><a data-toggle="tab" href="#'.$data.'">'.$type.'</a> </li>';
                }
                ?>
              </ul>
            </div>
            <div class="panel-heading headingnone">
              <h2 class="transac">Expense</h2>
              <button class="transaction btn btn-success"onclick="add_transaction()"  data-toggle="modal" data-target="#myModal">New Expense</button>
            </div>
            <form method="post" action="<?php echo site_url('expense/search');?>">
              <div  class="input" style="padding-top: 83px;">
                <label>From<input class="form-control" id="from" type="date" name="from" value="<?php echo $from ; ?>">
                </label>
                <label>To<input class="form-control" id="to" type="date" name="to" value="<?php echo $to ; ?>">
                </label>
                <button type="submit" id="submit" class="btn btn-default" >
                  <span class="glyphicon glyphicon-search"></span> Search
                </button>
                <div class="action_btn" >
                  <button type="button"  title="Export" data-toggle="tooltip" onclick="window.location='<?php echo site_url("user/setting");?>'"><i class="fa fa-cog fa-fw"></i></button>
                  <button type="button"  title="Export" data-toggle="tooltip" onclick="window.location='<?php echo site_url("Excel_export/action");?>'"><span class="glyphicon glyphicon-ice-lolly-tasted"></span></button>
                  <button class="p-btn" title="Print" onclick="printDiv('printableArea')" value="Print Invoice" data-toggle="tooltip"><span class="glyphicon glyphicon-print"></span></button>
                </div>  
              </div>
              <div id="status-area" class="f-message" style="display:none; background: #2fffea;" ></div>
            </form>
            <div class="panel-body " id="printableArea" >
              <div class="tab-content">
                <div id="home" class="tab-pane fade in active">
                  <table width="100%" class="table table-striped table-bordered table-hover mytable" id="dataTables-example">
                    <thead>
                      <tr>
                        <th><p><label><input type="checkbox" id="checkAll" name="check_list[]"/> </label></p></th>
                        <th>DATE</th>
                        <th>TYPE</th>
                        <th>CASH PAY</th>
                        <th>CB</th>
                        <th>CHEQUE PAY</th>
                        <th>TOTAL</th>
                        <th>ACTION</th>
                      </tr>
                      <button class="transaction btn btn-success" style="margin-top: -4px;" onclick="window.location='<?php echo site_url("expense/trash");?>'">[Trash]</button>
                      <input class="savebtn btn btn-danger" id="reset" type="submit" onclick="dmycheck()" value="Delete all"/>
                    </thead>
                    <tbody id="result">
                      <?php
                      if($from AND $to){
                          $cash=0.0;
                                    $card=0.0;
                                    $tr=0.0;
                                    $total=0.0;
                          $cheque=0.0;
                        $query=$this->db->query("select * from expense where (date BETWEEN '".$from."' AND '".$to."' ) and  status=1 and buisness_type='$b_type' ORDER BY `id` 
                         LIMIT ".$limit." ");
                        foreach ($query->result() as $key => $value){
                            $id=$value->id;
                            
                                    $cash+=$value->cash_pay;
                                       $card+=$value->bank_card;
                                        $cheque+=$value->cheque_pay;
                                       $total+=$value->total;
                            
                          echo '  <tr class="delete_mem'.$value->id.' ?>">
                          <td>'.'<input type="checkbox" id="chk" name="selected_item" value="'.$value->id.'" />'.'</td>
                          <td>'. date('d F,Y',strtotime($value->date)).'</td>
                          <td>'. ucfirst($value->type).'</td>
                          <td>'. number_format($value->cash_pay,2).'€'.'</td>
                          <td class="center">'. number_format($value->bank_card,2).'€'.'</td>
                          <td class="center">'.number_format($value->cheque_pay,2) .'€'.'</td>
                          <td class="center">'. number_format($value->total,2).'€'.'</td>
                          <td><a class="btn" data-toggle="modal" data-target="#myModal"  onclick="edit('.$id.')">Edit</a> <a class="btn btn-danger btn-xs" onclick="smyFunction('.$id.')" >Remove</a></td>
                          </tr>';
                        }
                         
                        
                      } 
                      else {
                                   $cash=0.0;
                                    $card=0.0;
                                    $tr=0.0;
                                    $total=0.0;
                        for ($i = 0; $i < count($deptlist); ++$i)     
                          { ?>
                           <div id="start">
                            <tr class="delete_mem<?php echo $deptlist[$i]->id; ?>">
                              <td><input type="checkbox"  id="chk" name="selected_item" value="<?php echo  $deptlist[$i]->id; ?>" /></td>
                              <td><?php echo  date('d F,Y',strtotime($deptlist[$i]->date)); ?></td>
                              <td><?php echo ucfirst($deptlist[$i]->type); ?></td>
                              <td ><?php echo number_format($deptlist[$i]->cash_pay,2) ; ?>€</td>
                              <td class="center bank"><?php echo  number_format($deptlist[$i]->bank_card,2); ?>€</td>
                              <td class="center ticket"><?php echo number_format($deptlist[$i]->cheque_pay,2); ?>€</td>
                              <td class="center total"><?php echo  number_format($deptlist[$i]->total,2); ?>€</td>
                              
                              <td><a class="btn" data-toggle="modal" data-target="#myModal"  onclick="edit(<?php echo $deptlist[$i]->id;?>)">Edit</a> <a  class="btn btn-danger btn-xs" onclick="smyFunction(<?php echo $deptlist[$i]->id;?>)" >Remove</a></td> 
                              
                            </tr>
                            </div>
                             <?php
            
                                    $cash+=$deptlist[$i]->cash_pay;
                                       $card+=$deptlist[$i]->bank_card;
                                        $tr+=$deptlist[$i]->cheque_pay;
                                       $total+=$deptlist[$i]->total;
                                       
                                    }
                       
                          ?>
                             
                            <?php }
                          ?>
                        
                          <tr class="total-value">
                            <td>
                            </td>
                            <td></td>
                            <td class="total-val">Total</td>
                            
                            <td id="cash"><?php echo number_format($cash,2) ;?>€</td>
                            <td id="totalbank"><?php echo  number_format($card,2) ; ?>€</td>
                            <td id="totalticket"><?php echo  number_format($tr,2) ;?>€</td>
                            <td id="totalsale"><?php echo  number_format($total,2) ;?>€</td>
                            <td></td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                    <?php 
                    $query=$this->db->query("select * from expense_type 
                     where buisness_type='$b_type' "); 
                    foreach ($query->result() as $key => $value){
                      $data_id=$value->id;
                      $data_type=$value->type;
                      echo '<div id="'.$data_id.'" class="tab-pane fade">'; ?>     
                        <table width="100%" class="table table-striped table-bordered table-hover mytable" id="dataTables-example">
                          <thead>
                            <tr>
                              <th><p><label><input type="checkbox" id="checkAll" name="check_list[]"/> </label></p></th>
                              <th>DATE</th>
                              <th>TYPE</th>
                              <th>CASH PAY</th>
                              <th>CB</th>
                              <th>CHEQUE PAY</th>
                              <th>TOTAL</th>
                              <th>ACTION</th>
                            </tr>
                            <button class="transaction btn btn-success" style="margin-top: -4px;" onclick="window.location='<?php echo site_url("expense/trash");?>'" >[Trash]</button>
                             <input class="savebtn btn btn-danger" id="reset" type="submit" onclick="dmycheck()" value="Delete all"/>
                          </thead>
                          <tbody>
                            <?php  
                            $cash_sale=0.0;
                            $bank_card=0.0;
                            $ticket_b=0.0;
                            $total_value=0.0;
                            $query=$this->db->query("select * from expense where type= '$data_type' and  status=1 and buisness_type='$b_type' order by id   limit ".$limit." ");
                            foreach ($query->result() as $key => $value){
                              $id=$value->id;
                              $type=$value->type;
                              $date=$value->date;
                              $cash=$value->cash_pay;
                              $bank=$value->bank_card;
                              $ticket=$value->cheque_pay;
                              $total=$value->total;
                              $cash_sale+=$value->cash_pay;
                              $bank_card+=$value->bank_card;
                              $ticket_b+=$value->cheque_pay;
                              $total_value+=$value->total;
                              ?>
                               <tr class="delete_mem<?php echo $id; ?>">
                                <td><input type="checkbox" id="chk" name="selected_item" value="<?php echo ($id) ;?>" /></td>
                                <td><?php echo  date('d F,Y',strtotime($date)) ;?></td>
                                <td> <?php echo  ucfirst($type) ;?></td>
                                <td><?php echo   number_format($cash,2)  ;?>€</td>
                                <td class="center"><?php echo  number_format($bank,2) ;?>€</td>
                                <td class="center"><?php echo  number_format($ticket,2) ;?>€</td>
                                <td class="center"><?php echo number_format($total,2) ;?>€</td>
                                <td><a class="btn"   onclick="edit(<?php echo $id; ?>)">Edit</a> <a class="btn btn-danger btn-xs" onclick="smyFunction(<?php echo $id; ?>)" >Remove</a></td> 
                              </tr>
                              <?php }?>
                              <tr class="total-value">
                                <td>
                                </td>
                                <td></td>
                                <td class="total-val">Total</td>
                                <td><?php echo number_format($cash_sale,2) ;?>€</td>
                                <td><?php echo  number_format($bank_card,2) ; ?>€</td>
                                <td><?php echo  number_format($ticket_b,2) ;?>€</td>
                                <td><?php echo  number_format($total_value,2) ;?>€</td>
                                <td></td>
                              </tr>
                            </tbody>
                          </table>
                          <?php echo  '</div>';                                 
                        }    
                        ?>
                      </div>
                    </div>
                    
                   
                     
                    <div class="modal fade" id="myModal" role="dialog">
                      <div class="modal-dialog">
                        <!-- Modal content-->
                        <div class="modal-content">
                          <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Expense</h4>
                          </div>
                          <div class="modal-body">
                            <form method="post" id="form" action="<?php echo site_url('expense/value');?>">
                              <input type="hidden" name="id" value="<?php echo $this->session->userdata('user_id'); ?>">
                              <div class="form-group">
                                <label for="usr" >Date</label>
                                <div class="input-group date form_datetime col-md-5" data-date="1979-09-16T05:25:07Z" data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1">
                                  <input class="form-control" size="16" type="date" name="date" value="<?php echo date("Y-m-d"); ?>" >
                                </div>
                                <input type="hidden" id="dtp_input1" value="" /><br/>
                              </div>
                              <label for="usr">Type:</label>
                             
                              <div id="autoUpdate" class="autoUpdate">
                                <div class="form-group type ">
                                  
                                  <select name="select" class="form-control select "  >
                                    <option id="sel"></option>
                                    <?php  $type=$this->db->query("select type from expense_type  where buisness_type='$b_type' 
                                      ");
                                    $data='';
                                    foreach ($type->result() as $key => $type){
                                      echo 
                                      '<option value="'.$type->type.'">'.$type->type.'</option>';
                                    }
                                    ?> 
                                  </select>
                                 
                                  
                                </div>
                              </div>
                            
                              <div class="grop">
                                <br>
                                
                              </div>
                              <div class="form-group cash">
                                <label for="pwd">Cash Pay:</label>
                                <input type="number" class="form-control" name="cash" value="00" id='Resources' onKeyPress="Calculate()" onKeyUp="Calculate()">
                              </div>
                              <div class="form-group bank">
                                <label for="pwd">Bank Card:</label>
                                <input type="number" class="form-control" name="bank" value="00" id='Minutes' onKeyPress="Calculate()" onKeyUp="Calculate()">
                              </div>
                              <div class="form-group ticket">
                                <label for="pwd">Cheque Pay:</label>
                                <input type="number" class="form-control" name="ticket" value="00" id='ticket' onKeyPress="Calculate()" onKeyUp="Calculate()">
                              </div>
                              <div class="form-group total">
                                <label for="pwd">Total:</label>
                                <input type="hidden" class="form-control" name="edit_id" id='id'>
                                <input type="hidden" class="form-control" name="total" id='total'>
                                <label  class="form-control" name="permin" value="00" id='answer'></label>
                              </div>
                              
                              <input type="submit"  class="btn btn-success" value="submit">
                            </form>
                          </div>
                          <div class="modal-footer">
                            <button type="button"  class="btn btn-danger" data-dismiss="modal">Close</button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="pagi" style ="float:right;">
           <?php echo $this->pagination->create_links(); ?>   
          </div>
          
          <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/jquery-1.8.3.min.js" charset="UTF-8"></script>
          <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap.min.js"></script>
          <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/my.js"></script>
          <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap-datetimepicker.fr.js" charset="UTF-8"></script>
          <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
     <script>
       $('#submit').click(function(){
      if($('#from').val() == '' && $('#to').val() == ''  ){
         swal({
          title: "Please Select a Date Range First!",
         
          type: "warning",
         
          confirmButtonText: 'ok!'
        });
        return false;
         }
         });
    </script>
   <script>
   function myFunction(ref) {
                var datastr=ref;
     
            $.ajax({
            url: "<?php echo base_url()?>expense/delete", 
            type: "POST",
            data    : {'uristring':datastr},
                 dataType: 'json',
            success: function(data){
            $(".delete_mem" + ref).fadeOut('slow');
                $('#cash').text(data.cash +'€');
               $('#totalbank').text(data.bank +'€');
                $('#totalticket').text(data.ticket +'€');
                $('#totalsale').text(data.total +'€');
      
            }
        });    
}
            
</script>
<script>
 
</script>
<script>
  $(document).ready(function () {
    $('#checkbox1').change(function () {
      if (!this.checked) 
//  ^
$('#autoUpdate').fadeIn('slow');
else 
  $('#autoUpdate').fadeOut('slow');
});
  });
</script>

<script>
     function dmycheck() {
          $('#reset').click(function () {
    if (!$('#chk').is(':checked')) {
        swal({
          title: "Please Select a Record Only!",
         
          type: "warning",
         
          confirmButtonText: 'ok!'
        });
        return false;
    }
               else{
                   swal({
          title: "Are you sure?",
          text: "You will not be able to recover this imaginary file!",
          type: "warning",
          showCancelButton: true,
          confirmButtonClass: 'btn-danger',
          confirmButtonText: 'Yes, delete it!',
          closeOnConfirm: false,
        },
        function(){
                                                     var fav = [];
 $.each($("input[name='selected_item']:checked"), function(){ 
          
   myFunction($(this).val());

 
}); 
          swal("Deleted!", "Your imaginary file has been deleted!", "success");
                       
        });
                           
               }       
});
     
    }

    </script>
    <script>
          function smyFunction(ref) {
        swal({
          
  title: "Are you sure?",
  text: "Your will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonClass: "btn-danger",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
},
function(){
              var datastr=ref;
     
            $.ajax({
            url: "<?php echo base_url()?>expense/delete", 
            type: "POST",
            dataType: 'json',
            data    : {'uristring':datastr},
               
           success: function(data){
               
       
               $('#cash').text(data.cash +'€');
               $('#totalbank').text(data.bank +'€');
                $('#totalticket').text(data.ticket +'€');
                $('#totalsale').text(data.total +'€');
                //$('#answer').html(data.total);
                //$('#id').val(data.id);
  }
        });
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
          $(".delete_mem" + ref).fadeOut('slow');
           //$('#totalcash').val(data.cash);
           
           
    
});
 }
    </script>
    <script>
          function edit(ref) {
                var datastr=ref;
            $.ajax({
            url: "<?php echo base_url()?>expense/show", 
            type: "POST",
            data    : {'uristring':datastr},
                dataType: 'json',
            success: function(data){
              
                $('#sel').text(data.type);
               $('#Resources').val(data.cash);
               $('#Minutes').val(data.bank);
                $('#ticket').val(data.ticket);
                $('#total').val(data.total);
                $('#answer').html(data.total);
                $('#id').val(data.id);
              
            }
        });    
}
    </script>
    <script>
        function updateresult(){
             //alert("hello");
            $.ajax({
            url: "<?php echo base_url()?>user/delete", 
           // type: "POST",
            dataType: 'json',
            success: function(data){
              
                $('#totalcash').val(data.cash);
               // $('#totalcash').val(data.ticket);
               //$('#Resources').val(data.cash);
               //$('#Minutes').val(data.bank);
               // $('#ticket').val(data.ticket);
               // $('#total').val(data.total);
                //$('#answer').html(data.total);
                //$('#id').val(data.id);
              
            }
        });    
}
        }
    </script>
   
   
</body>
</html>