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



<?php

if(isset($_GET["uid"]))
{
$uid=$_GET["uid"];
$rs=$objIms->getRows("view_purchase_supplier","md5(id)='$uid' and session_login_id = '".$_SESSION['login_id']."'");
$pur_id=$rs["id"];
$order_no=$rs["order_no"];
$purchase_date=$objIms->IntToDate($rs["purchase_date"]);
$supplier_name=$rs["supplier_name"];

}


	
?>

		 <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;}
.style2 {
	color: #00CC00;
	font-style: italic;
}
.style3 {color: #00CC00}


-->
</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>
<!-- 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>Purchase 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="10%" height="49" nowrap="nowrap"><strong>From Date </strong></td>
      <td width="1%"><strong>:</strong></td>
      <td width="25%"><input name="s_date" type="text" class="tcal" id="s_date" readonly=""  value="<?=date('d-m-Y')?>"/></td>
      <td width="13%"><strong>To Date </strong></td>
      <td width="3%"><strong>:</strong></td>
      <td width="21%"><input name="end_date" type="text" class="tcal" id="end_date" readonly=""  value="<?=date('d-m-Y')?>" /></td>-->
	  
	  <td width="11%"><strong>Supplier Name </strong></td>
      <td width="1%"><strong>:</strong></td>
      <td width="12%"><select name="supplier_name" id="supplier_name" style="width:200px;"  class="required form-control">
      <option value="">---Please Select---</option>
      <?php 
							$sqlc="select * from tbl_supplier 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["supplier_name"]){ ?> selected="selected" <?php }?>>
      <?=$cs["supplier_name"];?>
      </option>
      <?php }}?>
    </select></td>
      <td width="3%">&nbsp;</td>
      <td width="24%"><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"]);*/
