<?php 
   session_start(); 
   ob_start();
   include("../leadsnew/branch_connection.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_billing(){
             var rcompany = $('#rcompany').val();
             var rbranch = $('#rbranch').val();
             var rcust = $('#rcust').val();
             var recorders = $('#recorders').val();
             var ramount = $('#ramount').val();
             var rdiscount = $('#rdiscount').val();
             var rnet = $('#rnet').val();
             var rremarks = $('#rremarks').val();
             if($("#custsmssent").is(':checked'))
                 var custsmssent =1;
             else
                 var custsmssent =0;
               
             if(rcompany == ''){
                alert("Select Company");   	
             }  else if(rbranch == ''){
             	alert("Select Branch");
             } else if(rcust == ''){
             	alert("Select Customer");
             } else if(recorders == ''){
             	alert("Select Order");
             } else if(ramount == ''){
             	alert("Enter Amount");
             }
             else {                    
                  $.ajax({
                    type: "POST",
                    url: "ajaxmarket.php",
                     data: {
                          add_new_billing : 1,
                          rcompany         : rcompany,
                          rbranch          : rbranch,
                          rcust            : rcust,
                          recorders        : recorders,
                          ramount          : ramount,
                          rdiscount        : rdiscount,
                          rnet             : rnet,
                          rremarks         : rremarks,
                          custsmssent      : custsmssent
                         
                     },
                     cache: true,
                     success: function(res23)
                     {
                     	   	$('#rcompany').val('');
                     	   	$('#rbranch').val('');
                     	   	$('#rcust').val('');
                           	$('#recorders').val('');
                           	$('#ramount').val('');
                           	$('#rdiscount').val('');
                           	$('#rnet').val('');
                           	$('#rremarks').val('');
                           	$("#marketingdiv123").load("sales.php #marketingdiv123");   
                            datat2();                    
                      }
                   }); 
    	    } 	
    }


    	
    	
        function edit_news_billing()
        {
             var rcompany = $('#rcompany').val();
             var rbranch = $('#rbranch').val();
             var rcust = $('#rcust').val();
             var recorders = $('#recorders').val();
             var ramount = $('#ramount').val();
             var rdiscount = $('#rdiscount').val();
             var rnet = $('#rnet').val();
             var rremarks = $('#rremarks').val();
             var hidvall = $('#hidvall').val();
             if(rcompany == ''){
                alert("Select Company");   	
             } else if(rbranch == ''){
             	alert("Select Branch");
             } else if(rcust == ''){
             	alert("Select Customer");
             } else if(recorders == ''){
             	alert("Select Order");
             } else if(ramount == ''){
             	alert("Enter Amount");
             }
             else {                    
                  $.ajax({
                    type: "POST",
                    url: "ajaxmarket.php",
                     data: {
                          edit_new_billing : 1,
                          rcompany         : rcompany,
                          rbranch          : rbranch,
                          rcust            : rcust,
                          recorders        : recorders,
                          ramount          : ramount,
                          rdiscount        : rdiscount,
                          rnet             : rnet,
                          rremarks         : rremarks,
                          hidvall          : hidvall
                         
                     },
                     cache: true,
                     success: function(res23)
                     {
                     	   	$('#rcompany').val('');
                     	   	$('#rbranch').val('');
                     	   	$('#rcust').val('');
                           	$('#recorders').val('');
                           	$('#ramount').val('');
                           	$('#rdiscount').val('');
                           	$('#rnet').val('');
                           	$('#rremarks').val('');
                           	$("#marketingdiv123").load("sales.php #marketingdiv123");   
                            datat2();                    
                      }
                   }); 
    	    } 
        }
        
        
    	
    	    	
    	function delete_sales(id)
    	{
    		var result = confirm("Want to delete?");
			if (result) 
			{
     		$.ajax({
			type: "POST",
			url: "ajaxmarket.php",
			data: {
			btn_delete_sales: 1,
			id:id
			},
			cache: true,
			success: function(res)
			{
			      $("#marketingdiv123").load("sales.php #marketingdiv123");
			      datat();
         	
			}  
			});
   			}
    	}
    	
    	
    	function edit_sales(id)
    	{
    		$.ajax({
			type: "POST",
			url: "ajaxmarket.php",
			data: {
			btn_edit_market: 1,
			id:id
			},
			cache: true,
			success: function(res)
			{
			
	         	$("#marketingdiv2").load("sales.php?idd2="+id+" #marketingdiv2"); 	
			}  
			});
    	}
    	
         
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 orderno_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("order_amt_chang").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajaxmarket.php?order_amnt_auto="+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> Sales & Billing
          	<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">Sales & Billing</li> 
          </ol>
        </section>
  <!-- Main content -->
        <section class="content" >
          <div class="row" >
            <div class="col-xs-12" >





   <div class="box-body" id="marketingdiv2">
 	<?php
    
    $amo1='';
    $dis1='';
    $net1='';
    $remark1='';
    if(!empty($_GET['idd2'])){
 	  $idd2 = strip_tags(base64_decode($_GET['idd2']));
	  $sq = mysqli_query($con, "SELECT * FROM `p_billing` WHERE `bid`='$idd2' " );
	  $editRow = mysqli_fetch_assoc($sq);

  $amo1=$editRow['bamnt'];
$dis1=$editRow['bdiscount'];
$net1=$editRow['bnet'];
$remark1=$editRow['bremarks'];

    }

 	  
 	?>
			  	
	<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($conn,"SELECT cid, c_comp FROM `p_company`");
