Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- function ParseXml($Url)
- {
- $xml = new SimpleXMLElement(file_get_contents($Url));
- return $xml->list->Product;
- }
- function DB()
- {
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "cscart";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- mysqli_set_charset($conn, 'utf8');
- return $conn;
- }
- function GetProducts($SupplierID)
- {
- $conn = DB();
- $sql = "SELECT cscart_products.`product_code`,
- cscart_suppliers.`name`,
- cscart_products.`product_id`,
- cscart_suppliers.`supplier_code`,
- cscart_product_descriptions.`lang_code`,
- cscart_product_prices.`price`,
- cscart_products.`amount`
- FROM
- cscart_suppliers
- LEFT OUTER JOIN cscart_products ON cscart_suppliers.`supplier_code` = cscart_products.`supplier_code`
- LEFT OUTER JOIN cscart_product_descriptions ON cscart_products.`product_id` = cscart_product_descriptions.`product_id` AND cscart_product_descriptions.`lang_code`='ka'
- LEFT OUTER JOIN cscart_product_prices ON cscart_products.`product_id` = cscart_product_prices.`product_id`
- WHERE cscart_suppliers.`supplier_code`=" . $SupplierID . " AND cscart_products.product_id =13838";
- $result = $conn->query($sql);
- $Array = [];
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc())
- {
- $Array[trim($row['product_code'])] = $row;
- }
- }
- $conn->close();
- return $Array;
- }
- function PrintHtml($Amount, $Product)
- {
- echo '<tr><td>'.$Product['name'].'</td><td ><a>' . $Product['product_code'] . '</a></td>
- <td>' .$Product['amount']. ' </td> <td>!=</td> <td >'.$Product['quantity'].'</td><td>
- <div class="input-group mb-3">
- <input type="text" class="form-control" placeholder="'.$Product['quantity'].'" aria-label="Recipient" aria-describedby="basic-addon2">
- <div class="input-group-append">
- <button class="btn btn-outline-secondary" type="button">განაახლე</button>
- </div>
- </div></td><td>'.$Product['price'].'</td><td>
- <div class="input-group mb-3">
- <input type="text" class="form-control" placeholder="'.$Product['product_code'].'" aria-label="Recipient" aria-describedby="basic-addon2">
- <div class="input-group-append">
- <button class="btn btn-outline-secondary" type="button">განაახლე</button>
- </div>
- </div></td></tr>';
- }
- function UpdateProduct($XmlData, $Product)
- {
- $conn = DB();
- if((int)$XmlData->quantity != $Product['amount']){
- $res = $conn->query('UPDATE cscart_products
- SET
- amount = ' . (int)$XmlData->quantity . ',
- status = ' . ((int)$XmlData->quantity == 0 ? "'D'" : "'A'") . ',
- product_code = "'. $Product['product_code'].'",
- updated_timestamp = now()
- WHERE product_id = ' . $Product['product_id'] . ' ');
- }
- if((float)$XmlData->price > 0 && (float)$XmlData->price != $Product['price']){
- $res = $conn->query('UPDATE cscart_product_prices
- SET
- price = ' . (float)$XmlData->price . '
- WHERE product_id = ' . $Product['product_id'] . ' ');
- }
- }
- function UpdateProductDates($IDs)
- {
- $conn = DB();
- //$conn->query('UPDATE cscart_products SET compare_date = now() WHERE product_id IN(' . implode($ID, ',') . ') ');
- }
- /*function InsertProduct($Product, $SupplierID)
- {
- $conn = DB();
- $res = $conn->query('INSERT IGNORE INTO cscart_products
- SET
- amount = ' . $Product->quantity . ',
- status = "D",
- gigantid = ' . $Product->ID . ',
- product_code = "'.$Product->model.'",
- supplier_code = "' . $SupplierID . '",
- timestamp = now(), updated_timestamp = now()') or die('error');
- if($res){
- $ProductID = $conn->insert_id;
- $conn->query('INSERT INTO cscart_product_prices
- SET
- product_id = ' . $ProductID . ',
- lower_limit = 1,
- price = ' . $Product->price . ' ');
- $conn->query('INSERT INTO cscart_product_descriptions
- SET
- product_id = ' . $ProductID . ',
- lang_code = "ka",
- product = "' . $Product->model . '" ');
- $conn->query('INSERT INTO cscart_product_descriptions
- SET
- product_id = ' . $ProductID . ',
- lang_code = "en",
- product = "' . $Product->model . '" ');
- }
- }*/
- function Sync($SupplierID)
- {
- $Products = GetProducts($SupplierID);
- //print_r($Products);exit;
- //sxva magaziebzec rom gamoikeno es kodi ise davcerot
- $XmlData = ParseXml('rss.xml');
- $IDs = [];
- mb_internal_encoding('UTF-8');
- echo 'product in database: ' . count($Products) .'<br>';
- echo 'product in xml: ' . count($XmlData) . '<br>';
- $new = 0;
- $old = 0;
- foreach ($XmlData as $key => $value) {
- //echo trim($value->model);
- $i = 1;
- foreach ($Products as $k => $val) {
- $search = "[". Clean($val['product_code']). "]";
- $search = str_replace('(', '\(', $search);
- $search = str_replace(')', '\)', $search);
- //var_dump($search);
- $i++;
- if(trim($val['product_code']) != ''){
- if( mb_strlen(trim($value->model)) < 32){
- $matches = Clean($value->model) == Clean($val['product_code']) ? true : false;
- }else{
- preg_match($search, Clean($value->model), $matches);
- }
- //echo $val['product_code'] . '</br>';
- //print_r($matches);
- if($matches){
- //print_r($val['price'] . '=' . (float)$value->price);exit;
- if($val['amount'] != $value->quantity || $val['price'] != (float)$value->price){
- //
- $old++;
- $val['product_code'] = Clean($value->model);
- UpdateProduct($value, $val);
- }else{
- //
- }
- break;
- }
- if(!$matches && $i == count($Products) && $value->quantity != 0){
- //echo '<pre>';
- //echo Clean($value->model);
- $value->model = Clean($value->model);
- // aq moxdeba damateba
- InsertProduct($value, $SupplierID);
- $new++;
- }
- }else{
- //
- }
- }
- }
- echo 'new : ' . $new . '</br>';
- echo 'old not match : ' . $old;
- }
- function Clean($str)
- {
- $str = trim($str);
- $str = str_replace('?', '', $str);
- $str = str_replace(' ', ' ', $str);
- $str = str_replace(' ', ' ', $str);
- $str = str_replace(' ', ' ', $str);
- return $str;
- }
- Sync(131);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement