<?session_start();
include("../opendb.php");
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(isset($_POST['upd']))
{
    $ff=$_REQUEST['ff'];
	if($ff !="none")
	{
   		$imagename=$_FILES['ff']['name'];
        $filepath= "";
        $tmp = $_FILES['ff']['tmp_name'];
        move_uploaded_file($tmp, $filepath . $imagename) or die(' err:could not copy file');
	}
	$fp=fopen($filepath.$imagename,"r") or die("Cant open file");
	$i=0;
	//include("opendb.php");
	$dt=date("Y-m-d H:i:s");
	$msql[]='';
	$x=0;
	$err=0;
	while(!feof($fp))
	{
 		$line=fgets($fp,1024);
 		$spline=explode("\t",$line);
 		if(strlen($spline[0])>0)
 		{
 		    $mdate = str_replace("/","-",$spline[0]);
 		    if(substr($mdate,2,1)=='-')
			    $mdate	 =implode('-', array_reverse(explode('-', $mdate)));
			else
			    $mdate	 =$mdate;
			$mjvno	 = $jvno;
			$mtype	 = trim($spline[1]); 
			$mpartic= trim($spline[2]);
			$mpartic= str_replace('"',"",$mpartic);
			$mdramt = $spline[3];
			$mcramt = $spline[4];
			$mdramt = trim($mdramt,'"');
	        $mdramt = str_replace(',', '', $mdramt);
	        $mdramt = str_replace('-', '0', $mdramt);
	        if($mdramt=='')
	            $mdramt=0;
	        $mcramt = trim($mcramt,'"');
	        $mcramt = str_replace(',', '', $mcramt);
	        $mcramt = str_replace('-', '0', $mcramt);
	        if($mcramt=='')
	            $mcramt=0;
			$mnarr	 = trim($spline[5]);
			$mnarr  = trim($mnarr,'"');
			$mj_type= 'JV';
			$magainst='OFNO';
			$brres=mysqli_query($db,"select count(led) from ledger where brcode='$brcode' and led = '$mpartic' ");
			$brrow=mysqli_fetch_array($brres);
			if($brrow[0]<=0)
			{
				print "Line :".$x." Ledger : ".$mpartic." Not Found !!!<br>";
				$err=1;
			}
			if(($mdramt + $mcramt)>0)
			{
				$msql[$i++]="','".$mtype."','".$mpartic."','".trim($mdramt)."','".trim($mcramt)."','".$mnarr."','".$brcode."','".$mj_type."','".$magainst."')";
			}
			else
			{
				print "Line :".$x."Invalid data....!!!".$spline."<br>";
			}
 		}
		$x++;
	}
	if($err==0)
	{	
		$brres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
		$brrow=mysqli_fetch_array($brres);
		$mjvno = $brrow[0]+1;
		for($ctr=0;$ctr<=$i-1;$ctr++)
		{
			$sql="insert into journal2  (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against) values('".$mdate."','".$mjvno.$msql[$ctr];
			print "<br>".$sql;
			mysqli_query($db,$sql) or die(mysqli_error());
		}
		$mdate1=implode('-', array_reverse(explode('-', $mdate)));
		print "<script> alert(' JV No.: $mjvno || Date :$mdate1 Uploaded');</script>";
	}
	else
	{
		print "<script> alert('Please check your file for the errors....!!!');</script>";
	}
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:c1d799}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='upldjour.php' method='post' ENCTYPE="multipart/form-data">
<table border=1>
<tr><td>
<table>
<th colspan=2>Upload Journal Entries
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Columns :  <tr><td>Date | Type | Ledger| Debit Amount | Credit Amount | Narration
<tr><td> Sample
<tr><td>2017-10-30| Cr | TELSA MARKETING (P) LTD | 0 | 1000| TELSA MARKETING (P) LTD [ 32AAACT8736H1ZB ] 
</table>
</td></tr>
</table>
</form>
