<? session_start();
$brcode=$_SESSION['brcode'];
if(strlen($brcode)<=0)
{
 	print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 	return;
}

include("../opendb.php");
include("../test.php");
if(isset($_POST['Update']))
{
    $chasisno=$_REQUEST['chsisno'];
    $engineno=$_REQUEST['engineno'];
    $vehstat=$_REQUEST['vehstat'];
    $rsqry=mysqli_query($db,"select chasis from vehstatus where chasis='$chasisno' and brcode='$brcode'");
    $rwnum=mysqli_num_rows($rsqry);
    if($rwnum>0)
    {
        $sql10="update vehstatus set stsflg='$vehstat' where brcode='$brcode' and chasis='$chasisno'";
    }
    else
    {
        $sql10="insert into vehstatus (chasis,engno,brcode,stsflg) values('$chasisno','$engineno','$brcode','$vehstat')";  
    }
}
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
tr{background-color:white} 
th{background-color:c1d799;font-size:13}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<form action='<?print $PHP_SELF?>'>
<table bgcolor='silver'>
<th colspan=2>DATE WISE STOCK REPORT
<tr><td> Date  </td><td>dd<input type=text name='d' size=2 value='<?print date("d",time())?>'>
				mm<input type=text name='m' size=2 value='<?print date("m",time())?>'>
				yy<input type=text name='y' size=4 value='<?print date("Y",time())?>'>
			</td></tr>
<tr><td colspan=2 align=center><input type=submit name='show' value='Show'>
<input type=submit name='prn' value='Print'>
</td></tr>
</table>
</form>

<center>
<?
$prn = $_REQUEST['prn'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$d = $_REQUEST['d'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$d1 = $_REQUEST['d1'];
$show = $_REQUEST['show'];

if($prn)
{
   $df=$y.'-'.$m.'-'.$d;
   $dt=$y1.'-'.$m1.'-'.$d1;
       print "<script> window.open('prndwstkrepov.php?df=$df');</script>";
}

if($show)
{
	$adr='tmpkk';
	$adr.=str_replace('.','',$REMOTE_ADDR);
	$df=$y.'-'.$m.'-'.$d;
	print "<font size=3><b>Stock report for the day ".implode('-', array_reverse(explode('-', $df)))."</b>";
	$sql="drop table if  exists $adr";
	mysqli_query($db,$sql);
	
	$sql="create table if not exists ".$adr." (da varchar(100),config varchar(250),mcode varchar(100), engno varchar(200),";
	$sql.="chno varchar(100),pdate date, pvalue double(14,2), pinv varchar(100),rem varchar(20), loc varchar(200))";
	mysqli_query($db,$sql);
	
	$re=mysqli_query($db,"select dano,config,mcode,engno,chno,pdate,pval,pinv,loc from vehopstock where brcode='$brcode' order by pdate");

	while($row=mysqli_fetch_row($re))
	{
		$sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','OP','$row[8]')";
		mysqli_query($db,$sq) or die(mysqli_error());
	}

	$re=mysqli_query($db,"select da,config,mcode,engno,chasis,entrydate,amt2,invno,loc,type,taxtype,netamt from veh_purchase
	where  brcode='$brcode' and entrydate <='$df' order by entrydate")or die(mysqli_error());
	$i=1;
	while($row=mysqli_fetch_row($re))
	{
		if ($row[10]=='CST')
		{
			$sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[11]','$row[7]','Purchase','$row[8]')";
			mysqli_query($db,$sq) or die(mysqli_error());
		}
		else
		{
			$sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','Purchase','$row[8]')";
			mysqli_query($db,$sq) or die(mysqli_error());
		}
	}
	
	//*****************  sales return 
	$sql="select * from veh_salereturn where date<='$df' and brcode='$brcode' ";
	$result=mysqli_query($db,$sql)or die(mysqli_error());
	while($row=mysqli_fetch_array($result))
	{
        //$chrow=mysqli_fetch_array($chres);
	 	mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','','$row[da]','$row[date]','$row[amt2]','$row[ret_num]','Sales Ret','')")or die('err:'.mysqli_error());
	}

	//*****************  purchase return 
	$sql="select * from veh_purchreturn where date<='$df' and brcode='$brcode' ";
	$result=mysqli_query($db,$sql)or die(mysqli_error());
	
	while($row=mysqli_fetch_array($result))
	{
	    
	    if($row['da']=='MC2P2HRT0TA557125')
	    {
	        $chsno = $row['da'];
	    }
	    else
	    {
	        $sql21="select chasis from veh_purchase where da ='$row[da]'  and brcode='$brcode' ";
	        $result21=mysqli_query($db,$sql21) or die(mysqli_error());
		    $row21=mysqli_fetch_row($result21);
		    $chsno = $row21[0];
	    }
        //print "insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','','$row21[0]','$row[date]','$row[amt2]','$row[ret_num]','Purch Ret','')";	    
	 	mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','','$chsno','$row[date]','$row[amt2]','$row[ret_num]','Purch Ret','')")or die('err:'.mysqli_error());
	}

	//**********
	$re=mysqli_query($db,"select * from veh_invoice where brcode='$brcode' and invdate<='$df'  order by invdate");
	$i=1;
	print "<table cellspacing=1 cellpadding=2 bgcolor=gray>";
	//print "<th>No<th>Error<th>DA  <th>Purchase Date<th>Saleno <th>Saledate ";
	while($row=mysqli_fetch_array($re))
	{
	 	mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','','$row[chasis]','$row[invdate]','$row[amt2]','$row[invno]','Sale','')")or die('err:'.mysqli_error());	    
	}
	print "</table>";


	$sq="select distinct chno from ".$adr." where rem = 'OP' or rem = 'Purchase' order by pdate";
	//echo $sq;
	$res32=mysqli_query($db,$sq);
	if(mysqli_num_rows($res32)>0)
	{
 		print "<table cellspacing=1 cellpadding=2 bgcolor=gray>";
	 	print "<th>No<th>DA  <th>Config<th>M.Code <th>Eng NO <th>Ch No <th>Pur Date<th> P.Value <th>Pur Inv. <th>Rem <th>Loc<th>Age<th>Status<th>Update";
	 	$i=1;
		$t=0;
		while($row1=mysqli_fetch_array($res32))
		{
		    $mflg==1;
		    $chasisno = $row1[0];
		    $rsqry=mysqli_query($db,"select chno,rem from $adr where chno ='$row1[0]' ");
            $rwnum=mysqli_num_rows($rsqry);
            $op = 0;
		    while($row2=mysqli_fetch_array($rsqry))
		    {
		        if($row2[1]=='Sale')
		            $op = $op - 1;
		        else if($row2[1]=='Purchase')
		            $op = $op + 1;
		        else if($row2[1]=='OP')
		            $op = $op + 1;
		        else if($row2[1]=='Sales Ret')
		            $op = $op + 1;
		        else if($row2[1]=='Purch Ret')
		            $op = $op - 1;
		    }
		    
		    $rsqry=mysqli_query($db,"select stsflg from vehstatus where chasis='$row[4]' and brcode='$brcode'");
            $rwnum=mysqli_num_rows($rsqry);
            if($rwnum>0)
            {
                $rsrw=mysqli_fetch_array($rsqry);
                $vehremrks=$rsrw['stsflg'];
            }
            else
            {
                $vehremrks='';  
            }
            
            if($op>=1)
            {
                //echo "select * from $adr where chno='$chasisno' order by pdate";
                $rsqry3=mysqli_query($db,"select * from $adr where chno='$chasisno' order by pdate");
                $row=mysqli_fetch_array($rsqry3);
			    $ag=(strtotime($df)-strtotime($row['pdate']))/86400;
			    $lres=mysqli_query($db,"select lname from loc_master where code='$row[9]'");
			    $lorow=mysqli_fetch_row($lres);
			    print "<form action='form1' id='form1' method='POST' action=''>";
			    print "<tr><td align=center>".$i++."</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>";
	  		    print "<td>$row[4]</td><td>".implode('-', array_reverse(explode('-', $row[5])))."</td><td align=right>$row[6]</td><td>$row[7]</td><td align=center>$row[8]</td><td>";
	  		   $locval= getdmsloc($chasisno,$db);
	  		    if($locval>'')
	  		    {
	  		        echo $locval;
	  		    }
	  		    else
	  		    {
	  		        echo $lorow[0];
	  		    }
	  		    print "</td><td>$ag pp</td><td><textarea rows='2' cols='25' name='vehstat' id='vehstat' onkeyup='validvls()'>$vehremrks</textarea> </td><td><input type='button' name='statbtn' id='statbtn' value='Update' onclick='updatedtls()'><input type='hidden' name='chsisno' id='chsisno' value='$row[4]'></td><input type='hidden' name='engineno' id='engineno' value='$row[3]'></td>";
	  		    print "</form>";
	 		    $t+=$row[6];
            }
	 	}
		print "<tr><td colspan=7><center><b>Total</td><td align=right><b>$t</td><td colspan=3></td></tr>";
		print "</table>";
	}
	
} // end if show
	
	function getdmsloc($dmschasis,$db)
{
    
$data = [
    'chasis' => $dmschasis
    ];
    //print_r($data);
   //exit;
$str = http_build_query($data);


$ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://psnautos.in/psn/getconsigneeloc.php');
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec($ch);
	echo $output;

	print_r($output);
	//exit;
	return $output;
	curl_close($ch);
}
?>
<script>
    function updatedtls()
    {
        var chasisno=document.getElementById('chsisno').value;
        var engineno=document.getElementById('engineno').value;
        var statusflg=document.getElementById('vehstat').value;
        var url="vehstatupdate.php?chasisno="+chasisno+"&&engineno="+engineno+"&&statusflg="+statusflg;
        document.location=url,true;

    }
    function validvls()
{
	var cmplstr=document.getElementById("vehstat").value;
 //yy=abc.value
 if(cmplstr.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    //abc.value='';
  }
 if(cmplstr.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    //abc.value='';
  }
 if(cmplstr.indexOf('"')>-1) 
  {
    alert("\" is not allowed");
    //abc.value='';
  }

if(cmplstr.indexOf("&")>-1) 
  {
    alert("& is not allowed");
    //abc.value='';
  }
  
}

</script>