<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">
	 <script src="<?php echo base_url(); ?>assets/autocomplete/jquery.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/autocomplete/jquery-ui.min.js"></script>
	


 
            
        <script type="text/javascript">  
		
			   $(this).ready( function() {  
            $("#user2").autocomplete({  
 
                minLength: 1,  
                source:   
                function(req, add){  
                    $.ajax({  
                        url: "<?php echo base_url(); ?>Systemusers/customerSaleslookup",  
                        dataType: 'json',  
                        type: 'POST',  
                        data: req,  
                        success:      
                        function(data){  
                            if(data.response =="true"){  
                                add(data.message);  
                                console.log(data);
                            }  
                        },  
                    });  
                },  
                     
            });  
        });
		
		$(this).ready( function() {  
            $("#user3").autocomplete({  
 
                minLength: 1,  
                source:   
                function(req, add){  
                    $.ajax({  
                        url: "<?php echo base_url(); ?>Systemusers/customerSaleslookup",  
                        dataType: 'json',  
                        type: 'POST',  
                        data: req,  
                        success:      
                        function(data){  
                            if(data.response =="true"){  
                                add(data.message);  
                                console.log(data);
                            }  
                        },  
                    });  
                },  
                     
            });  
        });
		
		$(this).ready( function() {  
            $("#mysupplier").autocomplete({  
 
                minLength: 1,  
                source:   
                function(req, add){  
                    $.ajax({  
                        url: "<?php echo base_url(); ?>Systemusers/customerSaleslookup",  
                        dataType: 'json',  
                        type: 'POST',  
                        data: req,  
                        success:      
                        function(data){  
                            if(data.response =="true"){  
                                add(data.message);  
                                console.log(data);
                            }  
                        },  
                    });  
                },  
                     
            });  
        });
		
		
		
		   $(this).ready( function() {  
            $("#id").autocomplete({  
 
                minLength: 1,  
                source:   
                function(req, add){  
                    $.ajax({  
                        url: "<?php echo base_url(); ?>Systemusers/purchaselookup",  
                        dataType: 'json',  
                        type: 'POST',  
                        data: req,  
                        success:      
                        function(data){  
                            if(data.response =="true"){  
                                add(data.message);  
                                console.log(data);
                            }  
                        },  
                    });  
                },  
                     
            });  
        });
		
		
		 $(document).on("click", "#managebtndelete", function()
        {
          var itemid = $(this).data('id');
		  $("#itemid1").val(itemid); 
		  
		  
		   
        });
		
		
		
        </script>  


 
 <div class="row">
  
   	 
<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/viewallstatements";
		alert(" printing...........");
    }else {

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

		    // document.body.innerHTML = originalContents;

	}
}
	</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>

   	<div class="col-md-12">

  
		<div class="panel panel-default" data-collapsed="0">
        	<div class="panel-heading">
            	<div class="panel-title" id="redcolor" >
            		<i class="entypo-plus-circled"></i>

 <a id="non-printable" href="<?php echo base_url();?>user/startsales" class="btn btn-info">  Go back</a>	

			 <a data-toggle='modal' data-target='#statementModal' class="btn btn-info" id="non-printable"> 	Supplier & Customer Statement</a>


<div class="pull-right" id="non-printable">
			 
<form name="print" method="post" action="">
 <input type="submit" id="print"  accesskey="p" class="btn btn-primary"  name="print"onclick="printDiv('printableArea')" value="Print" />
</form></div>
 

 

				
            	</div>
            </div>
			<div class="panel-body">

			
	<div class="widget-content nopadding table_holder table-responsive">				


<?PHP

 echo $this->session->flashdata('imagemsg1'); 
