<?php
error_reporting(0);
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'];
$quotation_no=$_POST['qtn'];
$enquiry_ref_no=$_POST['enquiry_ref_no'];
$kind_atn=$_POST['kind_atn'];
$email_id=trim($_POST['email_id']);
$msisdn=$_POST['msisdn'];
$adr3=$_POST['adr3'];
$data=array();
$quotation=$_POST['quotation'];
$quotation=json_decode($quotation,false);
$test_report=$_POST['test_report'];
$warranty=$_POST['warranty'];
$delivery=$_POST['delivery'];
$payment_terms=$_POST['payment_terms'];
$to_pay_basis=$_POST['to_pay_basis'];
$shipment_mode=$_POST['shipment_mode'];
    $discount=isset($_POST['discount']) ? $_POST['discount'] : 0;

$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;
$freight=$_POST['freight'];
$quote_validity=$_POST['quote_validity'];

mysqli_autocommit($con, FALSE);
if($email_id!=""){
    if(filter_var($email_id, FILTER_VALIDATE_EMAIL)==""){
    die("Invalid Email Id");
}
}
$type=$_POST['type'];
if($type=='quotation'){

if($quote_validity==""||$freight==""){
die("All * fields are mandatory");
}
}
$sql="update quotation_details set quote_validity='".$quote_validity."', freight='".$freight."',shipment_mode='".$shipment_mode."',to_pay_basis='".$to_pay_basis."', discount='".$discount."',shipment_charges='".$shipment_charges."',"
        . "net_total='".$net_total."',discount_val='".$discount_val."',shipment_charges_val='".$shipment_charges_val."', adr3='".$adr3."',msisdn='".$msisdn."',email_id='".$email_id."',enquiry_ref_no='".$enquiry_ref_no."',kind_atn='".$kind_atn."', customer_name='".$customer_name."',adr1='".$adr1."',adr2='".$adr2."',gst_no='".$gst_no."',created_on='".$from_date."',test_report='".$test_report."',warranty='".$warranty."',delivery='".$delivery."',payment_terms='".$payment_terms."' where quotation_no='".$quotation_no."'";
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}else{
    
}

$sql="delete from  quotation_details_all where quotation_no='".$quotation_no."'";
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}else{
    


for($i=0;$i<count($quotation);$i++){

   $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);