

<link href="<?php echo base_url(); ?>assets/autocomplete/jquery-ui.css" rel="stylesheet">
    <link href="<?php echo base_url(); ?>assets/autocomplete/ui.theme.css" rel="stylesheet">
    <link href="<?php echo base_url(); ?>assets/autocomplete/autocomplete.css" rel="stylesheet">
	
 <link href="<?php echo base_url(); ?>assets/select/chosen.min.css" rel="stylesheet" media="screen">
	
		   
		   		   <script src="<?php echo base_url(); ?>assets/select/chosen.jquery.min.js"></script>

				   
         <script type="text/javascript">
  	
	 $(document).ready(function() { 
				$("#baddebt").hide();
               $("#bankname").show();
        	$("#transactioncode").hide();

					});
					
			
		         $(function () {
        $("#name").change(function () {
            if ($(this).val() == "payment") {
            
			$("#baddebt").hide();
               $("#bankname").show();
              		 
              

            } else if ($(this).val() == "baddebt") {
            
		 $("#baddebt").show();
               $("#bankname").hide();
		 
              

            }
			
			else {
               
			$("#baddebt").hide();

            
			
            }
        });
    });
		
		
	
		
		
		
		
		         $(function () {
        $("#method").change(function () {
            if ($(this).val() == "mpesa") {
            			$("#transactioncode").show();

        
		
            } else if ($(this).val() == "cheque") {
            
            			$("#transactioncode").show();

              

            }  else if ($(this).val() == "card") {
            
            			$("#transactioncode").show();

              

            }
			
			else {
               
			$("#transactioncode").hide();

            
			
            }
        });
    });
			
		
			
		 $(document).on("click", "#addgl", function()
        {
          var itemid = $(this).data('id');
		  $("#itemid2").val(itemid); 
		  
		   var invoice = $(this).data('invoice');
		  $("#invoice2").val(invoice); 
		   
        });
			
		
		 $(document).on("click", "#managebtndelete", function()
        {
          var itemid = $(this).data('id');
		  $("#itemid1").val(itemid); 
		  
		   var mybalance = $(this).data('balance');
		  $("#mybalance").val(mybalance); 
		   
        });
				
 $(this).ready( function() {  
            $("#phone").autocomplete({  
 
                minLength: 1,  
                source:   
                function(req, add){  
                    $.ajax({  
                        url: "<?php echo base_url(); ?>Systemusers/branchlookupmpesadebts",  
                        dataType: 'json',  
                        type: 'POST',  
                        data: req,  
                        success:      
                        function(data){  
                            if(data.response =="true"){  
                                add(data.message);  
                                console.log(data);
                            }  
                        },  
                    });  
                },  
                     
            });  
        });  
		
		var util = { };

document.addEventListener('keydown', function(e){

    var key = util.key[e.which];
    if( key ){
        e.preventDefault();
    }
  if( key === 'F1' ){        
      $("#id").focus();
    }
    if( key === 'F2' ){        
      $("#qty").focus();
	   $("#qty").focus().value = "";
	  document.getElementById("qty").value = "";
    }
	 if( key === 'F3' ){        
      $("#cash").focus();
	   $("#cash").focus().value = "";
	  document.getElementById("cash").value = "";
    }
	 if( key === 'F4' ){        
      $("#mpesa").focus();
    }
	 if( key === 'F5' ){        
      $("#card").focus();
    }
	 if( key === 'F6' ){        
      $("#credit").focus();
    }
	 if( key === 'F7' ){        
      $("#voucher").focus();
    }
	 if( key === 'F8' ){        
      $("#cheque").focus();
    }
	 if( key === 'F9' ){        
      $("#points").focus();
    }
	if( key === 'F10' ){        
      	  	  document.getElementById("phone").value = "";
$("#phone").focus();

	  	  document.getElementById("phone").value = "2547";

    }
		 if( key === 'right' ){        
      $("#btn_dontprint").focus();
    }
		 if( key === 'left' ){        
      $("#btn_savesales").focus();
    }
	
	if( key === 'alt' ){        
      $("#cardnumber").focus();
    }
	
	
	

	 if( key === 'enter' ){        
return true;
    }
	
})


  
util.key = { 
  9: "tab",
  //13: "enter",
  16: "shift",
  18: "alt",
  27: "esc",
  33: "rePag",
  34: "avPag",
  35: "end",
  36: "home",
  37: "left",
  38: "up",
  39: "right",
  40: "down",
  112: "F1",
  113: "F2",
  114: "F3",
  115: "F4",
  116: "F5",
  117: "F6",
  118: "F7",
  119: "F8",
  120: "F9",
  121: "F10",
  122: "F11",
  123: "F12"
}
		
    </script>   

<script>
	function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

    // document.body.innerHTML = printContents.fontsize(12);
     window.print();

	 if( window.print()) {
         window.location.href = "../user/paybill";
		alert(" printing...........");
    }else {

	
		      return      window.location.href = "../user/paybill";

		    // document.body.innerHTML = originalContents;

	}
}
	</script>

         <script type="text/javascript">
  
		
		 $(document).on("click", "#managebtndelete", function()
        {
          var itemid = $(this).data('id');
		  $("#itemid1").val(itemid); 
		  
		  
		   
        });
		
		
		
    </script>   
  <style type="text/css">
  
  

    #myprint { display: none; }
#print{  	


    background-repeat: no-repeat;
	font-size;20px;
  
    width:100%; 


}

#sale{  	


    background-repeat: no-repeat;
  
    width:100px; height:50px;


}
    @media print
    {
    	#non-printable { display: none; }
    	#printableArea { display: block; font-size:16; width:100%; }
		    	#myprint { display: block; font-size:16; width:100%; }

    }
    </style>
 

         <script type="text/javascript">
  
		
		 $(document).on("click", "#managebtndelete", function()
        {
          var itemid = $(this).data('id');
		  $("#itemid1").val(itemid); 
		  
		  
		   
        });
			$(document).on("click", "#paybtn", function()
        {
          var billid = $(this).data('id');
		  $("#billid").val(billid); 
		   
		     var amount = $(this).data('amount');
		   $("#amount1").val(amount); 
		      var 	paid = $(this).data('paid');
		   $("#paid1").val(paid); 
   var invoice = $(this).data('invoice');
		   $("#invoice1").val(invoice); 
		   
		   
		     var balance = $(this).data('balance');
		   $("#balance1").val(balance); 
		 
		  
		  
		   
        });
		
		
    </script>  

 
 <div class="row">
  
   	

   	<div class="col-md-12">
	
  
		<div class="panel panel-default" data-collapsed="0">
        	<div class="panel-heading" id="non-printable">
            	<div class="panel-title" id="redcolor" >
            		<i class="entypo-plus-circled"></i>
						 <span class="pull-right"> View paid bills</span>					                    
				 <a href="<?php echo base_url();?>user/receivebill" class="btn btn-default">All Bills</a>
				 <a href="<?php echo base_url();?>user/paybill" class="btn btn-default"> Pending Bills</a>
				 <a href="<?php echo base_url();?>user/viewpurchases" class="btn btn-default"> Pay Supplier </a>

	  	  				 <a href="<?php echo base_url();?>user/viewexpenses" class="btn btn-default"> View Expense</a>
	  	  				 <a href="<?php echo base_url();?>user/receivebill" class="btn btn-default"> Add bill/Supplier invoice</a>
 <a href="<?php echo base_url();?>user/allinvoices" class="btn btn-default"> All Invoices</a>
	  	  				 <a href="<?php echo base_url();?>user/incoicepayments" class="btn btn-default"> Receive Payments</a>

				
				 <div class="pull-right" id="non-printable" >
<form name="print" method="post" action="">
 <input type="submit" id="print"  accesskey="p" class="btn btn-default"  name="print"onclick="printDiv('sortable_table')" value="print all payments!" />
</form></div>
            	</div>
            </div>
			<div class="panel-body">

				<div class="widget-content nopadding table_holder table-responsive" >				
	<?PHP
	
	$session_data = $this->session->userdata('logged_in');

  
 	$data['id'] = $session_data['id'];
        
		 $userid = $data['id'];
		 $data['companyid'] = $session_data['companyid'];
	 $companyid = $data['companyid'];
					 
					 
					 
					  $data['usertype'] = $session_data['usertype'];
	 $usertype = $data['usertype'];	
					 
					 
					 
					 
					 
					 
      $mydate=date("Y-m-d H:i:s");

 echo $this->session->flashdata('deletemsg'); 

    $updateclosedinvoiceaction=$this->usermodel->populatuserpermissionsdataarray('purchases','updateclosedinvoice',$userid);

				echo $this->session->flashdata('msg'); 
								?>	
										
					<div class="row">
						<div class="col-md-12">
							
							<div class="panel-body">
	
									<h3><CENTER>PAYMENTS FOR THE  BILL NO: <?php echo $main[0]['id'] ; ?></CENTER></h3>
									
				<table class="tablesorter table table-bordered  table-hover" id="sortable_table">			

             <thead>
         <tr>
		     <th>	 Bill No</th>
		    <th>	 Original Amount</th>
			  <th>	 Paid Amount</th>
			  	<th>	 Balance</th>
	  <th>	 Method</th>
    	         <th>	Description</th>
			          <th>	Payment Date</th>
					  			          <th>	Code</th>

		<th id="non-printable">Print</th>

                 </tr>
             </thead>
<?php

 if(!is_array($viecollection)) 

 {


   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='5'>";
    
       echo '<div class="alert alert-warning text-center">There is no payment for the selected  bills ..</div>';
    echo "</td>";
   echo "</tr>";
    }else {
?>
    
             <?php 
  $total=0;
  	foreach ($viecollection as $c): 

		 

             ?>

							<?php
					
		
                 echo form_open('user/editsupplierpayment' , array('class' => 'form-horizontal form-groups-bordered validate', 'enctype' => 'multipart/form-data'));
				 if(validation_errors()):?>
<div class='alert alert-danger text-center'><?php echo validation_errors(); ?></div>
<?php endif;?>								
			

             <tr>
                
            <?php 
$id=$c->id;


  $total=$total+$c->amount;
 $invoice_number=$c->invoice;

?>
 <td><?php echo $main[0]['id'] ; ?></td> 
 <td>
 <?php echo $main[0]['amount'] ; ?>
 </td>
 <td>
   					<input type="hidden" name="id" id="id" value="<?php echo set_value('id',$c->id); ?>" class="form-control" required placeholder="invoice " >

  					<input type="hidden" name="invoice" id="invoice" value="<?php echo set_value('invoice',$c->invoice); ?>" class="form-control" required placeholder="invoice " >

 					<input type="text" name="amount" id="amount" value="<?php echo set_value('amount',$c->amount); ?>" class="form-control" required placeholder="amount " >
</td>
 <td>
 					<input type="text" name="balance" id="balance" value="<?php echo set_value('balance',$c->balance); ?>" class="form-control" required placeholder=" balance" >
</td>


	  <td>
			 <?php  $paymentmethodesid= $c->paymentmethodes ;
	 $mypaymentmethodes = $this->usermodel->populateallarray('glaccounts','number',$paymentmethodesid);

   //echo $mypaymentmethodes[0]['accountname'];
			  ?>
			  
			  
			  <select class="form-control" name="bankid" required>
	
	<option value=""  >---Select bank ---</option>
	
	<?php 
	
			$banking= $this->usermodel->populatealldatanoarray('glaccounts','companyid',$companyid,'number',$paymentmethodesid);

										
if(is_array($banking)) {

   foreach($banking as $bankingrow){

   //echo $courserow->coursename;
    ?>
		<option selected="selected" value="<?=  $bankingrow->number;   ?>"><?=  $bankingrow->accountname ."(Acc No:". $bankingrow->number.")"; ?></option>



	<?php

}} else {
    echo 'No bank available.';
}
	
	
	
	$allbanking= $this->usermodel->populatealldatanoarray('glaccounts','companyid',$companyid,'accounttype<>','1');

										
if(is_array($allbanking)) {

   foreach($allbanking as $allbankingrow){

   //echo $courserow->coursename;
    ?>
		<option value="<?=  $allbankingrow->number;   ?>"><?=  $allbankingrow->accountname ."(Acc No:". $allbankingrow->number .")"; ?></option>



	<?php

}} else {
    echo 'No bank available.';
}
	
	
	
	
	
	
?>
</select>
		
			  
			  
			  
			  
			  
			  
			  
			  
			</td>
			
			
 <td>
 					<input type="text" name="remarks" id="remarks" value="<?php echo set_value('remarks',$c->remarks); ?>" class="form-control" required placeholder=" remarks" >
</td>
 <td>
 					<input type="text" name="paiddate" id="paiddate" value="<?php echo set_value('paiddate',$c->paiddate); ?>" class="form-control" required placeholder=" paiddate" >
</td>

	  	 
	 <td>
	  					<input type="text" name="transactioncode"  value="<?php echo set_value('transactioncode',$c->transactioncode); ?>" class="form-control" required placeholder=" transactioncode" >

</td>  
		


 
 
                  <td> 

 
 
					<div class="btn-group">
                                    <button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown">
                                        More <span class="caret"></span>
                                    </button>
									<?php
										if($usertype=="admin"){
											
											?>
                                    <ul class="dropdown-menu dropdown-primary pull-right" role="menu">
                                        
                                        <!-- teacher EDITING LINK
                                      
										
											  <li> 
										 <a class="btn btn-danger managebtn" id="managebtndelete"		 data-id='<?php echo  $c->id;?>'    data-toggle='modal' data-target='#deleteModel<?php  $c->id; ?>'>	 


                                                <i class="glyphicon glyphicon-pencil"></i>
																Delete
                                                </a>
                                                        </li>
														 -->
														

										<?php
										}
										?>
									
                                        <li class="divider"></li>
                                        
  <button type="submit"  value="myedit" name="btn_editpaymentsdone" class="btn btn-info"> <span class="glyphicon glyphicon-pencil"></span> </a>   </button>
                                        <li class="divider"></li>
										
											  	  	  	 <a  id="non-printable" class="btn btn-default"  href="<?php echo base_url();?>user/printbillpayments?randomitemidentifier=<?php   {echo  $invoice_number;}  ?>&myid=<?php   {echo  $id;}  ?>">Print  </a>
                                        <li class="divider"></li>

											  	  	  	 <a  id="non-printable" class="btn btn-default"  href="<?php echo base_url();?>user/printbillpaymentsbulk?randomitemidentifier=<?php   {echo  $invoice_number;}  ?>&myid=<?php   {echo  $id;}  ?>&transactioncode=<?php   {echo  $c->transactioncode;}  ?>">Print  Bulk </a>
                                        <li class="divider"></li>


                                            <li> 
				<a  id="more" class=" btn-success"  href="<?php echo base_url();?>user/searchJE?searchterm=<?php   {echo  $c->invoice;}  ?> ">
								 
																							 

                                                <i class="glyphicon glyphicon-pencil"></i>
															View	GL Transactions
                                                </a>
																		
										</li>
										
										                                        <li class="divider"></li>

										
										
														  <li> 
										 <a class="btn btn-warning addgl" id="addgl"		 data-id='<?php echo  $c->id;?>'   data-invoice='<?php echo  $c->invoice;?>'   data-toggle='modal' data-target='#addglModel<?php  $c->id; ?>'>	 


                                                <i class="glyphicon glyphicon-pencil"></i>
																Crete New GL For this payment
                                                </a>
                                                        </li>

                                    </ul>
										
                                </div>
</td> 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
		

             </tr>
 <?php echo form_close();?>	
	<?php endforeach;} ?>
<tr>

<td colspan="3">
<h2>
Total Paid Amount
</h2>
</td>
<td colspan="10">
<h2>
<?php 
if(isset($total)) {echo $total; }


?>
</td>

</h2>


</tr>
           

        </table>

						
									
					</div>	

					
					
					
					
					
					
					
					
					
					<div id="payModel" class="modal fade" 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">Pay this bill</h4>
      </div>
      <div class="modal-body">
      
	  	

				<?PHP
//$session_data = $this->session->userdata('logged_in');



				echo $this->session->flashdata('msg'); 
		
                 echo form_open('user/updatebillpayment' , array('class' => 'form-horizontal form-groups-bordered validate', 'enctype' => 'multipart/form-data'));
				 if(validation_errors()):?>
<div class='alert alert-danger text-center'><?php echo validation_errors(); ?></div>
<?php endif;?>
	<input type="hidden" name="billid" value=""id="billid" class="form-control" required placeholder="Enter billid">
			  	<input type="hidden" name="invoice" value=""id="invoice1" class="form-control" required placeholder="Enter invoice">
		
	<div class="form-group">
    <label for="inputpayment" class="col-sm-2 control-label">	Amount To pay </label>
    <div class="col-sm-10">
	
		  		<input type="text" name="payment" value="<?php echo set_value('payment'); ?>" id="payment" class="form-control" required placeholder="Enter payment">
		 
				
		<span class="text-danger"> <?php echo form_error('payment'); ?>
		
		</span>
	</div>
  </div>
 
 
  	
	<div class="form-group">
    <label for="inputdescription" class="col-sm-2 control-label">	Description</label>
    <div class="col-sm-10">

				<textarea name="description" value="<?php echo set_value('description'); ?>"  id="description" class="form-control"  placeholder="Enter description"></textarea>
			
		<span class="text-danger"> <?php echo form_error('description'); ?>
		
		</span>
	</div>
  </div>
			<div class="form-group">
    <label for="inputamount" class="col-sm-2 control-label">Original Amount </label>
    <div class="col-sm-10">
	
		  		<input type="text" readonly name="amount" value="<?php echo set_value('amount'); ?>" id="amount1" class="form-control" required placeholder="Enter amount">
		 
				
		<span class="text-danger"> <?php echo form_error('amount'); ?>
		
		</span>
	</div>
  </div>
	<div class="form-group">
    <label for="inputpaid" class="col-sm-2 control-label">Paid Amount </label>
    <div class="col-sm-10">
	
		  		<input type="text" readonly name="paid" value="<?php echo set_value('amount'); ?>" id="paid1" class="form-control" required placeholder="Enter paid">
		 
				
		<span class="text-danger"> <?php echo form_error('paid'); ?>
		
		</span>
	</div>
  </div>
	
