<?php

require('../_app/Config.inc.php');

if (!empty($_FILES['arquivo']['tmp_name'])) {
    $arquivo = new DomDocument();
    $arquivo->load($_FILES['arquivo']['tmp_name']);
    //var_dump($arquivo);

    $linhas = $arquivo->getElementsByTagName("Row");
    //var_dump($linhas);

    $primeira_linha = true;

    foreach ($linhas as $linha) {
        if ($primeira_linha == false) {
            $Dados['Market_and_Exchange_Names'] = $linha->getElementsByTagName("Data")->item(0)->nodeValue;
            $Dados['As_of_Date_In_Form_YYMMDD'] = $linha->getElementsByTagName("Data")->item(1)->nodeValue;
            $Dados['Report_Date_as_MM_DD_YYYY'] = $linha->getElementsByTagName("Data")->item(2)->nodeValue;
            $Dados['CFTC_Contract_Market_Code'] = $linha->getElementsByTagName("Data")->item(3)->nodeValue;
            $Dados['CFTC_Market_Code'] = $linha->getElementsByTagName("Data")->item(4)->nodeValue;
            $Dados['CFTC_Region_Code'] = $linha->getElementsByTagName("Data")->item(5)->nodeValue;
            $Dados['CFTC_Commodity_Code'] = $linha->getElementsByTagName("Data")->item(6)->nodeValue;
            $Dados['Open_Interest_All'] = $linha->getElementsByTagName("Data")->item(7)->nodeValue;
            $Dados['NonComm_Positions_Long_All'] = $linha->getElementsByTagName("Data")->item(8)->nodeValue;
            $Dados['NonComm_Positions_Short_All'] = $linha->getElementsByTagName("Data")->item(9)->nodeValue;
            $Dados['NonComm_Postions_Spread_All'] = $linha->getElementsByTagName("Data")->item(10)->nodeValue;
            $Dados['Comm_Positions_Long_All'] = $linha->getElementsByTagName("Data")->item(11)->nodeValue;
            $Dados['Comm_Positions_Short_All'] = $linha->getElementsByTagName("Data")->item(12)->nodeValue;
            $Dados['Tot_Rept_Positions_Long_All'] = $linha->getElementsByTagName("Data")->item(13)->nodeValue;
            $Dados['Tot_Rept_Positions_Short_All'] = $linha->getElementsByTagName("Data")->item(14)->nodeValue;
            $Dados['NonRept_Positions_Long_All'] = $linha->getElementsByTagName("Data")->item(15)->nodeValue;
            $Dados['NonRept_Positions_Short_All'] = $linha->getElementsByTagName("Data")->item(16)->nodeValue;
            $Dados['Open_Interest_Old'] = $linha->getElementsByTagName("Data")->item(17)->nodeValue;
            $Dados['NonComm_Positions_Long_Old'] = $linha->getElementsByTagName("Data")->item(18)->nodeValue;
            $Dados['NonComm_Positions_Short_Old'] = $linha->getElementsByTagName("Data")->item(19)->nodeValue;
            $Dados['NonComm_Positions_Spread_Old'] = $linha->getElementsByTagName("Data")->item(20)->nodeValue;
            $Dados['Comm_Positions_Long_Old'] = $linha->getElementsByTagName("Data")->item(21)->nodeValue;
            $Dados['Comm_Positions_Short_Old'] = $linha->getElementsByTagName("Data")->item(22)->nodeValue;
            $Dados['Tot_Rept_Positions_Long_Old'] = $linha->getElementsByTagName("Data")->item(23)->nodeValue;
            $Dados['Tot_Rept_Positions_Short_Old'] = $linha->getElementsByTagName("Data")->item(24)->nodeValue;
            $Dados['NonRept_Positions_Long_Old'] = $linha->getElementsByTagName("Data")->item(25)->nodeValue;
            $Dados['NonRept_Positions_Short_Old'] = $linha->getElementsByTagName("Data")->item(26)->nodeValue;
            $Dados['Open_Interest_Other'] = $linha->getElementsByTagName("Data")->item(27)->nodeValue;
            $Dados['NonComm_Positions_Long_Other'] = $linha->getElementsByTagName("Data")->item(28)->nodeValue;
            $Dados['NonComm_Positions_Short_Other'] = $linha->getElementsByTagName("Data")->item(29)->nodeValue;
            $Dados['NonComm_Positions_Spread_Other'] = $linha->getElementsByTagName("Data")->item(30)->nodeValue;
            $Dados['Comm_Positions_Long_Other'] = $linha->getElementsByTagName("Data")->item(31)->nodeValue;
            $Dados['Comm_Positions_Short_Other'] = $linha->getElementsByTagName("Data")->item(32)->nodeValue;
            $Dados['Tot_Rept_Positions_Long_Other'] = $linha->getElementsByTagName("Data")->item(33)->nodeValue;
            $Dados['Tot_Rept_Positions_Short_Other'] = $linha->getElementsByTagName("Data")->item(34)->nodeValue;
            $Dados['NonRept_Positions_Long_Other'] = $linha->getElementsByTagName("Data")->item(35)->nodeValue;
            $Dados['NonRept_Positions_Short_Other'] = $linha->getElementsByTagName("Data")->item(36)->nodeValue;
            $Dados['Change_in_Open_Interest_All'] = $linha->getElementsByTagName("Data")->item(37)->nodeValue;
            $Dados['Change_in_NonComm_Long_All'] = $linha->getElementsByTagName("Data")->item(38)->nodeValue;
            $Dados['Change_in_NonComm_Short_All'] = $linha->getElementsByTagName("Data")->item(39)->nodeValue;
            $Dados['Change_in_NonComm_Spead_All'] = $linha->getElementsByTagName("Data")->item(40)->nodeValue;
            $Dados['Change_in_Comm_Long_All'] = $linha->getElementsByTagName("Data")->item(41)->nodeValue;
            $Dados['Change_in_Comm_Short_All'] = $linha->getElementsByTagName("Data")->item(42)->nodeValue;
            $Dados['Change_in_Tot_Rept_Long_All'] = $linha->getElementsByTagName("Data")->item(43)->nodeValue;
            $Dados['Change_in_Tot_Rept_Short_All'] = $linha->getElementsByTagName("Data")->item(44)->nodeValue;
            $Dados['Change_in_NonRept_Long_All'] = $linha->getElementsByTagName("Data")->item(45)->nodeValue;
            $Dados['Change_in_NonRept_Short_All'] = $linha->getElementsByTagName("Data")->item(46)->nodeValue;
            $Dados['Pct_of_Open_Interest_All'] = $linha->getElementsByTagName("Data")->item(47)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Long_All'] = $linha->getElementsByTagName("Data")->item(48)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Short_All'] = $linha->getElementsByTagName("Data")->item(49)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Spread_All'] = $linha->getElementsByTagName("Data")->item(50)->nodeValue;
            $Dados['Pct_of_OI_Comm_Long_All'] = $linha->getElementsByTagName("Data")->item(51)->nodeValue;
            $Dados['Pct_of_OI_Comm_Short_All'] = $linha->getElementsByTagName("Data")->item(52)->nodeValue;
            $Dados['Pct_of_OI_Tot_Rept_Long_All'] = $linha->getElementsByTagName("Data")->item(53)->nodeValue;
            $Dados['Pct_of_OI_Tot_Rept_Short_All'] = $linha->getElementsByTagName("Data")->item(54)->nodeValue;
            $Dados['Pct_of_OI_NonRept_Long_All'] = $linha->getElementsByTagName("Data")->item(55)->nodeValue;
            $Dados['Pct_of_OI_NonRept_Short_All'] = $linha->getElementsByTagName("Data")->item(56)->nodeValue;
            $Dados['Pct_of_Open_Interest_Old'] = $linha->getElementsByTagName("Data")->item(57)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Long_Old'] = $linha->getElementsByTagName("Data")->item(58)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Short_Old'] = $linha->getElementsByTagName("Data")->item(59)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Spread_Old'] = $linha->getElementsByTagName("Data")->item(60)->nodeValue;
            $Dados['Pct_of_OI_Comm_Long_Old'] = $linha->getElementsByTagName("Data")->item(61)->nodeValue;
            $Dados['Pct_of_OI_Comm_Short_Old'] = $linha->getElementsByTagName("Data")->item(62)->nodeValue;
            $Dados['Pct_of_OI_Tot_Rept_Long_Old'] = $linha->getElementsByTagName("Data")->item(63)->nodeValue;
            $Dados['Pct_of_OI_Tot_Rept_Short_Old'] = $linha->getElementsByTagName("Data")->item(64)->nodeValue;
            $Dados['Pct_of_OI_NonRept_Long_Old'] = $linha->getElementsByTagName("Data")->item(65)->nodeValue;
            $Dados['Pct_of_OI_NonRept_Short_Old'] = $linha->getElementsByTagName("Data")->item(66)->nodeValue;
            $Dados['Pct_of_Open_Interest_Other'] = $linha->getElementsByTagName("Data")->item(67)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Long_Other'] = $linha->getElementsByTagName("Data")->item(68)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Short_Other'] = $linha->getElementsByTagName("Data")->item(69)->nodeValue;
            $Dados['Pct_of_OI_NonComm_Spread_Other'] = $linha->getElementsByTagName("Data")->item(70)->nodeValue;
            $Dados['Pct_of_OI_Comm_Long_Other'] = $linha->getElementsByTagName("Data")->item(71)->nodeValue;
            $Dados['Pct_of_OI_Comm_Short_Other'] = $linha->getElementsByTagName("Data")->item(72)->nodeValue;
            $Dados['Pct_of_OI_Tot_Rept_Long_Other'] = $linha->getElementsByTagName("Data")->item(73)->nodeValue;
            $Dados['Pct_of_OI_Tot_Rept_Short_Other'] = $linha->getElementsByTagName("Data")->item(74)->nodeValue;
            $Dados['Pct_of_OI_NonRept_Long_Other'] = $linha->getElementsByTagName("Data")->item(75)->nodeValue;
            $Dados['Pct_of_OI_NonRept_Short_Other'] = $linha->getElementsByTagName("Data")->item(76)->nodeValue;
            $Dados['Traders_Tot_All'] = $linha->getElementsByTagName("Data")->item(77)->nodeValue;
            $Dados['Traders_NonComm_Long_All'] = $linha->getElementsByTagName("Data")->item(78)->nodeValue;
            $Dados['Traders_NonComm_Short_All'] = $linha->getElementsByTagName("Data")->item(79)->nodeValue;
            $Dados['Traders_NonComm_Spread_All'] = $linha->getElementsByTagName("Data")->item(80)->nodeValue;
            $Dados['Traders_Comm_Long_All'] = $linha->getElementsByTagName("Data")->item(81)->nodeValue;
            $Dados['Traders_Comm_Short_All'] = $linha->getElementsByTagName("Data")->item(82)->nodeValue;
            $Dados['Traders_Tot_Rept_Long_All'] = $linha->getElementsByTagName("Data")->item(83)->nodeValue;
            $Dados['Traders_Tot_Rept_Short_All'] = $linha->getElementsByTagName("Data")->item(84)->nodeValue;
            $Dados['Traders_Tot_Old'] = $linha->getElementsByTagName("Data")->item(85)->nodeValue;
            $Dados['Traders_NonComm_Long_Old'] = $linha->getElementsByTagName("Data")->item(86)->nodeValue;
            $Dados['Traders_NonComm_Short_Old'] = $linha->getElementsByTagName("Data")->item(87)->nodeValue;
            $Dados['Traders_NonComm_Spead_Old'] = $linha->getElementsByTagName("Data")->item(88)->nodeValue;
            $Dados['Traders_Comm_Long_Old'] = $linha->getElementsByTagName("Data")->item(89)->nodeValue;
            $Dados['Traders_Comm_Short_Old'] = $linha->getElementsByTagName("Data")->item(90)->nodeValue;
            $Dados['Traders_Tot_Rept_Long_Old'] = $linha->getElementsByTagName("Data")->item(91)->nodeValue;
            $Dados['Traders_Tot_Rept_Short_Old'] = $linha->getElementsByTagName("Data")->item(92)->nodeValue;
            $Dados['Traders_Tot_Other'] = $linha->getElementsByTagName("Data")->item(93)->nodeValue;
            $Dados['Traders_NonComm_Long_Other'] = $linha->getElementsByTagName("Data")->item(94)->nodeValue;
            $Dados['Traders_NonComm_Short_Other'] = $linha->getElementsByTagName("Data")->item(95)->nodeValue;
            $Dados['Traders_NonComm_Spread_Other'] = $linha->getElementsByTagName("Data")->item(96)->nodeValue;
            $Dados['Traders_Comm_Long_Other'] = $linha->getElementsByTagName("Data")->item(97)->nodeValue;
            $Dados['Traders_Comm_Short_Other'] = $linha->getElementsByTagName("Data")->item(98)->nodeValue;
            $Dados['Traders_Tot_Rept_Long_Other'] = $linha->getElementsByTagName("Data")->item(99)->nodeValue;
            $Dados['Traders_Tot_Rept_Short_Other'] = $linha->getElementsByTagName("Data")->item(100)->nodeValue;
            $Dados['Conc_Gross_LE_4_TDR_Long_All'] = $linha->getElementsByTagName("Data")->item(101)->nodeValue;
            $Dados['Conc_Gross_LE_4_TDR_Short_All'] = $linha->getElementsByTagName("Data")->item(102)->nodeValue;
            $Dados['Conc_Gross_LE_8_TDR_Long_All'] = $linha->getElementsByTagName("Data")->item(103)->nodeValue;
            $Dados['Conc_Gross_LE_8_TDR_Short_All'] = $linha->getElementsByTagName("Data")->item(104)->nodeValue;
            $Dados['Conc_Net_LE_4_TDR_Long_All'] = $linha->getElementsByTagName("Data")->item(105)->nodeValue;
            $Dados['Conc_Net_LE_4_TDR_Short_All'] = $linha->getElementsByTagName("Data")->item(106)->nodeValue;
            $Dados['Conc_Net_LE_8_TDR_Long_All'] = $linha->getElementsByTagName("Data")->item(107)->nodeValue;
            $Dados['Conc_Net_LE_8_TDR_Short_All'] = $linha->getElementsByTagName("Data")->item(108)->nodeValue;
            $Dados['Conc_Gross_LE_4_TDR_Long_Old'] = $linha->getElementsByTagName("Data")->item(109)->nodeValue;
            $Dados['Conc_Gross_LE_4_TDR_Short_Old'] = $linha->getElementsByTagName("Data")->item(110)->nodeValue;
            $Dados['Conc_Gross_LE_8_TDR_Long_Old'] = $linha->getElementsByTagName("Data")->item(111)->nodeValue;
            $Dados['Conc_Gross_LE_8_TDR_Short_Old'] = $linha->getElementsByTagName("Data")->item(112)->nodeValue;
            $Dados['Conc_Net_LE_4_TDR_Long_Old'] = $linha->getElementsByTagName("Data")->item(113)->nodeValue;
            $Dados['Conc_Net_LE_4_TDR_Short_Old'] = $linha->getElementsByTagName("Data")->item(114)->nodeValue;
            $Dados['Conc_Net_LE_8_TDR_Long_Old'] = $linha->getElementsByTagName("Data")->item(115)->nodeValue;
