Guest User

Untitled

a guest
Mar 14th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.61 KB | None | 0 0
  1. <?php
  2.  
  3. include "AuthHeader.php";
  4. include "GetHighResImageRequestType.php";
  5. require_once "Constants.php";
  6. require_once "Logging.php";
  7.  
  8. function getAuthHeader() {
  9.  
  10. $username = "";
  11. $key = "";
  12.  
  13. if( TEST_MODE == true ) {
  14.  
  15. $username = USERNAME_TEST;
  16. $key = KEY_TEST;
  17.  
  18. } else {
  19.  
  20. $username = USERNAME_LIVE;
  21. $key = KEY_LIVE;
  22.  
  23. }
  24.  
  25. $rand = rand( 0,1000000 );
  26. $objDateTime = new DateTime( 'NOW', new DateTimeZone( "Europe/London" ) );
  27. $concat = $rand . $objDateTime->format( 'c' ) . $key;
  28. $pass = base64_encode( sha1( $concat ) );
  29. $auth = new AuthHeader( $username, $pass, $rand, $objDateTime->format( 'c' ) );
  30.  
  31. return $auth;
  32.  
  33. }
  34.  
  35. function getToken() {
  36.  
  37. $postData = array(
  38. 'username' => USERNAME,
  39. 'password' => PASSWORD
  40. );
  41.  
  42. $ch = curl_init( URL_TOKEN );
  43. curl_setopt_array( $ch, array(
  44. CURLOPT_POST => TRUE,
  45. CURLOPT_RETURNTRANSFER => TRUE,
  46. CURLOPT_HTTPHEADER => array(
  47. 'Content-Type: application/json'
  48. ),
  49. CURLOPT_POSTFIELDS => json_encode( $postData ),
  50. CURLOPT_SSL_VERIFYPEER => 0,
  51. CURLOPT_SSL_VERIFYHOST => 0
  52. ));
  53.  
  54. $response = curl_exec( $ch );
  55.  
  56. if( $response === FALSE ) {
  57.  
  58. die( curl_error( $ch ) );
  59.  
  60. }
  61.  
  62. $responseData = json_decode( $response, TRUE );
  63.  
  64. return $responseData;
  65.  
  66. }
  67.  
  68. function downloadImage( $sku ) {
  69.  
  70. $client = new SoapClient( URL_BEAUTYFORT_SOAP );
  71. $authHeader = getAuthHeader();
  72. $GetHighResImageRequestType = new GetHighResImageRequestType( TEST_MODE, $sku, false );
  73.  
  74. $GetHighResImageParams = array(
  75. "GetHighResImageRequestType" => $GetHighResImageRequestType
  76. );
  77.  
  78. $paramsHeader = array( "AuthHeader" => $authHeader );
  79. $header = new SoapHeader( URL_BEAUTYFORT_API, "AuthHeader", $authHeader );
  80. $client->__setSoapHeaders( $header );
  81. $GetHighResImageResponse = $client->__soapCall( "GetHighResImage", $GetHighResImageParams );
  82.  
  83. return $GetHighResImageResponse;
  84.  
  85. }
  86.  
  87. function syncProduct( $token, $prod, $array_cat, $image, $i ) {
  88.  
  89. $dateTime = ( new DateTime())->format( 'Y-m-d' );
  90. $log = new Logging();
  91. $log->lfile( dirname(__FILE__) . "/log/" . $dateTime . ".txt" );
  92.  
  93. try { $qty = 0;
  94.  
  95. if( $prod[6] != "" ) {
  96.  
  97. if( $prod[6] >= 1 ) {
  98.  
  99. $qty = $prod[6];
  100.  
  101. } else { $qty = 0; }
  102.  
  103. } else if( $prod[7] != "" ) {
  104.  
  105. if( $prod[7] >= 1 ) {
  106.  
  107. $qty = $prod[7];
  108.  
  109. } else { $qty = 0; }
  110.  
  111. }
  112.  
  113. $upc_ean = explode( ",", $prod[14] );
  114.  
  115. if( $upc_ean[0] == "" ) {
  116.  
  117. $log->lwrite( $i." no-barcode -> ".$prod[0] );
  118. $upc_ean[0] = "000000000";
  119.  
  120. return;
  121. }
  122.  
  123. $log->lwrite( $i." ok -> ".$prod[0] );
  124.  
  125. $postData = array(
  126. "product" => array(
  127. "attribute_set_id" => 9,
  128. "sku" => $prod[0],
  129. "name" => utf8_encode( $prod[2] ),
  130. "price" => $prod[15],
  131. "status" => 1,
  132. "custom_attributes" => array(
  133. '0' => array(
  134. "attribute_code" => "category_ids",
  135. "value" => array( $array_cat[$prod[16]] ),
  136. ),
  137. '1' => array(
  138. "attribute_code" => "upc_ean",
  139. "value" => $upc_ean[0],
  140. ),
  141. '2' => array(
  142. "attribute_code" => "brand",
  143. "value" => utf8_encode( $prod[3] ),
  144. ),
  145. '3' => array(
  146. "attribute_code" => "url_key",
  147. "value" => $prod[0],
  148. )
  149. ),
  150. "extension_attributes" => array(
  151. "stock_item" => array(
  152. "is_in_stock" => 1,
  153. "qty" => $qty
  154. )
  155. )
  156. ),
  157.  
  158. "saveOptions" => true
  159.  
  160. );
  161.  
  162. if( $image == "" ) {
  163.  
  164. if( $prod[12] != "" ) {
  165.  
  166. $imageThumb = file_get_contents( $prod[12] );
  167.  
  168. if ( $imageThumb !== false ) {
  169.  
  170. $imageThumbb64 = base64_encode( $imageThumb );
  171.  
  172. }
  173.  
  174. $postData['product']['media_gallery_entries'] = array(
  175. array(
  176. "mediaType" => "image",
  177. "position" => 0,
  178. "disabled" => false,
  179. "types" => array( "thumbnail" ),
  180.  
  181. "content" => array(
  182. "base64_encoded_data" => $imageThumbb64,
  183. "type" => "image/jpeg",
  184. "name" => "img_" . $prod[0] . ".jpg"
  185. )
  186. )
  187. );
  188. }
  189.  
  190. } else {
  191.  
  192. $imageThumb = file_get_contents( $image );
  193.  
  194. if ( $imageThumb !== false ) {
  195.  
  196. $imageThumbb64 = base64_encode( $imageThumb );
  197.  
  198. }
  199.  
  200. $postData['product']['media_gallery_entries'] = array(
  201. array(
  202. "mediaType" => "image",
  203. "position" => 0,
  204. "disabled" => false,
  205. "types" => array( "thumbnail", "small_image", "image" ),
  206.  
  207. "content" => array(
  208. "base64_encoded_data" => $imageThumbb64,
  209. "type" => "image/jpeg",
  210. "name" => "img_" . $prod[0] . ".jpg"
  211. )
  212.  
  213. )
  214. );
  215. }
  216.  
  217. $ch = curl_init( URL_SYNCH_PRODOTTI.$prod[0] );
  218.  
  219. curl_setopt_array( $ch, array(
  220. CURLOPT_CUSTOMREQUEST => "PUT",
  221. CURLOPT_RETURNTRANSFER => TRUE,
  222. CURLOPT_HTTPHEADER => array(
  223. 'Content-Type: application/json',
  224. 'Authorization: Bearer '.$token,
  225. ),
  226. CURLOPT_POSTFIELDS => json_encode( $postData ),
  227. CURLOPT_SSL_VERIFYPEER => 0,
  228. CURLOPT_SSL_VERIFYHOST => 0
  229. ));
  230.  
  231. $response = curl_exec( $ch );
  232.  
  233. if( $response === FALSE ) {
  234.  
  235. die( curl_error( $ch ) );
  236.  
  237. }
  238.  
  239. $responseData = json_decode( $response, TRUE );
  240.  
  241. if ( array_key_exists( "price", $responseData ) ) {
  242.  
  243. $log->lwrite( "dataok" );
  244.  
  245. return $responseData;
  246.  
  247. } else {
  248.  
  249. if( $responseData['message'] == "URL key for specified store already exists." ) {
  250.  
  251. $log->lwrite( "Not saved url key error: " .$prod[0] );
  252. print_r( $responseData );
  253.  
  254. return;
  255.  
  256. }
  257.  
  258. $log->lwrite( $responseData["message"] );
  259.  
  260. syncProduct( $token, $prod, $array_cat, $image, $i );
  261.  
  262. }
  263.  
  264. } catch ( Exception $e ) {
  265.  
  266. echo( " Error - ".$e->getCode() );
  267. $log->lwrite( " Code: ".$e->getCode()." Message: ".$e->getMessage() );
  268.  
  269. syncProduct( $token, $prod, $array_cat, $image, $i );
  270. }
  271.  
  272. }
  273.  
  274. function getCategroy( $token ) {
  275.  
  276. $ch = curl_init( URL_SYNCH_CATEGORIE );
  277.  
  278. curl_setopt_array( $ch, array(
  279. CURLOPT_CUSTOMREQUEST => "GET",
  280. CURLOPT_RETURNTRANSFER => TRUE,
  281. CURLOPT_HTTPHEADER => array(
  282. 'Content-Type: application/json',
  283. 'Authorization: Bearer '.$token,
  284. ),
  285. CURLOPT_SSL_VERIFYPEER => 0,
  286. CURLOPT_SSL_VERIFYHOST => 0
  287. ));
  288.  
  289. $response = curl_exec( $ch );
  290.  
  291. if( $response === FALSE ) {
  292.  
  293. die( curl_error( $ch ) );
  294.  
  295. }
  296.  
  297. $responseData = json_decode( $response, TRUE );
  298.  
  299. return $responseData;
  300.  
  301. }
  302.  
  303. function syncCategory( $token, $cat ) {
  304.  
  305. $postData = array(
  306. 'category' => array(
  307. "name" => $cat,
  308. "is_active" => true
  309. )
  310. );
  311.  
  312. $ch = curl_init( URL_SYNCH_CATEGORIE );
  313.  
  314. curl_setopt_array( $ch, array(
  315. CURLOPT_CUSTOMREQUEST => "POST",
  316. CURLOPT_RETURNTRANSFER => TRUE,
  317. CURLOPT_HTTPHEADER => array(
  318. 'Content-Type: application/json',
  319. 'Authorization: Bearer '.$token,
  320. ),
  321. CURLOPT_POSTFIELDS => json_encode( $postData ),
  322. CURLOPT_SSL_VERIFYPEER => 0,
  323. CURLOPT_SSL_VERIFYHOST => 0
  324. ));
  325.  
  326. $response = curl_exec( $ch );
  327.  
  328. if( $response === FALSE ) {
  329.  
  330. die( curl_error( $ch ) );
  331.  
  332. }
  333.  
  334. $responseData = json_decode( $response, TRUE );
  335.  
  336. return $responseData;
  337.  
  338. }
  339.  
  340. function syncProductRemove( $token, $prod ) {
  341.  
  342. $dateTime = ( new DateTime() )->format( 'Y-m-d' );
  343. $log = new Logging();
  344. $log->lfile( dirname(__FILE__) . "/log/" . $dateTime . ".txt" );
  345.  
  346. try { $qty = 0;
  347.  
  348. $postData = array(
  349. 'product' => array(
  350. "sku" => $prod[0],
  351. "status" => 0,
  352. "extension_attributes" => array(
  353. "stock_item" => array(
  354. "is_in_stock" => 0,
  355. "qty" => 0
  356. )
  357. )
  358. ),
  359.  
  360. 'saveOptions' => true
  361.  
  362. );
  363.  
  364. $ch = curl_init( URL_SYNCH_PRODOTTI.$prod );
  365. curl_setopt_array( $ch, array(
  366. CURLOPT_CUSTOMREQUEST => "PUT",
  367. CURLOPT_RETURNTRANSFER => TRUE,
  368. CURLOPT_HTTPHEADER => array(
  369. 'Content-Type: application/json',
  370. 'Authorization: Bearer '.$token,
  371. ),
  372. CURLOPT_POSTFIELDS => json_encode( $postData ),
  373. CURLOPT_SSL_VERIFYPEER => 0,
  374. CURLOPT_SSL_VERIFYHOST => 0
  375. ));
  376.  
  377. $response = curl_exec( $ch );
  378.  
  379. if( $response === FALSE ) {
  380.  
  381. die( curl_error( $ch ) );
  382. }
  383.  
  384. $responseData = json_decode( $response, TRUE );
  385.  
  386. if (array_key_exists( "price", $responseData ) ) {
  387.  
  388. return $responseData;
  389.  
  390. } else {
  391.  
  392. if( $responseData['message'] == "URL key for specified store already exists" ) {
  393.  
  394. $log->lwrite( "Not saved url key error: " .$prod );
  395. print_r( $responseData );
  396.  
  397. return;
  398.  
  399. }
  400.  
  401. $log->lwrite( $responseData["message"] );
  402. syncProductRemove( $token, $prod );
  403.  
  404. }
  405.  
  406. } catch ( Exception $e ) {
  407.  
  408. echo( " Error - ".$e->getCode() );
  409. $log->lwrite( " Code: ".$e->getCode()." Message: ".$e->getMessage() );
  410. syncProductRemove( $token,$prod );
  411.  
  412. }
  413.  
  414. }
  415.  
  416. function getAllProd( $token, $cat_id ) {
  417.  
  418. $ch = curl_init( URL_SYNCH_CATEGORIE.$cat_id."/products" );
  419. curl_setopt_array($ch, array(
  420. CURLOPT_CUSTOMREQUEST => "GET",
  421. CURLOPT_RETURNTRANSFER => TRUE,
  422. CURLOPT_HTTPHEADER => array(
  423. 'Content-Type: application/json',
  424. 'Authorization: Bearer '.$token,
  425. ),
  426. CURLOPT_SSL_VERIFYPEER => 0,
  427. CURLOPT_SSL_VERIFYHOST => 0
  428. ));
  429.  
  430. $response = curl_exec( $ch );
  431.  
  432. if( $response === FALSE ) {
  433.  
  434. die( curl_error( $ch ) );
  435.  
  436. }
  437.  
  438. $responseData = json_decode( $response, TRUE );
  439.  
  440. return $responseData;
  441. }
  442.  
  443. <?php
  444.  
  445. include "GetStockFileRequestType.php";
  446. require_once "Functions.php";
  447.  
  448. $client = new SoapClient( URL_BEAUTYFORT_SOAP );
  449.  
  450. $increment = 1.59;
  451. $dateTime = ( new DateTime() )->format( 'Y-m-d' );
  452. $log = new Logging();
  453. $log2 = new Logging();
  454.  
  455. $log->lfile( dirname(__FILE__) . "/log/" . $dateTime . ".txt" );
  456. $log2->lfile( dirname(__FILE__) . "/log/" . $dateTime . "_productsynched.txt" );
  457. $file = dirname(__FILE__) . "/tmp/" . $dateTime . ".txt";
  458.  
  459. if( file_exists( $file ) ) {
  460.  
  461. echo( "Exist " );
  462. $log->lwrite( "Exist " );
  463.  
  464. }
  465.  
  466. $authHeader = getAuthHeader();
  467. $GetStockFileRequestType = new GetStockFileRequestType( TEST_MODE, "Delimited", null, null, "StockCode", ";" );
  468. $GetStockFileParams = array(
  469. "GetStockFileRequestType" => $GetStockFileRequestType
  470. );
  471.  
  472. $paramsHeader = array( "AuthHeader" => $authHeader );
  473. $header = new SoapHeader( URL_BEAUTYFORT_API, "AuthHeader" , $authHeader );
  474. $client->__setSoapHeaders( $header );
  475.  
  476. $GetStockFileResponse = $client->__soapCall( "GetStockFile", $GetStockFileParams );
  477.  
  478. echo file_put_contents( "BeautyFortCatalog.csv", $GetStockFileResponse->File );
  479.  
  480. $Data = str_getcsv( $GetStockFileResponse->File, "n" );
  481. $i=0;
  482. $arr_p = array();
  483. $arr_cat = array();
  484.  
  485. foreach( $Data as $Row ) {
  486.  
  487. $Row = str_getcsv( $Row, ";" );
  488.  
  489. if ( $i != 0 ) {
  490.  
  491. $log->lwrite( " -> " . $Row[0] . " - " . $Row[2] );
  492. $cat = explode( " > ", $Row[1] )[0];
  493.  
  494. if( $Row[9] == "" ) {
  495.  
  496. $price = $Row[8] * $increment;
  497.  
  498. } else {
  499.  
  500. $price = $Row[9];
  501.  
  502. }
  503.  
  504. $Row[15] = round( $price, 2, PHP_ROUND_HALF_UP );
  505. $Row[16] = $cat;
  506.  
  507. if ( !array_key_exists( $cat, $arr_cat ) ) {
  508.  
  509. $arr_cat[$cat] = 0;
  510.  
  511. }
  512.  
  513. } else {
  514.  
  515. $Row[15] = "Final Price";
  516. $Row[16] = "New Category";
  517.  
  518. }
  519.  
  520. $arr_p[] = $Row;
  521. $i++;
  522.  
  523. }
  524.  
  525. $authToken = getToken();
  526. $cat = getCategroy( $authToken );
  527. $array_p_magento = array();
  528.  
  529. foreach( $arr_cat as $key => $row ) {
  530.  
  531. foreach ( $cat["children_data"] as $key2 => $row2 ) {
  532.  
  533. if ( $row2["name"] == $key ) {
  534.  
  535. $arr_cat[$key] = $row2["id"];
  536.  
  537. }
  538.  
  539. }
  540.  
  541. if ( $arr_cat[$key] == 0 ) {
  542.  
  543. $category = syncCategory( $authToken, $key );
  544. $arr_cat[$key] = $category["id"];
  545.  
  546. }
  547.  
  548. $products = getAllProd( $authToken, $arr_cat[$key] );
  549.  
  550. foreach ( $products as $keyp => $product ) {
  551.  
  552. if( $product ) {
  553.  
  554. $array_p_magento[] = $product["sku"];
  555.  
  556. }
  557.  
  558. }
  559.  
  560. }
  561.  
  562. $file2 = dirname(__FILE__) . "/log/" . $dateTime . "_productsynched.txt";
  563. $esegui = true;
  564. $lastProduct = "";
  565.  
  566. if( file_exists( $file2 ) ) {
  567.  
  568. $esegui = false;
  569. $Data2 = str_getcsv( file_get_contents( $file2 ), "n" );
  570. $lastProduct = explode( " ", end( $Data2 ) );
  571.  
  572. }
  573.  
  574. $i=0;
  575.  
  576. foreach( $arr_p as $row ) {
  577.  
  578. if ( $i != 0 ) {
  579.  
  580. if( $lastProduct != "" ) {
  581.  
  582. if( $lastProduct[2] == $row[0] ) {
  583.  
  584. $esegui = true;
  585.  
  586. }
  587.  
  588. }
  589.  
  590. if( $row[15] > 1 ) {
  591.  
  592. if( $esegui ) {
  593.  
  594. $image = $row[13];
  595. syncProduct( $authToken, $row, $arr_cat, $image, $i );
  596. $log2->lwrite( $row[0] );
  597. }
  598.  
  599. if( ( $key = array_search( $row[0], $array_p_magento ) ) !== false ) {
  600.  
  601. unset( $array_p_magento[$key] );
  602.  
  603. }
  604.  
  605. }
  606.  
  607. }
  608.  
  609. $i++;
  610.  
  611. }
  612.  
  613. foreach( $array_p_magento as $row ) {
  614.  
  615. syncProductRemove( $authToken, $row );
  616. $log->lwrite("Removed - " . $row);
  617.  
  618. }
  619.  
  620. $log->lwrite( "Done Unlinking File" );
  621.  
  622. unlink( $file2 );
  623.  
  624. echo( "Done" );
  625.  
  626. $log->lwrite( "File Unlinked" );
  627.  
  628. $fp = fopen( $file, "wb" );
  629.  
  630. fwrite( $fp, $dateTime );
  631.  
  632. fclose( $fp );
  633.  
  634. $files = glob( dirname(__FILE__) . "/log/*" );
  635.  
  636. $now = time();
  637.  
  638. foreach ( $files as $file ) {
  639.  
  640. if (is_file( $file )) {
  641.  
  642. if ($now - filemtime( $file ) >= 60 * 60 * 24 * 2) {
  643.  
  644. unlink( $file );
  645.  
  646. }
  647.  
  648. }
  649.  
  650. }
Add Comment
Please, Sign In to add comment