<div class="form-group">
    <label for="inputbalance" class="col-sm-2 control-label">Balance Due </label>
    <div class="col-sm-10">
	
		  		<input type="text" readonly name="balance" value="<?php echo set_value('balance'); ?>" id="balance1" class="form-control" required placeholder="Enter balance">
		 
				
		<span class="text-danger"> <?php echo form_error('balance'); ?>
		
		</span>
	</div>
  </div>
	
	 <div class="form-group">

    <div class="col-sm-offset-2 col-sm-10">
     
	         <button type="submit"  value="bill" name="btn_paymybill" class="btn btn-default"> Make payment  </button>

    </div>
  </div>
  					<?php echo form_close();?>
			</div>							   
				
									
							
								
			<div class="modal-footer">

    

				</div>

		</div>
	    </div>
    </div>
		<div id="deleteModel" class="modal fade" 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">Delete</h4>
      </div>
      <div class="modal-body">
        <h3>Are you sure you want to delete this item? </h3>
      </div>
      <div class="modal-footer">
	  	

     
		<?PHP
				$session_data = $this->session->userdata('logged_in');
 echo $this->session->flashdata('deletemsg'); 

		
                 echo form_open('user/deletebyid' , array('class' => 'form-horizontal ', 'enctype' => 'multipart/form-data'));
				 if(validation_errors()):?>
<div class='alert alert-danger text-center'><?php echo validation_errors(); ?></div>
<?php endif;?>
 <input type="hidden" id="table"name="table" class="table" placeholder="table" value="collection">

 <input type="hidden" id="view"name="view" class="view" placeholder="view" value="user/viewbalance?invoice=<?php echo$itemid ;?>">
 <input type="hidden" id="itemid1"name="itemid" class="itemid" placeholder="itemid" value=""><br>
 <button type="button" class="btn btn-success" data-dismiss="modal">No</button>
 		  	         <button type="submit" name="btn_delete" value="deletebyid" class="btn btn-danger"> Delete  </button>

	<?php echo form_close();?>
      </div>
    </div>

  </div>

  </div>		
	
	
	
	
	
	
	
	
	<div id="addglModel" class="modal fade" 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">Create New Gl</h4>
      </div>
      <div class="modal-body">
        <h3>Are you sure you want to pass new Journal Entry for this payment? </h3>
      </div>
      <div class="modal-footer">
	  	

     
		<?PHP
				$session_data = $this->session->userdata('logged_in');
 echo $this->session->flashdata('deletemsg'); 

		
                 echo form_open('user/createnewpaymentgl' , array('class' => 'form-horizontal ', 'enctype' => 'multipart/form-data'));
				 if(validation_errors()):?>
<div class='alert alert-danger text-center'><?php echo validation_errors(); ?></div>
<?php endif;?>
 <input type="hidden" id="invoice2"name="invoice" class="invoice" placeholder="invoice" value="collection">
 <input type="hidden" id="category"name="category" class="category" placeholder="category" value="paid"><br>

 <input type="hidden" id="view"name="view" class="view" placeholder="view" value="user/viewbalance?invoice=<?php echo$itemid ;?>">
 <input type="hidden" id="itemid2"name="itemid" class="itemid" placeholder="itemid" value=""><br>
 <button type="button" class="btn btn-success" data-dismiss="modal">No</button>
 		  	         <button type="submit" name="btn_delete" value="deletebyid" class="btn btn-warning"> Yes  </button>

	<?php echo form_close();?>
      </div>
    </div>

  </div>

  </div>		
	
			


					
									
									
							</div>		
									
							</div>
						</div>
				

			
			</div>
		</div>
  
  </div>
			
			
			
		
		
  

 
</div>


