echo $this->session->flashdata('deletemsg'); 
	 $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'];	
	    $addproductaction=$this->usermodel->populatuserpermissionsdataarray('reports','add',$userid);

	 $updateaction=$this->usermodel->populatuserpermissionsdataarray('reports','update',$userid);
  $updatebuyingpriceaction=$this->usermodel->populatuserpermissionsdataarray('reports','update',$userid);
  $deleteaction=$this->usermodel->populatuserpermissionsdataarray('reports','delete',$userid);
	   $viewallaction=$this->usermodel->populatuserpermissionsdataarray('reports','viewall',$userid);


				echo $this->session->flashdata('msg'); 
   $userid=$data['id'];
 //$finalendate=date("Y-m-d H:59:59");
 // $finalstartdate=date("Y-m-d 00:00:00");
  
  
  
	 	 $smsconfig = $this->usermodel->populateallarray('smsconfig','id',$companyid);
		 		$currenttime = date('H:i');

		 
		  $year=date("Y");
		  $month=date("m");
		   $day=date("d");
		   
		   $subtract=$day-1;
		   $add=$day+1;
		        if ($subtract<10){
			  $subtract="0".$subtract;
		   }else{
			   $subtract=$subtract; 
		   }
		$finalstartdate="";
		$finalendate="";
		  $myfromdate=$smsconfig[0]['fromdate'];
		 $mytodate=$smsconfig[0]['todate'];
		 $starttime=$smsconfig[0]['starttime'];
		 $duetime=$smsconfig[0]['duetime'];
		 //echo $myfromdate;
		if(($myfromdate=="Current") &&($mytodate=="Current") &&  ($currenttime>=$starttime)){
	
		  $finalstartdate=$year.'-'.$month.'-'.$day.' '. $starttime.':00';
		
		 }else if(($myfromdate=="Current") &&($mytodate=="Next") &&  ($currenttime>=$starttime)){
	
		  $finalstartdate=$year.'-'.$month.'-'.$day.' '. $starttime.':00';
		
		 }
		 
		  else if(($myfromdate=="Current") &&($mytodate=="Current") && ($currenttime<$starttime)){
					  $finalstartdate=$year.'-'.$month.'-'.$day.' '. $starttime.':00';


				//	echo $finalstartdate;
		 }
		 
		 else if(($myfromdate=="Current") &&($mytodate=="Next") && ($currenttime<$starttime)){
						 if(($day=="01") || ($day=="1")){
				 
				 $currentdate=$year.'-'.$month.'-'.$day;

 $mynewfromdate = strtotime ( '-1 month' , strtotime ( $currentdate ) ) ;
$newfromdate = date ( 'Y-m-d' , $mynewfromdate );
$finalstartdate = date("Y-m-t", strtotime($newfromdate)).' '.$starttime.':00';
			 }else{
			
								  $finalstartdate=$year.'-'.$month.'-'.$subtract.' '. $starttime.':00';

			
			 }
	
		 }



		
		 
		 
		if(($mytodate=="Next") && ($currenttime>=$duetime)){
	
		  $finalendate=$year.'-'.$month.'-'.$add.' '. $duetime.':00';
				//echo $finalstartdate;
		
		 }else if(($mytodate=="Next") && ($currenttime<$duetime)){
			
		  $finalendate=$year.'-'.$month.'-'.$day.' '. $duetime.':00';
		  
				//	echo $finalstartdate;
		 }else  if(($mytodate=="Current") && ($currenttime>=$duetime)){
		 		  $finalendate=$year.'-'.$month.'-'.$day.' '. $duetime.':00';

		 }else  if(($mytodate=="Current") && ($currenttime<$duetime)){
					  $finalendate=$year.'-'.$month.'-'.$day.' '. $duetime.':00';

		  
				//	echo $finalstartdate;
		 }
		 
		 
		 
  
  
  
  
  
  
  
  
  
     $mydate=date("Y-m-d");

  
  
   //$finalendate=date("Y-m-d H:i:s");
 
   $myuser=$this->usermodel->geteuser($userid);
   $myname=$myuser[0]['firstname'];
   function formatMoney($number, $fractional=false) {
						if ($fractional) {
							$number = sprintf('%.2f', $number);
						}
						while (true) {
							$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
							if ($replaced != $number) {
								$number = $replaced;
							} else {
								break;
							}
						}
						return $number;
					}	
?>

 
					<div class="row">
						<div class="col-md-12">
							
							<div class="panel-body">
	
								<table border="0" width="100%" id="printableArea">
				
						<tr>
						<td colspan="3" style="border-radius:4px;margin:0px  ">
					
