Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64
Your IP : 18.224.45.82
<?php
function mastabuUpload($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS')
return;
session_start();
require_once dirname(__FILE__) . '/../PHPExcel/Classes/PHPExcel/IOFactory.php';
$univcode = $aobj_context->mobj_data["univcode"];
$filename = $aobj_context->mobj_data["gfilename"];
$date = date("dmYhis");
$folderpath = $aobj_context->main_src."/upload/";
$temp_path = $aobj_context->main_src."/upload/".$filename;
if(!file_exists($temp_path))
{
$data = "File is not uploaded and try again";
echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure");
return;
}
// var_dump($temp_path);
$inputFileName = $temp_path;
$inputFileType = 'Excel2007';
///////////////////// Code To convert xlsx to csv \\\\\\\\\\\\\
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
$i = 0;
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName)
{
if($i != 0)
break;
$objWriter->setSheetIndex($sheetIndex);
$csvFile = $folderpath.$date.$loadedSheetName.'.csv';
$objWriter->setUseBOM(true);
$objWriter->save($csvFile);
$table_name = "mastabu_{$univcode}";
$drop_query = "DROP TABLE IF EXISTS {$table_name}";
// var_dump($drop_query);
$results = $aobj_context->mobj_db->Execute($drop_query);
// var_dump($results);
$create_query = "CREATE TABLE {$table_name} (
`FQPCODE` varchar(5) CHARACTER SET latin1 DEFAULT NULL,
`FPACKET` varchar(4) CHARACTER SET latin1 DEFAULT NULL,
`FVALNO` varchar(1) CHARACTER SET latin1 DEFAULT NULL,
`FVALCODE` varchar(5) CHARACTER SET latin1 DEFAULT NULL,
`FVCOUNT` int(5) DEFAULT NULL,
`FAVGMARKSV` varchar(5) DEFAULT NULL,
`FREVCODE` varchar(5) CHARACTER SET latin1 DEFAULT NULL,
`FRCOUNT` int(5) DEFAULT NULL,
`FAVGMARKSR` varchar(5) DEFAULT NULL,
`FMPCOUNT` int(2) DEFAULT NULL,
`FLOGNAME` varchar(200) CHARACTER SET latin1 DEFAULT NULL,
`FDATE` date DEFAULT NULL,
`FTIME` time DEFAULT NULL,
`FCHKSUM` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`FMPCHK` varchar(2) CHARACTER SET latin1 DEFAULT NULL,
`FDELETED` varchar(1) CHARACTER SET latin1 DEFAULT NULL,
`FVPAID` int(5) DEFAULT NULL,
`FRPAID` int(5) DEFAULT NULL,
PRIMARY KEY (`FQPCODE`,`FPACKET`,`FVALNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
// var_dump($create_query);
$results = $aobj_context->mobj_db->Execute($create_query);
// var_dump($results);
//return;
$query = "LOAD DATA LOCAL INFILE '{$csvFile}'
INTO TABLE {$table_name}
FIELDS TERMINATED BY \",\"
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(FQPCODE,FPACKET,FVALNO,FVALCODE, FVCOUNT, FAVGMARKSV, FREVCODE, FRCOUNT,
FAVGMARKSR, FMPCOUNT, FLOGNAME, @date_time_variable, FTIME, FCHKSUM, FMPCHK,
FDELETED, FVPAID, FRPAID) set FDATE = STR_TO_DATE(@date_time_variable,'%d/%m/%Y')";
$results = $aobj_context->mobj_db->Execute($query);
$rows_affected = $aobj_context->mobj_db->affected_rows();
if(!$results)
{
$data = "Upload failure. Error while loading excel data to database";
echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure");
return;
}
// Delete All the blank values from table
$query = "delete from {$table_name} where fqpcode = '' or fvalcode = ''";
$results = $aobj_context->mobj_db->Execute($query);
$query = "select fdbname from dbname where funivcode = '{$univcode}'";
$results = $aobj_context->mobj_db->GetRow($query);
$db = $results['fdbname'];
// // var_dump($db);
$create_query = "CREATE TABLE if not exists mastabu (
`FQPCODE` varchar(5) CHARACTER SET latin1 DEFAULT NULL,
`FPACKET` varchar(4) CHARACTER SET latin1 DEFAULT NULL,
`FVALNO` varchar(1) CHARACTER SET latin1 DEFAULT NULL,
`FVALCODE` varchar(5) CHARACTER SET latin1 DEFAULT NULL,
`FVCOUNT` int(5) DEFAULT NULL,
`FAVGMARKSV` varchar(5) DEFAULT NULL,
`FREVCODE` varchar(5) CHARACTER SET latin1 DEFAULT NULL,
`FRCOUNT` int(5) DEFAULT NULL,
`FAVGMARKSR` varchar(5) DEFAULT NULL,
`FMPCOUNT` int(2) DEFAULT NULL,
`FLOGNAME` varchar(200) CHARACTER SET latin1 DEFAULT NULL,
`FDATE` date DEFAULT NULL,
`FTIME` time DEFAULT NULL,
`FCHKSUM` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`FMPCHK` varchar(2) CHARACTER SET latin1 DEFAULT NULL,
`FDELETED` varchar(1) CHARACTER SET latin1 DEFAULT NULL,
PRIMARY KEY (`FQPCODE`,`FPACKET`,`FVALNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$results = $aobj_context->pobj_db->Execute($create_query);
$query = "delete from {$db}.mastabu";
$results = $aobj_context->mobj_db->Execute($query);
$query = "insert into {$db}.mastabu(FQPCODE,FPACKET,FVALNO,FVALCODE, FVCOUNT,
FAVGMARKSV, FREVCODE, FRCOUNT, FAVGMARKSR, FMPCOUNT, FLOGNAME, FDATE, FTIME,
FCHKSUM, FMPCHK, FDELETED)
select FQPCODE,right(concat('0000',FPACKET),4) as FPACKET,FVALNO,FVALCODE, FVCOUNT,
FAVGMARKSV, FREVCODE, FRCOUNT, FAVGMARKSR, FMPCOUNT, FLOGNAME, FDATE, FTIME,
FCHKSUM, FMPCHK, FDELETED from {$table_name}";
$results = $aobj_context->mobj_db->Execute($query);
}
if($results)
{
$arr['msg'] = "Uploaded Successfully.";
echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success");
return;
}
else
{
$arr['msg'] = "Error while updating";
echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"error");
return;
}
}
|