<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once('opendb.php');
$savecount=$_REQUEST['savecount'];
$cus=$_REQUEST['cus'];
$pmode=$_REQUEST['pmode'];
$invno=$_REQUEST['invno'];
if($savecount==0)
{
	if(($cus=='-select-')||($cus==''))
    	{
    		echo "Data could not saved <br> Invalid branch 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;
	$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++;
    		}

                 $sql="select partcode from partmaster where partcode='$partcode[$j]'";
                $rsq=mysqli_query($db,$sql);
                $countnm=mysqli_num_rows($rsq);
                if($countnm==0)
                {
                           $spersonflg++;
                }
	}
	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>";
 	}
         elseif($spersonflg>0)
 	{
  		echo "<small>Item Code dosn't exists</small>";
 	}
	else
 	{
		for($n=0;$n<$i;$n++)
       					{
							//mysqli_query($db,"update sparestock set curstk=(curstk+'$qty[$n]') where itemcode='$partcode[$n]' and brcode='$brcode'");
							if($itmtyp[$n]=='D')
								{
									mysqli_query($db,"update sparestock set curstk_dam=(curstk_dam-'$qty[$n]') where itemcode='$partcode[$n]' and brcode='$brcode'");
								}
							elseif($itmtyp[$n]=='S')
								{
									mysqli_query($db,"update sparestock set curstk_soil=(curstk_soil-'$qty[$n]') where itemcode='$partcode[$n]' and brcode='$brcode'");
								}
							elseif($itmtyp[$n]=='C')
								{
									mysqli_query($db,"update sparestock set curstk_cut=(curstk_cut-'$qty[$n]') where itemcode='$partcode[$n]' and brcode='$brcode'");
								}
							else
								{
									mysqli_query($db,"update sparestock set curstk=(curstk-'$qty[$n]') where itemcode='$partcode[$n]' and brcode='$brcode'");
								}
								
						}
			$find_jv=mysqli_query($db,"select * from brtransferin where invno='$invno' and brcode='$brcode'");
  			$f_j=mysqli_fetch_row($find_jv);
  			$j_no=$f_j[18];
	 		mysqli_query($db,"delete from brtransferin where invno='$invno' and brcode='$brcode'");
	 		if($j_no >0)
				{
		 			mysqli_query($db,"delete from journal2 where jvno='$j_no' and brcode='$brcode'");
  		     		}
			$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'];
			$slsengnr=$_REQUEST['slsengnr'];
			$mba=$_REQUEST['mba_amt'];
                        $commision=$_REQUEST['cmsn_amt'];
			$discount=$_REQUEST['dscnt_amt'];
			$tax_perc=$_REQUEST['tax_perc'];
			$tax_amt=$_REQUEST['tax_amt'];
			$mrgn=$_REQUEST['mrgn'];
			$pfchrgs=$_REQUEST['pfchrgs'];
			$othrchrgs=$_REQUEST['othrchrgs'];
                        $refno=$_REQUEST['refno'];
			$refdt=$_REQUEST['ref_date'];
			mysqli_query($db,"insert into brtransferin(invno,invdate,customer,cusid,item,item_code,qty,unitprice,itmvl,total,salesman,brcode,pmode,mba,comision,jvno,discount,tax,taxamt,itemtyp,mrgin,pfchrgs,othr_chrgs,received,tobrcode,gname,refno,refdt) values('$invno','$invdt','$cus','$reg','$partname[$m]','$partcode[$m]','$qty[$m]','$unitprice[$m]','$itmvl[$m]','$tot[$m]','$slsengnr','$brcode','$pmode','$mba','$commision','$jvno','$discount','$tax_perc','$tax_amt','$itmtyp[$m]','$mrgn','$pfchrgs','$othrchrgs','N','$cus','$grp_name','$refno','$refdt')");
			$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,"update sparestock set curstk_dam=(curstk_dam-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
						mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk_dam,brcode,item_grp) values('$partcode[$m]','$partname[$m]','$qty[$m]','$brcode','$grp_name') ");
					}
				elseif($itmtyp[$m]=='S')
					{
						//mysqli_query($db,"update sparestock set curstk_soil=(curstk_soil-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
						mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk_soil,brcode,item_grp) values('$partcode[$m]','$partname[$m]','$qty[$m]','$brcode','$grp_name') ");
					}
				elseif($itmtyp[$m]=='C')
					{
						//mysqli_query($db,"update sparestock set curstk_cut=(curstk_cut-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
						mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk_cut,brcode,item_grp) values('$partcode[$m]','$partname[$m]','$qty[$m]','$brcode','$grp_name') ");
					}
				else
					{
						//mysqli_query($db,"update sparestock set curstk=(curstk-'$qty[$m]') where itemcode='$partcode[$m]' and brcode='$brcode'");
						mysqli_query($db,"insert into sparestock(itemcode,itemname,curstk,brcode,item_grp) values('$partcode[$m]','$partname[$m]','$qty[$m]','$brcode','$grp_name') ");
					}
				
				
			}

	  		
			$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 = $mba;
		$mcomsn = $commision;
		$mdiscount = $discount;

		$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;
  
//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','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt 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','$minv_tot','0','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt 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','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt',' ','','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','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt',' ','','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 Branch Receipt','$mmba','0','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt',' ','','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 Branch Receipt','$mcomsn','0','Khadi Wholesale Inv No.: $invno','$brcode','Branch Receipt',' ','','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 Branch Receipt','$mdiscount','0','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt',' ','','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 Branch Receipt','0','$mmrgn','Branch Receipt Inv No.: $invno','$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 Branch Receipt','0','$mpfchrgs','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt',' ','','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 Branch Receipt','0','$mothrchrgs','Branch Receipt Inv No.: $invno','$brcode','Branch Receipt',' ','','0')";
			mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		}

		if($flg1==$i) 
 		{
			
			echo "Data  saved with invoice number:".$invno;
			echo "<script> document.getElementById('savecount').value=1;document.location='branchreceipt.php';</script>";
		}  
 	}
	}
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='branchreceipt.php';</script>";
}
?>
