<?php 
session_start();
ob_start();
include("../../pidbco/pidbjoin.php");
$sujpath="./";
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Projects</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="<?php echo $sujpath; ?>bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="<?php echo $sujpath; ?>font-awesome/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="<?php echo $sujpath; ?>font-awesome/ionicons.min.css">
    <!-- DataTables -->
    <link rel="stylesheet" href="<?php echo $sujpath; ?>plugins/datatables/dataTables.bootstrap.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="<?php echo $sujpath; ?>dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="<?php echo $sujpath; ?>dist/css/skins/_all-skins.min.css">
   
     <link rel="stylesheet" type="text/css" href="tcal.css" />    
    <script type="text/javascript" src="tcal.js"></script>
    
    
  <script>
   function add_new_purchase(){
      var rcompany = $('#rcompany').val();
      var rbranch = $('#rbranch').val();
      var billno = $('#billno').val();
      var purchs_date = $('#purchs_date').val();
      var accnt_name = $('#accnt_name').val();
      var prodnam = $('#prodnam').val();
      var vvendr = $('#vvendr').val();
      var pamount = $('#pamount').val();
      var pdiscount = $('#pdiscount').val();
      var pnet = $('#pnet').val();
      var pdescrptn = $('#pdescrptn').val();
      
      if(rcompany == ''){
         alert("Select Company");
      } else if(rbranch == ''){
      	 alert("Select Branch");
      } else if(billno == ''){
      	alert("Enter Bill No.");
      } else if(accnt_name == ''){
      	alert("Enter Accountant Name");
      } else if(prodnam == ''){ 
        alert("Enter Product Name");
      } else if(pamount == ''){
      	alert("Enter the Amount");
      } else {	
      
      var purchase_register =1;
      var input = document.getElementById("billfile");
      file = input.files[0];
     // if(file != undefined){
      formData= new FormData();
     // if(!!file.type.match(/image.*/)){
      formData.append("image", file);
      
      formData.append("purchase_register", purchase_register);
      formData.append("rcompany", rcompany);
      formData.append("rbranch", rbranch);
      formData.append("billno", billno);
      formData.append("purchs_date", purchs_date);
      formData.append("accnt_name", accnt_name);
      formData.append("prodnam", prodnam);
      formData.append("vvendr", vvendr);
      formData.append("pamount", pamount);
      formData.append("pdiscount", pdiscount);
      formData.append("pnet", pnet);
      formData.append("pdescrptn", pdescrptn);
      
        $.ajax({
        url: "ajaxmarket.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        cache: false,
        success: function(data){
        	$('#rcompany').val('');
        	$('#rbranch').val('');
        	$('#billno').val('');
        	$('#purchs_date').val('');
        	$('#accnt_name').val('');
        	$('#prodnam').val('');
        	$('#vvendr').val('');
        	$('#pamount').val('');
        	$('#pdiscount').val('');
        	$('#pnet').val('');
        	$('#pdescrptn').val('');
        	$("#marketingdiv123").load("purchase.php #marketingdiv123");
            datat2();
        }
     });   
 /* } else {
      alert('Not a valid image!');
    }
    } else {
    alert('Please Enter Values');
    }   */  
    }	
   }


    	
    	
   function edit_new_purchase(){
   	
   	  var rcompany = $('#rcompany').val();
      var rbranch = $('#rbranch').val();
      var billno = $('#billno').val();
      var purchs_date = $('#purchs_date').val();
      var accnt_name = $('#accnt_name').val();
      var prodnam = $('#prodnam').val();
      var vvendr = $('#vvendr').val();
      var pamount = $('#pamount').val();
      var pdiscount = $('#pdiscount').val();
      var pnet = $('#pnet').val();
      var pdescrptn = $('#pdescrptn').val();
      var hidvall = $('#hidvall').val();
      
      if(rcompany == ''){
         alert("Select Company");
      } else if(rbranch == ''){
      	 alert("Select Branch");
      } else if(billno == ''){
      	alert("Enter Bill No.");
      } else if(accnt_name == ''){
      	alert("Enter Accountant Name");
      } else if(prodnam == ''){ 
        alert("Enter Product Name");
      } else if(pamount == ''){
      	alert("Enter the Amount");
      } else {	
      	
      var purchase_register_edit =1;
      var input = document.getElementById("billfile");
      file = input.files[0];
     // if(file != undefined){
      formData= new FormData();
     // if(!!file.type.match(/image.*/)){
      formData.append("image", file);
      
      formData.append("purchase_register_edit", purchase_register_edit);
      formData.append("rcompany", rcompany);
      formData.append("rbranch", rbranch);
      formData.append("billno", billno);
      formData.append("purchs_date", purchs_date);
      formData.append("accnt_name", accnt_name);
      formData.append("prodnam", prodnam);
      formData.append("vvendr", vvendr);
      formData.append("pamount", pamount);
      formData.append("pdiscount", pdiscount);
      formData.append("pnet", pnet);
      formData.append("pdescrptn", pdescrptn);
      formData.append("hidvall", hidvall);
      
        $.ajax({
        url: "ajaxmarket.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        cache: false,
        success: function(data){
        	$('#rcompany').val('');
        	$('#rbranch').val('');
        	$('#billno').val('');
        	$('#purchs_date').val('');
        	$('#accnt_name').val('');
        	$('#prodnam').val('');
        	$('#vvendr').val('');
        	$('#pamount').val('');
        	$('#pdiscount').val('');
        	$('#pnet').val('');
        	$('#pdescrptn').val('');
        	$("#marketingdiv123").load("purchase.php #marketingdiv123");
            datat2();
        }
     });   
 /* } else {
      alert('Not a valid image!');
    }
    } else {
    alert('Please Enter Values');
    }   */  
     }
   }
        
        
    	
    	    	
    	function delete_purchase(id)
    	{
    		var result = confirm("Want to delete?");
			if (result) 
			{
     		$.ajax({
			type: "POST",
			url: "ajaxmarket.php",
			data: {
			btn_delete_purchase: 1,
			id:id
			},
			cache: true,
			success: function(res)
			{
			      $("#marketingdiv123").load("purchase.php #marketingdiv123");
			      datat();
         	
			}  
			});
   			}
    	}
    	
    	
    	function edit_purchase(id)
    	{
    		$.ajax({
			type: "POST",
			url: "ajaxmarket.php",
			data: {
			btn_edit_market: 1,
			id:id
			},
			cache: true,
			success: function(res)
			{
			
	         	$("#marketingdiv2").load("purchase.php?idd2="+id+" #marketingdiv2"); 	
			}  
			});
    	}
    	
    	


 function amount_type(amt_typ)
        {
         if(amt_typ == 'Cheque'){
           document.getElementById('cheque_idd').style.display='block';
	     } else {
           document.getElementById('cheque_idd').style.display='none';
         } 
        }
        
        
