Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once('tools.php');
- Class Ewe
- {
- protected $mysqli = '';
- protected $tools = NULL;
- protected $oc_productSQL = '';
- protected $oc_product_descriptionSQL = '';
- protected $oc_product_to_categorySQL = '';
- protected $oc_product_to_storeSQL = '';
- protected $insertakcija_sql = '';
- protected $akc = 0;
- protected $updaterasp = array();
- protected $ewe_local_image = 'data/ewe/';
- protected $ewe_image_path = 'https://www.ewe.rs/slike-proizvoda';
- protected $path = "/var/www/clients/client0/web1/web/sync/ewe/";
- protected $username = 'bus';
- protected $password = '6dc50';
- protected $banned_cats = array();
- // protected $banned_cats = array('91', '172', '171', '160', '159', '136', '137', '148', '146', '138', '140', '145', '139', '147', '141', '142', '143', '144');
- public function __construct ()
- {
- try {
- $db_database = 'db_empty';
- $db_hostname = '46.174.100.19';
- $db_username = 'root';
- $db_password = '19busweb123';
- $this->mysqli = mysqli_connect($db_hostname, $db_username, $db_password, $db_database) or die("Unable to connect to mysqli: ". mysqli_error());
- $this->mysqli->set_charset("utf8");
- $this->mysqli->query('set name utf8');
- }
- catch (PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- public function set_tools($tools)
- {
- $this->tools = $tools;
- }
- public function test()
- {
- $url = 'http://www.ewe.rs';
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_NOBODY, true);
- //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_exec($ch);
- $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if (200==$retcode) {
- return true;
- } else {
- return true;
- }
- }
- public function run()
- {
- $isDoomed = $this->doom();
- if($isDoomed == true)
- {
- $getEwe = $this->getData();
- if($getEwe == true)
- {
- //echo "Pre inserta Ewe\r";
- $insEWE = $this->insertData();
- if($insEWE == true)
- {
- $isnertIntoShop = $this->insertEwe();
- if($isnertIntoShop)
- {
- // echo "Uradio insert Ewe\r";
- $insertEweCategories = $this->insertCategories();
- if($insertEweCategories)
- {
- //echo "Ubacio sam kategorije";
- return true;
- }
- }
- else return ("GRESKA KOD UNOSA EWE ARTIKALA U SHOP: ".$isnertIntoShop);
- }
- else return ("GRESKA KOD UNOSA EWE ARTIKALA: ".$insEWE);
- }
- else return ("GRESKA KOD POVLACENJA EWE ARTIKALA");
- }
- else return ("GRESKA KOD BRISANJA EWE PROIZVODA");
- }
- public function getData()
- {
- $url = 'http://api.ewe.rs/share/backend/?user='.$this->username.'&secretcode='.$this->password.'&images=1';
- $ch =curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_VERBOSE, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
- $result = curl_exec ($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $file = $this->path.'ewe.xml';
- if($httpcode==200)
- {
- curl_close ($ch);
- if (file_exists($file))
- {
- $file0 = fopen($file, 'w+');
- fwrite($file0, $result);
- fclose($file0);
- // $config = array(
- // 'indent' => true,
- // 'input-xml' => true,
- // 'output-xml' => true,
- // 'output-encoding' => 'UTF-8',
- // 'input-encoding' => 'UTF-8',
- // 'wrap' => false);
- // $tidy = new tidy;
- // $tidy->parseFile($file, $config);
- // $tidy->cleanRepair();
- // $file1 = fopen($file, 'w+');
- // fwrite($file1, $tidy);
- // fclose($file1);
- return true;
- }
- else
- {
- exit('Error.');
- }
- }
- else
- {
- echo curl_error($ch);
- }
- }
- public function insertData()
- {
- $data = array();
- $xmlp = simplexml_load_file($this->path.'ewe.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
- $i = 0;
- $this->mysqli->autocommit(FALSE);
- $megaInsert = "REPLACE INTO oc_product_ewe(id, manufacturer, name, image, category, subcategory, price, price_rebate, recommended_retail_price, vat, ean, product_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- $stmt = $this->mysqli->prepare($megaInsert);
- $stmt->bind_param('ssssssddddsi', $id, $manufacturer, $name, $image, $category, $subcategory, $price, $price_rebate, $recommended_retail_price, $vat, $ean, $product_id);
- foreach($xmlp as $row)
- {
- $id = 'EWE_'.$row->id;
- $manufacturer = $row->manufacturer;
- $name = $row->name;
- $category = $row->category;
- $subcategory = $row->subcategory;
- $price = floatval(str_replace(',', '.', $row->price));
- $price_rebate = floatval(str_replace(',', '.', $row->price_rebate));
- $vat = floatval(str_replace(',', '.', $row->vat));
- $ean = $row->ean;
- if(isset($row->recommended_retail_price))
- $recommended_retail_price = $row->recommended_retail_price;
- else
- $recommended_retail_price = NULL;
- if(isset($row->images))
- $image = $row->images[0]->image;
- else
- $image = NULL;
- if(!$stmt->execute())
- return $stmt->error;
- $i++;
- if(($i % 500) === 0) $this->mysqli->commit();
- }
- $this->mysqli->commit();
- return true;
- }
- public function insertEwe()
- {
- $now = date('Y-m-d H:i:s');
- $dateAvailable = date('Y-m-d');
- $i = 0;
- $getDataSQL = "SELECT * FROM oc_product_ewe WHERE ean is not null AND ean != ''";
- $getData = $this->mysqli->query($getDataSQL);
- if($getData->num_rows > 0)
- {
- $this->mysqli->autocommit(FALSE);
- // ARTIKAL
- $oc_productSQL = "REPLACE INTO oc_product(product_id, model, ean, quantity, stock_status_id, image, manufacturer_id, price, price_retail, date_available, status, date_added, imported_from) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- // $oc_productSQL = "REPLACE INTO oc_product(model, ean, quantity, stock_status_id, image, manufacturer_id, price, price_retail, date_available, status, date_added, imported_from) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- $stmtOcP = $this->mysqli->prepare($oc_productSQL);
- $stmtOcP->bind_param('sssiisiddsiss', $product_id, $model, $ean, $quantity, $stock_status_id, $image, $manufacturer_id, $price, $price_retail, $dateAvailable, $status, $now, $imported_from);
- // OPIS - SR / EN
- $oc_productDescriptionSQL = "REPLACE INTO oc_product_description (product_id, language_id, name, description) VALUES (?, ?, ?, ?)";
- $stmtOcPDSR = $this->mysqli->prepare($oc_productDescriptionSQL);
- $stmtOcPDEN = $this->mysqli->prepare($oc_productDescriptionSQL);
- $stmtOcPDSR->bind_param('siss', $product_id, $language_sr, $name, $short_description);
- $stmtOcPDEN->bind_param('siss', $product_id, $language_en, $name, $short_description);
- // KATEGORIJE - PARENT / CHILD
- $oc_product_to_categorySQL = "REPLACE INTO oc_product_to_category(product_id, category_id) VALUES (?, ?)";
- $stmtOcPCATP = $this->mysqli->prepare($oc_product_to_categorySQL);
- $stmtOcPCATC = $this->mysqli->prepare($oc_product_to_categorySQL);
- $stmtOcPCATP->bind_param('si', $product_id, $parent_id);
- $stmtOcPCATC->bind_param('si', $product_id, $category_id);
- // STORE
- $oc_product_to_storeSQL = "REPLACE INTO oc_product_to_store(product_id, store_id) VALUES (?, ?)";
- $stmtOcPSTORE = $this->mysqli->prepare($oc_product_to_storeSQL);
- $stmtOcPSTORE->bind_param('si', $product_id, $store_id);
- while($row = $getData->fetch_array(MYSQLI_ASSOC))
- {
- $category_id = '9999';
- $parent_id = '0';
- $catIDS = $this->getBUSCategoryID($row['category'], $row['subcategory']);
- if($catIDS != false)
- {
- $parent_id = $catIDS['parent_id'];
- $category_id = $catIDS['category_id'];
- }
- if ( ! in_array( $category_id, $this->banned_cats))
- {
- $product_id = $this->tools->convert_to_ascii_sum($row['id']); //$nextID['ID'];//'EWE_'.$row['id'];
- $BarcodeValue = $this->mysqli->real_escape_string($row['ean']);
- $ima = "SELECT product_id FROM oc_product WHERE product_id = '".$product_id."'";
- // echo $ima.'<br>';
- $jelima = $this->mysqli->query($ima);
- if($jelima->num_rows > 0)
- {
- $jelima->close();
- continue;
- }
- else
- {
- if($BarcodeValue != '')
- {
- $qImaBarkod = "SELECT product_id FROM oc_product WHERE ean = '".$BarcodeValue."'";
- $result = $this->mysqli->query($qImaBarkod);
- if($result->num_rows > 0)
- {
- $jelima->close();
- continue;
- }
- }
- $language_sr = 4;
- $language_en = 1;
- $store_id = 0;
- $getmanufacturerID = "SELECT manufacturer_id FROM oc_manufacturer WHERE name LIKE '%".$row['manufacturer']."%' LIMIT 0,1";
- $mID = '';
- $manID = $this->mysqli->query($getmanufacturerID);
- $maID = $manID->fetch_array(MYSQLI_ASSOC);
- $manufacturer_id = $maID['manufacturer_id'];
- $catIDS = false;
- $getNextID = "SELECT max(product_id) + 1 AS ID FROM oc_product";
- $rsNextID = $this->mysqli->query($getNextID);
- $nextID = $rsNextID->fetch_array(MYSQLI_ASSOC);
- // $product_id = $this->tools->convert_to_ascii_sum($row['id']); //$nextID['ID'];//'EWE_'.$row['id'];
- $model = '';
- if (strpos($row['manufacturer'], $row['name']) !== false)
- $name = $row['name'];
- else $name = $row['manufacturer'].' '.$row['name'];
- $short_description = '';
- $ean = $row['ean'];
- $quantity = 0;
- $stock_status_id = 6;
- $image = "";
- if(!empty($row['image']) && $row['image'] != '')
- {
- $imagename = substr($row['image'], strrpos($row['image'], '/') + 1);
- $image = $this->ewe_local_image.$imagename;
- // if(!file_exists($image))
- // {
- // $EWEimage = file_get_contents($row['image']);
- // file_put_contents($image, $EWEimage);
- // }
- }
- if(!empty($row['recommended_retail_price']) && $row['recommended_retail_price'] != '')
- {
- $price = $row['recommended_retail_price'];
- }
- else
- {
- $price = $row['price_rebate'];
- if($price < 500)
- $newPrc = $price * 1.2 * 1.3;
- else if($price > 499 && $price < 1000)
- $newPrc = $price * 1.2 * 1.25;
- else if($price > 999 && $price < 3000)
- $newPrc = $price * 1.2 * 1.2;
- else if($price > 2999 && $price < 5000)
- $newPrc = $price * 1.2 * 1.15;
- else if($price > 4999 && $price < 20000)
- $newPrc = $price * 1.2 * 1.12;
- else if($price > 19999 && $price < 29000)
- $newPrc = $price * 1.2 * 1.1;
- else if($price > 28999 && $price < 49999)
- $newPrc = $price * 1.2 * 1.07;
- else if($price > 49999 && $price < 99999)
- $newPrc = $price * 1.2 * 1.03;
- else if($price > 99999)
- $newPrc = $price * 1.2 * 1.02;
- $price = $newPrc;
- }
- $price_retail = $price / 0.87;
- $price_retail = round($price_retail, 0, PHP_ROUND_HALF_UP);
- $price_retail = ((int) ($price_retail/10)) * 10;
- $price = round($price, 0, PHP_ROUND_HALF_UP);
- $price = ((int) ($price/10)) * 10;
- $status = 1;
- if($price == 0) $status = 0;
- $imported_from = 'EWE';
- // print_r($stmtOcP);
- if(!$stmtOcP->execute())
- return $stmtOcP->error;
- else
- {
- $qUpdateProductID = "UPDATE oc_product_ewe SET product_id = ".$product_id." WHERE id = '".$row['id']."'";
- $rsUpdateProductID = $this->mysqli->query($qUpdateProductID);
- }
- if(!$stmtOcPDSR->execute())
- return $stmtOcPDSR->error;
- if(!$stmtOcPDEN->execute())
- return $stmtOcPDEN->error;
- $qImaKat = "select count(*) as broj_definisanih_kategorija from oc_product_to_category where product_id = ".$product_id;//." and category_id = ".$rows['SIFRA_KATEGORIJE'];
- $imaKat = $this->mysqli->query($qImaKat);
- $provera = $imaKat->fetch_array(MYSQLI_ASSOC);
- if($provera['broj_definisanih_kategorija'] == 0)
- {
- if(!$stmtOcPCATP->execute())
- return $stmtOcPCATP->error;
- if(!$stmtOcPCATC->execute())
- return $stmtOcPCATC->error;
- }
- else
- {
- // echo " NE dodajem kategoriju<br>";
- }
- $imaKat->close();
- if(!$stmtOcPSTORE->execute())
- return $stmtOcPSTORE->error;
- // $oc_product_to_category_parent = "REPLACE INTO oc_product_to_category(product_id, category_id) VALUES (".$product_id.", ".$parent_id.")";
- // $oc_product_to_category = "REPLACE INTO oc_product_to_category(product_id, category_id) VALUES (".$product_id.", ".$category_id.")";
- // $this->mysqli->query($oc_product_to_category_parent);
- // $this->mysqli->query($oc_product_to_category);
- // if($product_id == 1829){
- //// echo $product_id.'. '.$name.' ('.$parent_id.', '.$category_id.')<br>';
- //// echo $product_id.'. '.$name.' ('.$row['category'].' / '.$row['subcategory'].')<br><br>';
- //print_r($stmtOcPCATC.'<br>');
- // }
- $i++;
- if(($i % 500) === 0) $this->mysqli->commit();
- }
- }
- }
- $this->mysqli->commit();
- $getData->close();
- return true;
- }
- else return "NEMA STA ZA UNETI";
- }
- public function insertCategories()
- {
- $sql = "select distinct subcategory, category from oc_product_ewe";
- $result = $this->mysqli->query($sql);
- $i = 0;
- $megaCatInsert = "INSERT INTO oc_category_ewe (category, subcategory) VALUES (?, ?)";
- $stmt = $this->mysqli->prepare($megaCatInsert);
- $stmt->bind_param('ss', $category, $subcategory);
- // echo "Uradjena priprema";
- while($row = $result->fetch_array(MYSQLI_ASSOC))
- {
- try {
- $category = $row['category'];
- $subcategory = $row['subcategory'];
- $i++;
- // echo $megaCatInsert . ': ';
- // echo $category;
- // echo $subcategory . '\r';
- $qImaKategoriju = "SELECT count(*) FROM oc_category_ewe WHERE category = '".$category."' AND subcategory = '".$subcategory."'";
- $rsImaKategoriju = $this->mysqli->query($qImaKategoriju);
- if($rsImaKategoriju->num_rows == 0){
- if(!$stmt->execute()){
- echo $stmt->error;
- return $stmt->error;
- }
- }
- $i++;
- if(($i % 50) === 0) $this->mysqli->commit();
- } catch (Exception $e) {}
- }
- $this->mysqli->commit();
- //echo "Kraj unosa kategorija";
- return true;
- }
- public function doom()
- {
- $del1 = "TRUNCATE TABLE oc_product_ewe";
- $goblin = $this->mysqli->query($del1);
- if(!$goblin) return false;
- else return true;
- }
- // VRACA BUS COMPUTERS ID KATEGORIJA
- public function getBUSCategoryID($category, $subcategory)
- {
- // $query = "SELECT category_id FROM oc_category_ewe_bus INNER JOIN oc_category_ewe cc ON ewe_category_id = cc.id WHERE cc.category = ? AND cc.subcategory = ?";
- // if ($stmt = $this->mysqli->prepare($query)){
- // $stmt->bind_param("ss", $category, $subcategory);
- // if($stmt->execute()){
- // $stmt->store_result();
- // $email_check= "";
- // $stmt->bind_result($email_check);
- // $stmt->fetch();
- // if ($stmt->num_rows == 1){
- // echo "That Email already exists.";
- // exit;
- // }
- // }
- // else return false;
- // }
- $result = array();
- $getCAT = "SELECT category_id FROM oc_category_ewe_bus INNER JOIN oc_category_ewe cc ON ewe_category_id = cc.id WHERE cc.category = '".$category."' AND cc.subcategory = '".$subcategory."'";
- $catID = $this->mysqli->query($getCAT);
- if($catID->num_rows > 0)
- {
- $rCatID = $catID->fetch_array(MYSQLI_ASSOC);
- $result['category_id'] = $rCatID['category_id'];
- $parentIDsql = "SELECT parent_id FROM oc_category WHERE category_id = '".$result['category_id']."'";
- $parcatID = $this->mysqli->query($parentIDsql);
- $pcID = $parcatID->fetch_array(MYSQLI_ASSOC);
- $result['parent_id'] = $pcID['parent_id'];
- return $result;
- }
- else return false;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement