MaxBeer
Профессор
- Регистрация
- 10 Ноя 2013
- Сообщения
- 109
- Реакции
- 38
- Автор темы
- #1
Здравия всем желаю!
Помогите переписать парсер прайсов, написанный изначально под OsCommerce.
Я догадываюсь, что нужно где-то указать параметры подключения к БД и изменить запрос tep_db_query , как - ?
Заранее спасибо!
Помогите переписать парсер прайсов, написанный изначально под OsCommerce.
<?php
include "includes/application_top.php";
$rules_file = "import/rules.txt";
$rules = parse_rules($rules_file);
process_prices($rules);
/*************************************************************
* Helper Stuff
************************************************************/
function parse_rules($rules_file = '')
{
$rules = array();
$lines = array();
if (file_exists($rules_file)) {
$lines = file($rules_file);
}
foreach ($lines as $line) {
$options = preg_split("/[:|;]/", $line);
if (7 == sizeof($options)) {
$code = $options[0];
$rules[$code] = array(
'discount' => $options[1],
'margin' => $options[2],
'discount_a' => $options[4],
'margin_a' => $options[3],
'vat' => $options[5],
'curr_rate' => $options[6]
);
}
}
return $rules;
}
function process_prices($rules)
{
$data = opendir("import");
while ($filename = readdir($data)) {
if ($filename != '.' && $filename != '..') {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ('csv' == $ext || 'CSV' == $ext) {
parse_price("import/" . $filename, $rules);
}
}
}
closedir($data);
}
function parse_price($filename, $rules)
{
$lines = file($filename);
$matches = array();
if (sizeof($lines) > 1) {
for ($i = 1; $i < sizeof($lines); $i++) {
$options = preg_split("/;/", $lines[$i]);
if (3 == sizeof($options)) {
$code = $options[0];
preg_match('/^([A-Z,0-9]{1}[A-Z]{1,2}).*/', $code, $matches);
if (sizeof($matches) > 1) {
$code_group = $matches[1];
$price = str_replace(',', '.', $options[1]);
$qty = str_replace('>', '', $options[2]);
if ((int)$qty > 0) {
$qty = 1;
} else {
$qty = 0;
}
if (isset($rules[$code_group])) {
if ($rules[$code_group]['discount'] > 0) {
$price = $price * (100 - $rules[$code_group]['discount'])/100;
} elseif ($rules[$code_group]['margin'] > 0) {
$price = $price * (100 + $rules[$code_group]['margin'])/100;
}
if ($rules[$code_group]['discount_a'] > 0) {
$price -= $rules[$code_group]['discount_a'];
} elseif ($rules[$code_group]['margin_a'] > 0) {
$price += $rules[$code_group]['margin_a'];
}
if ($rules[$code_group]['vat'] > 0) {
$price = $price * 100/(100 + $rules[$code_group]['vat']);
}
if ($rules[$code_group]['curr_rate']) {
$price *= $rules[$code_group]['curr_rate'];
}
tep_db_query("update " . TABLE_PRODUCTS . " set products_date_added=now(), products_price = " . (float)$price . ", products_quantity = " . (int)$qty . " where products_model = '" . $code . "'");
}
}
}
}
}
}
include "includes/application_top.php";
$rules_file = "import/rules.txt";
$rules = parse_rules($rules_file);
process_prices($rules);
/*************************************************************
* Helper Stuff
************************************************************/
function parse_rules($rules_file = '')
{
$rules = array();
$lines = array();
if (file_exists($rules_file)) {
$lines = file($rules_file);
}
foreach ($lines as $line) {
$options = preg_split("/[:|;]/", $line);
if (7 == sizeof($options)) {
$code = $options[0];
$rules[$code] = array(
'discount' => $options[1],
'margin' => $options[2],
'discount_a' => $options[4],
'margin_a' => $options[3],
'vat' => $options[5],
'curr_rate' => $options[6]
);
}
}
return $rules;
}
function process_prices($rules)
{
$data = opendir("import");
while ($filename = readdir($data)) {
if ($filename != '.' && $filename != '..') {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ('csv' == $ext || 'CSV' == $ext) {
parse_price("import/" . $filename, $rules);
}
}
}
closedir($data);
}
function parse_price($filename, $rules)
{
$lines = file($filename);
$matches = array();
if (sizeof($lines) > 1) {
for ($i = 1; $i < sizeof($lines); $i++) {
$options = preg_split("/;/", $lines[$i]);
if (3 == sizeof($options)) {
$code = $options[0];
preg_match('/^([A-Z,0-9]{1}[A-Z]{1,2}).*/', $code, $matches);
if (sizeof($matches) > 1) {
$code_group = $matches[1];
$price = str_replace(',', '.', $options[1]);
$qty = str_replace('>', '', $options[2]);
if ((int)$qty > 0) {
$qty = 1;
} else {
$qty = 0;
}
if (isset($rules[$code_group])) {
if ($rules[$code_group]['discount'] > 0) {
$price = $price * (100 - $rules[$code_group]['discount'])/100;
} elseif ($rules[$code_group]['margin'] > 0) {
$price = $price * (100 + $rules[$code_group]['margin'])/100;
}
if ($rules[$code_group]['discount_a'] > 0) {
$price -= $rules[$code_group]['discount_a'];
} elseif ($rules[$code_group]['margin_a'] > 0) {
$price += $rules[$code_group]['margin_a'];
}
if ($rules[$code_group]['vat'] > 0) {
$price = $price * 100/(100 + $rules[$code_group]['vat']);
}
if ($rules[$code_group]['curr_rate']) {
$price *= $rules[$code_group]['curr_rate'];
}
tep_db_query("update " . TABLE_PRODUCTS . " set products_date_added=now(), products_price = " . (float)$price . ", products_quantity = " . (int)$qty . " where products_model = '" . $code . "'");
}
}
}
}
}
}
Я догадываюсь, что нужно где-то указать параметры подключения к БД и изменить запрос tep_db_query , как - ?
Заранее спасибо!