<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>


		 <style>
			select.error { background-color:#ffa8a8; border: 1px dotted red;  }
			input.error{ background-color:#ffa8a8;   }
			textarea.error{ background-color:#ffa8a8; border: 1px dotted red;  }
         </style>
		 
        


<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}


-->
</style>

<script type="text/javascript">
 
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

</script>
<script type="text/javascript">
	$(document).ready(function() {                
	$("#form1").validate({
	rules: {
	
	client_name: "required",
	
	
	},
	messages: {
	
	client_name:"",
	
	},
	});                
	});
	</script>
<!-- Page wrapper  -->
        <div class="page-wrapper">
           
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body" id="printableArea">

							 <h4>Sale Report <span style="float:right"><button onclick="goBack()" class="btn btn-info btn-sm">Go Back</button><script>
function goBack() {
   window.history.back();
}
</script></span></h4>
						<hr />
<form name="form1" id="form1" method="post">
  <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
    <tr>
      <!--<td width="6%" height="49" nowrap="nowrap"><strong>From Date </strong></td>
      <td width="1%"><strong>:</strong></td>
      <td width="15%"><input name="s_date" type="text" class="tcal" id="s_date" readonly=""  value="<?=date('d-m-Y')?>"/></td>
      <td width="7%"><strong>To Date </strong></td>
      <td width="1%"><strong>:</strong></td>
      <td width="23%"><input name="end_date" type="text" class="tcal" id="end_date" readonly=""  value="<?=date('d-m-Y')?>" /></td>-->
      <td width="11%"><strong>Client Name </strong></td>
      <td width="1%"><strong>:</strong></td>
      <td width="12%"><select name="client_name" id="client_name" style="width:200px;"  class="required form-control">
      <option value="">---Please Select---</option>
      <?php 
							$sqlc="select * from tbl_client where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
      <option value="<?=$cs["id"];?>" <?php if(($cs["id"])==$_POST["client_name"]){ ?> selected="selected" <?php }?>>
      <?=$cs["client_name"];?>
      </option>
      <?php }}?>
    </select></td>
      <td width="1%">&nbsp;</td>
      <td width="22%"><input type="submit" name="Search" value="Search" class="btn btn-info" /></td>
      </tr>
  </table>
</form>
<br />
<form method="post">
<?php 
if(isset($_POST["Search"]))
{
/*$s_date=$objIms->DateToInt($_POST["s_date"]);
$end_date=$objIms->DateToInt($_POST["end_date"]);*/
$client_name=$_POST["client_name"];
$whr1="id='$client_name' and session_login_id = '".$_SESSION['login_id']."'";
$get=$objIms->getRows('tbl_client',$whr1);
?>
     
            
		
            <?php 
				 $sql_viewtmp="select * from view_sales_master where session_login_id = '".$_SESSION['login_id']."' and  cancel_status=0 and client_id='$client_name' order by id desc";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					?>
					<div class="panel">
					<div class="panel-heading">Sales To 
					  <strong>
					  <?=$get["client_name"]?>
					  </strong>					</div>
					<p>&nbsp;</p>
                     <table width="100%" class="table table-bordered">
                     <tr height="32" align="left">
            <th nowrap="nowrap" width="6%">Si No</th>
            <th nowrap="nowrap" width="20%">Date </th>
            <th nowrap="nowrap" width="30%">Client Name</th>
           
            <th nowrap="nowrap" width="15%">Particulars </th>
			 <th nowrap="nowrap" width="15%">Amount </th>
            <th nowrap="nowrap" width="13%">View</th>
            <th nowrap="nowrap" width="18%">Payment</th>
            </tr>
            <?php
			$sum=0;
				while($rows=mysql_fetch_assoc($res_viewtmp))
					{
						
						
						
						
							
							
			?>
           
            <tr height="32">
            <td nowrap="nowrap" width="6%"><?php echo $i++;?></td>
           
		    <td nowrap="nowrap" width="24%"><?php echo $objIms->IntToDate($rows["stampdate"]);?></td>
		    <td nowrap="nowrap" width="35%"><?php echo $rows["client_name"];?></td>
            
            <td nowrap="nowrap" width="17%"><?php echo $rows["sale_no"];?></td>
			<td nowrap="nowrap" width="17%"><?php  $sql_viewtmp1="select * from tbl_account where session_login_id = '".$_SESSION['login_id']."' and mas_id='". $rows["id"]."' and sales_type in('Sales','Sales_Due' ) and cancel_status ='0' order by id limit 1";
				$res_viewtmp1=mysql_query($sql_viewtmp1);
				$rows1=mysql_fetch_assoc($res_viewtmp1);
			$amount =	$rows1['actual_amt'];
			echo $amount;
				?></td>
            <td nowrap="nowrap" width="18%"><a onclick="PopupCenter('Salespopup_view.php?uid=<?php echo md5($rows["id"]);?>','xtf','1000','700');" style="cursor:pointer; text-decoration:none">View</a></td>
            <td nowrap="nowrap" width="18%"><a href="sale_payment.php?uid=<?php echo md5($rows["id"]);?>">Payment</a></td>
            </tr>   <?php 
			$sum +=$amount;
			
			}?>
			<tr><td colspan="4">Total amount</td>
			<td colspan="4"><?php echo $sum;?></td>
			</tr>
			
			<tr><td colspan="4">Paid amount</td>
			<td ><?php   $sql_viewtmp2="select sum(Cr_Amt) as paid from tbl_account where session_login_id = '".$_SESSION['login_id']."' and mas_id in(select id from tbl_sales where client_id = '".$_POST["client_name"]."' and session_login_id = '".$_SESSION['login_id']."') and sales_type='Sales' and cancel_status ='0' and clear ='1'";
				$res_viewtmp2=mysql_query($sql_viewtmp2);
				$rows2=mysql_fetch_assoc($res_viewtmp2);
			$amount_paid =	$rows2['paid'];
			 
			
			
			 $sql_viewtmp4="select sum(Cr_Amt) as paid_amount from tbl_account where session_login_id = '".$_SESSION['login_id']."' and  mas_id='".$_POST["client_name"]."' and sales_type in('Sale Paid') and cancel_status='0' and clear ='1'";
			 $res_viewtmp4=mysql_query($sql_viewtmp4);
				$rows4=mysql_fetch_assoc($res_viewtmp4);
			 $next_paid =	$rows4['paid_amount'];
			 $total_amt = $amount_paid+$next_paid;
			 echo $total_amt;
				?></td>
				<td><a onclick="PopupCenter('sale_paid_popup_view.php?sid=<?php echo md5($_POST["client_name"]);?>','xtf','1000','700');" style="cursor:pointer; text-decoration:none">View</a></td>
			</tr>
			
			<tr><td colspan="4">Due amount</td>
			<td colspan="4"><?php echo $due_amt = $sum-$amount_paid-$next_paid;?></td>
			</tr>
				<?php if($due_amt!='0'){?><tr><td colspan="4">Add To Bank</td>
			<td colspan="2"><select name="bank" id="bank"   class="required form-control" required>
    
      <?php 
							$sqlc="select * from tbl_bank_detail where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
      <option value="<?=$cs["id"];?>" >
      <?=$cs["bank_name"];?> (AC/NO:  <?=$cs["acc_no"];?>)
      </option>
      <?php }}?>
    </select></td>
			</tr>
		<tr><td colspan="4">Amount Paying</td>
			<td colspan="4"><input type="text" name="payble_amt" onkeypress="return isNumberKey(event)" style="width:70px" required /><input type="hidden" name="client_id" value="<?php echo $_POST["client_name"];?>" style="width:70px" required /></td>
			</tr>
			
			
			<tr><td colspan="2"><b>PAYMENT MODE</b></td></tr>
		  <tr><td colspan="2">
               
                  <input type="radio" id="r3" name="r3" value="Cash" class="flat-red" checked>
				  Cash
               
               
                  <input type="radio" id="r3" name="r3" style="margin-left:20px" value="Cheque" class="flat-red">
                  Cheque
               
        	
		
	</td></tr><?php }?>
	 </table>
<script type="text/javascript">
   function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
} 
   
</script>

	<hr style="border:6px solid #CCCC00;display:none;" id="cheq_hide_header" />
