<?php
require_once "config.php";
$customer_name=$_POST['customer_name'];
$adr1=$_POST['adr1'];
$adr2=$_POST['adr2'];
$gst_no=$_POST['gst_no'];
$from_date=$_POST['from_date'];
$enquiry_ref_no=$_POST['enquiry_ref_no'];
$email_id=trim($_POST['email_id']);
$kind_atn=$_POST['kind_atn'];
$msisdn=$_POST['msisdn'];
$adr3=$_POST['adr3'];

$test_report=$_POST['test_report'];
$warranty=$_POST['warranty'];
$delivery=$_POST['delivery'];
$payment_terms=$_POST['payment_terms'];
$freight=$_POST['freight'];
$quote_validity=$_POST['quote_validity'];
$data=array();
$quotation=$_POST['quotation'];
$quotation=json_decode($quotation,false);
$type=$_POST['type'];
mysqli_autocommit($con, FALSE);
if($email_id!=""){
    if(filter_var($email_id, FILTER_VALIDATE_EMAIL)==""){
    die("Invalid Email Id");
}
}

$sql="select date(now()) as date";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    $current_date=$result['date'];
}
if($type=="proforma"||$type=='purchase'){
  $discount = (!isset($_POST['discount']) || $_POST['discount'] == 'NaN') ? 0: $_POST['discount'] ;

$shipment_charges=isset($_POST['shipment_charges']) ? $_POST['shipment_charges'] : 0;
$net_total=isset($_POST['net_total']) ? $_POST['net_total'] : 0;
$discount_val=isset($_POST['discount_val']) ? $_POST['discount_val'] : 0;
$shipment_charges_val=isset($_POST['shipment_charges_val']) ? $_POST['shipment_charges_val'] : 0;
$to_pay_basis=isset($_POST['to_pay_basis']) ? $_POST['to_pay_basis'] : 0;
}else{
    $discount=0;
$to_pay_basis=0;
$shipment_charges=0;
$net_total=0;
$discount_val=0;
$shipment_charges_val=0;
}
$shipment_mode="";
if($type=='quotation'){

if($quote_validity==""||$freight==""){
die("All * fields are mandatory");
}

$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);
   $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)){
       $next_date=$result2['date'];
       if($current_date>=$next_date){
           
//           $sql3="update quotation_no_table set quotation_no=1";
//    $query3=mysqli_query($con,$sql3);
     $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;
    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; 
    }
           
//            $sql3="UPDATE quotation_date SET date = DATE_ADD(date, INTERVAL 1 YEAR) ";
//    $query3=mysqli_query($con,$sql3);
   }
       }else{
            if($type=="proforma"){
           $quotation_no= "AP/PI/".$quotation_no."/18-19";
       }else if($type=="quotation"){
       $quotation_no= "AP/QTN/".$quotation_no."/18-19";
       }else{
           $quotation_no= "AP/PO/".$quotation_no."/18-19";
       }
       }
   }
       
       
      
       
       
   }
}

}


if($type=='proforma'){
$sql="select * from quotation_no_table_proforma";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    $sql1="update quotation_no_table_proforma set quotation_no=quotation_no+1";
    $query1=mysqli_query($con,$sql1);
   $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)){
       $next_date=$result2['date'];
       if($current_date>=$next_date){
           
//           $sql3="update quotation_no_table_proforma set quotation_no=1";
//    $query3=mysqli_query($con,$sql3);
     $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;
    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; 
    }
           
//            $sql3="UPDATE quotation_date SET date = DATE_ADD(date, INTERVAL 1 YEAR) ";
//    $query3=mysqli_query($con,$sql3);
   }
       }else{
            if($type=="proforma"){
           $quotation_no= "AP/PI/".$quotation_no."/18-19";
       }else if($type=="quotation"){
       $quotation_no= "AP/QTN/".$quotation_no."/18-19";
       }else{
           $quotation_no= "AP/PO/".$quotation_no."/18-19";
       }
       }
   }
       
       
      
       
       
   }
}

}


if($type=='purchase'){
    $shipment_mode=$_POST['shipment_mode'];
$sql="select * from quotation_no_table_purchase";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    $sql1="update quotation_no_table_purchase set quotation_no=quotation_no+1";
    $query1=mysqli_query($con,$sql1);
   $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)){
       $next_date=$result2['date'];
       if($current_date>=$next_date){
           
//           $sql3="update quotation_no_table_purchase set quotation_no=1";
//    $query3=mysqli_query($con,$sql3);
     $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;
    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; 
    }
           
//            $sql3="UPDATE quotation_date SET date = DATE_ADD(date, INTERVAL 1 YEAR) ";
//    $query3=mysqli_query($con,$sql3);
   }
       }else{
            if($type=="proforma"){
           $quotation_no= "AP/PI/".$quotation_no."/18-19";
       }else if($type=="quotation"){
       $quotation_no= "AP/QTN/".$quotation_no."/18-19";
       }else{
           $quotation_no= "AP/PO/".$quotation_no."/18-19";
       }
       }
   }
       
       
      
       
       
   }
}

}

if($quotation_no==""){
    die("Quotation number is blank");
}


$sql="select * from quotation_details where quotation_no='".$quotation_no."'";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    die("Duplicate Quotation no");
}


$sql="select * from customer_details where customer_name='".$customer_name."'";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    $sql="update customer_details set email_id='".$email_id."', adr3='".$adr3."',msisdn='".$msisdn."', customer_name='".$customer_name."',adr1='".$adr1."',adr2='".$adr2."',gst_no='".$gst_no."',modified_on=now() where id='".$result['id']."'";
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}
}else{
    $sql="insert into customer_details(customer_name,adr1,adr2,gst_no,created_on,email_id,msisdn,adr3) values('".$customer_name."',"
        . "'".$adr1."','".$adr2."','".$gst_no."','".$from_date."','".$email_id."','".$msisdn."','".$adr3."')";
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}
}


$sql="insert into quotation_details(quote_validity,freight,shipment_mode,to_pay_basis,type,customer_name,adr1,adr2,gst_no,created_on,quotation_no,enquiry_ref_no,kind_atn,email_id,msisdn,adr3,test_report,warranty,delivery,payment_terms,discount,shipment_charges,net_total,discount_val,shipment_charges_val) values(
'".$quote_validity."','".$freight."','".$shipment_mode."','".$to_pay_basis."','".$type."','".$customer_name."',"
        . "'".$adr1."','".$adr2."','".$gst_no."','".$from_date."','".$quotation_no."','".$enquiry_ref_no."','".$kind_atn."','".$email_id."','".$msisdn."','".$adr3."'"
        . ",'".$test_report."','".$warranty."','".$delivery."','".$payment_terms."','".$discount."','".$shipment_charges."','".$net_total."','".$discount_val."','".$shipment_charges_val."')";

$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}else{
    
}

for($i=0;$i<count($quotation);$i++){
if($type=="purchase"){
   $sql="insert into quotation_details_all(product_name,qty,unit,base_price,gst,grand_total,quotation_no,fda) values('".$quotation[$i]->product."',"
        . "'".$quotation[$i]->qty."','".$quotation[$i]->unit."','".$quotation[$i]->base_price."','".$quotation[$i]->gst."','".$quotation[$i]->grand_total."','".$quotation_no."','".$quotation[$i]->fda."')";
$query=mysqli_query($con,$sql);
//echo $sql;die();
if($query==false){
    die(mysqli_error($con));
}else{
    
} 
}else{
    $sql="insert into quotation_details_all(product_name,hsn,qty,unit,base_price,gst,grand_total,quotation_no,specification,fda) values('".$quotation[$i]->product."',"
        . "'".$quotation[$i]->hsn."','".$quotation[$i]->qty."','".$quotation[$i]->unit."','".$quotation[$i]->base_price."','".$quotation[$i]->gst."','".$quotation[$i]->grand_total."','".$quotation_no."','".$quotation[$i]->specification."','".$quotation[$i]->fda."')";
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}else{
    
} 
}
}
echo json_encode(
        array("quotation_no"=>$quotation_no,"data"=>mysqli_affected_rows($con))
        );
mysqli_commit($con);