<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
$savecount=$_REQUEST['savecount'];
$cus=$_REQUEST['cus'];
if($savecount==0)
{
	if(($cus=='-select-')||($cus==''))
    	{
    		echo "Data could not saved <br> Invalid unit name";
    	}
  	else
  	{
 	$i=0;
	$partname=$_REQUEST['partname'];
	foreach($partname as $a)
	{
 		$partname[$i]=$a; 
		$i++;
	}
 	 
	$i=0;
	$partcode=$_REQUEST['partcode'];
	foreach($partcode as $a)
	{
 		$partcode[$i]=$a; 
 		$i++;
	}
	$i=0;
	$qty=$_REQUEST['qty'];
	foreach($qty as $a)
	{
 		$qty[$i]=$a; 
 		$i++;
	}
	$i=0;
	$unitprice=$_REQUEST['unitprice'];
	foreach($unitprice as $a)
	{
 		$unitprice[$i]=$a; 
 		$i++;
	}
	$i=0;
	$tot=$_REQUEST['tot'];
	foreach($tot as $a)
	{
		$tot[$i]=$a; 
		$i++;
	}
		
	$i=0;
	$ntamt=$_REQUEST['ntamt'];
	foreach($ntamt as $a)
	{
 		$ntamt[$i]=$a; 
		$i++;
	}
	

	$flg=0;
	$spersonflg=0;
	$totflg=0;
	for($j=0;$j<$i;$j++)
	{
		$k=$j+1;
		if(($qty[$j]=='')||($qty[$j]=='0'))
		{
    			print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
 			$flg++;
    		}
   		if(($tot[$j]=='')||($tot[$j]=='0'))
    		{
    			print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
     			$totflg++;
    		}
	}
	if($flg>0)
	{
  		echo "<small>Check the  itemname/Qty of Red marked row(s)</br></small>";
 	}
	elseif($totflg>0)
 	{
  		echo "<small>Enter Total of Red marked row(s)</small>";
 	}
	else
 	{
		$re1=mysqli_query($db,"select inv_prefix from branch where brcode='$brcode'");
		$row=mysqli_fetch_array($re1);
		$mprefix = $row[0];
  		$re=mysqli_query($db,"select max(invno) from deliverychallanout where brcode='$brcode' ");
		if(mysqli_num_rows($re)<1)
		{
			$inv=$mprefix.'000001'; 
		}
		else
		{
			$row=mysqli_fetch_array($re);
		       $inv=substr($row[0],-6)+1;
			$inv=substr('000000'.$inv,-6);
			if($inv==0)
			{
				$inv='000001'; 
			}
			$inv=$mprefix.$inv; 
		}
	 	$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
		$jvrow=mysqli_fetch_row($jvres);
		if($jvrow[0]>0)
 			$jvno=($jvrow[0]+1);
		else
                     $jvno=1;
	 	$flg1=0;
		$inv_tot = 0;
		$mitem_sale = 0;
		$mtax_tot = 0;
		$mreduction = 0;
		$mrebate = 0;
		$mkvic = 0;
		$mkvicspl = 0;
		$mvat_per[] = 0;
		$mvat_amt[] = 0;
		$mgroup_name[] = 0;
		$mgroup_amt[] = 0;
		$mgrp_ctr=0;
		$mctr=0;
		for($m=0;$m<$i;$m++)
       	{
			$invdt=$_REQUEST['invdt'];
			$reg=$_REQUEST['reg'];
			$slsengnr=$_REQUEST['slsengnr'];
			$RadioGroup1=$_REQUEST['RadioGroup1'];

			mysqli_query($db,"insert into deliverychallanout (invno,invdate,customer,vehno,item,item_code,qty,unitprice,itmvl,total,nettot,salesman,brcode,pmode,redct,mba,strbtnml,jvno,strbtspl,befrtx,tax,taxamt,itemtyp,staffdisc,biltpe) values('$inv','$invdt','$cus','$reg','$partname[$m]','$partcode[$m]','$qty[$m]','$unitprice[$m]','$itmvl[$m]','$tot[$m]','$ntamt[$m]','$slsengnr','$brcode','$pmode','$redct[$m]','$rebat[$m]','$kvic[$m]','$jvno','$kvicspl[$m]','$befrtx[$m]','$tax[$m]','$taxamt[$m]','$itmtyp[$m]','$stfdisc[$m]','$RadioGroup1')");
			$reqry="select * from sparestock where itemcode='$partcode[$m]' and brcode='$brcode'";
			$rers=mysqli_query($db,$reqry);
			$num=mysqli_num_rows($rers);
			if($num>0)
			{
				//$sql="update sparestock set curstk=(curstk-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'";
				mysqli_query($db,"update sparestock set curstk=(curstk-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
			}
			else
			{
				//$sql="insert into sparestock(itemcode,itemname,curstk,brcode) values('$partcode[$m]','$partname[$m]','-$qty[$m]','$brcode')";
				//echo $sql;
				mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk,brcode) values('$partcode[$m]','$partname[$m]','-$qty[$m]','$brcode') ");
			}
			
	  		$grp=mysqli_query($db,"select gname from partmaster where partcode='$partcode[$m]'");
        		$grp_row=mysqli_fetch_array($grp);
       	       $grp_name=$grp_row[0];
			$marray_grp_pos = array_search($grp_name, $mgroup_name);
			if($marray_grp_pos==FALSE)
			{
				$mgroup_name[$mgrp_ctr]=$grp_name;
				$mgroup_amt[$mgrp_ctr]=$tot[$m];
				$mgrp_ctr++;
			}
			else
			{
				$mgroup_amt[$marray_grp_pos]=$mgroup_amt[$marray_grp_pos]+$tot[$m];
			}


			$mitem_sale = $mitem_sale + $tot[$m];
			$inv_tot = $inv_tot + $ntamt[$m];
			$mtax_tot = $mtax_tot + $taxamt[$m];
			$mreduction = $mreduction + $redct[$m];
			$mrebate = $mrebate + $rebat[$m];
			$mkvic = $mkvic + $kvic[$m];
			$mkvicspl = $mkvicspl + $kvicspl[$m];
			$marray_pos = array_search($tax[$m], $mvat_per);
			if($marray_pos==FALSE)
			{
				$mvat_per[$mctr]=$tax[$m];
				$mvat_amt[$mctr]=$taxamt[$m];
				$mctr++;
			}
			else
			{
				$mvat_amt[$marray_pos]=$mvat_amt[$marray_pos]+$taxamt[$m];
			}
		       $flg1++;
	      	} 
 
//Correction Required Ledger explode
		if ($pmode=='CREDIT')
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','$cus','$inv_tot','0','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT) Credit',' ','','0')";
		else
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','Cash','$inv_tot','0','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT) Cash',' ','','0')";
     		mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		if($mitem_sale>0)
		{
			for($m1=0;$m1<=$mgrp_ctr-1;$m1++)
       		{
				if($mgroup_amt[$m1]>0)
				{
					$mled = $mgroup_name[$m1].' Sale';
					$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Cr','$mled','0','$mgroup_amt[$m1]','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT)',' ','','0')";
					mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
				}
			}
		}
		if($mtax_tot>0)
		{
			for($m1=0;$m1<=$mctr-1;$m1++)
       		{
				if($mvat_amt[$m1]>0)
				{
					$mled = 'Output Tax - VAT@'.$mvat_per[$m1].'%';
					$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Cr','$mled','0','$mvat_amt[$m1]','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT)',' ','','0')";
					mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
				}
			}
		}
		if($mreduction>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','Reduction on Khadi Sale','$mreduction','0','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT)',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mrebate>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','MDA on Item Sale','$mrebate','0','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT)',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mkvic>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','State Rebate(Normal)on Khadi Sale','$mkvic','0','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT)',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mkvicspl>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','State Rebate(Special)on Khadi Sale','$mkvicspl','0','Khadi Transfer Against Register (OUT) Inv No.: $inv','$brcode','Transfer Against Register (OUT)',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}


//  		$sql="insert into journal2 values('$invdt','$jvno','Dr','$cus','$taxdrrow[0]','0','Counter saleno: $inv,PBill:$coup_no','$brcode','Transfer Against Register (OUT) Credit',' ','','0')";
//     		mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
//    		$sql="insert into journal2 values('$invdt','$jvno','Cr','Counter Sale Credit','0','$taxdrrow[0]','Counter saleno: $inv,PBill:$coup_no','$brcode','Transfer Against Register (OUT) Credit',' ','','0')";
//     		mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
		if($flg1==$i) 
 		{
			//print "<script> window.open('workshop_printinv.php?inno=$inv'); </script>";
			echo "Data  saved with invoice number:".$inv;
			echo "<script> document.getElementById('savecount').value=1;document.location='delvrchlnout.php';</script>";
		}  
 	}
	}
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='delvrchlnout.php';</script>";
}
?>