while($res2 = mysqli_fetch_array($sq2)){
   if(!empty($editRow) && $editRow['bcomp'] == $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-3" 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 = !empty($editRow) ? $editRow['bbranch'] : '';
$p_comp = !empty($editRow) ? $editRow['bcomp'] : '';

		$bselect = mysqli_query($conn,"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-3" style="padding-left: 2px; padding-right: 0px;">					
  <div class="form-group">
	<select class="form-control" id="rcust" name="rcust" onchange="cust_change(this.value);">
		<option value="">Customer</option>
	<?php
$cust = mysqli_query($conn, "
    SELECT DISTINCT tel_id, customer_name
    FROM marketing_order
    WHERE status = 'Completed'
      AND category = 'Order Confirmed'
      AND customer_name != ''
");

while($sj = mysqli_fetch_assoc($cust)){
    $selected = (!empty($editRow) && $editRow['bcust'] == $sj['tel_id']) ? 'selected' : '';
    echo '<option value="'.$sj['tel_id'].'" '.$selected.'>'
         .$sj['customer_name'].
         '</option>';
}
?>

	</select>	
 </div></div>	
 
  <div class="col-md-3" style="padding-left: 2px; padding-right: 0px;">					
  <div class="form-group">
	<select class="form-control" id="recorders" name="recorders" onchange="orderno_change(this.value);">
		<option value="">Order No.</option>
		<?php
		$border_no = !empty($editRow) ? $editRow['border_no'] : '';
$bcust     = !empty($editRow) ? $editRow['bcust'] : '';
		$slcust = mysqli_query($con, "SELECT * FROM `p_orders` WHERE `ocustm`='$bcust'");
		while($rlcust = mysqli_fetch_array($slcust)){ 
		  if($border_no == $rlcust['order_id']){ ?>
			<option value="<?php echo $rlcust['order_id']; ?>" selected><?php echo $rlcust['ordr_type']."-".$rlcust['order_id']; ?></option>			
  <?php } else { ?>				
			<option value="<?php echo $rlcust['order_id']; ?>"><?php echo $rlcust['ordr_type']."-".$rlcust['order_id']; ?></option>
  <?php } } ?>
	</select>
 </div></div>
 

 
  
 	
 	</div>	

</div>	


<div class="row" >	
<div class="col-md-12" style="" >
	
<div id="order_amt_chang">
	
			
<div class="col-md-2"  style=" padding-left: 2px; padding-right: 0px; ">			
<div class="form-group">
     <input name="ramount" id="ramount" 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="rdiscount" id="rdiscount" 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="rnet" id="rnet" onkeypress="return isNumber2(event)" class="form-control" placeholder="Net Amount" value="<?php echo $net1; ?>" >
</div></div>

</div>


<div class="col-md-4" style=" padding-left: 2px; padding-right: 0px; ">					
<div class="form-group">
   <textarea name="rremarks" id="rremarks" class="form-control" placeholder="Remarks" style=" height: 35px;"><?php echo $remark1; ?></textarea>		
</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 $editRow['bid']; ?>" />
<input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Update" onclick="edit_news_billing();" style=" float: right; 
margin-right: 20px; " /> 

<?php } else { ?>
	<input type="checkbox" name="custsmssent" id="custsmssent" value="sms" >&nbsp; Sent SMS
<input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Submit" onclick="add_new_billing();" 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>Bill No.</th>                                        	
<th>Branch</th>
<th>Customer</th>
<th>Order No</th>
<th>Amount</th>
<th>Date</th>
<th></th>
</tr>
                    </thead>
                    <tbody>
<?php 
$sq = mysqli_query($con, "SELECT * FROM `p_billing` ORDER BY `bid` DESC ");
$k=1;
while($rs = mysqli_fetch_array($sq)){
 $rdate = $rs['bdate'];
 $exp = explode("-",$rdate);
 $yearr =  substr($exp[0], 0, 4);
 $bill_dat = $exp[2].'/'.$exp[1].'/'.$yearr; 
 
 $comid = $rs['bcomp'];	
 $sq1 = mysqli_query($con,"SELECT c_comp FROM `p_company` WHERE `cid`='$comid' ");
 $rs1 = mysqli_fetch_array($sq1);	
 $cusid = $rs['bcust'];
 $sq2 = mysqli_query($con,"SELECT cname FROM `p_customers` WHERE `cid`='$cusid' ");
 $rs2 = mysqli_fetch_array($sq2);
?>
<tr>
<td><?php echo $k; ?></td>
<td><?php echo $rs['bill_no']; ?></td>
<td><?php echo $rs['bbranch']; ?></td>
<td><?php echo $rs2['cname']; ?></td>
<td><?php echo $rs['border_no']; ?></td>
<td><?php echo $rs['bamnt']; ?></td>
<td><?php echo $bill_dat; ?></td>
<td>
&nbsp;<a href="invoice/index.php?bill=<?php echo base64_encode($rs['bid']); ?>" style=" cursor: pointer;" target="_blank"><span class="fa fa-print" style="font-size:14px;"></span></a>	
&nbsp;&nbsp;  <a href="" data-toggle="modal" data-target="#myModal<?php echo $rs['bid']; ?>" style=" cursor: pointer;"><span class="glyphicon glyphicon-eye-open" style="font-size:14px;"></span></a> &nbsp; &nbsp;                	
    <a onClick="edit_sales('<?php echo base64_encode($rs['bid']); ?>');" style=" cursor: pointer;" ><span class="fa fa-pencil" style="font-size:14px;"></span></a> &nbsp; &nbsp;
    <a onClick="delete_sales('<?php echo base64_encode($rs['bid']); ?>');" style=" cursor: pointer;" ><span class="fa fa-trash-o" style="font-size:14px;"></span></a> 	
</td>	
</tr>


<div class="modal fade" id="myModal<?php echo $rs['bid']; ?>" 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">Order No #<?php echo $rs["border_no"]; ?></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 </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["bbranch"]; ?></div>
     <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Customer </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs2['cname']; ?></div>
     <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Amount </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["bamnt"]; ?></div>
     <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Discount Amount </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["bdiscount"]; ?></div>
     <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Net Amount</div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["bnet"]; ?></div>
     <div style=" float:left; width:150px;font-size:14px;color:#0000FF;">Remarks </div><div style="width:375px;">: &nbsp;&nbsp; <?php echo $rs["bremarks"]; ?></div>
  </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>