function cust_change(str)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("recorders").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajaxmarket.php?cust_idd="+str,true);
xmlhttp.send();
}

function branch_change(str)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("rbranch").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajaxmarket.php?comp_idd="+str,true);
xmlhttp.send();
}
    </script>
    
  </head>
  <body class="hold-transition skin-blue sidebar-mini">
     <div class="wrapper">
  <?php include("head.php");
    include("left.php"); ?> 
	 
      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper" >
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1> Purchase
          	<small>- Control panel</small>
          	</h1>
          <ol class="breadcrumb">
            <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
            <li class="active">Purchase</li> 
          </ol>
        </section>
  <!-- Main content -->
        <section class="content" >
          <div class="row" >
            <div class="col-xs-12" >





   <div class="box-body" id="marketingdiv2">
 	<?php
    
    $billno1='';
    $purch1='';
    $accNo1='';
    $prod1='';
    $amo1='';
    $dis1='';
    $net1='';
    $des1='';
    if(!empty($_GET['idd2'])){
      $idd2 = strip_tags(base64_decode($_GET['idd2']));
    
	    $sq = mysqli_query($con, "SELECT * FROM `p_purchase` WHERE `pid`='$idd2' ");
	    $rss = mysqli_fetch_assoc($sq);	
      $billno1=$rss['p_billno'];
      $purch1=$rss['p_purdat'];
      $accNo1=$rss['p_acount'];
      $prod1=$rss['prod_nam'];
      $amo1=$rss['p_amnt'];
      $dis1=$rss['p_discnt'];;
      $net1=$rss['p_netamnt'];
      $des1=$rss['p_descre'];
      }  
    ?>		  	
	<div class="box-body" >
 <div class="row" >	
<div class="col-md-12" style="" >
	
<div class="col-md-3" style="padding-left: 2px; padding-right: 0px;">					
<div class="form-group">
	<select class="form-control" id="rcompany" name="rcompany" onChange="branch_change(this.value);">
	<option value="">Company</option>
	<?php
	$sq2 = mysqli_query($con,"SELECT cid,c_comp FROM `p_company` ");
	while($res2 = mysqli_fetch_array($sq2)){
		if($rss['p_comp'] == $res2['cid']){
	?>	
	    <option value="<?php echo $res2['cid']; ?>" selected><?php echo $res2['c_comp']; ?></option>
	<?php } else { ?>
		<option value="<?php echo $res2['cid']; ?>"><?php echo $res2['c_comp']; ?></option>
    <?php } } ?>
	</select>	
</div></div>

<div class="col-md-2" style="padding-left: 2px; padding-right: 0px;">					
  <div class="form-group">
	<select class="form-control" id="rbranch" name="rbranch">
		<option value="">Branch</option>
		<?php
		$p_bran = $rss['p_bran'];
		$p_comp = $rss['p_comp'];
		$bselect = mysqli_query($con,"SELECT * FROM `p_branch` WHERE `b_comp`='$p_comp' ");
		while($rsult = mysqli_fetch_array($bselect)) { 
		    if($p_bran == $rsult['b_branchna']){ ?>
		  <option value="<?php echo $rsult['b_branchna']; ?>" selected><?php echo $rsult['b_branchna']; ?></option> 	
	     <?php } else { ?>		
		  <option value="<?php echo $rsult['b_branchna']; ?>"><?php echo $rsult['b_branchna']; ?></option>	
		<?php } } ?>
	</select>
 </div></div>
	
 <div class="col-md-2" style="padding-left: 2px; padding-right: 0px;">					
  <div class="form-group">
	<input name="billno" id="billno" class="form-control" placeholder="Bill No." value="<?php echo $billno1; ?>" />	
 </div></div>	
 <div class="col-md-2" style="padding-left: 2px; padding-right: 0px;">					
  <div class="form-group">
	<input name="purchs_date" id="purchs_date" class="form-control tcal" placeholder="Purchase Date" value="<?php echo $purch1; ?>" />	
 </div></div>
 
 
  <div class="col-md-3" style="padding-left: 2px; padding-right: 0px;">					
  <div class="form-group">
	<input name="accnt_name" id="accnt_name" class="form-control" placeholder="Accountant Name" value="<?php echo $accNo1; ?>" />
 </div></div>
 

 
  
 	
 	</div>	

</div>	


<div class="row" >	
<div class="col-md-12" style="" >
	
		
<div class="col-md-3"  style=" padding-left: 2px; padding-right: 0px; ">			
<div class="form-group">
<input name="prodnam" id="prodnam" class="form-control" placeholder="Product Name" value="<?php echo  $prod1; ?>" />
</div></div>


<div class="col-md-2" style="padding-left: 2px; padding-right: 0px;" >					
<div class="form-group">
    <select class="form-control" id="vvendr" name="vvendr" onChange="vend_change(this.value);">
		<option value="">Vendor</option>
	<?php 
	$sel = mysqli_query($con, "SELECT * FROM `p_vendor`");
	while($res = mysqli_fetch_array($sel)){
		if($rss['p_vendor'] == $res['vid']){
	?>	
	<option value="<?php echo $res['vid']; ?>" selected><?php echo $res['v_company']; ?></option>
	<?php } else { ?>
	<option value="<?php echo $res['vid']; ?>"><?php echo $res['v_company']; ?></option>
	<?php } } ?>
	</select>
