<?php
// error_reporting(E_ERROR | E_PARSE);
include 'Auth.php';
include '../class/ClassForm.php';
include '../tools/decrypt_QR.php';
require '../Classes/PHPExcel.php';

// foreach ($_POST as $key => $value) {
//     echo "key val  = ".$key." And Value = ".$value."\n\r"; // one of the quiz answer
// }

$Qr_data = isset($_POST['Qr_data'])?$_POST['Qr_data'] : '';
$Inv_no = isset($_POST['Inv_no'])?$_POST['Inv_no'] : '';
$Inv_Date = isset($_POST['Inv_Date'])?$_POST['Inv_Date'] : '';
$SellerGstin = isset($_POST['SellerGstin'])?$_POST['SellerGstin'] : '';
$BuyerGstin = isset($_POST['BuyerGstin'])?$_POST['BuyerGstin'] : '';
$TotInvVal = isset($_POST['TotInvVal'])?$_POST['TotInvVal'] : '';
$ItemCnt = isset($_POST['ItemCnt'])?$_POST['ItemCnt'] : '';
$Irn = isset($_POST['Irn'])?$_POST['Irn'] : '';
$Qr_url = isset($_POST['Qr_url'])?$_POST['Qr_url'] : '';
$json_url = isset($_POST['json_url'])?$_POST['json_url'] : '';

$key = "-----BEGIN PUBLIC KEY-----
        MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArxd93uLDs8HTPqcSPpxZrf0
        Dc29r3iPp0a8filjAyeX4RAH6lWm9qFt26CcE8ESYtmo1sVtswvs7VH4Bjg/FDlRpd+MnAlX
        uxChij8/vjyAwE71ucMrmZhxM8rOSfPML8fniZ8trr3I4R2o4xWh6no/xTUtZ02/yUEXbphw3
        DEuefzHEQnEF+quGji9pvGnPO6Krmnri9H4WPY0ysPQQQd82bUZCk9XdhSZcW/am8w
        BulYokITRMVHlbRXqu1pOFmQMO5oSpyZU3pXbsx+OxIOc4EDX0WMa9aH4+snt18WA
        XVGwF2B4fmBk7AtmkFzrTmbpmyVqA3KO2IjzMZPw0hQIDAQAB
        -----END PUBLIC KEY-----";
// $encrypted = 'kwLp6MJVHAd9fNf0dImSiPg47tF/8l7tFY4Bl6HZqjDjeCHynzBWhx4WoacWlnIX7Oy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3C1oNIwcPjLYWw/wNsw7l4HgKcO+Cug0SWCgxuL/+QPSD6CDNBpoqZFC/FmGFI+BwVc7D9M74caiShG9P3ctotQs41xAb 8Pxba5nd0UOELYDP96NQvFVbHFdjVFeF/pi8hM30Ep4EdYRtjjtzimRntyKINkeVHMKKvnYm/tp2a2Zlv939nEkC4elRQ4M+WmEENVUa1EyuuWEx9HKqEeT9EuQbApYmLsBJLH/B66vC7VNvJ0q ohqW0du+U9OcYgvHvEbRwl/fYQZ6eu9FXMQ2zvcydoCRluiaECIufY5rpradvu3W55XqoH57gwG5GYepz+a5aOttjQjHGbyLC1o7Bf7reyjKbbIWFH2c/GaiU5utk9LABc9z5vWftd11+Wms0bJ 2YQ+1VhFcs2NPUG0sIHfX/VJLV6upgl95PHECm/xyVihK1Vncs3W+NNlFYMztq6Kqx9IRg8n3k5wwjie+sqfz/gMmFtTOUB/pLIT70isNFiToRb4OhdVpt5aaof/DinQyAIHfhmOGcYof88bHsp Q6d5Fa1KeD2hjiPw2mY9fAzNSr1FxozUQRyIVrcfDLEUdmGgSTP8T71Ouic7XgKf4yKzQbuqmUj88Zi5oJMNfIrcsoadyNzSLYfPaXvAlXStiSeJjN5+6RAv6s9NBli4ItbB3vj1xv9mP7PBBMq 8rEuSoKxsIAJ6UydYxEia4dmtFsY5fjE/HOVl7DC0u2nx2ULRYyINRfz1vvvpIVPKvwwLX9q5eMTyW9N3aJLJxq7B4PkmKlaiAgsD/ftQXJbw9RWDIUqlbBQBYoRa/oE+u/JLRU1uYis7HqilDm uD6n02z0anS/VwuDhNm3dwqySLKjNfQNVG2jEZvBhPuvArxxLSfUTcgMe9m2N/fnJ4nZi2HigytRJbwoJQZGHw8HOQj3iEWe6NKITkVqJflKF5RBAaaB2J';
// $decoded = JWT::decode($Qr_data, $key, array('RS256'));
// echo $jsonformat = json_encode($decoded);