<div id="cheq_hide" style="display:none; margin:1px;">
 	<table width="100%" class="table table-bordered">
	<tr><td>Bank Name</td>
		<td>
			<input type="text" id="bank_name" name="bank_name" onkeypress="return isAlfa(event)"  onblur="toTitleCase()" class="w100 form-control" />
		</td>
		<td>Branch Name</td>
		<td>
			<input type="text" id="branch_name" name="branch_name" onkeypress="return isAlfa(event)"  onblur="toTitleCase()" class="w100 form-control"/>
		</td>
		</tr>
		<tr>
		<td>Cheque Number</td>
		<td>
			<input type="text" id="cheque_no" name="cheque_no" class="w100 form-control"/>
		</td>
		<td>Cheque Date</td>
		<td>
		 <input name="check_date" type="date" id="cheque_date"  style="width:185px; background-color:#FFF"  value="<?=$dt?>" class="tcal form-control" />	</div>
			</td>
			</tr>
		</table>
			</div>
		</div>	
          <?php if($due_amt!='0'){?> <input type="submit" name="paid" value="save" class="btn btn-info" /><?php }else{?><input type="submit" disabled="disabled" value="save" class="btn btn-info" />
		   <?php }?>
		   </div>
    <?php }
	else
	{
	echo '<span style="color:#FF0000">No Data Found</span>';
	}
	}?>
	
		

   
<p></p>
		<br />
       
</form>
<?php
if(isset($_POST['paid'])){
$payable_amt = $_POST['payble_amt'];
$client_id = $_POST['client_id'];
$tran_date =$objIms->DateToInt(date('d-m-Y'));	
$type = 'Sale Paid';
$payment_mode=$_POST["r3"];
$bank=$_POST["bank"];



$sql_ins="insert into tbl_account(mas_id,trans_date,Cr_Amt,sales_type,cash_type,clear, payment_mode, bank_id, session_login_id) values('$client_id','$tran_date','$payable_amt','$type','Debit','1','$payment_mode','$bank', '".$_SESSION['login_id']."')";
				$res_ins=mysql_query($sql_ins);
				$accid=mysql_insert_id();

if($payment_mode=='Cheque')
					{
					$cheque_date = $_POST['check_date'];
					$cheque_date = date('Y-m-d', strtotime($cheque_date));
					 $sql_ck_dtl="INSERT INTO `tbl_cheque_detail` (`account_id`, `bank_name`, `branch_name`, `checque_no`, `checque_date`, `session_login_id`) VALUES ('$accid', '".$_POST['bank_name']."', '".$_POST['branch_name']."', '".$_POST['cheque_no']."', '".$cheque_date."', '".$_SESSION['login_id']."');";
					 $res_ck_dtl=mysql_query($sql_ck_dtl);
					  $sql_bank="INSERT into tbl_bank(head, bankdet_id, debit, stampdate, session_login_id) values('Sale_Payment_Recieved', '$bank', '$payable_amt', '$tran_date', '".$_SESSION['login_id']."');";
					$res_bank=mysql_query($sql_bank);
					}
					$sql_ins="update tbl_account set invoice_no = '$accid' where id ='$accid' and session_login_id = '".$_SESSION['login_id']."'";
				$res_ins=mysql_query($sql_ins);
					?>
					<script>
					alert('Success');
					</script>
					<?php
}
?>


<div align="center"><span class="btn btn-info" onClick="printDiv('printableArea')">Print</span></div>

  </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
<?php include 'footer.php' ?>
 <script src="../js/lib/datatables/datatables.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
    <script src="../js/lib/datatables/cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"></script>
    <script src="../js/lib/datatables/datatables-init.js"></script>
<script>
$("input[name='r3']").click(function() {
    var status = $(this).val();
    if (status == 'Cheque') {
        $("#cheq_hide_header").show('slow');
		$("#cheq_hide").show('slow');
    } else {
		$("#cheq_hide").hide();
		$("#cheq_hide_header").hide('slow');
    }
});
</script>
<script type="text/javascript">
	function isAlfa(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 32 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
        return false;
    }
    return true;
}
</script>
<script type="text/javascript">
function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
</script>
