<?php
session_start();
include "connect.php";
$brmid=$_SESSION['linkarisebranchid'];
	$umid = $_SESSION['linkariseumid'];
	$cmpmid=$_SESSION['linkarisecmpmid'];


		//$salesmid=$_SESSION['umid'];
		$suppliermid=$_POST['suppliermid'];
		$pomid=$_POST['pomid'];
		$porefno=$_POST['porefno'];
		$podate=implode('-', array_reverse(explode('-', $_POST['podate'])));
		$fdate=implode('-', array_reverse(explode('-', $_POST['fdate'])));
		$poterms=$_POST['terms'];
		$copypo=$_POST['copypo'];
		$createdby="";
		$kindatten="";
		if($copypo==1)
		{
			$pomid=0;
		}
		date_default_timezone_set("Asia/Kolkata");
		$mdate= date('Y-m-d');
		$mdatetime= date('Y-m-d H:i:s');
		$fyear='20-21';
		$prefix='PO-';
		/* $evelastyear = "select * from res_companysetting_master limit 0,1";
		$relastyear = mysqli_query($conn, $evelastyear);
		while($rtlastyear = mysqli_fetch_assoc($relastyear))
		{
			$cmpcurrentyear=$rtlastyear['cmpcurrentyear'];
		} */
		$evelastyear = "select * from res_company_master where cmpmid=$cmpmid";
	$relastyear = mysqli_query($conn, $evelastyear);
	while($rtlastyear = mysqli_fetch_assoc($relastyear))
	{
		$fyear=$rtlastyear['cmpyear'];
		$prefix=$rtlastyear['poprefix'];
	}
			

		if($pomid==0)
		{
			$maxpomno=0;
			$evemaxq = "SELECT max( pomno ) AS maxpomno FROM res_po_master where poyear='$fyear' and cmpmid='$cmpmid'";
			$remaxq = mysqli_query($conn, $evemaxq);
			while($rtmaxq = mysqli_fetch_assoc($remaxq))
			{
				$maxpomno=$rtmaxq['maxpomno'];
			}
			$maxpomno1=$maxpomno+1;
			$maxpomno2=$prefix.$maxpomno1;
			$popdf=$maxpomno2.'_'.date('YmdHis').rand(1000,9999).'.pdf';
			$eve1 = "INSERT INTO res_po_master (postatus,poprefix,pomno,pofordisplay,poyear,addedby,addedon,cmpmid,popdf) 
			VALUES('In Review','$prefix','$maxpomno1','$maxpomno2','$fyear','$umid','$mdatetime','$cmpmid','$popdf')";
			if ($conn->query($eve1) === TRUE)
			{
				$pomid = $conn->insert_id;
				
			}
		}
		date_default_timezone_set("Asia/Kolkata");
		$mdate= date('Y-m-d');
		$eve1 = "update res_po_master set suppliermid='$suppliermid',poterms='$poterms',porefno='$porefno',podate='$podate' where pomid=$pomid";
		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;
				 $podid=$_POST['qdid'][$n];
			
				$srate=$_POST['pprice'][$n];
				$sqty=$_POST['pqty'][$n];
				$iunit=$_POST['iunit'][$n];
				$sdesc=$_POST['pdesc'][$n];
				$pdiscper=$_POST['pdiscper'][$n];
				$eve_id_2 = "select * from res_item_master WHERE itemid='$itemid'";
				$re_id_2 = mysqli_query($conn, $eve_id_2);
				$pid=0;
				$tcper1 = 0;
				$tcper2 = 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'];
						$ptax = $rt_id_2['itemtaxper'];
						$tcper1 = $ptax/2;
						$tcper2 = $ptax/2;	
						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;
				$tamount=$samount+$taxcalc+$taxcalc2;
				$gtamount=$gtamount+$tamount;
				$grosstotal=$grosstotal+$tamount;
				$n = $n + 1;
				$n2++;
				if($podid==1)
				{
					if($itemid==0 || $sqty==0)
					{}
					else
					{
						$query = "INSERT INTO res_po_detail (pomid,itemid,itemname,itemunit,itemqty,itemiprice,itemamount,itemgstper,itemcgstamount,itemsgstamount,itemgstamount,itemtotalamount,itemhsncode,itemdesc,itemdiscper,itemdiscamt,cmpmid)
						VALUES('$pomid','$itemid','$itemname','$iunit','$sqty','$srate','$samount','$ptax','$taxcalc','$taxcalc2','$taxcalctotal','$tamount','$hsncode','$sdesc','$pdiscper','$pdiscamt','$cmpmid')";
						$result= mysqli_query($conn, $query) or die("Error");
					}
				}
				else
				{
					if($podid=='0')
					{
						if($itemid==0 || $sqty==0)
						{}
						else
						{
						$query = "INSERT INTO res_po_detail (pomid,itemid,itemname,itemunit,itemqty,itemiprice,itemamount,itemgstper,itemcgstamount,itemsgstamount,itemgstamount,itemtotalamount,itemhsncode,itemdesc,itemdiscper,itemdiscamt) 
						VALUES('$pomid','$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_po_detail where podid=$podid";
							$result_del= mysqli_query($conn, $query_del) or die("Error");
						}
						else
						{
						 $query = "update res_po_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 podid=$podid";
						$result= mysqli_query($conn, $query) or die("Error");
						}
					}
				}
			}
			
			$netamount1=round($gtamount);
			$invoiceroundoffamount=$netamount1-$gtamount;
			$eve3 = "update res_po_master set poroundoffamount='$invoiceroundoffamount',pototalitem='$sqty',pototaltaxamount='$totaltaxcalc1+$totaltaxcalc2',pototalamount='$subtotal',pogrossamount='$grosstotal',ponetamount='$netamount1' ,addedby='$umid',addedon='$mdatetime' where pomid='$pomid'";
			
			$re3 = mysqli_query($conn, $eve3);
		}
		$_SESSION['msg'] = "Purchase Order added";
		header("Location: purchaseorderpdf.php?pomid=$pomid");
?>