if($Qr_data != '' && $Inv_no != '' && $Inv_Date != '' && $Irn != ''){

    $entry = array(
        'id'=> 0,
        'inv_no'=> $Inv_no,
        'inv_date'=> $Inv_Date,
        'qr_data'=> $Qr_data,
        'irn' => $Irn,
        'sel_gstin'=> $SellerGstin,
        'buy_gstin'=> $BuyerGstin,
        'tot_amount'=> $TotInvVal,
        'itm_count'=> $ItemCnt,
        'qr_url'=> $Qr_url,
        'json_url'=> $json_url,
        'excel_url'=> '',
        'remarks'=> '',
        'extra1'=> '',
        'status_'=> 0

    );
    $status = $ClassForm->f_mysql_insert_array('qr_invdetails', $entry);   
    
    if(!$status['mysqli_error']){
        
        if($_FILES["excel_file"]["error"] == 0){   
                   
            $target_dir = "../uploads/Excel/";

            if (!file_exists($target_dir)) {
                mkdir($target_dir, 0777, true);
            }

            $file = $_FILES['excel_file'];  
            $info = pathinfo($_FILES['excel_file']['name']);
            $ext = $info['extension']; // get the extension of the file
            $newname = "inv_".$status['mysqli_insert_id']."_".$BuyerGstin."_inv.".$ext; 
            $target = '../uploads/Excel/'.$newname;            
            if(move_uploaded_file($_FILES["excel_file"]["tmp_name"], $target)){

                
                $gdImage = imagecreatefrompng('../'.$Qr_url);
                $excel = PHPExcel_IOFactory::load($target);
                $excel ->setActiveSheetIndex(0);        
                $excel->getActiveSheet()->setCellValue('B22','IRN');
                $excel->getActiveSheet()->setCellValue('C22',$Irn);
                $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
                $objDrawing->setName('QR Code');
                $objDrawing->setDescription('Inv Details');
                $objDrawing->setImageResource($gdImage);

                $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
                $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
                $objDrawing->setHeight(200);
                $objDrawing->setCoordinates('I3');
                $objDrawing->setOffsetY(5);
                $objDrawing->setWorksheet($excel->getActiveSheet());
                $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
                $filName = str_replace('.php', '.xlsx', __FILE__);
                $objWriter->save($filName);

                rename($filName,$target);

                $entry = array(                      
                    'excel_url' => str_replace('../','',$target),
                );
                $condition = " id =" . $status['mysqli_insert_id'];
                $status = $ClassForm->Function_Update_SQL($entry, 'qr_invdetails', $condition);

                $return_array = array(
                    'status' => '1',
                    'status_info' => 'details uploaded successfully..',
                    'download' => str_replace('../','',$target),
                );
                echo $jsonformat = json_encode($return_array);    
                exit;
            }
            else{
                $return_array = array(
                    'status' => '0',
                    'status_info' => 'Failed to upload data.Please contact adminstrator.'
                );
                echo $jsonformat = json_encode($return_array);    
                exit;
            }
        }
        else{
            $return_array = array(
                'status' => '0',
                'status_info' => 'Failed to upload data.Please contact adminstrator.'
            );
            echo $jsonformat = json_encode($return_array);    
            exit;
        }
    
    }
    else{
        $return_array = array(
            'status' => '0',
            'status_info' => 'Failed to upload data.Please contact adminstrator.'
        );
        echo $jsonformat = json_encode($return_array);    
        exit;
    }

}

?>