<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$savecount=$_REQUEST['savecount'];
$cus=$_REQUEST['cus'];
$pmode=$_REQUEST['pmode'];
if($savecount==0)
{
	if(($cus=='-select-')||($cus==''))
    	{
    		echo "Data could not saved <br> Invalid supplier name";
    	}
	elseif(($pmode=='')||($pmode=='-select-'))
    	{
    		echo "Data could not saved <br> Invalid Payment Mode";
    	}
  	else
  	{
 	$i=0;
	$partname=$_REQUEST['partname'];
	foreach($partname as $a)
	{
 		$partname[$i]=$a; 
		$i++;
	}
 	if(count(array_unique($partname))<count($partname))
	{
    	}  
	$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;
	$itmtyp=$_REQUEST['itmtyp'];
	foreach($itmtyp as $a)
	{
		$itmtyp[$i]=$a; 
		$i++;
	}
	$i=0;
	$itmvl=$_REQUEST['itmvl'];
	foreach($itmvl as $a)
	{
		$itmvl[$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 purchretn 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;
		$mmba = 0;
		$mcomsn = 0;
		$mdiscount = 0;
		$mvat_per = 0;
		$mvat_amt = 0;
		$mpfchrgs = 0;
		$mothrchrgs = 0;
		$mgrp_ctr=0;
		$mctr=0;
		for($m=0;$m<$i;$m++)
       	{

	  		$grp=mysqli_query($db,"select gname from partmaster where partcode='$partcode[$m]'");
        		$grp_row=mysqli_fetch_array($grp);
		        $grp_name=$grp_row[0];
			$invdt=$_REQUEST['invdt'];
			$reg=$_REQUEST['reg'];
			
			$purchopt=$_REQUEST['purchopt'];
			$mba=$_REQUEST['mba'];
			$comsn=$_REQUEST['comsn'];
			$discount=$_REQUEST['discount'];
			$tax_perc=$_REQUEST['tax_perc'];
			$tax_amt=$_REQUEST['tax_amt'];
			$mrgn=$_REQUEST['mrgn'];
			$pfchrgs=$_REQUEST['pfchrgs'];
			$othrchrgs=$_REQUEST['othrchrgs'];
			

			mysqli_query($db,"insert into purchretn(invno,invdate,customer,cusid,item,item_code,unit,qty,unitprice,itmvl,total,flag,puroption,brcode,pmode,mba,comision,jvno,discount,tax,taxamt,itemtyp,mrgin,pfchrgs,othr_chrgs,gname) values('$inv','$invdt','$cus','$reg','$partname[$m]','$partcode[$m]','','$qty[$m]','$unitprice[$m]','$itmvl[$m]','$tot[$m]','','$purchopt','$brcode','$pmode','$mba','$comsn','$jvno','$discount','$tax_perc','$tax_amt','$itmtyp[$m]','$mrgn','$pfchrgs','$othrchrgs','$grp_name')");
			$reqry="select * from sparestock where itemcode='$partcode[$m]' and brcode='$brcode'";
			$rers=mysqli_query($db,$reqry);
			$num=mysqli_num_rows($rers);
		
			if($num>0)
			{
				if($itmtyp[$m]=='D')
					{
						mysqli_query($db,"update sparestock set curstk_dam=(curstk_dam-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
					}
				elseif($itmtyp[$m]=='S')
					{
						mysqli_query($db,"update sparestock set curstk_soil=(curstk_soil-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
					}
				elseif($itmtyp[$m]=='C')
					{
						mysqli_query($db,"update sparestock set curstk_cut=(curstk_cut-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
					}
				else
					{
						mysqli_query($db,"update sparestock set curstk=(curstk-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
					}
				
			}
			else
			{
				if($itmtyp[$m]=='D')
					{
						mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk_dam,brcode) values('$partcode[$m]','$partname[$m]','-$qty[$m]','$brcode') ");
					}
				elseif($itmtyp[$m]=='S')
					{
						mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk_soil,brcode) values('$partcode[$m]','$partname[$m]','-$qty[$m]','$brcode') ");
					}
				elseif($itmtyp[$m]=='C')
					{
						mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk_cut,brcode) values('$partcode[$m]','$partname[$m]','-$qty[$m]','$brcode') ");
					}
				else
					{
						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];
		       $flg1++;
	      	} 
		$mmba = ($mitem_sale *$mba/100);
		$mcomsn = ($mitem_sale *$comsn/100);
		$mdiscount = ($mitem_sale *$discount/100);

		$mpfchrgs = $pfchrgs;
		$mothrchrgs = $othrchrgs ;
		$mmrgn = ($mitem_sale*$mrgn/100);
		$mvat_per =$tax;
		$mvat_amt =$tax_amt;
		$minv_tot = $mitem_sale - ($mmba + $mcomsn + $mdiscount) + $mvat_amt + $mmrgn + $mpfchrgs + $mothrchrgs;
//print $mitem_sale."-".$mmba."+".$mcomsn."+".mdiscount."+".$mvat_amt."+".$mmrgn."+".$mpfchrgs."+".mothrchrgs;
//		$taxdrres=mysqli_query($db,"select sum(nettot) from workshop_invoice where invno='$inv' and  brcode='$brcode'");
//		$taxdrrow=mysqli_fetch_array($taxdrres);
  		//$ledger='Spare Purchase';  
//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','$minv_tot','0','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale Credit',' ','','0')";
		else
			$sql="insert into journal2 values('$invdt','$jvno','Dr','Cash','$minv_tot','0','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale Cash',' ','','0')";
     		mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		if($mitem_sale>0)
		{
			$mled = $mgroup_name[0].' 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','$mitem_sale','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mvat_amt>0)
		{
			$mled = 'Output Tax - VAT@'.$mvat_per.'%';
			$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','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mmba>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 Khadi Sale','$mmba','0','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mcomsn>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','Commission on Item Sale','$mcomsn','0','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mdiscount>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','Discount on Khadi Sale','$mdiscount','0','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
		if($mmrgn>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Cr','Margin on Khadi Sale','0','$mmrgn','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}

		if($mpfchrgs>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Cr','P F Charges on Khadi Sale','0','$mpfchrgs','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}

		if($mothrchrgs>0)
		{
			$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Cr','Other Charges on Khadi Sale','0','$mothrchrgs','Khadi Wholesale Inv No.: $inv','$brcode','Counter Sale',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}
//  		$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Dr','$cus','$taxdrrow[0]','0','Counter saleno: $inv,PBill:$coup_no','$brcode','Counter Sal Credit',' ','','0')";
//     		mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
//    		$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdt','$jvno','Cr','Counter Sale Credit','0','$taxdrrow[0]','Counter saleno: $inv,PBill:$coup_no','$brcode','Counter Sal 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='purchaseretn.php';</script>";
		}  
 	}
	}
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='purchaseretn.php';</script>";
}
?>