$supplier_name=$_POST["supplier_name"];
$whr1="id='$supplier_name' and session_login_id = '".$_SESSION['login_id']."'";
$get=$objIms->getRows('tbl_supplier',$whr1);
?>
     
            
		
            <?php 
				  $sql_viewtmp="select * from view_purchase_supplier where session_login_id = '".$_SESSION['login_id']."' and supplier_id = '$supplier_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">Purchase Detail </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="24%">Purchase Date </th>
            <th nowrap="nowrap" width="35%">Supplier Name</th>
           
            <th nowrap="nowrap" width="17%">Order No </th>
			<th nowrap="nowrap" width="17%">Taxable Amount </th>
			<th nowrap="nowrap" width="17%">Cgst </th>
			<th nowrap="nowrap" width="17%">Sgst </th>
			<th nowrap="nowrap" width="17%">Igst </th>
			<th nowrap="nowrap" width="17%">Amount </th>
            <th nowrap="nowrap" width="18%">View</th>
            <th nowrap="nowrap" width="18%">Payment</th>
            </tr>
            <?php
			$tot_amt = 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["purchase_date"]);?></td>
		    <td nowrap="nowrap" width="35%"><?php echo $rows["supplier_name"];?></td>
            
            <td nowrap="nowrap" width="17%"><?php echo $rows["order_no"];?></td>
			
			<td nowrap="nowrap" width="17%"><?php $sql_viewtmp2="select sum((pur_qty*pur_price)-(pur_qty*pur_price*discount/100)) as taxable_value , sum(((pur_qty*pur_price)-(pur_qty*pur_price*discount/100))*cgst/100) as cgst_value , sum(((pur_qty*pur_price)-(pur_qty*pur_price*discount/100))*sgst/100) as sgst_value , sum(((pur_qty*pur_price)-(pur_qty*pur_price*discount/100))*igst/100) as igst_value from tbl_purchase_details where session_login_id = '".$_SESSION['login_id']."' and purmas_id = '".$rows["id"]."'";
			$res_viewtmp2=mysql_query($sql_viewtmp2);
			$rows2=mysql_fetch_assoc($res_viewtmp2);
			
			 echo round($rows2['taxable_value'],2);?></td>
			<td nowrap="nowrap" width="17%"><?php echo round($rows2['cgst_value'],2);?></td>
			<td nowrap="nowrap" width="17%"><?php echo round($rows2['sgst_value'],2);?></td>
			<td nowrap="nowrap" width="17%"><?php echo round($rows2['igst_value'],2);?></td>
			
			<td nowrap="nowrap" width="17%"><?php echo $rows["round_off"]; $tot_amt+= $rows["round_off"];?></td>
            <td nowrap="nowrap" width="18%"><a onclick="PopupCenter('purchasepopup_view.php?uid=<?php echo md5($rows["id"]);?>','xtf','1000','700');" style="cursor:pointer; text-decoration:none">View</a></td>
            <td nowrap="nowrap" width="17%"><a href="payment.php?uid=<?php echo md5($rows["id"]);?>">Payment</a></td>
            </tr>   <?php }?>
			
			
			<tr><td colspan="8">Total amount</td>
			<td colspan="5"><?php echo $tot_amt;?></td>
			</tr>
			
			<tr><td colspan="8">Paid amount</td>
			<td ><?php
			 $sql_viewtmp2="select sum(Dr_Amt) as paid from tbl_account where session_login_id = '".$_SESSION['login_id']."' and mas_id in(select id from tbl_purchase_master where supplier_id = '".$_POST["supplier_name"]."') and sales_type='Purchase' and clear='1'";
				$res_viewtmp2=mysql_query($sql_viewtmp2);
				$rows2=mysql_fetch_assoc($res_viewtmp2);
			 $amount_paid =	$rows2['paid'];
			
			 $sql_viewtmp4="select sum(Dr_Amt) as paid_amount from tbl_account where session_login_id = '".$_SESSION['login_id']."' and  mas_id='".$_POST["supplier_name"]."' and sales_type='Purchase Paid' and cancel_status='0'";
			 $res_viewtmp4=mysql_query($sql_viewtmp4);
				$rows4=mysql_fetch_assoc($res_viewtmp4);
			 $next_paid =	$rows4['paid_amount'];
			 $total_amt_paid = $amount_paid+$next_paid;
			 echo $total_amt_paid;
			?></td>
				<td><a onclick="PopupCenter('purchase_paid_popup_view.php?sid=<?php echo md5($_POST["supplier_name"]);?>','xtf','1000','700');" style="cursor:pointer; text-decoration:none">View</a></td>
			</tr>
			
			<tr><td colspan="8">Due amount</td>
			<td colspan="4"><?php echo $due_amt =$tot_amt-$total_amt_paid ;?></td>
			<?php if($due_amt!='0'){?><tr><td colspan="6">Pay From Bank</td>
			<td colspan="4"><select name="bank" id="bank"  onchange="fetch()"  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>
			<tr><td colspan="8">Amount Paying</td>
			<td colspan="4"><input type="text" name="payble_amt" id="payble_amt" onkeyup="fetch()" onkeypress="return isNumber(event,this);" style="width:70px" required /><input type="hidden" name="supplier_id" value="<?php echo $_POST["supplier_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 isNumber(evt, element) {
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57) && !(charCode == 46 || charCode == 8))
    return false;
  else {
    var len = $(element).val().length;
    var index = $(element).val().indexOf('.');
    if (index > 0 && charCode == 46) {
      return false;
    }
    if (index > 0) {
      var CharAfterdot = (len + 1) - index;
      if (CharAfterdot > 3) {
        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" class="w100 form-control" />
		</td>
		<td>Branch Name</td>
		<td>
			<input type="text" id="branch_name" name="branch_name" 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>
           </table>
		   </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'];
$supplier_id = $_POST['supplier_id'];
$tran_date =$objIms->DateToInt(date('d-m-Y'));	
$type = 'Purchase Paid';
$payment_mode=$_POST["r3"];
$bank=$_POST["bank"];

 $sql_ins="insert into tbl_account(mas_id,trans_date,sales_type,Dr_Amt,cash_type,clear, payment_mode, bank_id, session_login_id) values('$supplier_id','$tran_date','$type','$payable_amt','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_date2 = 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_date2."', '".$_SESSION['login_id']."');";
					 $res_ck_dtl=mysql_query($sql_ck_dtl);
					 $sql_bank="INSERT into tbl_bank(head, bankdet_id, credit, stampdate, session_login_id) values('purchase_payment', '$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);
					

					
					$sql_su="INSERT into supplier_details(name_id, dr_amount, sales_type, session_login_id) values('purchase_payment', '$accid', '$payable_amt', '$tran_date', '".$_SESSION['login_id']."');";
					$res_su=mysql_query($sql_su);


if($payment_mode=='Cash'){
					$sql_cash="INSERT into cash_detail(sales_type, cr_amount, stampdate, session_login_id) values('purchase_payment', '$payable_amt', '$tran_date', '".$_SESSION['login_id']."');";
					$res_cash=mysql_query($sql_cash);

					$sql_sup="INSERT into supplier_details(name_id, dr_amount, sales_type, session_login_id) values('purchase_payment', '$accid', '$payable_amt', '$tran_date', '".$_SESSION['login_id']."');";
					$res_sup=mysql_query($sql_sup);
}
					?>
					<script>
					alert('Success');
					</script>
					<?php
					
}
?>   

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

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

function fetch(){

/*var bank = $('#bank').val();
var amt = $('#payble_amt').val();

 $.ajax({
		type: "POST",
		url: "check_balance.php",
		data: "bank_id="+bank,
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var amount = data[0];
				
				if(eval(amt) > eval(amount)){
				alert('Dont have sufficient balance, please add amount to bank');
				$('#payble_amt').val('');
				$('#payble_amt').focus();
				}
				
				
			}
			});
   */
	


}


</script>
<script type="text/javascript">
    
    $(function() {

  $('#bank_name').keydown(function (e) {
  
    if (e.ctrlKey || e.altKey) {
    alert("only character allow");
      e.preventDefault();
      
    } else {
    
      var key = e.keyCode;
      
      if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
        alert("only character allow");
        e.preventDefault();
        
      }

    }
    
  });
  
});
</script>
<script type="text/javascript">
    
    $(function() {

  $('#branch_name').keydown(function (e) {
  
    if (e.ctrlKey || e.altKey) {
    alert("only character allow");
      e.preventDefault();
      
    } else {
    
      var key = e.keyCode;
      
      if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
        alert("only character allow");
        e.preventDefault();
        
      }

    }
    
  });
  
});
</script>