//            $Dados['Conc_Net_LE_8_TDR_Short_Old'] = $linha->getElementsByTagName("Data")->item(116)->nodeValue;
//            $Dados['Conc_Gross_LE_4_TDR_Long_Other'] = $linha->getElementsByTagName("Data")->item(117)->nodeValue;
//            $Dados['Conc_Gross_LE_4_TDR_Short_Other'] = $linha->getElementsByTagName("Data")->item(118)->nodeValue;
//            $Dados['Conc_Gross_LE_8_TDR_Long_Other'] = $linha->getElementsByTagName("Data")->item(119)->nodeValue;
//            $Dados['Conc_Gross_LE_8_TDR_Short_Other'] = $linha->getElementsByTagName("Data")->item(120)->nodeValue;
//            $Dados['Conc_Net_LE_4_TDR_Long_Other'] = $linha->getElementsByTagName("Data")->item(121)->nodeValue;
//            $Dados['Conc_Net_LE_4_TDR_Short_Other'] = $linha->getElementsByTagName("Data")->item(122)->nodeValue;
//            $Dados['Conc_Net_LE_8_TDR_Long_Other'] = $linha->getElementsByTagName("Data")->item(123)->nodeValue;
//            $Dados['Conc_Net_LE_8_TDR_Short_Other'] = $linha->getElementsByTagName("Data")->item(124)->nodeValue;
//            $Dados['Contract_Units'] = $linha->getElementsByTagName("Data")->item(125)->nodeValue;
            //converter a data

            $data = $Dados['As_of_Date_In_Form_YYMMDD'];
            $YY = substr($data, 0, 2);
            $MM = substr($data, 2, 2);
            $DD = substr($data, 4, 2);
            $Dados['indice_data'] = "20{$YY}-{$MM}-{$DD}";
            $data_indice = date("Y-m-d", strtotime($Dados['indice_data']));

//            echo $data_indice;
//            echo "<hr>";
            if ($Dados['indice_data'] > "2018-10-01"):
                echo " insert {$data_indice}";
                echo "<hr>";
                $Create = new Create;
                $Create->ExeCreate("tp_indice", $Dados);
            endif;
        }
        $primeira_linha = false;
    }
}