<?php
session_start();
error_reporting(E_ERROR | E_PARSE);
function mkstemp( $template ) {
    $attempts = 238328; // 62 x 62 x 62
    $letters  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $length   = strlen($letters) - 1;
  
    if( mb_strlen($template) < 6 || !strstr($template, 'XXXXXX') )
      return FALSE;
  
    for( $count = 0; $count < $attempts; ++$count) {
      $random = "";
  
      for($p = 0; $p < 6; $p++) {
        $random .= $letters[mt_rand(0, $length)];
      }
  
      $randomFile = str_replace("XXXXXX", $random, $template);
  
    //   if( !($fd = @fopen($randomFile, "x+")) )
    //     continue;
  
    //   return $fd;
        return $randomFile;
    }
  
    return FALSE;
}
$item = array(
    "SlNo" => "1",
    "PrdDesc" => "RBL/BS/016-EXP-RD9207",
    "IsServc" => "N",
    "HsnCd" => "8708",
    "Qty" => 50,
    "Unit" => "SET",
    "UnitPrice" => 384.52,
    "TotAmt" => 19225.75,
    "Discount" => 0,
    "PreTaxVal" => 0,
    "AssAmt" => 19225.75,
    "GstRt" => 0,
    "IgstAmt" => 0,
    "CgstAmt" => 0,
    "SgstAmt" => 0,
    "CesRt" => 0,
    "CesAmt" => 0,
    "CesNonAdvlAmt" => 0,
    "StateCesRt" => 0,
    "StateCesAmt" => 0,
    "StateCesNonAdvlAmt" => 0,
    "OthChrg" => 0,
    "TotItemVal" => 19225.75
);
$ItemList = array();
$ItemList[] = $item;
$ItemList[] = $item;
$TranDtls = array(
    
        "TaxSch" => "GST",
        "SupTyp" => "EXPWOP",
        "IgstOnIntra" => "N",
        "RegRev" => null,
        "EcmGstin" => null
    
);
$DocDtls = array(
    "Typ" => "INV",
    "No" => "2001000060",
    "Dt" => "29/10/2020"
);
$SellerDtls = array(
    "Gstin" => "33AADCR7688H1ZZ",
    "LglNm" => "RANE BRAKE LINING LIMITED",
    "TrdNm" => "RANE BRAKE LINING LIMITED",
    "Addr1" => "30 INDUSTRIAL ESTATE",
    "Addr2" => "AMBATTUR",
    "Loc" => "CHENNAIT",
    "Pin" => 600058,
    "Stcd" => "33",
    "Ph" => null,
    "Em" => null
);
$BuyerDtls = array(
    "Gstin" => "URP",
    "LglNm" => "DIESEL",
    "TrdNm" => "POST BOX NO.33",
    "Pos" => "96",
    "Addr1" => "COLOMBO",
    "Addr2" => null,
    "Loc" => "SRI LANKA",
    "Pin" => 999999,
    "Stcd" => "96",
    "Ph" => null,
    "Em" => null
);
$ValDtls = array(
    "AssVal" => 19225.75,
    "IgstVal" => 0,
    "CgstVal" => 0,
    "SgstVal" => 0,
    "CesVal" => 0,
    "StCesVal" => 0,
    "Discount" => 0,
    "OthChrg" => 0,
    "RndOffAmt" => 0,
    "TotInvVal" => 19225.75
);
$Version = array(
    'Version' => '1.1',
    'TranDtls' => $TranDtls,
    'DocDtls' => $DocDtls,
    'SellerDtls' => $SellerDtls,
    'BuyerDtls' => $BuyerDtls,
    'ValDtls' => $ValDtls,
    'ItemList' => $ItemList
);
$json = array( 
    $Version    
);
// header('Content-Type: application/json');
// echo json_encode($json);

$data = json_encode($json);
// echo $data;
$jsonFile  = $BuyerDtls['Gstin'].'.json';
file_put_contents($jsonFile, $data);

if (file_exists($jsonFile)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($jsonFile).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($jsonFile));
    readfile($jsonFile);
    exit;
}
else{
    echo "file Not Found";
}



?>