Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. <?php
  2.  
  3. header("Content-Type: text/plain");
  4.  
  5. function file_get_contents_curl($url)
  6. {
  7. $ch = curl_init();
  8.  
  9. curl_setopt($ch, CURLOPT_HEADER, 0);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
  11. curl_setopt($ch, CURLOPT_URL, $url);
  12.  
  13. $data = curl_exec($ch);
  14. curl_close($ch);
  15.  
  16. return $data;
  17. }
  18.  
  19. // replace any non-ascii character with its hex code.
  20. function escape($value) {
  21. return addslashes($value);
  22. }
  23.  
  24. // Display output while loading
  25. function flush_it()
  26. {
  27. //echo(str_repeat(' ',256));
  28. // check that buffer is actually set before flushing
  29. if (ob_get_length()){
  30. @ob_flush();
  31. @flush();
  32. @ob_end_flush();
  33. }
  34. @ob_start();
  35. }
  36.  
  37. // Get paths as CRON won't work correctly without them
  38.  
  39. define('DIR_TMP', '/XXXXXX); // absolute path required
  40. define('DIR_BASE', 'XXXXXXXXX); // absolute path required
  41.  
  42. require('XXXXXXXXXXXX);
  43.  
  44. // Database tuning
  45. mysql_query("SET SESSION BULK_INSERT_BUFFER_SIZE=256217728");
  46. mysql_query("SET SESSION MYISAM_SORT_BUFFER_SIZE=256217728");
  47. mysql_query("SET GLOBAL KEY_BUFFER_SIZE=256217728");
  48. mysql_query("ALTER TABLE stock DISABLE KEYS");
  49.  
  50. set_time_limit(300);
  51.  
  52. //$latest_stock_file = file_get_contents_curl('http://XXXXXXXX/XXXXXXX);
  53. $latest_stock_file = file_get_contents_curl('hXXXXXXXXXXXXXX);
  54.  
  55. if (!$latest_stock_file) die("Unable to read Autopart prices file. nn");
  56.  
  57. set_time_limit(300);
  58.  
  59. // Create file to hold stock
  60. $stock_file = fopen(DIR_TMP . 'prices.csv', 'w') or die("Unable to create temporary file to save prices in. nn");
  61. $bytes = fwrite($stock_file, $latest_stock_file) or die("Unable to write prices to temporary CSV file. nn");
  62. fclose($stock_file);
  63.  
  64. flush_it();
  65.  
  66. echo "Prices obtained. ($bytes bytes)nn";
  67.  
  68. flush_it();
  69.  
  70. mysql_query('DROP TABLE IF EXISTS tmp_product_prices');
  71.  
  72. // Create temporary table to hold prices
  73. mysql_query('CREATE TABLE tmp_product_prices (
  74. part VARCHAR(60),
  75. rrp DECIMAL(15,2),
  76. cond VARCHAR(3)
  77. )');
  78.  
  79. mysql_query('ALTER TABLE tmp_product_prices ADD INDEX part (part)');
  80.  
  81. flush_it();
  82.  
  83. echo "Cleared temporary table.nn";
  84.  
  85. flush_it();
  86.  
  87. set_time_limit(300);
  88.  
  89. mysql_query("LOAD DATA LOCAL INFILE '" . DIR_TMP . "prices.csv' INTO TABLE `tmp_product_prices` FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '".'n'
  90. ."' (`part`, `rrp`, `cond`)");
  91.  
  92. set_time_limit(300);
  93.  
  94. flush_it();
  95.  
  96. echo "Prices imported to temporary table (".mysql_info()."). nn";
  97.  
  98. flush_it();
  99.  
  100. // Show warnings
  101.  
  102. $warning = mysql_query('SHOW WARNINGS');
  103. echo '2';
  104. if (mysql_num_rows($warning) > 0)
  105. {
  106. while ($row = mysql_fetch_assoc($warning))
  107. {
  108. //print_r($row);
  109. echo $row['Level'] . ' ' . $row['Code'] . ' ' . $row['Message'] . "n";
  110. }
  111. }
  112.  
  113. echo "n";
  114.  
  115. unlink(DIR_TMP . 'prices.csv');
  116.  
  117. if ( mysql_error())
  118.  
  119. {
  120.  
  121. echo mysql_error();
  122.  
  123. }
  124.  
  125. else
  126.  
  127. {
  128.  
  129. //Now output list of changed
  130.  
  131. $changed_parts_query = mysql_query(' SELECT tpp.part, tpp.cond, m.manufacturers_name, pd.products_name, p.products_id, p.products_price AS web_price, tpp.rrp AS mam_price, (tpp.rrp-p.products_price) AS difference
  132. FROM tmp_product_prices tpp
  133. INNER JOIN fec_products p ON tpp.part = p.products_model
  134. LEFT JOIN fec_products_description pd ON p.products_id = pd.products_id
  135. LEFT JOIN fec_manufacturers m ON p.manufacturers_id = m.manufacturers_id
  136. WHERE (tpp.cond != "W")
  137. AND pd.language_id = 1
  138. HAVING difference != 0');
  139.  
  140. set_time_limit(300);
  141.  
  142. flush_it();
  143.  
  144. if (mysql_num_rows($changed_parts_query) > 0)
  145. {
  146. echo 'The following ' . $changeCount . ' parts have a different retail price in MAM compared to the website, please review (copy into Excel, Data, Text to Columns):' . "nn";
  147.  
  148. echo "Part_NumbertConditiontBrandtDescriptiontProduct_IDtWeb_PricetMAM_PricetDifferencen";
  149.  
  150. while ($row = mysql_fetch_array($changed_parts_query))
  151. {
  152. // Update query - use with caution!!!
  153. mysql_query(" UPDATE fec_products SET products_price = " . $row['mam_price'] . " WHERE products_model = '" . $row['part'] . "'");
  154.  
  155. // Show results (copy into Notepad then into Excel - tab delimited)
  156. echo escape($row['part']) . "t" . escape($row['cond']) . "t" . escape($row['manufacturers_name']) . "t" . escape($row['products_name']) . "t" . escape($row['products_id']) . "t" . escape($row['web_price']) . "t" . escape($row['mam_price']) . "t" . escape($row['difference']) . "n";
  157. }
  158.  
  159. // Recalculate special offer prices
  160. mysql_query(' UPDATE ' . TABLE_SPECIALS . ' s, ' . TABLE_PRODUCTS . ' p
  161. SET s.specials_new_products_price = ROUND(p.products_price - p.products_price / 100 * s.specials_new_products_discount, 2)
  162. WHERE s.specials_type = 'P'
  163. AND p.products_id = s.products_id');
  164.  
  165. }
  166. else
  167. {
  168. echo "No price changes today. All web prices are correct.";
  169. }
  170.  
  171. flush_it();
  172.  
  173. }
  174.  
  175.  
  176.  
  177. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement