<?php include("sass/icon_for_datepicker_and_datatable.php");?>
          
        
      <?php
if(isset($_POST['submit']))
    {
		$have_to_pay_original = $_POST['have_to_pay_original'];
$have_to_pay_en = $_POST['have_to_pay'];
$transport_name = $_POST['transport_name'];
$cash_en = $_POST['cash'];
$bank_en = $_POST['bank'];
$transport_cost = $_POST['transport_cost'];

$vehicle_cost = $_POST['vehicle_cost'];
$plus_minus = $_POST['plus_minus'];

if(empty($have_to_pay_en)) {
	
$have_to_pay_en = $have_to_pay_original ;
	
}


if ($plus_minus == "p") {

$have_to_pay_en = $have_to_pay_en + $vehicle_cost ;

}
else {
$have_to_pay_en = $have_to_pay_en - $vehicle_cost ;

}

$beton = $_POST['beton'];
$discount = $_POST['discount'];
$tax = $_POST['tax'];
$bank_id = $_POST['bank_id'];
$payment = ($cash_en + $bank_en);
$due = ($have_to_pay_en + $transport_cost + $beton + $tax) - ($payment + $discount) ;
$cheque_no = $_POST['cheque_no'];
$cheque_date = $_POST['cheque_date'];
if(!empty($cheque_date)) {
$newDate = date("Y-m-d", strtotime($cheque_date));
}
else {
$newDate = "";
}
$customer_id = $_POST['customer_id'];
$retail_name = $_POST['retail_name'];
$retail_address = $_POST['retail_address'];
$saledate = date("Y-m-d");
$sql="select MAX(RIGHT(invoice_no,10)) as invoice_no from sale where invoice_no like '$branch_id_for_header_user-INV%'";
	$result=mysqli_query($connection,$sql);

	if($row=mysqli_fetch_array($result))
	{
	$invoice_no=$row['invoice_no'];
	}
	else
	{
	$invoice_no=0;
	}
	$invoice_no++;
	$invoice_no="$branch_id_for_header_user-INV".str_pad($invoice_no,10,0,STR_PAD_LEFT);


if (empty($have_to_pay_en)) {
 $message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
      aria-hidden='true'>
      &times;
   </button>Please Write Have to Pay!
</div>";
 
 }

 
 else {
 

	mysqli_query($connection,"UPDATE sale SET invoice_no ='$invoice_no' , date ='$saledate' , status = 'complete' , transport_cost = '$transport_cost' WHERE status = 'pending' AND branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error()); 
				
	$query_present_stock = "SELECT * FROM sale where invoice_no ='$invoice_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'];
										
										 
										
										$product_id_prensent_stock = $row_present_stock['product_id'];
										
										$carton_prensent_stock = $row_present_stock['carton'];
										
										if ($carton_prensent_stock !=0) { 
										
										$quantity_prensent_stock = $carton_prensent_stock ;
										}
										
										
					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());
										
										}	

if (empty($customer_id)) { 

mysqli_query($connection,"INSERT INTO sale_taka(invoice_no, have_to_pay, payment, cash, bank, vehicle_cost, beton, discount, due, tax, cheque_no, cheque_date, bank_id, date, retail_name, retail_address, transport_name, v_plus_minus, branch_id) 
VALUES ('$invoice_no','$have_to_pay_en',$payment,'$cash_en','$bank_en','$vehicle_cost','$beton','$discount','$due','$tax','$cheque_no','$newDate','$bank_id','$saledate','$retail_name','$retail_address','$transport_name','$plus_minus','$branch_id_for_header_user')")
or die(mysqli_error());

if(!empty($bank_id)) {


		/*mysqli_query($connection,"UPDATE bank_name SET balance = balance + $bank_en WHERE bank_id = $bank_id")
				or die(mysqli_error());*/
		
		mysqli_query($connection,"INSERT INTO bank_statement (amount, ref, payment_type, reason, customer_id, bank_id, cheque_no, cheque_date, date, branch_id) VALUES ('$bank_en','$invoice_no','debit','9',0,'$bank_id','$cheque_no','$newDate','$newDate','$branch_id_for_header_user')")
		or die(mysqli_error()); 
		
		
		
		
		}
		else {
		
		 mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, debit, hint, payment_type, branch_id) VALUES ('$saledate','$invoice_no','9','$cash_en','sale retail customer','cash','$branch_id_for_header_user')")
		or die(mysqli_error());	
		 
		 
		 
		 mysqli_query($connection,"UPDATE cash SET cash = cash + $cash_en WHERE branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error());
		
		}

}

else {
			$result_for_cus_name = mysqli_query($connection,"SELECT opening_due, due FROM customer_setup WHERE customer_id  = $customer_id");
		$test_for_cus_name = mysqli_fetch_array($result_for_cus_name);
				
				$opening_due=$test_for_cus_name['opening_due'];
				$due_bf=$test_for_cus_name['due'];
				$beforedue = $opening_due + $due_bf ;

mysqli_query($connection,"INSERT INTO sale_taka(invoice_no, have_to_pay, payment, beforedue, cash, bank, vehicle_cost, beton, discount, due, tax, cheque_no, cheque_date, bank_id, date, customer_id, v_plus_minus, branch_id) 
VALUES ('$invoice_no','$have_to_pay_en','$payment','$beforedue','$cash_en','0','$vehicle_cost','$beton','$discount','$due','$tax','$cheque_no','$newDate','$bank_id','$saledate','$customer_id','$plus_minus','$branch_id_for_header_user')")
or die(mysqli_error());

if(!empty($bank_id)) {


		/*mysqli_query($connection,"UPDATE bank_name SET balance = balance + $bank_en WHERE bank_id = $bank_id")
				or die(mysqli_error());*/
		
		mysqli_query($connection,"INSERT INTO bank_statement (amount, ref, payment_type, reason, customer_id, bank_id, cheque_no, cheque_date, date, branch_id) VALUES ('$bank_en','$invoice_no','debit','9','$customer_id','$bank_id','$cheque_no','$newDate','$newDate','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		/*if(!empty($customer_id)) { 
		mysqli_query($connection,"INSERT INTO transaction (date, ref_id, customer_id, ledger, debit, cheque_no, cheque_date, bank_id, hint, payment_type) VALUES ('$saledate','$invoice_no','$customer_id','9','$bank_en','$cheque_no','$newDate','$bank_id','sale permanent customer','bank')")
		or die(mysqli_error());
		}
		
		else {
		
		mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, debit, cheque_no, cheque_date, bank_id, hint, payment_type) VALUES ('$saledate','$invoice_no','9','$bank_en','$cheque_no','$newDate','$bank_id','sale retail customer','bank')")
		or die(mysqli_error());
		
		}*/
		 
		}
		
		if(!empty($cash_en)) { 
		if(!empty($customer_id)) { 
			mysqli_query($connection,"INSERT INTO transaction (date, ref_id, customer_id, ledger, debit, hint, payment_type, branch_id) VALUES ('$saledate','$invoice_no','$customer_id','9','$cash_en','sale permanent customer','cash','$branch_id_for_header_user')")
		or die(mysqli_error());	
		
		 }
		 
		 else {
		 mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, debit, hint, payment_type, branch_id) VALUES ('$saledate','$invoice_no','9','$cash_en','sale retail customer','cash','$branch_id_for_header_user')")
		or die(mysqli_error());	
		 
		 }
		 
		 mysqli_query($connection,"UPDATE cash SET cash = cash + $cash_en WHERE branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error());
		 
		 
		 	}
			
			
			if(!empty($due)) { 
		if(!empty($customer_id)) { 
			mysqli_query($connection,"INSERT INTO transaction (date, ref_id, customer_id, ledger, debit, hint, payment_type, branch_id) VALUES ('$saledate','$invoice_no','$customer_id','9','$due','sale due permanent customer','due','$branch_id_for_header_user')")
		or die(mysqli_error());	
			
		 mysqli_query($connection,"UPDATE customer_setup SET due = due + $due WHERE customer_id = '$customer_id'")
				or die(mysqli_error());
		 }
		 
		 else {
		 mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, debit, hint, payment_type, branch_id) VALUES ('$saledate','$invoice_no','9','$due','sale due retail customer','due','$branch_id_for_header_user')")
		or die(mysqli_error());	
		 
		 }
		 
		 
		 
		 
		 	}
		
}



print "<script>";
	print "self.location='invoice.php?inv=$invoice_no';";
	print "</script>";
}

}
?>