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

if($_FILES["file"]["error"] == 0){
    $file = $_FILES['file'];  
    $info = pathinfo($_FILES['file']['name']);
    $ext = $info['extension']; 
    $target = 'temp_excel.'.$ext;

    $currentFile = "pages/excelImport/".$target;
    if(move_uploaded_file($_FILES['file']['tmp_name'],$target)){

        $excel = PHPExcel_IOFactory::load($target);
        $excel ->setActiveSheetIndex(1); 
        $i = 2; 
        $total_BilledTo = 0;
        $total_product = 0;  
        
        while($excel->getActiveSheet()->getCell('B'.$i)->getValue() != ""){        
            $entry = array(
                'id' => 0,
                'inv_id' => $excel->getActiveSheet()->getCell('B'.$i)->getValue(),
                'biller_Name' => $excel->getActiveSheet()->getCell('C'.$i)->getValue(),
                'biller_Addr1' => $excel->getActiveSheet()->getCell('D'.$i)->getValue(),
                'biller_Addr2' => $excel->getActiveSheet()->getCell('E'.$i)->getValue(),
                'biller_Addr3' => $excel->getActiveSheet()->getCell('F'.$i)->getValue(),
                'biller_Addr4' => $excel->getActiveSheet()->getCell('G'.$i)->getValue(),
                'biller_GSTIN' => $excel->getActiveSheet()->getCell('H'.$i)->getValue(),
                'biller_PAN' => $excel->getActiveSheet()->getCell('I'.$i)->getValue(),
                'biller_Email' => '',
                'biller_Mobile' => '',    
                'extra' => '', 
                'status_' => 1
            );
            
            $status = $ClassForm->f_mysql_insert_array('biller_details', $entry);            
            $i++;        
        }

        $excel ->setActiveSheetIndex(0); 
        $i = 2; 
        while($excel->getActiveSheet()->getCell('B'.$i)->getValue() != ""){
            $query = "select * from biller_details where inv_id = '".$excel->getActiveSheet()->getCell('B'.$i)->getValue()."'";
            $req = $ClassForm->Fetch_Result_Array($query);
            print_r($req);
            $Inv_entry = array(
                'id' => 0,
                'inv_no' => $excel->getActiveSheet()->getCell('B'.$i)->getValue(),
                'inv_date' => $excel->getActiveSheet()->getCell('C'.$i)->getValue(),
                'inv_pos' => $excel->getActiveSheet()->getCell('D'.$i)->getValue(),
                'inv_sc' => $excel->getActiveSheet()->getCell('E'.$i)->getValue(),
                'inv_poref' => $excel->getActiveSheet()->getCell('F'.$i)->getValue(),
                'inv_irn' => $excel->getActiveSheet()->getCell('G'.$i)->getValue(),
                'inv_qrLoc' => '',
                'inv_reverseCharge' => $excel->getActiveSheet()->getCell('I'.$i)->getValue(),
                'billed_to' => $req['id'],
                'tax_id' => 0,    
                'template_id' => 1, 
                'inv_signedDetails' => 0,    
                'extra' => '', 
                'status_' => 1
            );           
            $inv_status = $ClassForm->f_mysql_insert_array('domestic_invS', $Inv_entry);
            $i++;
        }
        
        $excel ->setActiveSheetIndex(2); 
        $i = 2; 
        $k = 0;
        while($excel->getActiveSheet()->getCell('B'.$i)->getValue() != ""){
            $k++;
            $query = "select * from domestic_invs where inv_no = '".$excel->getActiveSheet()->getCell('B'.$i)->getValue()."'";
            $req = $ClassForm->Fetch_Result_Array($query);
            $pro_entry = array(
                'id' => 0,
                'inv_id' => $req['id'],
                'slno' => $k,
                'particulars' => $excel->getActiveSheet()->getCell('C'.$i)->getValue(),
                's_code' => $excel->getActiveSheet()->getCell('D'.$i)->getValue(),
                'amount' => $excel->getActiveSheet()->getCell('E'.$i)->getValue(),
                'extra' => $excel->getActiveSheet()->getCell('B'.$i)->getValue(),                 
                'status_' => 1
            );            
            $pro_status = $ClassForm->f_mysql_insert_array('domestic_Service', $pro_entry);
            $i++;
        }

        $excel ->setActiveSheetIndex(3); 
        $i = 2; 
        $k = 0;
        while($excel->getActiveSheet()->getCell('B'.$i)->getValue() != ""){
            $query = "select * from domestic_invs where inv_no = '".$excel->getActiveSheet()->getCell('B'.$i)->getValue()."'";
            $req = $ClassForm->Fetch_Result_Array($query);
            $tax_entry = array(
                'id' => 0,
                'inv_id' => $req['id'],
                'taxable_amt' => $excel->getActiveSheet()->getCell('C'.$i)->getValue(),
                'cgst_per' => $excel->getActiveSheet()->getCell('D'.$i)->getValue(),
                'sgst_per' => $excel->getActiveSheet()->getCell('E'.$i)->getValue(),
                'igst_per' => $excel->getActiveSheet()->getCell('F'.$i)->getValue(),
                'cgst_val' => $excel->getActiveSheet()->getCell('G'.$i)->getValue(),
                'sgst_val' => $excel->getActiveSheet()->getCell('H'.$i)->getValue(),
                'igst_val' => $excel->getActiveSheet()->getCell('I'.$i)->getValue(),
                'roindOff' => $excel->getActiveSheet()->getCell('J'.$i)->getValue(),
                'total' => $excel->getActiveSheet()->getCell('K'.$i)->getValue(),    
                'extra' => '',             
                'status_' => 1
            );   
            print_r($tax_entry)         ;
            $tax_status = $ClassForm->f_mysql_insert_array('domestic_taxDetails', $tax_entry);
            $i++;
        }

        $result = array(
            error_code => '0',
            error_derail => 'no error found',
            success => true    
        );
        echo json_encode($result);
        
    }
    else{
        $result = array(
            error_code => '1',
            error_derail => 'Please Check Excel Import Details or Contact administraor',
            success => false    
        );
        echo json_encode($result);
    }
    
}
else{
    $result = array(
        error_code => '1',
        error_derail => 'Please Check Excel Import Details or Contact administraor',
        success => false    
    );
    echo json_encode($result);
}
?>