<?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'];	

								$profile = $this->usermodel->populateall('smsconfig','id',$companyid);
	   $currentdate = date("d/m/Y");
	
		if(!is_array($profile)) 

 {
	 $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a><strong>Error!</strong>Upload photo</div>');
							

    }else {

    
          
  	foreach ($profile as $c): 
$photo= $c->logo;
		 	if($photo!=""){
		 
		 
		
		
	
			
		
		$img=$c->logo;
	
		$img1='uploads/';
	$url = base_url().$img1.$img;

 
 ?>
 <div >
<img class="img-circle" src="<?php echo $url ?>" height="100" width="30%" style="border-radius:4px;margin:0px  ">		 

</div>



	 <?php }else{
		 ?>

				<?php
	 }
		 endforeach; ?>

              <?php }?>
					
				
						<?php $description=$configuration[0]['description']; if(isset($description)) {echo   $description; }?>
							
					
					
						</td>
						
						<td colspan="2" >
							<h2 style="margin:0px;"> Supplier Statement 	</h2>
				Printed Date:  <?php echo$currentdate;?><BR>
				Supplier  Id: <?php echo$viewreceipt[0]['id'];?><BR>
				From: <?php echo$startdate;?><BR>
				To  : <?php echo$enddate;?><BR>

						
						Prepared by:  <?php 
						
							$user=$this->usermodel->populateallarray('people','id', $userid);
						echo$names=$user[0]['firstname']. " ".$user[0]['surname'] ;
						
						
						?><BR>
					
						</td>
						
						</tr>
						
					<TR>
					<td colspan="6">
					<hr style="border-top: 2px solid gray;">

					</td>
					</TR>
						<tr>
						
					
						<td colspan="3" id="mytitle">
						<B>Billing From:</B>
					
						<?php 

												
					
					$alldetais="";
						 
						 if($viewreceipt[0]['company']!==""){
							 $alldetais.= $viewreceipt[0]['company']."<br>";
						 }
						  if($viewreceipt[0]['firstname']!==""){
							 $alldetais.= $viewreceipt[0]['firstname']." ". $viewreceipt[0]['surname']."<br>";
						 }
						  if($viewreceipt[0]['idno']!==""){
							 $alldetais.= $viewreceipt[0]['idno']."<br>";
						 }
						  if($viewreceipt[0]['phone']!==""){
							 $alldetais.= $viewreceipt[0]['phone']."<br>";
						 } if($viewreceipt[0]['email']!==""){
							 $alldetais.= $viewreceipt[0]['email']."<br>";
						 }
						  if($viewreceipt[0]['website']!==""){
							// $alldetais= $mysupplier[0]['website']."<br>";
						 }
						  if($viewreceipt[0]['zip']!==""){
							 $alldetais.= $viewreceipt[0]['zip']."<br>";
						 }
						   if($viewreceipt[0]['code']!==""){
							 $alldetais.= $viewreceipt[0]['code']."<br>";
						 }
						 
						  if($viewreceipt[0]['city']!==""){
							 $alldetais.= $viewreceipt[0]['city']."<br>";
						 }
						
						  if($viewreceipt[0]['comments']!==""){
							 $alldetais.= $viewreceipt[0]['comments']."<br>";
						 }
						 
						 
													echo "<br>".$alldetais;

						//echo "<br>".$comments=$mysupplier[0]['comments'];
							?>
						
						
						
						
						
						
						
						
						</td>
						
							<td colspan="3"  id="mytitle">
					
						
						
	
						</td>
						
						
						
							</tr>
																				
																				
																				
																				</table >
									
  <table cellpadding="0" cellspacing="0" border="1" class="table  table-striped" id="example" style="border-color:#CCCCCC; width:100%;"  >

             <thead>
				
         <tr style="background:gray">
     <th> Date</th>

   <th id="non-printable">Due Date</th>
   
     <th>Days</th>

   <th>Invoice</th>
		     <th>Type</th>
	     <th>Debit</th>
<th>Credit</th>	

       <th>Balance</th>	
       <th id="non-printable">More</th>	

	   </tr>
             </thead><tbody>
