<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
 return;
}
include("../opendb.php");
if(isset($_POST['upd']))
{
	$i=0;
    $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[]='';
	$msl_no[]='';
	$mdt[]='';
	$x=0;
	$err=0;
	
	while(!feof($fp))
	{
 		$line=fgets($fp,1024);
 		$spline=explode("\t",$line);
 		if(strlen($spline[0])>0)
 		{
			$df =implode('-', array_reverse(explode('-', $spline[0])));  
			$mdate	 = $df;
			$mjvno	 = $jvno;
			$mtype	 = trim($spline[1]); 
			$mpartic= trim($spline[2]);
			$mdramt = $spline[3];
			$mcramt = $spline[4];
			$mslno  = $spline[6];

			$mdramt = trim($mdramt,'"');
	        $mdramt = str_replace(',', '', $mdramt);
	        $mdramt = str_replace(' ', '', $mdramt);
            $mcramt = trim($mcramt,'"');
		    $mcramt = str_replace(',', '', $mcramt);
		    $mcramt = str_replace(' ', '', $mcramt);
			$mnarr	 = trim($spline[5]);
			$mnarr  = trim($mnarr,'"');
			if($mdramt<='' || $mdramt=='-')$mdramt=0;
			if($mcramt<='' || $mcramt=='-')$mcramt=0;
			$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)
			{
			    
				$mdt[$i] =$mdate;
				$msl_no[$i]= $mslno;
				
				$msql[$i++]="','".$mtype."','".$mpartic."','".$mdramt."','".$mcramt."','".$mnarr."','".$brcode."','".$mj_type."','".$magainst."')";
			    //print "pppp";	
			}
			else
			{
				print "Line :".$x."Invalid data....!!!".$spline."<br>";
			}
			//print "yyyyy";
 		}
		$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;
		$msln  = 0;
		for($ctr=0;$ctr<=$i-1;$ctr++)
		{
			if($msln==$msl_no[$ctr])
				$mjvno = $mjvno;
			else
			{
				$mjvno = $mjvno + 1;
				$msln  = $msl_no[$ctr];
			}
			$sql="insert into journal2  (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against) values('".$mdt[$ctr]."','".$mjvno.$msql[$ctr];
			print "<br>".$sql;
			mysqli_query($db,$sql) or die(mysql_error());
		}
		print "<script> alert('Done');</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='upldjournew.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 | Sl no
<tr><td> Sample
<tr><td>2017-10-30| Cr | TELSA MARKETING (P) LTD | 0 | 1000| TELSA MARKETING (P) LTD [ 32AAACT8736H1ZB ] | 1
</table>
</td></tr>
</table>
</form>
