
<?php include("sass/icon_for_datepicker_and_datatable.php");?>
        
   
        <?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$product_name = $_POST['product_name'];
	$product_name = explode("-",$product_name);
	$product_id = $product_name[0];
	$product_name_last = $product_name[1];
	
	$quantity = $_POST['quantity'];
	$quantity_input = $quantity;
	$buying_price = $_POST['buying_price'];
	$buying_price_input = $buying_price;
	$selling_price = $_POST['selling_price'];
	$selling_price_input = $selling_price;
	$supplier_id = $_POST['supplier_id'];
	$cash = $_POST['cash'];
	$bank = $_POST['bank'];
	$due = $_POST['due'];
	$chalan_no = $_POST['chalan_no'];
	$bank_id = $_POST['bank_id'];
	$check_number = $_POST['check_number'];
	$check_date = $_POST['check_date'];
	if(!empty($check_date)) {
$newDate = date("Y-m-d", strtotime($check_date));
}
else {
$newDate = "";
}
	$date = $_POST['date'];
	if(!empty($date)) {
$date = date("Y-m-d", strtotime($date));
}
else {
$date = "";
}
	if(empty($product_name)) $errMsg="write product name!";
	elseif (empty($quantity)) $errMsg="Write Quantity";
	if(empty($errMsg))
	{
		$check=mysqli_query($connection,"select * from product_setup where product_name='$product_name_last'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows=0){$errMsg="Sorry!! enter product name first.";}
		
		else{
		
		mysqli_query($connection,"INSERT INTO store (product_id, quantity, buying_price, selling_price, supplier_id, chalan_no, date, branch_id) VALUES ('$product_id','$quantity_input','$buying_price_input','$selling_price_input','$supplier_id','$chalan_no','$date','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		mysqli_query($connection,"UPDATE product_setup SET buy_price ='$buying_price_input', selling_price ='$selling_price_input' WHERE product_id = '$product_id'")
				or die(mysqli_error()); 
				
				
				mysqli_query($connection,"UPDATE present_stock SET present_stock = present_stock + '$quantity_input' WHERE product_id = '$product_id' and branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error()); 
				
				
				mysqli_query($connection,"INSERT INTO supplier_payment (supplier_id, cash, bank, due, check_number, chalan_no, check_date, bank_id, sup_date, branch_id) VALUES ('$supplier_id','$cash','0','$due','$check_number','$chalan_no','$newDate','$bank_id','$date','$branch_id_for_header_user')")
		or die(mysqli_error()); 
		
		
		if(!empty($bank_id)) {
		//mysqli_query($connection,"UPDATE bank_name SET balance = balance - $bank WHERE bank_id = $bank_id")
				//or die(mysqli_error());
		
		mysqli_query($connection,"INSERT INTO bank_statement (amount, ref, payment_type, reason, supplier_id, bank_id, cheque_no, cheque_date, date, branch_id) VALUES ('$bank','$chalan_no','credit','4','$supplier_id','$bank_id','$check_number','$newDate','$newDate','$branch_id_for_header_user')")
		or die(mysqli_error()); 
		
		
		//mysqli_query($connection,"INSERT INTO transaction (date, ref_id, supplier_id, ledger, credit, cheque_no, cheque_date, bank_id, hint, payment_type) VALUES ('$date','$chalan_no','$supplier_id','4','$bank','$check_number','$newDate','$bank_id','supplier payment','bank')")
		//or die(mysqli_error());
		
		
		
		
		}
		else {}
		
		if(!empty($cash)) { 
		
		 
		
			mysqli_query($connection,"INSERT INTO transaction (date, ref_id, supplier_id, ledger, credit, hint, payment_type, branch_id) VALUES ('$date','$chalan_no','$supplier_id','4','$cash','supplier payment','cash','$branch_id_for_header_user')")
		or die(mysqli_error());	
		
		mysqli_query($connection,"UPDATE cash SET cash = cash - $cash WHERE branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error());
		
		 }
		 
		if(!empty($due)) { 
		
			mysqli_query($connection,"INSERT INTO transaction (date, ref_id, supplier_id, ledger, credit, hint, payment_type, branch_id) VALUES ('$date','$chalan_no','$supplier_id','5','$due','supplier due','due','$branch_id_for_header_user')")
		or die(mysqli_error());	
		
		
		 }
		 
		$errMsg="Product Purchased successfully";
	}
}
}
?>
<!--For connect database end-->
<?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;
}

  ?>

   <script>
  $(function() {
  
  var availableTags = JSON.parse('<?php echo json_encode($all_product); ?>');
  
    
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>         
        
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- 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">Purchase Product</li>
                    </ol>
                </section>
                 <section class="content">
                 <!---Main Content--->
                 
                     <div class="row">
                     
                         <div class="col-md-8 col-md-offset-2">
                   
                       
                        <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                        
                         <div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">Purchase Product</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Product Name</label>
                                            <input type="text" class="form-control" name="product_name" id="tags" placeholder="write product name" required>
                                        </div>  
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Quantity</label>
                                            <input type="text" class="form-control" name="quantity" id="" placeholder="write quantity" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Buy Price</label>
                                            <input type="text" class="form-control" name="buying_price" id="" placeholder="write buy price" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Selling Price</label>
                                            <input type="text" class="form-control" name="selling_price" id="" placeholder="write selling price" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Supplier</label>
                                            <select name="supplier_id" class="form-control">
                                            
												<?php  
                                                $query = "select * from supplier_setup ";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['supplier_id'] . "'>" . $row_techname['supplier_name'] . "</option>";}?> 
                                            
                                            </select>
                                            
                                            
                                            
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cash Payment</label>
                                            <input type="number" class="form-control" name="cash" id="" placeholder="Enter">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Bank Payment</label>
                                            <input type="number" class="form-control" name="bank" id="" placeholder="Enter">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Bank Name</label>
                                            <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>
                                            
                                            
                                            
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Due</label>
                                            <input type="number" class="form-control" name="due" id="" placeholder="Enter">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cheque Number</label>
                                            <input type="text" class="form-control" name="check_number" id="" placeholder="Enter">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Chalan Number</label>
                                            <input type="text" class="form-control" name="chalan_no" required placeholder="Enter">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cheque Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="check_date" placeholder="Enter">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="date" value="<?php echo date("d-m-Y");?>" placeholder="Enter">
                                        </div>
                                    </div><!-- /.box-body -->

                                    <div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                   

  


                                </form>
                            </div>
                         
                         </div>
                         
                         
                        
                     
                     </div>
                 
                 
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->

 <script>
function EditProduct(id) {
    window.open('edit_store.php?id='+id,'logos','width=500,height=500');}
</script>

  <!-- jQuery 2.0.2 -->
       
      <?php include("sass/footer_for_datepicker_and_datatable.php");?>

    </body>
</html>