<?php include("sass/icon_for_datepicker_and_datatable.php");?>


<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
		
        return;
    } else { 
	
	var tags = document.getElementById('tags').value ;
		var quantity = document.getElementById('quantity').value ;
		var sell = document.getElementById('sell').value ;
		
		document.getElementById('tags').value = '';
		document.getElementById('quantity').value = '';
		document.getElementById('sell').value = '';
		document.getElementById("tags").focus();
	
        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 (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "getsale.php?tags=" + tags + "&quantity=" + quantity + "&sell=" + sell, true);
        xmlhttp.send();
		
		
    }
}
</script>

        
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>
<?php


$query_product = "SELECT * FROM product_setup";
$result_product = mysqli_query($connection,$query_product);

while($row_product=mysqli_fetch_array($result_product))
{
	$product_id = $row_product['product_id'];
	$product_name = $row_product['product_name'];
	
	$all_product[] = $product_id."-".$product_name;
}

  ?>
<link rel="stylesheet" href="js1/jquery-ui.css">
  <script src="js1/jquery-2.1.1.min.js"></script>
  <script src="js1/jquery-ui.js"></script>
   <script>
  $(function() {
  
  var availableTags = JSON.parse('<?php echo json_encode($all_product); ?>');
  
    
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script> 
  
  <?php
if(isset($_POST['new_single_sale_submit']))
    {
$product_name_input = $_POST['product_name'];
$quantity_input = $_POST['quantity'];


$carton = $_POST['carton'];

$pk_quantity = $_POST['pk_quantity'];
if (!empty($pk_quantity)) { 
$quantity_input_en = $carton ;
}
$sell = $_POST['sell'];
$pk_sell = $_POST['pk_sell'];

$product_name_ex = explode("-",$product_name_input);
	$product_id_ex = $product_name_ex[0];
	$product_name_last = $product_name_ex[1];


if (empty($product_name_input)) {
 $message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
      aria-hidden='true'>
      &times;
   </button>
  please write product name
</div>";
 
 }
 elseif (empty($quantity_input) && empty($pk_quantity)) {
 $message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
      aria-hidden='true'>
      &times;
   </button>
  please write product quantity
</div>";
 
 }
 
 
 else {
 
 $check=mysqli_query($connection,"select * from product_setup INNER JOIN present_stock
ON product_setup.product_id=present_stock.product_id where present_stock.product_id = $product_id_ex AND present_stock.present_stock < $quantity_input and present_stock.branch_id  = '$branch_id_for_header_user'");
$checkrows=mysqli_num_rows($check);
if($checkrows>0){$message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
      aria-hidden='true'>
      &times;
   </button>
   sorry!!! stock is short. 
</div>";}

else{ 

$result_for_product_insert = mysqli_query($connection,"SELECT * FROM product_setup INNER JOIN present_stock
ON product_setup.product_id=present_stock.product_id WHERE product_setup.product_id  = $product_id_ex AND present_stock.branch_id  = '$branch_id_for_header_user'");
		$test_for_product_insert = mysqli_fetch_array($result_for_product_insert);
				
				$buy_price=$test_for_product_insert['buy_price'];
				if(empty($sell)){
				$selling_price=$test_for_product_insert['selling_price'];
				}
				else {
				$selling_price=$sell ;
				}
				if(!empty($pk_sell)){
				$selling_price=$pk_sell ;
				}
				$present_stock=$test_for_product_insert['present_stock'];
				$unit_type_id=$test_for_product_insert['unit_type_id'];
				
				

if (!empty($pk_quantity)) {
mysqli_query($connection,"INSERT INTO sale(product_id, quantity, buy_price, selling_price, status, carton, stock_of, branch_id) 
VALUES ('$product_id_ex','$pk_quantity','$buy_price','$selling_price','pending','$carton','$stock_of','$branch_id_for_header_user')")
or die(mysqli_error());

}

else {
mysqli_query($connection,"INSERT INTO sale(product_id, quantity, buy_price, selling_price, status, branch_id) 
VALUES ('$product_id_ex','$quantity_input','$buy_price','$selling_price','pending','$branch_id_for_header_user')")
or die(mysqli_error());

}


}

}

}
?>
<style type="text/css">
#sub3 {
display: none;
}
#sub4 {
display: none;
margin-top: 5px;
}
</style>
<script language="JavaScript">
function setVisibility(id) {
if(document.getElementById('bt1').value=='Hide'){
document.getElementById('bt1').value = 'Show';
document.getElementById(id).style.display = 'none';
}else{
document.getElementById('bt1').value = 'Hide';
document.getElementById(id).style.display = 'inline';
}
} 
</script>

<script language="JavaScript">
function setPakSale(id) {
if(document.getElementById('bt2').value=='Hide'){
document.getElementById('bt2').value = 'Show';
document.getElementById(id).style.display = 'none';
}else{
document.getElementById('bt2').value = 'Hide';
document.getElementById(id).style.display = 'inline';
}
} 
</script>
 <script type="text/javascript"><!--
function updatesum() {
document.form.sum.value = ((document.form.sum1.value -0) * (document.form.ctw.value -0) );
}
//--></script>
  
            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Dashboard</li>
                    </ol>
                </section>
                 <section class="content">
                 	 <div class="row">
                        <div class="col-md-8">
                 <!---Main Content--->
                 		
                 			
                            <div class="box box-success">
                                <div class="box-header">
                                    <i class="fa fa-shopping-cart"></i>
                                    <h3 class="box-title">Sell Product</h3>
                                
                                </div><!-- /.box-header -->
                                <div class="box-body">
                                 <?php
							if (!empty($message))
							{echo "$message";}
						
                        ?> <form role="form" name="form" action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                   
                                    <div class="row">
                                    	<div class="col-md-4">
                                        <input type="input" class="form-control" name="product_name" id="tags" placeholder="write product name" autofocus required>
                                        </div>
                                        <div class="col-md-2">
                                        	<input class="form-control" type="number" name="quantity" id="quantity" placeholder="quantity" required onKeyUp="enableok()" />
                                        </div>
                                        <div class="col-md-3">
                                        	<input class="form-control" type="input" name="sell" id="sell" placeholder="write per unit price"/>
                                        </div>
                                    </div>
                                    <div class="row" id="sub4">
                                    	<div class="col-md-2">
                                        <input class="form-control" type="text" id="ctw" value="50" />
                                        </div>
                                        <div class="col-md-5">
                                        
                                            <input type="text" class="form-control" name="carton" id="sum1" placeholder="write sack number" onKeyUp="updatesum()" >
                                          
                                        </div>
                                        <div class="col-md-3">
                                         <input type="text" class="form-control" name="pk_quantity" id="sum" >
                                        </div>
                                        
                                        <div class="col-md-2">
                                         <input type="text" class="form-control" name="pk_sell"  placeholder="write selling price">
                                        </div>
                                     </div>
                                    <div class="row">
                                        <div class="col-md-3 pull-right" style="margin-top:5px;">
                                        	<input type="button" name="new_single_sale_submit" id="ok" onclick="showUser(this.value)" class="btn btn-success" value="OK" disabled>
                                        </div>
                                        </div>
                                    </form>
                                    <br />
                                    <form role="form" target="_blank" action="sale_submit.php" method="post">
                                    <div class="row">
									
									
									<span id="txtHint">
									
									
										
									
                                    <table class="table table-hover">
 										<tr>
                                        <th>Serial</th>
                                        <th>Name</th>
                                        <th>Per Unit Price</th>
                                        <th>Total Unit</th>
                                        <th>In Stock</th>
                                        <th>Total Price</th>
                                        </tr>
                                        
                                       <?php
                                        
                                        $query = "SELECT * FROM sale where status = 'pending' AND user_id = '$user_id_for_header_user' AND branch_id = '$branch_id_for_header_user'";
                                        $result = mysqli_query($connection,$query);
                                        
										$i=0;
										$grand_total = 0 ;
                                        while($row=mysqli_fetch_array($result))
                                        {
                                        
										 $i++;
                                        $sale_id = $row['sale_id'];
                                        $product_id_to = $row['product_id'];
										
										

$result_for_product = mysqli_query($connection,"SELECT * FROM product_setup INNER JOIN present_stock
ON product_setup.product_id=present_stock.product_id WHERE product_setup.product_id  = $product_id_to and present_stock.branch_id = '$branch_id_for_header_user'");
		$test_for_product = mysqli_fetch_array($result_for_product);
				
				$product_name_to=$test_for_product['product_name'];
					$present_stock=$test_for_product['present_stock'];
					 $present_stock = $present_stock ;
					 $present_stock_show = ($present_stock);
					 
				$unit_type_id=$test_for_product['unit_type_id'];
				
				$result_for_unit_type = mysqli_query($connection,"SELECT * FROM unit_type WHERE unit_type_id  = '$unit_type_id'");
		$test_for_unit_type = mysqli_fetch_array($result_for_unit_type);
				
				$unit_type_name=$test_for_unit_type['unit_type_name'];
										
										if($unit_type_name == "packet") {
										$present_stock=$test_for_product['present_stock'];
					
					 $present_stock_show = ($present_stock);
										}
										
                                         $quantity = $row['quantity'];
										 $quantity_con = $quantity ;
										  $quantity_show = ($quantity_con);
										  
										$selling_price = $row['selling_price'];
                                       $selling_price_show = ($selling_price);
                                        $total = $quantity * $selling_price ;
										
										$grand_total = $grand_total + $total ;
										
                                        $total_show = ($total);
											$carton = $row['carton'];
											$packate = $row['packate'];	
																		
                                        echo "<tr>
                                                <td>$i</td>
                                                <td>$product_name_to</td>
                                                <td>$selling_price_show</td>";
												if($carton == 0 && $packate == 0) {
												
                                               echo "<td>$quantity_show $unit_type_name</td>";
												}
												else {
												
												if($carton <> 0 && $packate == 0) {
												echo "<td>$carton sack(s)</td>";
												}
												elseif($carton == 0 && $packate <> 0) {
												echo "<td>$packate packet</td>";
												}
												else {
												echo "<td>$carton carton $packate packet</td>";
												}
												
												}
												
                                           if($carton <> 0 || $packate <> 0) {
												echo "<td>$present_stock_show sack(s)</td>";
												}
										   else {
										   echo     "<td>$present_stock_show $unit_type_name</td>";
										   }
                                            echo    "<td>$total_show</td>
                                                                                            
                                                <td>
                                                
                                             <button type='button' class='btn btn-danger' data-toggle='modal' data-target='#myModal$i'><i class='fa fa-minus-square'></i>&nbsp; Remove</button> 
                                                </td>
                                            </tr>";
											echo "<div class='modal fade' id='myModal$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>be sure</h4>
      </div>
      <div class='modal-body'>
       are you sure want to delete <strong> $product_name_to </strong>?
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
        <a href='confirm_delete_sale.php?sale_id=$sale_id'><button type='button' class='btn btn-danger'>Yes</button></a>
      </div>
    </div>
  </div>
</div>";
											}	
                                        ?>
                                        <tr>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th> Total Price </th>
                                        <th><?php echo ($grand_total); ?> <input type="hidden" name="have_to_pay_original" class="form-control" value="<?php echo ($grand_total); ?>"  id="total" /></th>
                                        </tr>
									</table>
									
									
									</span>
									
									
									
									
									
								
                                    </div>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                     
                 
                 		</div>
                        <div class="col-md-4">
                        	
                            	<div class="box box-info">
                                <div class="box-header">
                                    <i class="fa fa-file-text"></i>
                                    <h3 class="box-title">Sale Details</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body">
                                
                                	<table style="margin:auto">
<input type="hidden" name="decrese" value="yes" />
	<input type="hidden" class="form-control" value="<?php echo ($grand_total); ?>" name="have_to_pay" /></span>
	<span class="input-wrap">	<input type="hidden" class="form-control" name="transport_name" value="" /></span> 

<tr><td valign="top">
<label for="text">Cash Payment</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" value="<?php echo ($grand_total); ?>" name="cash" /></span> 
</td></tr>
<tr><td valign="top">
<label for="text">Bank Payment</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="bank" value="" /></span> 
</td></tr>
	<span class="input-wrap">	<input type="hidden" class="form-control" name="transport_cost" value="" /></span> 

	<span class="input-wrap">	<input type="hidden" class="form-control" name="vehicle_cost" value="" /></span> <br />
 <input name="plus_minus" type="hidden" value="p" checked /> <input name="plus_minus" type="hidden" value="m" />

	<span class="input-wrap">	<input type="hidden" class="form-control" name="beton" value="" /></span> 

<tr><td valign="top">
<label for="text">Discount</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="discount" value="" /></span> 
</td></tr>


<tr><td valign="top">
<label for="text">Vat(%)</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" id="percenttax" onKeyUp="updatesumtax()" /></span> 
</td></tr>



<tr><td valign="top">
<label for="text">Vat</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="tax" value="0"  id="tax"/></span> 
</td></tr>

<tr><td valign="top">
<label for="text">Bank Name</label>	
</td><td>
	<span class="input-wrap">	<select name="bank_id" class="form-control">
                                            <option value="">Select</option>
												<?php  
                                                $query_bank = "select * from bank_name";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
                                                
                                                echo "<option value='" . $row_bank['bank_id'] . "'>" . $row_bank['bank_name'] . "</option>";}?> 
                                            
                                            </select></span> 
</td></tr>

<tr><td valign="top">
<label for="text">Cheque Number</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="cheque_no" value="" /></span> 
</td></tr>

<tr><td valign="top">
<label for="text">Cheque Date</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control datepicker" autocomplete="off" name="cheque_date" value="" /></span> 
</td></tr>



<tr><td valign="top">
<label for="text">Wholeseller Name</label>	
</td><td>
<select name="customer_id" class="form-control">
 <option value="">Select</option>
                                            
												<?php  
                                                $query = "select * from customer_setup WHERE branch_id = '$branch_id_for_header_user'";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['customer_id'] . "'>" . $row_techname['customer_name'] . "</option>";}?> 
                                            
                                            </select>
</td></tr>
<tr>
<td>
<label for="text">Retail Sell</label>
</td>
<td>
<input type="button" name="type" id='bt1' value='Show' onclick="setVisibility('sub3');";> 
</td>
</tr>

<tr id="sub3">
<td>
<label for="text">Details of retail Seller</label>
</td>
<td>
<input type="input" class="form-control" name="retail_name" placeholder="name"/>
<br />
<input type="input" class="form-control" name="retail_address" placeholder="address"/>
</td>
</tr>

<tr>
<td>
<label for="text"></label>	
</td>
<td>
<input style="width:90px"  type="submit" name="submit" value="Submit" />
</td>
</tr>
	
</table>

                                
                                </div>
                            	
                            
                            
                        
                        
                        </div>
                    </div>
                 
                 </form>
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->
<script type="text/javascript">
function updatesum() {
var total = document.getElementById('total').value ;
var percent = document.getElementById('percent').value ;
var discount = document.getElementById('discount')

discount.value = ((percent -0) / 100) * (total -0) ;
}

function updatesumtax() {
var total = document.getElementById('total').value ;
var percenttax = document.getElementById('percenttax').value ;
var tax = document.getElementById('tax')

tax.value = ((percenttax -0) / 100) * (total -0) ;
}


function enableok() {
document.getElementById("ok").disabled = false;
}
</script>
        <!-- add new calendar event modal -->
 <?php include("sass/footer_for_datepicker_and_datatable.php");?>
    </body>
</html>