</div></div>


	
<div class="col-md-3" style="padding-left: 2px; padding-right: 0px;">					
<div class="form-group">
    <input name="pamount" id="pamount" onkeypress="return isNumber2(event)" class="form-control" placeholder="Amount" value="<?php echo $amo1; ?>">
</div></div>

<div class="col-md-2" style="padding-left: 2px; padding-right: 0px;">					
<div class="form-group">
    <input name="pdiscount" id="pdiscount" onkeypress="return isNumber2(event)" class="form-control" placeholder="Discount Amount" value="<?php echo $dis1; ?>">
</div></div>

<div class="col-md-2" style="padding-left: 2px; padding-right: 0px;" >					
<div class="form-group">
    <input name="pnet" id="pnet" onkeypress="return isNumber2(event)" class="form-control" placeholder="Net Amount" value="<?php echo $net1; ?>">
</div></div>









<div class="col-md-4" style=" padding-left: 2px; padding-right: 0px; ">					
<div class="form-group">
   <textarea name="pdescrptn" id="pdescrptn" class="form-control" placeholder="Descreption" style=" height: 35px;"><?php echo $des1; ?></textarea>		
</div></div>

<div class="col-md-1" style=" padding-left: 2px; padding-right: 0px; ">					
<div class="form-group">
&nbsp; &nbsp;Upload Bill: 
</div></div>

<div class="col-md-3" style=" padding-left: 2px; padding-right: 0px; ">					
<div class="form-group">
   <input type="file" name="billfile" id="billfile" />		
</div></div>
	

<div class="col-md-2" style="padding-left: 2px; padding-right: 0px;">				
<div class="form-group">
  <?php if(!empty($_GET)){ ?>
	<input type="hidden" name="hidvall" id="hidvall" value="<?php echo $rss['pid']; ?>" />
<input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Update" onClick="edit_new_purchase();" style=" float: right; 
margin-right: 20px; " /> 

<?php } else { ?>
<input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Submit" onClick="add_new_purchase();" style=" float: right; 
margin-right: 20px; " />	
<?php } ?> 
</div></div>

</div>




</div>



	
	

</div>
  </div>
  
  
  
  
  
  
<div id="marketingdiv123">


<div class="box" >
   <div class="box-body" >
        <div class="box-body table-responsive no-padding" >
   
  	                 	
                  <table id="example1" class="table table-bordered table-striped" >
                    <thead >
<tr >
<th>#</th>
<th>Company</th>
<th>Bill No.</th>
<th>Accountant</th>
<th>Product Name</th>
<th>Vendor</th>
<th>Amount</th>
<th></th>
</tr>
                    </thead>
                    <tbody>