<?php
	
 if(!is_array($cashflow)) 

 {


   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='13'>";
    
       echo '<div class="alert alert-warning text-center">There is no record found. Try again.</div>';
    echo "</td>";
   echo "</tr>";
    }else {
?>

   <tr  style="margin-bottom:0px;margin-top:0px;">;
     			         <th id="non-printable"></th>	

    <td colspan='6'>
    <b> Balance  Carried forward:</b>
   </td>
    <td>
	<b>
    <?php
	$prebalance=0;
	$prebalance=$previousbalance;
	echo formatMoney($prebalance , true);
	?>
	</b>
   </td>
  </tr>
             <?php 
			  $balance=0;
			  			  $mybalance=0;
						  

  $totaldebt=0;
    $totalcredit=0;
  	foreach ($cashflow as $c): 

		

		  
             ?>



             <tr>
                
            <?php 
$id=$c->id;
  $totaldebt=$totaldebt+$c->finaldebit;
    $totalcredit= $totalcredit+$c->finalcredit;
			  			  $mybalance=$mybalance+$c->finaldebit-$c->finalcredit;;
 		$myinvoice = $c->code;

?>


       <td><?php  
	   
	   echo$newDate = date("d-m-Y", strtotime($c->crateddate));
	   ?></td>
        
 <td id="non-printable"><?php

	   echo$duedate = date("d-m-Y", strtotime($c->paiddate));
?> 
 </td>
 

  <td><?php

 	$finaldate = strtotime(date($c->fromdate));
	$mycurrentdate = strtotime(date($c->paiddate));

									$secs = $mycurrentdate-$finaldate;// == <seconds between the two times>
									$days = $secs / 86400;
							echo $finaldays=round($days,0);
?> 
 </td>
 
 


 
 
  <td>
	
	
	
		   <a  id="non-printable" class=" btn-default" target="_blank"  href="<?php echo base_url();?>user/searchallmysalesorder?searchterm=<?php   if(isset($myinvoice)) {echo $myinvoice; } ?>&view=userdata/allinvoices"> 		
		   <?php //echo $c->mydescription;
   if($c->ordernumber!=""){
   echo "<BR>INV#".$c->ordernumber;
   
   }
    ?>   </a>   </a>


						 <span id="myprint">
						 
						 <?php //echo $c->mydescription;
   if($c->ordernumber!=""){
   echo "<BR>INV#".$c->ordernumber;
   
   }
    ?>
						 </span>
	
	
	
	
	</td>
 
 
 
 
 
 
 
 
 
 
 
 
  <td><?php 
  
  //$accountdata=$this->usermodel->populateallarray('glaccounts','number', $c->account);

//$accountname=$accountdata[0]['accountname'];
		echo  $c->mytype;
			if($c->transactioncode !=""){
				echo "Code: ".  $c->transactioncode;
			
		}
  $finaldebit=0;
  
  if($c->finaldebit==0){
	  
	    $finaldebit=0;
  }else{
	    $finaldebit  =$c->finaldebit;
	
	  
  }
  
    $finalcredit=0;
  
  if($c->finalcredit==0){
	  
	    $finalcredit=0;
  }else{
	    $finalcredit  =$c->finalcredit;
	
	  
  }
  ?></td>
   
     	 <td><?php echo formatMoney($finalcredit , true); ?></td>
   <td><?php echo formatMoney($finaldebit , true); ?></td>


	  <td><?php 

$balance= ($prebalance+($totaldebt-$totalcredit));
	echo formatMoney($balance , true);
	  ?></td>
	 
 <td id="non-printable"> 

 
 
					<div class="btn-group">
                                    <button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown">
                                        More <span class="caret"></span>
                                    </button>
									
									
				
								
                                    <ul class="dropdown-menu dropdown-primary pull-right" role="menu">
                                        
                                        <!-- teacher EDITING LINK -->
                                      
											<?php
										if($usertype=="admin"){
											
											?>
                                        
                                     	  <li> 

	  	  	  	 <a  id="more" class="btn btn-info" target="_blank" href="<?php echo base_url();?>user/editjournalentry?randomitemidentifier=<?php   {echo  $id;}  ?>">

                                                <i class="glyphicon glyphicon-pencil"></i>
																Reconcile/Update
                                                </a>
                                                        </li>
										   <li class="divider"></li>

										
														
									
										<?php
										}
										?>
                                     
                                        
                                    </ul>
										
                                </div>
</td>
 
             </tr>

	<?php endforeach;} ?>
         <tr style="background:gray">
		       <th ></th>	
			         <th id="non-printable"></th>	
    <th colspan="3"><CENTER>TOTAL BALANCE</CENTER></th>
	
	 

		   <th> <h3><?php if(isset($totalcredit)) {echo formatMoney($totalcredit, true); }?></h3></th>
	   <th> <h3><?php if(isset($totaldebt)) {echo formatMoney($totaldebt, true); }?></h3></th>

	   	   <th> <h3><?php if(isset($balance)) {echo formatMoney($balance, true); }?></h3></th>
