<?php include("sass/icon_for_datepicker_and_datatable.php");?>
          
        
      <?php
if(isset($_POST['submit']))
    {

		$have_to_pay_en = $_POST['have_to_pay'];
		$cash = $_POST['cash'];
		$bank = $_POST['bank'];
		$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 = "";
		}
		$supplier_id = $_POST['supplier_id'];
		$chalan_no = $_POST['chalan_no'];
		
		$due = $have_to_pay_en -($cash + $bank) ;
		
		mysqli_query($connection,"UPDATE store_product SET chalan_no ='$chalan_no', status ='2' WHERE status = '1' AND branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());

	$query_present_stock = "SELECT * FROM store_product where chalan_no ='$chalan_no' AND branch_id  = '$branch_id_for_header_user'";
                                        $result_present_stock = mysqli_query($connection,$query_present_stock);
                                        
										
										
                                        while($row_present_stock=mysqli_fetch_array($result_present_stock))
                                        {
                                        
										
                                        $quantity_prensent_stock = $row_present_stock['quantity'];
										
									$buying_price = $row_present_stock['buying_price'];
$selling_price = $row_present_stock['selling_price'];									
										
										$product_id_prensent_stock = $row_present_stock['product_id'];
										$buying_price = $row_present_stock['buying_price'];
										$selling_price = $row_present_stock['selling_price'];
										
						mysqli_query($connection,"UPDATE product_setup SET buy_price ='$buying_price', selling_price ='$selling_price' WHERE product_id = '$product_id_prensent_stock'")
				or die(mysqli_error()); 
										
							$check=mysqli_query($connection,"select * from present_stock WHERE product_id = $product_id_prensent_stock AND branch_id  = '$branch_id_for_header_user'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>0){
			
			
			mysqli_query($connection,"UPDATE present_stock SET present_stock = present_stock + $quantity_prensent_stock WHERE product_id = $product_id_prensent_stock AND branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error()); 
			
			
		}
		
		else{	
				mysqli_query($connection,"INSERT INTO present_stock (product_id, present_stock, branch_id) VALUES ('$product_id_prensent_stock','$quantity_prensent_stock','$branch_id_for_header_user')")
		or die(mysqli_error());
		}
										
										}
										
										
				mysqli_query($connection,"INSERT INTO store (amount, supplier_id, chalan_no, stock_of, date, branch_id) VALUES ('$have_to_pay_en','$supplier_id','$chalan_no','$branch_id_for_header_user','$date','$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,"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','$date','$branch_id_for_header_user')")
		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,"UPDATE supplier_setup SET due = due + $due WHERE supplier_id = '$supplier_id'")
				or die(mysqli_error());			
		
			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());	
		
		
		 }
		
										

print "<script>";
	print "self.location='purchase_product.php';";
	print "</script>";

}
?>