<?php
session_start();
include "connect.php";
include "removespecial.php";
		$cmpmid=$_SESSION['linkarisecmpmid'];

		$salesmid=$_SESSION['linkariseumid'];
		$cmid=$_POST['cmid'];
		$qmid=$_POST['qmid'];
		$followup=$_POST['followup'];
		$followdate=implode('-', array_reverse(explode('-', $_POST['fdate'])));;
		$followremarks=$_POST['inqfremarks'];
		$qrefno=$_POST['qrefno'];
		$qdate=implode('-', array_reverse(explode('-', $_POST['qdate'])));
		$fdate=implode('-', array_reverse(explode('-', $_POST['fdate'])));
		$tc="";
		$tcinput=$_POST['tcinput'];
		$qdescription2=$_POST['qdescription2'];
		$qmno1=$_POST['qmno1'];
		$reviseqt1=$_POST['revise1'];
		$copyqt=$_POST['copyqt'];
		$createdby="";
		$kindatten="";
		if($copyqt==1)
		{
			$qmid=0;
		}
		date_default_timezone_set("Asia/Kolkata");
		$mdate= date('Y-m-d');
		$mdatetime= date('Y-m-d H:i:s');
		$qyear='18-19';
		$qprefix='Q';
		
		$evelastyear = "select * from res_company_master where cmpmid=$cmpmid";
		$relastyear = mysqli_query($conn, $evelastyear);
		while($rtlastyear = mysqli_fetch_assoc($relastyear))
		{
			$qyear=$rtlastyear['cmpyear'];
			$qprefix=$rtlastyear['qprefix'];
		}
	
		if($qmid==0)
		{
			
			$maxqmno=0;
			$evemaxq = "SELECT max( qmno ) AS maxqmno FROM res_quotation_master where cmpmid=$cmpmid and qyear='$qyear'";
			$remaxq = mysqli_query($conn, $evemaxq);
			while($rtmaxq = mysqli_fetch_assoc($remaxq))
			{
				$maxqmno=$rtmaxq['maxqmno'];
			}
			$maxqmno1=$maxqmno+1;
			$maxqno1=$qprefix.$maxqmno1;
			$qpdf=$maxqno1.'_'.date('YmdHis').rand(10,99).'.pdf';
			$eve1 = "INSERT INTO res_quotation_master (cmpmid,qstatus,qprefix,qmno,qnofordisplay,qyear,addedby,addedon,followup,followdate,followremarks,qpdf) VALUES($cmpmid,'In Review','$qprefix','$maxqmno1','$maxqno1','$qyear','$salesmid','$mdatetime','$followup','$followdate','$followremarks','$qpdf')";
			if ($conn->query($eve1) === TRUE)
			{
				$qmid = $conn->insert_id;
				if($followup=='Yes'){
				$evefup = "INSERT INTO res_followup_detail (cmpmid,fdate,qmid,ftype,fremarks,fstatus,faddedby,fupdatedby,faddedon,fupdatedon,partymid) VALUES('$cmpmid','$fdate',$qmid,'Quotation','$followremarks','Open','$salesmid','$salesmid','$mdatetime','$mdatetime','$cmid')";

				if ($conn->query($evefup) === TRUE)
				{
				}
				}
			}
		}
		else if($reviseqt1==1){

				//-------Revise-----
			
			$eve11="select * from res_quotation_master where qmno=$qmno1 order by reviseno desc LIMIT 0,1";
			$re11 = mysqli_query($conn, $eve11);
			while($rt11 = mysqli_fetch_assoc($re11))
			{
				$revise1=$rt11['reviseno'];
			}
			$revise2=$revise1 + 1;
			$qno=$qprefix.$qmno1."-R".$revise2;
			$qpdf=$qno.'_'.date('YmdHis').rand(10,99).'.pdf';
			//----------------------

			 $eve123 = "INSERT INTO res_quotation_master (cmpmid,qprefix,qstatus,qmno,reviseno,qnofordisplay,qyear,addedby,addedon,qpdf,followup,followdate,followremarks) VALUES($cmpmid,'$qprefix','In Review',$qmno1,$revise2,'$qno','$qyear','$salesmid','$mdatetime','$qpdf','$followup','$followdate','$followremarks')";
			if ($conn->query($eve123) === TRUE)
			{
				$qmid = $conn->insert_id;
				if($followup=='Yes'){
				$evefup = "INSERT INTO res_followup_detail (cmpmid,fdate,qmid,ftype,fremarks,fstatus,faddedby,fupdatedby,faddedon,fupdatedon,partymid) VALUES('$cmpmid','$fdate',$qmid,'Quotation','$followremarks','Open','$salesmid','$salesmid','$mdatetime','$mdatetime','$cmid')";
				if ($conn->query($evefup) === TRUE)
				{
				}
				$eve111="update res_quotation_master set qstatus='Previous' where qmno=$qmno1 and qstatus='In Review' and qmid not in('$qmid')";
				$re111 = mysqli_query($conn, $eve111);
				}
			}
		}
		date_default_timezone_set("Asia/Kolkata");
		$mdate= date('Y-m-d');
		 $eve1 = "update res_quotation_master set partymid='$cmid',qterms='$tcinput',qrefno='$qrefno',qfootertext='$qdescription2',qdate='$qdate' where qmid=$qmid";
		if ($conn->query($eve1) === TRUE)
		{
			$n=0;
			$n2=0;
			$subtotal=0;
			$grosstotal=0;
			$amount=0;
			$gtamount=0;
			$totaltaxcalc1=0;
			$totaltaxcalc2=0;
			$delete_checkbox_array = $_POST['itemid'];
			foreach ($delete_checkbox_array as $one_by_one_sid)
			{
				$itemid=$one_by_one_sid;
				$qdid=$_POST['qdid'][$n];
				$srate=$_POST['pprice'][$n];
				$sqty=$_POST['pqty'][$n];
				//$iunit=$_POST['iunit'][$n];
				$sdesc=removespecialchar($_POST['pdesc'][$n]);
				$pdiscper=$_POST['pdiscper'][$n];
				$ptax = $_POST['pgst'][$n];
						$tcper1 = $ptax/2;
						$tcper2 = $ptax/2;	
				$eve_id_2 = "select * from res_item_master WHERE itemid='$itemid'";
				$re_id_2 = mysqli_query($conn, $eve_id_2);
				$pid=0;
				
				$hsncode = "";
				if(mysqli_num_rows($re_id_2) > 0)
				{
					while($rt_id_2 = mysqli_fetch_assoc($re_id_2))
					{
						$itemname = $rt_id_2['itemname'];
						$hsncode = $rt_id_2['itemhsncode'];
						$iunit = $rt_id_2['itemunit'];
						
						break;
					}
				}				
				$amount=floatval($srate)*floatval($sqty);
				$pdiscamt=($amount*$pdiscper)/100;
				$samount=$amount-$pdiscamt;
				$subtotal=$subtotal+$samount;
				$taxcalc=($samount * $tcper1)/100;
				$taxcalc2=($samount * $tcper2)/100;
				$taxcalctotal=($samount * $ptax)/100;
				$totaltaxcalc1=$totaltaxcalc1+$taxcalc;;
				$totaltaxcalc2=$totaltaxcalc2+$taxcalc2;
				$totaltax=$totaltaxcalc1+$totaltaxcalc2;
				$tamount=$samount+$taxcalc+$taxcalc2;
				$gtamount=$gtamount+$tamount;
				$grosstotal=$grosstotal+$tamount;
				$n = $n + 1;
				$n2++;
				if($reviseqt1==1 || $copyqt==1)
				{
					if($itemid==0 || $sqty==0)
					{}
					else
					{
						$query = "INSERT INTO res_quotation_detail (qmid,itemid,itemname,itemunit,itemqty,itemiprice,itemamount,itemgstper,itemcgstamount,itemsgstamount,itemgstamount,itemtotalamount,itemhsncode,itemdesc,itemdiscper,itemdiscamt)
						VALUES('$qmid','$itemid','$itemname','$iunit','$sqty','$srate','$samount','$ptax','$taxcalc','$taxcalc2','$taxcalctotal','$tamount','$hsncode','$sdesc','$pdiscper','$pdiscamt')";
						$result= mysqli_query($conn, $query) or die("Error");
					}
				}
				else
				{
					if($qdid=='0')
					{
						if($itemid==0 || $sqty==0)
						{}
						else
						{
						$query = "INSERT INTO res_quotation_detail (qmid,itemid,itemname,itemunit,itemqty,itemiprice,itemamount,itemgstper,itemcgstamount,itemsgstamount,itemgstamount,itemtotalamount,itemhsncode,itemdesc,itemdiscper,itemdiscamt) 
						VALUES('$qmid','$itemid','$itemname','$iunit','$sqty','$srate','$samount','$ptax','$taxcalc','$taxcalc2','$taxcalctotal','$tamount','$hsncode','$sdesc','$pdiscper','$pdiscamt')";
						$result= mysqli_query($conn, $query) or die("Error");
						}
					}
					else
					{
						if($itemid==0 || $sqty==0)
						{
							 $query_del = "delete from res_quotation_detail where qdid=$qdid";
							$result_del= mysqli_query($conn, $query_del) or die("Error");
						}
						else
						{
						 $query = "update res_quotation_detail set itemname='$itemname',itemunit='$iunit',itemqty='$sqty',itemiprice='$srate',itemamount='$samount',itemdiscper='$pdiscper',itemdiscamt='$pdiscamt',itemgstper='$ptax',itemcgstamount='$taxcalc',itemsgstamount='$taxcalc2',itemgstamount='$taxcalctotal',itemtotalamount='$tamount',itemdesc='$sdesc' where qdid=$qdid";
						$result= mysqli_query($conn, $query) or die("Error");
						}
					}
				}
			}
			
			$netamount1=round($gtamount);
			$qroundoffamount=$netamount1-$gtamount;
			$eve3 = "update res_quotation_master set qroundoffamount='$qroundoffamount',qtotalitem='$sqty',qtotaltaxamount='$totaltax',qtotalamount='$subtotal',qgrossamount='$grosstotal',qnetamount='$netamount1' ,addedby='$salesmid',addedon='$mdatetime' where qmid='$qmid'";
			$re3 = mysqli_query($conn, $eve3);
		}
		$_SESSION['msg'] = "Quotation Added";
		header("Location: pdfquotation.php?qmid=$qmid");
?>