<th></th>
  
                 </tr>
         <tr>
		 
		 <td colspan="9">
		  </br> </br>
		 <b>
		 <center>
		 Name----------------------------------------------------------Date------------------------------------------Signature---------------------------
		  </center>
		 </b>
		 </td>
		 </tr>
       <tbody> </table>
	   
              </div>

   
			
	<div id="statementModal" 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">Customer and Supplier statement</h4>
      </div>
      <div class="modal-body">
      
	  	

				<?PHP
//$session_data = $this->session->userdata('logged_in');
 //$mydate=date("Y-m-d");
 					 // $mydate=date("Y-m-d H:i:s");
 $fromdate=date("Y-m-d 00:00:00");


				$month= Date('Y-m');
			$fromdate=$month."-01";
				$date=$month."-30";
				
				echo $this->session->flashdata('msg'); 
		
                 echo form_open('user/viewallstatements' , 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;?>

	
	
	 <div class="form-group">
    <label for="inputbranchname" class="col-sm-2 control-label">Counter</label>
    <div class="col-sm-10">
	<select class="form-control" name="branchid"  id="branchid" required>
	
	
	<option value=""  >---Select Branch/Counter Name--</option>
<?php
									 if( $viewallaction==true){
										 ?>
					<option value="all"  >All branches/Counters</option>
									<?php
									 }
							
	?>
	
	<?php 
					 
					 		$maincategory = $this->usermodel->populateall('branch','companyid',$companyid);


							


if(is_array($maincategory)) {

   foreach($maincategory as $maincategoryrow){

   //echo $courserow->coursename;
    ?>
					<option selected="selected" value="<?=  $maincategoryrow->id;   ?>"><?=  $maincategoryrow->name; ?></option>



	<?php

}} else {
    echo 'No main counter available.';
}
	
?>
</select>
		

		
	</div>
  </div>
  
  
  
  
  
  
   <div class="form-group">
    <label for="inputtype" class="col-sm-2 control-label">Counter</label>
    <div class="col-sm-10">
	<select class="form-control" name="type"  id="branchid" required>
	
	
	<option value="" >---Select Statement type--</option>

	<option value="Customer"  selected="selected" >Customer</option>
	<option value="Supplier"  >Supplier</option>


</select>
		

		
	</div>
  </div>
  
  
  
  
  
  
  
	  <div class="form-group">
    <label for="inputsupplier" class="col-sm-2 control-label">Customer name</label>
    <div class="col-sm-10">
	
		  		<input type="text" name="supplier" id="mysupplier" autofocus value="<?php echo set_value('supplier'); ?>" class="form-control" required placeholder="Enter Customer/Supplier name">
		 
				
		<span class="text-danger">Enter "all" to view everyone <?php echo form_error('supplier'); ?>
		
		</span>
	</div>
  </div>
  
	

						    <label for="inputfromdatexx" class="col-sm-2 control-label">From Date </label>

				<div class="col-sm-5">
					<div class="form-group">

                     		  		<input type="date" name="startdate" value="<?php echo set_value('fromdate',$finalstartdate); ?>" class="form-control" required placeholder="Enter start date">

					</div>
				</div>
				<div class="col-sm-5">
					<div class="form-group">
					

							  		<input type="text"  name="frotime" value="<?php echo set_value('frotime',$starttime.":00"); ?>" class="form-control" required placeholder="Enter start time">

					</div>
				</div>
			

			
			
						    <label for="inputfromdate" class="col-sm-2 control-label">To Date </label>

				<div class="col-sm-5">
					<div class="form-group">

		  		<input type="date" name="enddate" value="<?php echo set_value('todate',$finalendate); ?>" class="form-control" required placeholder="Enter End date">

					</div>
				</div>
				<div class="col-sm-5">
					<div class="form-group">
					

							  		<input type="text"  name="totime" value="<?php echo set_value('totime',$duetime.":00"); ?>" class="form-control" required placeholder="Enter stop time">

					</div>
				</div>
			
  
   
	

	

	
	 <div class="form-group">

    <div class="col-sm-offset-2 col-sm-10">
     
	         <button type="submit"  value="cash" name="btn_viewcash" class="btn btn-primary"> View statement</button>

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

    

				</div>

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

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

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

 
</div>


















