Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header("Content-Type: text/plain");
- function file_get_contents_curl($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
- curl_setopt($ch, CURLOPT_URL, $url);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- // replace any non-ascii character with its hex code.
- function escape($value) {
- return addslashes($value);
- }
- // Display output while loading
- function flush_it()
- {
- //echo(str_repeat(' ',256));
- // check that buffer is actually set before flushing
- if (ob_get_length()){
- @ob_flush();
- @flush();
- @ob_end_flush();
- }
- @ob_start();
- }
- // Get paths as CRON won't work correctly without them
- define('DIR_TMP', '/XXXXXX); // absolute path required
- define('DIR_BASE', 'XXXXXXXXX); // absolute path required
- require('XXXXXXXXXXXX);
- // Database tuning
- mysql_query("SET SESSION BULK_INSERT_BUFFER_SIZE=256217728");
- mysql_query("SET SESSION MYISAM_SORT_BUFFER_SIZE=256217728");
- mysql_query("SET GLOBAL KEY_BUFFER_SIZE=256217728");
- mysql_query("ALTER TABLE stock DISABLE KEYS");
- set_time_limit(300);
- //$latest_stock_file = file_get_contents_curl('http://XXXXXXXX/XXXXXXX);
- $latest_stock_file = file_get_contents_curl('hXXXXXXXXXXXXXX);
- if (!$latest_stock_file) die("Unable to read Autopart prices file. nn");
- set_time_limit(300);
- // Create file to hold stock
- $stock_file = fopen(DIR_TMP . 'prices.csv', 'w') or die("Unable to create temporary file to save prices in. nn");
- $bytes = fwrite($stock_file, $latest_stock_file) or die("Unable to write prices to temporary CSV file. nn");
- fclose($stock_file);
- flush_it();
- echo "Prices obtained. ($bytes bytes)nn";
- flush_it();
- mysql_query('DROP TABLE IF EXISTS tmp_product_prices');
- // Create temporary table to hold prices
- mysql_query('CREATE TABLE tmp_product_prices (
- part VARCHAR(60),
- rrp DECIMAL(15,2),
- cond VARCHAR(3)
- )');
- mysql_query('ALTER TABLE tmp_product_prices ADD INDEX part (part)');
- flush_it();
- echo "Cleared temporary table.nn";
- flush_it();
- set_time_limit(300);
- 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'
- ."' (`part`, `rrp`, `cond`)");
- set_time_limit(300);
- flush_it();
- echo "Prices imported to temporary table (".mysql_info()."). nn";
- flush_it();
- // Show warnings
- $warning = mysql_query('SHOW WARNINGS');
- echo '2';
- if (mysql_num_rows($warning) > 0)
- {
- while ($row = mysql_fetch_assoc($warning))
- {
- //print_r($row);
- echo $row['Level'] . ' ' . $row['Code'] . ' ' . $row['Message'] . "n";
- }
- }
- echo "n";
- unlink(DIR_TMP . 'prices.csv');
- if ( mysql_error())
- {
- echo mysql_error();
- }
- else
- {
- //Now output list of changed
- $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
- FROM tmp_product_prices tpp
- INNER JOIN fec_products p ON tpp.part = p.products_model
- LEFT JOIN fec_products_description pd ON p.products_id = pd.products_id
- LEFT JOIN fec_manufacturers m ON p.manufacturers_id = m.manufacturers_id
- WHERE (tpp.cond != "W")
- AND pd.language_id = 1
- HAVING difference != 0');
- set_time_limit(300);
- flush_it();
- if (mysql_num_rows($changed_parts_query) > 0)
- {
- 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";
- echo "Part_NumbertConditiontBrandtDescriptiontProduct_IDtWeb_PricetMAM_PricetDifferencen";
- while ($row = mysql_fetch_array($changed_parts_query))
- {
- // Update query - use with caution!!!
- mysql_query(" UPDATE fec_products SET products_price = " . $row['mam_price'] . " WHERE products_model = '" . $row['part'] . "'");
- // Show results (copy into Notepad then into Excel - tab delimited)
- 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";
- }
- // Recalculate special offer prices
- mysql_query(' UPDATE ' . TABLE_SPECIALS . ' s, ' . TABLE_PRODUCTS . ' p
- SET s.specials_new_products_price = ROUND(p.products_price - p.products_price / 100 * s.specials_new_products_discount, 2)
- WHERE s.specials_type = 'P'
- AND p.products_id = s.products_id');
- }
- else
- {
- echo "No price changes today. All web prices are correct.";
- }
- flush_it();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement