<?
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');
       // print "File copied :".$fl;
   }
	$fp=fopen("./$file_dir/$ff_name","r") or die("Cant open file");

	//include("opendb.php");
	$dt=date("Y-m-d H:i:s");
	$msql[]='';
	$msl_no[]='';
	$x=0;
	$err=0;
	while(!feof($fp))
	{
 		$line=fgets($fp,1024);
 		$spline=split("\t",$line);
 		if(strlen($spline[0])>0)
 		{
			$mdate	 = $df=implode('-', array_reverse(explode('-', $spline[0])));  
			$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);
	              	$mcramt = trim($mcramt,'"');
		        $mcramt = str_replace(',', '', $mcramt);
			$mnarr	 = trim($spline[5]);
			$mnarr  = trim($mnarr,'"');
			$mj_type= 'CR';
			$magainst='OFNO';
			$brres=mysql_query("select count(led) from ledger where brcode='$brcode' and led = '$mpartic' ");
			$brrow=mysql_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."')";
			}
			else
			{
				print "Line :".$x."Invalid data....!!!".$spline."<br>";
			}
 		}
		$x++;
	}
	if($err==0)
	{	
		$brres=mysql_query("select max(jvno) from journal2 where brcode='$brcode'");
		$brrow=mysql_fetch_array($brres);
		$mjvno = $brrow[0]+1;

		$crres=mysql_query("select max(crno) from cshrec where brcode='$brcode'");
		$crrow=mysql_fetch_array($crres);
		$mcrno = $crrow[0]+1;

		$msln  = 0;
		for($ctr=0;$ctr<=$i-1;$ctr++)
		{
			if($msln==$msl_no[$ctr])
			{
				$mjvno = $mjvno;
				$mcrno = $mcrno;
			}
			else
			{
				$mjvno = $mjvno + 1;
				$msln  = $msl_no[$ctr];
				$mcrno = $mcrno + 1;
			}
			$sql="insert into journal2  (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against) values('".$mdt[$ctr]."','".$mjvno.$msql[$ctr];
			print "<br>".$sql;
			mysql_query($sql) or die(mysql_error());
			$re=mysql_query("select * from journal2 where jvno = $mjvno");
			while($row23=mysql_fetch_array($re))
			{
				$narr1 = $row23[narr]."(CR:$mcrno)";
				$sql1 = "update journal2 set narr = '$narr1' where jvno = $mjvno and partic = '$row23[partic]'";	
				mysql_query($sql1) or die(mysql_error());
				if($row23[type]=='Cr')
				{
					$sql23="insert into cshrec (date,crno,type,partic,dramt,cramt,narr,brcode,j_type)	values('".$row23[date]."','".$mcrno."','Cr','".$row23[partic]."','0','".$row23[cramt]."','".$narr1."','".$brcode."','".$mjvno."')";
					print "<br>".$sql23;
					mysql_query($sql23) 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='upldcr.php' method='post' ENCTYPE="multipart/form-data">
<table border=1>
<tr><td>
<table>
<th colspan=2>Upload CASH Receipt 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>
