<?php

echo "<br> Script Starts At : ". date("Y-m-d H:i:s");
//1 - Authenticate with TechNet. The authentication ticket will be stored in $auth_ticket. Note this MUST be HTTPS.
$auth_url = "https://technet.rapaport.com/HTTP/Authenticate.aspx";
$post_string = "username=88047&password=" . urlencode("Colby&Helix");


//create HTTP POST request with curl:
$request = curl_init($auth_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$auth_ticket = curl_exec($request); // execute curl post and store results in $auth_ticket
curl_close ($request);


//2 - prepare HTTP request for data. Copy the URL from the RapLink Feed page in RapNet.com: 
// go to: http://www.rapnet.com/RapNet/DownloadListings/Download.aspx, choose your parameters, and click
// generate code. Make sure to specify the columns wanted. This can produce a very long URL.
// $feed_url = "http://technet.rapaport.com/HTTP/RapLink/download.aspx?LabIDs=1&SortBy=Owner&White=1&Fancy=1&Programmatically=yes&Version=1.0";

$feed_url = "http://technet.rapaport.com/HTTP/RapLink/download.aspx?SortBy=Owner&White=1&Fancy=1&Programmatically=yes&Version=1.0&UseCheckedCulommns=1&cCT=1&cCERT=1&cCLAR=1&cCOLR=1&cCRTCM=1&cCountry=1&cCITY=1&cCROWN=1&cCulet=1&cCuletSize=1&cCuletCondition=1&cCUT=1&cDPTH=1&cFancyColor=1&cFLR=1&cGIRDLE=1&cGirdleMin=1&cGirdleMax=1&cFancyColorIntensity=1&cLOTNN=1&cMEAS=1&cMeasLength=1&cMeasWidth=1&cMeasDepth=1&cFancyColorOvertone=1&cPAVILION=1&cPOL=1&cPX=1&cDPX=1&cTPr=1&cCashTot=1&cRapSpec=1&cRatio=1&cCashDisc=1&cCash=1&cOWNER=1&cAct=1&cNC=1&cSHP=1&cSTATE=1&cSTOCK_NO=1&cSYM=1&cTBL=1&cSTONES=1&cCertificateImage=1&cCertID=1&cAvailability=1&cStockNumberPairs=1&cMactchingPairSeparable=1&cFluorColor=1&cFluorIntensity=1&cTreatment=1&cDateUpdated=1";

$feed_url .= "&ticket=".$auth_ticket; //add authentication ticket:

//prepare to save response as file.
$fp = fopen('import/rapnetfeed.csv', 'wb');
if ($fp == FALSE)
{
echo "File not opened";
exit;
}

//create HTTP GET request with curl 
$request = curl_init($feed_url); // initiate curl object
curl_setopt($request, CURLOPT_FILE, $fp); //Ask cURL to write the contents to a file
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_TIMEOUT, 300); //set timeout to 5 mins
curl_exec($request); // execute curl post
// additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object 
fclose($fp); //close file;

echo "<br> Download Completes At : ". date("Y-m-d H:i:s");


$con=mysqli_connect("localhost","silver_tresor","admin.123","silver_tresorjewellery");
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"START TRANSACTION");
mysqli_query($con,"TRUNCATE TABLE `rapnet`");
mysqli_query($con,"TRUNCATE TABLE `rapnet_tbl`");
 
mysqli_query($con,"LOAD DATA LOCAL  INFILE '/home/silver/public_html/tresorjewellery/rapnet/import/rapnetfeed.csv' replace INTO TABLE `rapnet` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\'' LINES TERMINATED BY '\\r\\n' IGNORE 1 LINES ");

mysqli_query($con,"insert into rapnet_tbl select *,0,0 FROM `rapnet`");

mysqli_query($con,"COMMIT");

mysqli_query($con,"UPDATE rapnet_tbl  SET clarity =  REPLACE(clarity, '+', '') ");
mysqli_query($con,"UPDATE rapnet_tbl  SET color =  REPLACE(color, '+', '') ");
mysqli_query($con,"UPDATE rapnet_tbl  SET shape =  upper(shape) ");
mysqli_query($con,"UPDATE rapnet_tbl  SET shape =  'EMERALD/OCTAGONAL' where shape='EMERALD' ");
mysqli_query($con,"UPDATE rapnet_tbl  SET shape =  'EMERALD/OCTAGONAL' where shape='OCTAGONAL' ");
mysqli_query($con,"UPDATE rapnet_tbl  SET shape =  'PRINCESS/SQUARE' where shape='PRINCESS' ");
mysqli_query($con,"UPDATE rapnet_tbl  SET shape =  'PRINCESS/SQUARE' where shape='SQUARE' ");

mysqli_query($con,"UPDATE rapnet_tbl  SET color =  REPLACE(color, '+', '') ");
mysqli_query($con,"COMMIT");

mysqli_query($con,"UPDATE rapnet_tbl a, colour b SET a.colour_id = b.colour_id WHERE a.color = b.colour_code");
mysqli_query($con,"COMMIT");

mysqli_query($con,"UPDATE rapnet_tbl a, clarity b SET a.clarity_id = b.clarity_id WHERE a.clarity = b.clarity_code");
mysqli_query($con,"COMMIT");


mysqli_query($con,"UPDATE rapnet_tbl SET rapnet_final_price = greatest (rapnet_total_price, cash_total_price)");
mysqli_query($con,"COMMIT");

mysqli_close($con);
echo "<br> Databaes Load Completes At : ". date("Y-m-d H:i:s");


?>

