<?php
include 'connection.php';
session_start();
if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    } 
date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$time = date("h:i A"); 
$currentyear = date("Y");
$f_year = $_REQUEST['f_year'];
 $max_contact = $_POST['cntcount'];

function randomPassword() {
    $alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 5; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

$unique_id = randomPassword();

date_default_timezone_set('Asia/Calcutta');

$today = date("Y-m-d");

$today1 = date("d-m-Y");

$time = date("h:i A"); 

$currentyear = date("Y");

$currentmonth = date("m"); 

$currentyear = date("y"); 

if($currentmonth>03) { 

$cuyear = date("Y");

$cuyear1 = date("y")+1;



$cuyear2 = date("y");

$cuyear3 = date("y")+1;






} else { 

$cuyear = date("Y")-1;

$cuyear1 =  date("y");



$cuyear2 = date("y")-1;

$cuyear3 =  date("y");

 } 

$year = "$cuyear"."-"."$cuyear1";

$year_cha = "$cuyear2"."-"."$cuyear3";



?>


<?php

$inv_id = $_REQUEST['inv_id'];
$debit_date=$_POST['debit_date'];


$querydp = "select * from debit_no where auto_debit_no!='' and f_year='$year'";

$resultdp = mysqli_query($conn,$querydp);

$num_rowsdp = mysqli_num_rows($resultdp);

if($num_rowsdp =='0') { 

$inv_id1 = 0;

$inv_id2=$inv_id1+1;

$auto_debit_no= sprintf("%03d", $inv_id2);

} else {

while($row=mysqli_fetch_array($resultdp))

{

$inv_id1 = $row['auto_debit_no'];

$inv_id2=$inv_id1+1;

$auto_debit_no= sprintf("%04d", $inv_id2);

}

}

$debit_no = "DR/"."$auto_debit_no"."/"."$year_cha";

$query2kn = "select * from debit_no where auto_debit_no = '$auto_debit_no' and f_year ='$year'";

//echo  $query1kn;

$result2kn = mysqli_query($conn,$query2kn);

$num_rows2kn = mysqli_num_rows($result2kn);

if($num_rows2kn =='0') { 

$qryautodpi = "insert into debit_no(auto_debit_no,debit_no,f_year) values ('$auto_debit_no','$debit_no','$year')";

$rautodpi=mysqli_query($conn,$qryautodpi);

}
	$queryls = "select * from customer_master where customer_id='".$_REQUEST['customer_id']."' ORDER BY customer_name";
	
	//echo $queryls;
	$resultls = mysqli_query($conn,$queryls);	
	while($row=mysqli_fetch_array($resultls))
	{
	$customer_name = $row['customer_name'];
	$ship_address =  $row['ship_address'];
	
	$sphone =  $row['sphone'];
	$billtoGSTN= $row['billtoGSTN'];
	$customer_id= $row['customer_id'];
	
	
	}	  
	
$GST_Btype=$_POST['GST_Btype'];
$customer_id = $_REQUEST['customer_id'];
$customer_name=$_POST['customer_name'];

$debit_date=$_POST['debit_date'];
$refer_date=$_POST['refer_date'];

$reference_no=$_POST['reference_no'];
$query14 = "insert into debit_main (customer_id,customer_name,debit_no,debit_date,type,entry_by,entry_date,unique_id,f_year,GST_Btype,reference_no,refer_date) values ('$customer_id','$customer_name','$debit_no','$debit_date','CUSTOMER','$staff_name','$today','$unique_id','$year','$GST_Btype','$reference_no','$refer_date')";
$r14=mysqli_query($conn,$query14);



 $qry9 = "select * from debit_main where debit_no = '$debit_no' and customer_id='$customer_id'";
$re9 = mysqli_query($conn,$qry9);
while($row=mysqli_fetch_array($re9))
		{  
		 $debit_main_id = $row['debit_main_id'];
		}
 $max_contact = $_POST['cntcount'];
if($max_contact!=''){
for ($x = 1; $x <= $max_contact; $x++) {

$details= $_POST['details'.$x];
$qty= $_POST['qty'.$x];
$rate= $_POST['price'.$x];
$HSN_code= $_POST['HSN_code'.$x];


$query21 = "select * from  HSN_code_master where HSN_code='$HSN_code'";
// echo $query21;
$result21 = mysqli_query($conn,$query21);
$total = mysqli_num_rows($result21);
while($row=mysqli_fetch_array($result21))
{
$HSN_code = $row['HSN_code'];
$CGST_rate = $row['CGST'];
$SGST_rate = $row['SGST'];
$IGST_rate = $row['IGST'];


}
 $query14 = "insert into debit_details_more (details,qty,rate,HSN_code,CGST_rate,SGST_rate,IGST_rate,customer_id,customer_name,debit_no,debit_date,debit_main_id) values ('$details','$qty','$rate','$HSN_code','$CGST_rate','$SGST_rate','$IGST_rate','$customer_id','$customer_name','$debit_no','$debit_date','$debit_main_id')";
$r14=mysqli_query($conn,$query14);
}
}

//exit();
?> 

<script>
javascript:window.location.href="view_all_debit_note.php";
</script>