<?php 
$sq = mysqli_query($con, "SELECT * FROM `p_purchase` ORDER BY `pid` DESC ");
$k=1;
while($rs = mysqli_fetch_array($sq)){
 $comid = $rs['p_comp'];	
 $sq1 = mysqli_query($con,"SELECT c_comp FROM `p_company` WHERE `cid`='$comid' ");
 $rs1 = mysqli_fetch_array($sq1);	
 $vendid = $rs['p_vendor'];
 $sq2 = mysqli_query($con,"SELECT v_company FROM `p_vendor` WHERE `vid`='$vendid' ");
 $rs2 = mysqli_fetch_array($sq2);
?>
<tr>
<td><?php echo $k; ?></td>
<td><?php echo $rs1['c_comp']; ?></td>
<td><?php echo $rs['p_billno']; ?></td>
<td><?php echo $rs['p_acount']; ?></td>
<td><?php echo $rs['prod_nam']; ?></td>
<td><?php echo $rs2['v_company']; ?></td>
<td><?php echo $rs['p_netamnt']; ?></td>
<td>
&nbsp;&nbsp;  <a href="" data-toggle="modal" data-target="#myModal<?php echo $rs['pid']; ?>" style=" cursor: pointer;"><span class="glyphicon glyphicon-eye-open" style="font-size:14px;"></span></a> &nbsp; &nbsp;                	
    <a onClick="edit_purchase('<?php echo base64_encode($rs['pid']); ?>');" style=" cursor: pointer;" ><span class="fa fa-pencil" style="font-size:14px;"></span></a> &nbsp; &nbsp;
    <a onClick="delete_purchase('<?php echo base64_encode($rs['pid']); ?>');" style=" cursor: pointer;" ><span class="fa fa-trash-o" style="font-size:14px;"></span></a> &nbsp;	
</td>	
</tr>



<div class="modal fade" id="myModal<?php echo $rs['pid']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header" style=" background-color: #E8E8E8;">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Purchase #<?php echo $rs['pid']; ?></h4>
      </div>
   <div class="modal-body" id="myModal2">
  
   <div style=" width:550px; ">
   <div style=" float:left; width:150px;  font-size:14px; color:#0000FF; ">Company Name</div><div style="width:375px; ">: &nbsp;&nbsp; <?php echo $rs1['c_comp']; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Branch Name</div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_bran"]; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Bill No.</div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_billno"]; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Purchase Date </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_purdat"]; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Account Name </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_acount"]; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Product Name </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["prod_nam"]; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Vendor </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs2['v_company']; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Amount </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_amnt"]; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Discount Amount </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_discnt"]; ?></div>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Net Amount </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_netamnt"]; ?></div>
   
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Descreption </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["p_descre"]; ?></div>
   <?php if($rs["p_bill"] != ''){ ?>
   <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Uploaded Bill </div><div style="width:375px;">: &nbsp;&nbsp;
   	   <a href="Bill/<?php echo $rs["p_bill"]; ?>" target="_blank">Click Here</a>
   	</div>
   <?php } ?>
   </div>
  
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
       
      </div>
    </div>
  </div>
</div>
<?php $k=$k+1; } ?>
                    </tbody>
                    </table>
  
         		  </div>
                </div><!-- /.box-body -->
              </div><!-- /.box -->
	
	
</div>
	
		
			
			
			








                           
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
  </div><!-- /.content-wrapper -->
  </div>
    <!-- jQuery 2.1.4 -->
    <script src="<?php echo $sujpath; ?>plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="<?php echo $sujpath; ?>bootstrap/js/bootstrap.min.js"></script>
    <!-- DataTables -->
	<script src="<?php echo $sujpath; ?>plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="<?php echo $sujpath; ?>plugins/datatables/dataTables.bootstrap.min.js"></script>
    <!-- SlimScroll -->
    <script src="<?php echo $sujpath; ?>plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- FastClick -->
    <script src="<?php echo $sujpath; ?>plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="<?php echo $sujpath; ?>dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="<?php echo $sujpath; ?>dist/js/demo.js"></script>
    <!-- page script -->
    
  <script type="text/javascript">
    function PreviewImage(no) {
    	var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage"+no).files[0]);
        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview"+no).src = oFREvent.target.result;
        };
    }
</script>

<style>
label.filebutton {
    width:120px;
    height:80px;
    overflow:hidden;
    position:relative;
}
label span input {
    z-index: 999;
    line-height: 0;
    font-size: 50px;
    position: absolute;
    top: -2px;
    left: -700px;
    opacity: 0;
    filter: alpha(opacity = 0);
    -ms-filter: "alpha(opacity=0)";
    cursor: pointer;
    _cursor: hand;
    margin: 0;
    padding:0;
}
</style>



    <script>
      $(document).ready(function() {
        $("#example1").DataTable();
      });
      
	function datat(){
	  alert("Data Deleted Successfully.");
      $(document).ready(function() {
        $("#example1").DataTable();
       
      });
    }
    function datat2(){
	  alert("Data Added Successfully");
      $(document).ready(function() {
        $("#example1").DataTable();
       
      });
    }
    function datat3(){
	  alert("Updated Successfully");
      $(document).ready(function() {
        $("#example1").DataTable();
       
      });
    }
   </script>  
   
   
   
  </body>
</html>
