<?php
// error_reporting(E_ERROR | E_PARSE);
session_start();
include '../../class/ClassForm.php';
require '../../Classes/PHPExcel.php';
// echo getIndianCurrency(floatval ("9894354.00"));
// echo str_replace('tmp','json',tempnam("../../uploads/json/","invson00"));
// $inv_id = $_GET['id'];
$inv_id = isset($_GET['id']) ? $_GET['id'] : 0;
$filName = 'Download.xlsx';
if($inv_id != 0 ){
    $inv_query = "select distinct * from domestic_invS where id='".$_GET['id']."'";
    $result = $ClassForm->Fetch_Result_Set($inv_query);
    $row = mysqli_fetch_assoc($result);

    $inv_temQuery = "select distinct * from inv_template where id='".$row['template_id']."'";
    $teml_res = $ClassForm->Fetch_Result_Set($inv_temQuery);
    $temRow = mysqli_fetch_assoc($teml_res);

    $biller_query = "select * from biller_details where inv_id = '".$row['inv_no']."'";
    $bill_resule = $ClassForm->Fetch_Result_Set($biller_query);
    $billRow = mysqli_fetch_assoc($bill_resule);

    $tax_Query = "select * from domestic_taxdetails where inv_id ='".$_GET['id']."'";
    $tax_resule = $ClassForm->Fetch_Result_Set($tax_Query);
    $taxRow = mysqli_fetch_assoc($tax_resule);

    $ser_Query = "select * from domestic_service where inv_id ='".$_GET['id']."'";
    $ser_resule = $ClassForm->Fetch_Result_Set($ser_Query);

    $target = 'Base Copy.xlsx';
    $excel = PHPExcel_IOFactory::load($target);
    $excel ->setActiveSheetIndex(0);

    //print_r($billRow);
    // Biller Details
    $excel->getActiveSheet()->setCellValue('B14',$billRow['biller_Name']);
    $excel->getActiveSheet()->setCellValue('B15',$billRow['biller_Addr1']);
    $excel->getActiveSheet()->setCellValue('B16',$billRow['biller_Addr2']);
    $excel->getActiveSheet()->setCellValue('B17',$billRow['biller_Addr3']);
    $excel->getActiveSheet()->setCellValue('B18',$billRow['biller_Addr4']);
    $excel->getActiveSheet()->setCellValue('D19',$billRow['biller_GSTIN']);
    $excel->getActiveSheet()->setCellValue('D20',$billRow['biller_PAN']);

    // QR code Image Details
    // $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
    // $objDrawing->setName('QR image');
    // $objDrawing->setDescription('QR Details');
    // $objDrawing->setImageResource($gdImage);
    // $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
    // $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
    // $objDrawing->setHeight(250);
    // $objDrawing->setCoordinates('I3');
    // $objDrawing->setWorksheet($excel->getActiveSheet());

    $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    
    // $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
    $objWriter->save($filName);
    echo $filName;
    // if (file_exists($filName)) {
    //     header('Content-Description: File Transfer');
    //     header("Content-Type: application/vnd.ms-excel");
    //     header('Content-Disposition: attachment; filename="'.basename($filName).'"');
    //     header('Expires: 0');
    //     header('Cache-Control: must-revalidate');
    //     header('Pragma: public');
    //     header('Content-Length: ' . filesize($filName));
    //     readfile($filName);
    //     exit;
    // }
}
else{
    
    $location = '../../uploads/json/New Text Document.JSON.json';
    $string = file_get_contents($location);
    $data = json_decode($string, true);
    print_r($data);
    // $colla = $data['colla'];
    // print_r($colla);
    // echo $colla[0]['items'];
    // foreach($colla as $col){
    //     echo $col['items'];
    // }
}

function getIndianCurrency(float $number)
{
    $decimal = round($number - ($no = floor($number)), 2) * 100;
    $hundred = null;
    $digits_length = strlen($no);
    $i = 0;
    $str = array();
    $words = array(0 => '', 1 => 'one', 2 => 'two',
        3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
        7 => 'seven', 8 => 'eight', 9 => 'nine',
        10 => 'ten', 11 => 'eleven', 12 => 'twelve',
        13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
        16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen',
        19 => 'nineteen', 20 => 'twenty', 30 => 'thirty',
        40 => 'forty', 50 => 'fifty', 60 => 'sixty',
        70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
    $digits = array('', 'hundred','thousand','lakh', 'crore');
    while( $i < $digits_length ) {
        $divider = ($i == 2) ? 10 : 100;
        $number = floor($no % $divider);
        $no = floor($no / $divider);
        $i += $divider == 10 ? 1 : 2;
        if ($number) {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
        } else $str[] = null;
    }
    $Rupees = implode('', array_reverse($str));
    $paise = ($decimal > 0) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
    return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
}

?>