Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.72 KB | None | 0 0
  1. <?php
  2.  
  3. include_once('tools.php');
  4.  
  5. Class Ewe
  6.  
  7. {
  8.  
  9.     protected $mysqli = '';
  10.     protected $tools = NULL;
  11.     protected $oc_productSQL             = '';
  12.     protected $oc_product_descriptionSQL = '';
  13.     protected $oc_product_to_categorySQL = '';
  14.     protected $oc_product_to_storeSQL    = '';
  15.     protected $insertakcija_sql          = '';
  16.     protected $akc                       = 0;
  17.     protected $updaterasp                = array();
  18.     protected $ewe_local_image           = 'data/ewe/';
  19.     protected $ewe_image_path            = 'https://www.ewe.rs/slike-proizvoda';
  20.     protected $path                      = "/var/www/clients/client0/web1/web/sync/ewe/";
  21.     protected $username                  = 'bus';
  22.     protected $password                  = '6dc50';
  23.     protected $banned_cats               = array();
  24.     // protected $banned_cats               = array('91', '172', '171', '160', '159', '136', '137', '148', '146', '138', '140', '145', '139', '147', '141', '142', '143', '144');
  25.  
  26.     public function __construct ()
  27.     {
  28.  
  29.         try {
  30.  
  31.                
  32.                 $db_database = 'db_empty';
  33.                 $db_hostname = '46.174.100.19';
  34.                 $db_username = 'root';
  35.                 $db_password = '19busweb123';
  36.  
  37.                 $this->mysqli = mysqli_connect($db_hostname, $db_username, $db_password, $db_database) or die("Unable to connect to mysqli: ". mysqli_error());
  38.                 $this->mysqli->set_charset("utf8");
  39.                 $this->mysqli->query('set name utf8');
  40.             }
  41.             catch (PDOException $e)
  42.             {
  43.                 echo $e->getMessage();
  44.             }
  45.  
  46.  
  47.     }
  48.  
  49.     public function set_tools($tools)
  50.     {
  51.         $this->tools = $tools; 
  52.     }
  53.  
  54.     public function test()
  55.     {
  56.         $url = 'http://www.ewe.rs';
  57.         $ch = curl_init($url);
  58.         curl_setopt($ch, CURLOPT_NOBODY, true);
  59.         //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  60.         curl_exec($ch);
  61.         $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  62.         curl_close($ch);
  63.         if (200==$retcode) {
  64.             return true;
  65.         } else {
  66.             return true;
  67.         }
  68.     }
  69.  
  70.         public function run()
  71.         {
  72.  
  73.             $isDoomed = $this->doom();
  74.  
  75.         if($isDoomed == true)
  76.         {
  77.             $getEwe = $this->getData();
  78.  
  79.             if($getEwe == true)
  80.             {
  81.                 //echo "Pre inserta Ewe\r";
  82.                 $insEWE = $this->insertData();
  83.  
  84.                 if($insEWE == true)
  85.                 {
  86.                     $isnertIntoShop = $this->insertEwe();
  87.                     if($isnertIntoShop)
  88.                     {
  89.                         // echo "Uradio insert Ewe\r";
  90.                         $insertEweCategories = $this->insertCategories();
  91.                        
  92.                         if($insertEweCategories)
  93.                         {
  94.                             //echo "Ubacio sam kategorije";
  95.                             return true;
  96.                         }
  97.                     }
  98.                     else return ("GRESKA KOD UNOSA EWE ARTIKALA U SHOP: ".$isnertIntoShop);
  99.                 }
  100.                 else return ("GRESKA KOD UNOSA EWE ARTIKALA: ".$insEWE);
  101.             }
  102.             else return ("GRESKA KOD POVLACENJA EWE ARTIKALA");
  103.         }
  104.             else return ("GRESKA KOD BRISANJA EWE PROIZVODA");
  105.         }
  106.  
  107.         public function getData()
  108.         {
  109.  
  110.             $url  = 'http://api.ewe.rs/share/backend/?user='.$this->username.'&secretcode='.$this->password.'&images=1';
  111.  
  112.  
  113.             $ch =curl_init();
  114.             curl_setopt($ch, CURLOPT_URL, $url);
  115.             curl_setopt($ch, CURLOPT_VERBOSE, 1);
  116.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  117.             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
  118.  
  119.             $result   = curl_exec ($ch);
  120.             $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);     
  121.             $file     = $this->path.'ewe.xml';  
  122.             if($httpcode==200)
  123.               {
  124.                 curl_close ($ch);
  125.                 if (file_exists($file))
  126.                   {
  127.                         $file0 = fopen($file, 'w+');
  128.                         fwrite($file0, $result);
  129.                         fclose($file0);
  130.                         // $config = array(
  131.                   //          'indent'       => true,
  132.                   //          'input-xml'    => true,
  133.                   //          'output-xml'       => true,
  134.                   //          'output-encoding' => 'UTF-8',
  135.                         //    'input-encoding'  => 'UTF-8',
  136.                   //          'wrap'         => false);
  137.  
  138.                   //    $tidy = new tidy;
  139.                         // $tidy->parseFile($file, $config);
  140.                         // $tidy->cleanRepair();
  141.  
  142.  
  143.                         // $file1 = fopen($file, 'w+');
  144.                         // fwrite($file1, $tidy);
  145.                         // fclose($file1);
  146.  
  147.  
  148.                         return true;
  149.                   }
  150.                 else
  151.                   {
  152.                     exit('Error.');
  153.                   }
  154.               }
  155.               else
  156.               {
  157.                  echo curl_error($ch);
  158.  
  159.               }
  160.  
  161.  
  162.         }
  163.  
  164.         public function insertData()
  165.         {
  166.             $data      = array();
  167.             $xmlp      = simplexml_load_file($this->path.'ewe.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
  168.             $i         = 0;
  169.  
  170.             $this->mysqli->autocommit(FALSE);
  171.  
  172.             $megaInsert    = "REPLACE INTO oc_product_ewe(id, manufacturer, name, image, category, subcategory, price, price_rebate, recommended_retail_price, vat, ean, product_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  173.             $stmt          = $this->mysqli->prepare($megaInsert);
  174.             $stmt->bind_param('ssssssddddsi', $id, $manufacturer, $name, $image, $category, $subcategory, $price, $price_rebate, $recommended_retail_price, $vat, $ean, $product_id);
  175.            
  176.             foreach($xmlp as $row)
  177.                 {
  178.  
  179.                     $id             = 'EWE_'.$row->id;
  180.                     $manufacturer   = $row->manufacturer;
  181.                     $name           = $row->name;
  182.                     $category       = $row->category;
  183.                     $subcategory    = $row->subcategory;
  184.                     $price          = floatval(str_replace(',', '.', $row->price));
  185.                     $price_rebate   = floatval(str_replace(',', '.', $row->price_rebate));
  186.                     $vat            = floatval(str_replace(',', '.', $row->vat));
  187.                     $ean            = $row->ean;
  188.  
  189.                     if(isset($row->recommended_retail_price))
  190.                         $recommended_retail_price = $row->recommended_retail_price;
  191.                     else
  192.                         $recommended_retail_price = NULL;
  193.  
  194.                     if(isset($row->images))
  195.                         $image = $row->images[0]->image;
  196.                     else
  197.                         $image = NULL;
  198.  
  199.                     if(!$stmt->execute())
  200.                         return $stmt->error;
  201.                     $i++;
  202.                    
  203.                     if(($i % 500) === 0) $this->mysqli->commit();  
  204.                
  205.                 }
  206.                
  207.             $this->mysqli->commit();
  208.                
  209.             return true;
  210.         }
  211.  
  212.         public function insertEwe()
  213.         {
  214.             $now           = date('Y-m-d H:i:s');
  215.             $dateAvailable = date('Y-m-d');
  216.             $i             = 0;
  217.  
  218.             $getDataSQL  = "SELECT * FROM oc_product_ewe WHERE ean is not null AND ean != ''";
  219.  
  220.             $getData     = $this->mysqli->query($getDataSQL);
  221.            
  222.  
  223.             if($getData->num_rows > 0)
  224.             {
  225.  
  226.                     $this->mysqli->autocommit(FALSE);
  227.  
  228.                     // ARTIKAL
  229.                     $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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  230.                     // $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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  231.                     $stmtOcP       = $this->mysqli->prepare($oc_productSQL);
  232.                     $stmtOcP->bind_param('sssiisiddsiss', $product_id, $model, $ean, $quantity, $stock_status_id, $image, $manufacturer_id, $price, $price_retail, $dateAvailable, $status, $now, $imported_from);
  233.  
  234.  
  235.                    
  236.                     // OPIS - SR / EN
  237.                     $oc_productDescriptionSQL = "REPLACE INTO oc_product_description (product_id, language_id, name, description) VALUES (?, ?, ?, ?)";
  238.                     $stmtOcPDSR               = $this->mysqli->prepare($oc_productDescriptionSQL);
  239.                     $stmtOcPDEN               = $this->mysqli->prepare($oc_productDescriptionSQL);
  240.                     $stmtOcPDSR->bind_param('siss', $product_id, $language_sr, $name, $short_description);
  241.                     $stmtOcPDEN->bind_param('siss', $product_id, $language_en, $name, $short_description);
  242.  
  243.                     // KATEGORIJE - PARENT / CHILD                 
  244.                     $oc_product_to_categorySQL   = "REPLACE INTO oc_product_to_category(product_id, category_id) VALUES (?, ?)";
  245.                     $stmtOcPCATP                 = $this->mysqli->prepare($oc_product_to_categorySQL);
  246.                     $stmtOcPCATC                 = $this->mysqli->prepare($oc_product_to_categorySQL);
  247.                     $stmtOcPCATP->bind_param('si', $product_id, $parent_id);
  248.                     $stmtOcPCATC->bind_param('si', $product_id, $category_id);
  249.  
  250.  
  251.                     // STORE
  252.                     $oc_product_to_storeSQL      = "REPLACE INTO oc_product_to_store(product_id, store_id) VALUES (?, ?)";
  253.                     $stmtOcPSTORE                = $this->mysqli->prepare($oc_product_to_storeSQL);
  254.                     $stmtOcPSTORE->bind_param('si', $product_id, $store_id);
  255.                    
  256.                     while($row = $getData->fetch_array(MYSQLI_ASSOC))
  257.                     {  
  258.  
  259.                         $category_id           = '9999';                               
  260.                         $parent_id             = '0';
  261.                         $catIDS                = $this->getBUSCategoryID($row['category'], $row['subcategory']);
  262.                        
  263.                         if($catIDS != false)
  264.                         {
  265.                             $parent_id   = $catIDS['parent_id'];
  266.                             $category_id = $catIDS['category_id'];
  267.                         }
  268.  
  269.                         if ( ! in_array( $category_id, $this->banned_cats))
  270.                         {
  271.                                 $product_id        = $this->tools->convert_to_ascii_sum($row['id']); //$nextID['ID'];//'EWE_'.$row['id'];
  272.                                 $BarcodeValue      = $this->mysqli->real_escape_string($row['ean']);
  273.                                 $ima    =  "SELECT product_id FROM oc_product WHERE product_id = '".$product_id."'";
  274.                                 // echo $ima.'<br>';
  275.                                 $jelima =  $this->mysqli->query($ima);
  276.                                 if($jelima->num_rows > 0)
  277.                                 {
  278.                                     $jelima->close();
  279.                                     continue;
  280.                                 }
  281.                                 else
  282.                                 {
  283.                                     if($BarcodeValue != '')
  284.                                     {
  285.                                         $qImaBarkod    =  "SELECT product_id FROM oc_product WHERE ean = '".$BarcodeValue."'";
  286.                                         $result =  $this->mysqli->query($qImaBarkod);
  287.                                         if($result->num_rows > 0)
  288.                                         {
  289.                                             $jelima->close();
  290.                                             continue;
  291.                                         }
  292.                                     }
  293.                                    
  294.                                     $language_sr           = 4;
  295.                                     $language_en           = 1;
  296.  
  297.                                     $store_id              = 0;
  298.  
  299.                                     $getmanufacturerID     = "SELECT manufacturer_id FROM oc_manufacturer WHERE name LIKE '%".$row['manufacturer']."%' LIMIT 0,1";
  300.                                     $mID                   = '';
  301.                                     $manID                 = $this->mysqli->query($getmanufacturerID);
  302.                                     $maID                  = $manID->fetch_array(MYSQLI_ASSOC);
  303.                                     $manufacturer_id       = $maID['manufacturer_id'];
  304.                                     $catIDS                = false;
  305.                                    
  306.                                     $getNextID         = "SELECT max(product_id) + 1 AS ID FROM oc_product";
  307.                                     $rsNextID          = $this->mysqli->query($getNextID);
  308.                                     $nextID            = $rsNextID->fetch_array(MYSQLI_ASSOC);
  309.                                     // $product_id     = $this->tools->convert_to_ascii_sum($row['id']); //$nextID['ID'];//'EWE_'.$row['id'];
  310.                                     $model             = '';
  311.  
  312.                                     if (strpos($row['manufacturer'], $row['name']) !== false)
  313.                                          $name         = $row['name'];
  314.                                     else $name         = $row['manufacturer'].' '.$row['name'];
  315.                                     $short_description = '';
  316.                                     $ean               = $row['ean'];
  317.                                     $quantity          = 0;
  318.                                     $stock_status_id   = 6;
  319.                                     $image             = "";
  320.                        
  321.                                     if(!empty($row['image']) && $row['image'] != '')
  322.                                     {
  323.                                         $imagename = substr($row['image'], strrpos($row['image'], '/') + 1);
  324.                                         $image     = $this->ewe_local_image.$imagename;
  325.                                        
  326.                                         // if(!file_exists($image))
  327.                                         // {
  328.                                             // $EWEimage = file_get_contents($row['image']);
  329.                                             // file_put_contents($image, $EWEimage);
  330.                                         // }
  331.                                     }
  332.  
  333.                                     if(!empty($row['recommended_retail_price']) && $row['recommended_retail_price'] != '')
  334.                                     {
  335.                                         $price     = $row['recommended_retail_price'];
  336.                                     }
  337.                                     else
  338.                                     {
  339.  
  340.                                         $price     = $row['price_rebate'];
  341.  
  342.                                         if($price < 500)
  343.                                             $newPrc   = $price * 1.2 * 1.3;
  344.                                         else if($price > 499 && $price < 1000)
  345.                                             $newPrc   = $price * 1.2 * 1.25;
  346.                                         else if($price > 999 && $price < 3000)
  347.                                             $newPrc   = $price * 1.2 * 1.2;
  348.                                         else if($price > 2999 && $price < 5000)
  349.                                             $newPrc   = $price * 1.2 * 1.15;
  350.                                         else if($price > 4999 && $price < 20000)
  351.                                             $newPrc   = $price * 1.2 * 1.12;
  352.                                         else if($price > 19999 && $price < 29000)
  353.                                             $newPrc   = $price * 1.2 * 1.1;
  354.                                         else if($price > 28999 && $price < 49999)
  355.                                             $newPrc   = $price * 1.2 * 1.07;
  356.                                         else if($price > 49999 && $price < 99999)
  357.                                             $newPrc   = $price * 1.2 * 1.03;
  358.                                         else if($price > 99999)
  359.                                             $newPrc   = $price * 1.2 * 1.02;
  360.  
  361.  
  362.                                         $price         = $newPrc;
  363.                
  364.                                     }
  365.  
  366.                                     $price_retail  = $price / 0.87;
  367.                                     $price_retail  = round($price_retail, 0, PHP_ROUND_HALF_UP);
  368.                                     $price_retail  = ((int) ($price_retail/10)) * 10;
  369.  
  370.                                     $price         = round($price, 0, PHP_ROUND_HALF_UP);
  371.                                     $price         = ((int) ($price/10)) * 10;
  372.  
  373.                                     $status        = 1;
  374.                                     if($price == 0) $status = 0;
  375.                                    
  376.                                     $imported_from = 'EWE';
  377.  
  378.                                     // print_r($stmtOcP);
  379.                                     if(!$stmtOcP->execute())
  380.                                         return $stmtOcP->error;
  381.                                     else
  382.                                     {
  383.                                         $qUpdateProductID               = "UPDATE oc_product_ewe SET product_id = ".$product_id." WHERE id = '".$row['id']."'";
  384.                                         $rsUpdateProductID              = $this->mysqli->query($qUpdateProductID);
  385.                                     }
  386.  
  387.                                     if(!$stmtOcPDSR->execute())
  388.                                         return $stmtOcPDSR->error;
  389.  
  390.                                     if(!$stmtOcPDEN->execute())
  391.                                         return $stmtOcPDEN->error;
  392.  
  393.                                     $qImaKat = "select count(*) as broj_definisanih_kategorija from oc_product_to_category where product_id = ".$product_id;//." and category_id = ".$rows['SIFRA_KATEGORIJE'];
  394.                                     $imaKat = $this->mysqli->query($qImaKat);
  395.                                     $provera = $imaKat->fetch_array(MYSQLI_ASSOC);
  396.                                     if($provera['broj_definisanih_kategorija'] == 0)
  397.                                     {
  398.                                         if(!$stmtOcPCATP->execute())
  399.                                             return $stmtOcPCATP->error;
  400.  
  401.                                         if(!$stmtOcPCATC->execute())
  402.                                             return $stmtOcPCATC->error;
  403.                                     }
  404.                                     else
  405.                                     {
  406.                                         // echo "&nbsp;&nbsp;&nbsp;&nbsp;NE dodajem kategoriju<br>";
  407.                                     }
  408.                                     $imaKat->close();
  409.  
  410.                                     if(!$stmtOcPSTORE->execute())
  411.                                         return $stmtOcPSTORE->error;
  412.  
  413.                                    
  414.                                     // $oc_product_to_category_parent   = "REPLACE INTO oc_product_to_category(product_id, category_id) VALUES (".$product_id.", ".$parent_id.")";
  415.                                     // $oc_product_to_category          = "REPLACE INTO oc_product_to_category(product_id, category_id) VALUES (".$product_id.", ".$category_id.")";
  416.                                    
  417.                                     // $this->mysqli->query($oc_product_to_category_parent);
  418.                                     // $this->mysqli->query($oc_product_to_category);
  419.                                     // if($product_id == 1829){
  420.                                         //// echo $product_id.'. '.$name.' ('.$parent_id.', '.$category_id.')<br>';
  421.                                         //// echo $product_id.'. '.$name.' ('.$row['category'].' / '.$row['subcategory'].')<br><br>';
  422.                                         //print_r($stmtOcPCATC.'<br>');
  423.                                     // }
  424.                                    
  425.                                     $i++;
  426.                                     if(($i % 500) === 0) $this->mysqli->commit();
  427.  
  428.                              }
  429.                         }
  430.                            
  431.                     }
  432.  
  433.                     $this->mysqli->commit();
  434.                     $getData->close();
  435.  
  436.            
  437.                     return true;
  438.             }
  439.             else return "NEMA STA ZA UNETI";
  440.  
  441.  
  442.         }
  443.  
  444.         public function insertCategories()
  445.         {
  446.             $sql    = "select distinct subcategory, category from oc_product_ewe";
  447.             $result = $this->mysqli->query($sql);
  448.             $i      = 0;
  449.  
  450.             $megaCatInsert = "INSERT INTO oc_category_ewe (category, subcategory) VALUES (?, ?)";
  451.             $stmt          = $this->mysqli->prepare($megaCatInsert);
  452.             $stmt->bind_param('ss', $category, $subcategory);
  453.  
  454.             // echo "Uradjena priprema";
  455.             while($row = $result->fetch_array(MYSQLI_ASSOC))
  456.             {  
  457.                 try {
  458.                     $category    = $row['category'];
  459.                     $subcategory = $row['subcategory'];
  460.                     $i++;
  461.                    
  462.                     // echo $megaCatInsert . ': ';
  463.                     // echo $category;
  464.                     // echo $subcategory . '\r';
  465.                
  466.                     $qImaKategoriju = "SELECT count(*) FROM oc_category_ewe WHERE category = '".$category."' AND subcategory = '".$subcategory."'";
  467.                     $rsImaKategoriju = $this->mysqli->query($qImaKategoriju);
  468.                    
  469.                     if($rsImaKategoriju->num_rows == 0){
  470.                         if(!$stmt->execute()){
  471.                             echo $stmt->error;
  472.                             return $stmt->error;
  473.                         }
  474.                     }
  475.                     $i++;
  476.                    
  477.                     if(($i % 50) === 0) $this->mysqli->commit();
  478.                 } catch (Exception $e) {}
  479.             }
  480.            
  481.             $this->mysqli->commit();
  482.            
  483.             //echo "Kraj unosa kategorija";
  484.            
  485.             return true;
  486.         }
  487.  
  488.  
  489.         public function doom()
  490.         {
  491.             $del1  = "TRUNCATE TABLE oc_product_ewe";
  492.             $goblin = $this->mysqli->query($del1);
  493.             if(!$goblin) return false;
  494.             else return true;
  495.         }
  496.  
  497.  
  498.         // VRACA BUS COMPUTERS ID KATEGORIJA
  499.         public function getBUSCategoryID($category, $subcategory)
  500.         {
  501.  
  502.             // $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 = ?";
  503.  
  504.             // if ($stmt = $this->mysqli->prepare($query)){
  505.  
  506.                     // $stmt->bind_param("ss", $category, $subcategory);
  507.  
  508.             //         if($stmt->execute()){
  509.             //             $stmt->store_result();
  510.  
  511.             //             $email_check= "";        
  512.             //             $stmt->bind_result($email_check);
  513.             //             $stmt->fetch();
  514.  
  515.             //             if ($stmt->num_rows == 1){
  516.  
  517.             //             echo "That Email already exists.";
  518.             //             exit;
  519.  
  520.             //             }
  521.             //         }
  522.             //         else return false;
  523.             //     }
  524.  
  525.             $result = array();
  526.             $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."'";
  527.             $catID  = $this->mysqli->query($getCAT);
  528.  
  529.             if($catID->num_rows > 0)
  530.             {
  531.                 $rCatID = $catID->fetch_array(MYSQLI_ASSOC);
  532.                 $result['category_id'] = $rCatID['category_id'];
  533.  
  534.                 $parentIDsql = "SELECT parent_id FROM oc_category WHERE category_id = '".$result['category_id']."'";
  535.                 $parcatID    = $this->mysqli->query($parentIDsql);
  536.                 $pcID        = $parcatID->fetch_array(MYSQLI_ASSOC);
  537.                 $result['parent_id'] = $pcID['parent_id'];
  538.  
  539.                 return $result;
  540.             }  
  541.             else return false;
  542.  
  543.         }
  544.  
  545. }
  546.  
  547.  
  548. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement