<? 
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("test.php");
include("../opendb.php");
include ("../test.php");
$invno=$_REQUEST['trnsno'];
$trnspnm=$_REQUEST['trnspnm'];
$transid=$_REQUEST['transid'];
$transdist=$_REQUEST['transdist'];
$vehicleno=$_REQUEST['vehno'];
$suptype=$_REQUEST['suptype'];
$docno=$_REQUEST['docno'];
$trantype=$_REQUEST['trantype'];
if($suptype==8)
{
    $suptyped="Stock transfer";
}
else
{
    $suptyped="";
}
if($invno>0)
{
    $re1=mysqli_query($db,"select to_branch,sum(qty*amount),tr_no,entrydate,taxp,brcode,braddr,brcity,brpin,hsncode from sparetransout_eway where tr_no='$invno' and brcode='$brcode'");
              // print "select to_branch,sum(qty*amount),tr_no,entrydate,taxp,brcode from sparetransout_eway where tr_no='$invno' and brcode='$brcode'";
    $rr1=mysqli_fetch_array($re1);
    $tobrcode=$rr1[to_branch];
    if(mysqli_num_rows($re1)<=0)
    {
        print "Invoice No does not exist";
        return;
    }
    $frqry=mysqli_query($db,"select * from branch where brcode='$brcode'");
    $frar=mysqli_fetch_array($frqry);
    $frgstin=$frar[cstno];
    $frstcod = substr($frgstin, 0, 2);
    $frbrnm='PSN AUTOMOTIVE MARKETING PVT LTD';
    
    $fraddr1=$frar[braddr];
    $fraddr1=substr($fraddr1, 0, 50);
    $fraddr1=preg_replace("/[^ \w]+/", "", $fraddr1);
    $fraddr2=$frar[brcity];
    $fraddr2=substr($fraddr2, 0, 50);
    $fraddr2=preg_replace("/[^ \w]+/", "", $fraddr2);
    $frpin=substr($frar[brcity],-6);
    //$toqry=mysqli_query($db,"select * from branch where brcode='$tobrcode'");
    //$toar=mysqli_fetch_array($toqry);
    $togstin=$frar[cstno];
    $tostcod = substr($togstin, 0, 2);
    $tobrnm='PSN AUTOMOTIVE MARKETING PVT LTD';
    $toaddr1=$rr1[braddr];
    $toaddr1=substr($toaddr1, 0, 50);
    $toaddr1=preg_replace("/[^ \w]+/", "", $toaddr1);
    $toaddr2=$rr1[brcity];    
    $toaddr2=substr($toaddr2, 0, 50);
    $toaddr2=preg_replace("/[^ \w]+/", "", $toaddr2);
    $topin=$rr1[brpin];
    $entrydate=$rr1[entrydate];
    $entrydate1=implode('/', array_reverse(explode('-', $entrydate)));
    $mtotinvvalue =$rr1[1];
    $myJSON.='{"version":"1.0.0621","billLists":[';
    $myJSON.= '{"userGstin":"'.$frgstin.'","supplyType":"O","subSupplyType":"'.addslashes($suptype).'","subSupplyDesc":"'.addslashes($suptyped).'","docType":"CHL","docNo":"'.addslashes($rr1[tr_no]).'","docDate":"'.addslashes($entrydate1).'","transType":"'.addslashes($trantype).'","fromGstin":"'.$frgstin.'","fromTrdName":"'.$frbrnm.'","fromAddr1":"'.$fraddr1.'","fromAddr2":"'.$fraddr2.'","fromPlace":"'.$fraddr2.'","fromPincode":"'.$frpin.'","fromStateCode":"'.$frstcod.'","actualFromStateCode":"'.$frstcod.'","toGstin":"'.$togstin.'","toTrdname":"'.$tobrnm.'","toAddr1":"'.$toaddr1.'","toAddr2":"'.$toaddr2.'","toPlace":"'.$toaddr2.'","toPincode":"'.$topin.'","toStateCode":"'.$tostcod.'","actualToStateCode":"'.$tostcod.'","totalValue":"'.addslashes($rr1[1]).'","cgstValue":"'.addslashes(0).'","sgstValue":"'.addslashes(0).'","igstValue":"'.addslashes(0).'","cessValue":"'.addslashes(0).'","TotNonAdvolVal":"1","OthValue":"1","totInvValue":"'.addslashes($mtotinvvalue).'","transMode":"1","transDistance":"'.addslashes($transdist).'","transporterName":"'.addslashes($trnspnm).'","transporterId":"'.addslashes($transid).'","transDocNo":"'.addslashes($docno).'","transDocDate":"'.addslashes($entrydate1).'","vehicleNo":"'.addslashes($vehicleno).'","vehicleType":"R","mainHsnCode":"'.addslashes($rr1[hsncode]).'",';
   $myJSON.= '"itemList":[';
//$jcrow=mysqli_fetch_array($re);
//$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
//$kar=mysqli_fetch_array($ker);
//if($foc=='foc')
//$re=mysqli_query($db,"select * from spareinvoicefoc where invno='$invno' and invtype='$invtype' and brcode='$brcode'")or die('err:'.mysqli_error($db));
//else 
//$re=mysqli_query($db,"select * from spareinvoicedc where invno='$invno'  and brcode='$brcode' and invdate>='2017-07-01'  ")or die(mysqli_error($db));
//$row=mysqli_fetch_array($re)or die("Fetch Error:".mysqli_error($db));
//$myJSON="";
$re=mysqli_query($db,"select * from sparetransout_eway where tr_no='$invno' and brcode='$brcode'");
$sql="select * from sparetransout_eway where tr_no='$invno' and brcode='$brcode'";
//echo $sql;
$i=1;
while($row=mysqli_fetch_array($re))
{
//    $hsnqry=mysqli_query($db,"select hsncode from partmaster where partcode='$row[partcode]'");
    //$hsnarr=mysqli_fetch_array($hsnqry);
	 $myJSON1.= '{"itemNo":"'.$i++.'","productName":"'.addslashes($row[partcode]).'","productDesc":"'.addslashes($row[partname]).'","hsnCode":"'.addslashes($row[hsncode]).'","quantity":"'.addslashes($row[qty]).'","qtyUnit":"NOS","taxableAmount":"'.addslashes(($row[qty]*$row[amount])).'","sgstRate":"'.addslashes(0).'","cgstRate":"'.addslashes(0).'","igstRate":"'.addslashes(0).'","cessRate":"'.addslashes(0).'","cessNonAdvol":"0"},';
//$i++;
}
$myJSON1[strrpos($myJSON1, ',')] = '';
//$myJSON1.= trim($myJSON1,',').']';
//echo $myJSON1;
$myJSON1=trim($myJSON1);
$myJSON.=$myJSON1;
//echo trim($myJSON,',').']';
//echo '['.trim($myJSON,',').']';
$myJSON.=']}]}';

//echo $myJSON;

$json_data = $myJSON;
file_put_contents('results.json', $json_data);

    # send the file to the browser as a download
header('Content-disposition: attachment; filename=myjson.json');
header('Content-type: application/json');
echo $json_data;
//print "<script>document.location='retreive.php'</script>";
return;
}


?>
