<?php
require_once "config.php";

//$sql="select LPAD(quotation_no,4,'0') as quotation_no from quotation_no_table";
//$query=mysqli_query($con,$sql);
//if($query==false){
//    die(mysqli_error($con));
//}
//if($result=mysqli_fetch_array($query)){
//    
//   
//      $quotation_no= "AP/QTN/".$result['quotation_no']."/18-19";
//       echo $quotation_no;
//}


mysqli_autocommit($con, FALSE);
$type=$_POST['type'];
$sql="select date(now()) as date";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    $current_date=$result['date'];
}
;
if($type=="quotation"){
$sql="select * from quotation_no_table";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    
   $sql1="select LPAD(quotation_no,4,'0') as quotation_no from quotation_no_table";
   $query1=mysqli_query($con,$sql1);
   if($result1=mysqli_fetch_array($query1)){
       $quotation_no=$result1['quotation_no'];
       $sql2="select * from quotation_date";
        $query2=mysqli_query($con,$sql2);
   if($result2=mysqli_fetch_array($query2)){
       //echo $current_date."<>".$result2['date'];
       $next_date=$result2['date'];
       if($current_date>=$next_date){
          
     $sql4="select LPAD(quotation_no,4,'0') as quotation_no from quotation_no_table";
   $query4=mysqli_query($con,$sql4);
   if($result4=mysqli_fetch_array($query4)){       
    
    $d1=$next_date[2];
    $d2=$next_date[3];
    $d=$d1.$d2;
    $n=$d+1;
   // echo $current_date.$d1."<>".$d2;die();
    if($type=="proforma"){
           $quotation_no= "AP/PI/".$result4['quotation_no']."/".$d."-".$n;
    }else if($type=="quotation"){
        $quotation_no= "AP/QTN/".$result4['quotation_no']."/".$d."-".$n;
    }else{
        $quotation_no= "AP/PO/".$result4['quotation_no']."/".$d."-".$n;
    }       
    echo $quotation_no;
   }
       }else{
           if($type=="proforma"){
              
           $quotation_no= "AP/PI/".$quotation_no."/24-25";
           }else if($type=="quotation"){
               $quotation_no= "AP/QTN/".$quotation_no."/24-25";
           }else{
               $quotation_no= "AP/PO/".$quotation_no."/24-25";
           }
           echo $quotation_no;
       }
   }
       
       
      
       
       
   }
}
}


if($type=="proforma"){
$sql="select * from quotation_no_table_proforma";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    
   $sql1="select LPAD(quotation_no,4,'0') as quotation_no from quotation_no_table_proforma";
   $query1=mysqli_query($con,$sql1);
   if($result1=mysqli_fetch_array($query1)){
       $quotation_no=$result1['quotation_no'];
       $sql2="select * from quotation_date";
        $query2=mysqli_query($con,$sql2);
   if($result2=mysqli_fetch_array($query2)){
       //echo $current_date."<>".$result2['date'];
       $next_date=$result2['date'];
       if($current_date>=$next_date){
          
     $sql4="select LPAD(quotation_no,4,'0') as quotation_no from quotation_no_table_proforma";
   $query4=mysqli_query($con,$sql4);
   if($result4=mysqli_fetch_array($query4)){       
    
    $d1=$next_date[2];
    $d2=$next_date[3];
    $d=$d1.$d2;
    $n=$d+1;
   // echo $current_date.$d1."<>".$d2;die();
    if($type=="proforma"){
           $quotation_no= "AP/PI/".$result4['quotation_no']."/".$d."-".$n;
    }else if($type=="quotation"){
        $quotation_no= "AP/QTN/".$result4['quotation_no']."/".$d."-".$n;
    }else{
        $quotation_no= "AP/PO/".$result4['quotation_no']."/".$d."-".$n;
    }       
    echo $quotation_no;
   }
       }else{
           if($type=="proforma"){
              
           $quotation_no= "AP/PI/".$quotation_no."/24-25";
           }else if($type=="quotation"){
               $quotation_no= "AP/QTN/".$quotation_no."/24-25";
           }else{
               $quotation_no= "AP/PO/".$quotation_no."/24-25";
           }
           echo $quotation_no;
       }
   }
       
       
      
       
       
   }
}
}




if($type=="purchase"){
$sql="select * from quotation_no_table_purchase";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    
   $sql1="select LPAD(quotation_no,4,'0') as quotation_no from quotation_no_table_purchase";
   $query1=mysqli_query($con,$sql1);
   if($result1=mysqli_fetch_array($query1)){
       $quotation_no=$result1['quotation_no'];
       $sql2="select * from quotation_date";
        $query2=mysqli_query($con,$sql2);
   if($result2=mysqli_fetch_array($query2)){
       //echo $current_date."<>".$result2['date'];
       $next_date=$result2['date'];
       if($current_date>=$next_date){
          
     $sql4="select LPAD(quotation_no,4,'0') as quotation_no from quotation_no_table_purchase";
   $query4=mysqli_query($con,$sql4);
   if($result4=mysqli_fetch_array($query4)){       
    
    $d1=$next_date[2];
    $d2=$next_date[3];
    $d=$d1.$d2;
    $n=$d+1;
   // echo $current_date.$d1."<>".$d2;die();
    if($type=="proforma"){
           $quotation_no= "AP/PI/".$result4['quotation_no']."/".$d."-".$n;
    }else if($type=="quotation"){
        $quotation_no= "AP/QTN/".$result4['quotation_no']."/".$d."-".$n;
    }else{
        $quotation_no= "AP/PO/".$result4['quotation_no']."/".$d."-".$n;
    }       
    echo $quotation_no;
   }
       }else{
           if($type=="proforma"){
              
           $quotation_no= "AP/PI/".$quotation_no."/24-25";
           }else if($type=="quotation"){
               $quotation_no= "AP/QTN/".$quotation_no."/24-25";
           }else{
               $quotation_no= "AP/PO/".$quotation_no."/24-25";
           }
           echo $quotation_no;
       }
   }
       
       
      
       
       
   }
}
}


mysqli_commit($con);

//require_once "config.php";
//$con->autocommit(FALSE);
//$sql="select * from quotation_no_table";
//$query=mysqli_query($con,$sql);
//if($result=mysqli_fetch_array($query)){
//    $sql1="update quotation_no_table set quotation_no=quotation_no+1";
//    $query1=mysqli_query($con,$sql1);
//    echo "AMUL-".$result['quotation_no'];
//}
//$con->commit();
//$con->close();