Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5.  
  6. function ParseXml($Url)
  7. {
  8. $xml = new SimpleXMLElement(file_get_contents($Url));
  9. return $xml->list->Product;
  10. }
  11. function DB()
  12. {
  13. $servername = "localhost";
  14. $username = "root";
  15. $password = "";
  16. $dbname = "cscart";
  17. // Create connection
  18. $conn = new mysqli($servername, $username, $password, $dbname);
  19. // Check connection
  20. if ($conn->connect_error) {
  21. die("Connection failed: " . $conn->connect_error);
  22. }
  23. mysqli_set_charset($conn, 'utf8');
  24. return $conn;
  25. }
  26. function GetProducts($SupplierID)
  27. {
  28. $conn = DB();
  29. $sql = "SELECT cscart_products.`product_code`,
  30. cscart_suppliers.`name`,
  31. cscart_products.`product_id`,
  32. cscart_suppliers.`supplier_code`,
  33. cscart_product_descriptions.`lang_code`,
  34. cscart_product_prices.`price`,
  35. cscart_products.`amount`
  36. FROM
  37. cscart_suppliers
  38. LEFT OUTER JOIN cscart_products ON cscart_suppliers.`supplier_code` = cscart_products.`supplier_code`
  39. LEFT OUTER JOIN cscart_product_descriptions ON cscart_products.`product_id` = cscart_product_descriptions.`product_id` AND cscart_product_descriptions.`lang_code`='ka'
  40. LEFT OUTER JOIN cscart_product_prices ON cscart_products.`product_id` = cscart_product_prices.`product_id`
  41. WHERE cscart_suppliers.`supplier_code`=" . $SupplierID . " AND cscart_products.product_id =13838";
  42. $result = $conn->query($sql);
  43. $Array = [];
  44. if ($result->num_rows > 0) {
  45. // output data of each row
  46. while($row = $result->fetch_assoc())
  47. {
  48. $Array[trim($row['product_code'])] = $row;
  49. }
  50. }
  51. $conn->close();
  52. return $Array;
  53. }
  54.  
  55. function PrintHtml($Amount, $Product)
  56. {
  57. echo '<tr><td>'.$Product['name'].'</td><td ><a>' . $Product['product_code'] . '</a></td>
  58. <td>' .$Product['amount']. ' </td> <td>!=</td> <td >'.$Product['quantity'].'</td><td>
  59. <div class="input-group mb-3">
  60. <input type="text" class="form-control" placeholder="'.$Product['quantity'].'" aria-label="Recipient" aria-describedby="basic-addon2">
  61. <div class="input-group-append">
  62. <button class="btn btn-outline-secondary" type="button">განაახლე</button>
  63. </div>
  64. </div></td><td>'.$Product['price'].'</td><td>
  65.  
  66. <div class="input-group mb-3">
  67. <input type="text" class="form-control" placeholder="'.$Product['product_code'].'" aria-label="Recipient" aria-describedby="basic-addon2">
  68. <div class="input-group-append">
  69. <button class="btn btn-outline-secondary" type="button">განაახლე</button>
  70. </div>
  71. </div></td></tr>';
  72. }
  73.  
  74. function UpdateProduct($XmlData, $Product)
  75. {
  76. $conn = DB();
  77.  
  78. if((int)$XmlData->quantity != $Product['amount']){
  79. $res = $conn->query('UPDATE cscart_products
  80. SET
  81. amount = ' . (int)$XmlData->quantity . ',
  82. status = ' . ((int)$XmlData->quantity == 0 ? "'D'" : "'A'") . ',
  83. product_code = "'. $Product['product_code'].'",
  84. updated_timestamp = now()
  85. WHERE product_id = ' . $Product['product_id'] . ' ');
  86. }
  87.  
  88. if((float)$XmlData->price > 0 && (float)$XmlData->price != $Product['price']){
  89. $res = $conn->query('UPDATE cscart_product_prices
  90. SET
  91. price = ' . (float)$XmlData->price . '
  92. WHERE product_id = ' . $Product['product_id'] . ' ');
  93. }
  94.  
  95. }
  96.  
  97. function UpdateProductDates($IDs)
  98. {
  99. $conn = DB();
  100. //$conn->query('UPDATE cscart_products SET compare_date = now() WHERE product_id IN(' . implode($ID, ',') . ') ');
  101. }
  102.  
  103. /*function InsertProduct($Product, $SupplierID)
  104. {
  105. $conn = DB();
  106.  
  107. $res = $conn->query('INSERT IGNORE INTO cscart_products
  108. SET
  109. amount = ' . $Product->quantity . ',
  110. status = "D",
  111. gigantid = ' . $Product->ID . ',
  112. product_code = "'.$Product->model.'",
  113. supplier_code = "' . $SupplierID . '",
  114. timestamp = now(), updated_timestamp = now()') or die('error');
  115.  
  116.  
  117. if($res){
  118. $ProductID = $conn->insert_id;
  119. $conn->query('INSERT INTO cscart_product_prices
  120. SET
  121. product_id = ' . $ProductID . ',
  122. lower_limit = 1,
  123. price = ' . $Product->price . ' ');
  124. $conn->query('INSERT INTO cscart_product_descriptions
  125. SET
  126. product_id = ' . $ProductID . ',
  127. lang_code = "ka",
  128. product = "' . $Product->model . '" ');
  129. $conn->query('INSERT INTO cscart_product_descriptions
  130. SET
  131. product_id = ' . $ProductID . ',
  132. lang_code = "en",
  133. product = "' . $Product->model . '" ');
  134. }
  135.  
  136.  
  137. }*/
  138.  
  139. function Sync($SupplierID)
  140. {
  141. $Products = GetProducts($SupplierID);
  142. //print_r($Products);exit;
  143. //sxva magaziebzec rom gamoikeno es kodi ise davcerot
  144. $XmlData = ParseXml('rss.xml');
  145. $IDs = [];
  146. mb_internal_encoding('UTF-8');
  147. echo 'product in database: ' . count($Products) .'<br>';
  148. echo 'product in xml: ' . count($XmlData) . '<br>';
  149. $new = 0;
  150. $old = 0;
  151. foreach ($XmlData as $key => $value) {
  152. //echo trim($value->model);
  153. $i = 1;
  154.  
  155. foreach ($Products as $k => $val) {
  156. $search = "[". Clean($val['product_code']). "]";
  157.  
  158. $search = str_replace('(', '\(', $search);
  159. $search = str_replace(')', '\)', $search);
  160. //var_dump($search);
  161. $i++;
  162. if(trim($val['product_code']) != ''){
  163. if( mb_strlen(trim($value->model)) < 32){
  164. $matches = Clean($value->model) == Clean($val['product_code']) ? true : false;
  165. }else{
  166. preg_match($search, Clean($value->model), $matches);
  167. }
  168.  
  169. //echo $val['product_code'] . '</br>';
  170. //print_r($matches);
  171. if($matches){
  172. //print_r($val['price'] . '=' . (float)$value->price);exit;
  173. if($val['amount'] != $value->quantity || $val['price'] != (float)$value->price){
  174. //
  175. $old++;
  176. $val['product_code'] = Clean($value->model);
  177. UpdateProduct($value, $val);
  178. }else{
  179. //
  180. }
  181. break;
  182. }
  183. if(!$matches && $i == count($Products) && $value->quantity != 0){
  184. //echo '<pre>';
  185. //echo Clean($value->model);
  186.  
  187. $value->model = Clean($value->model);
  188. // aq moxdeba damateba
  189. InsertProduct($value, $SupplierID);
  190. $new++;
  191. }
  192.  
  193. }else{
  194. //
  195. }
  196.  
  197. }
  198. }
  199. echo 'new : ' . $new . '</br>';
  200. echo 'old not match : ' . $old;
  201. }
  202.  
  203. function Clean($str)
  204. {
  205. $str = trim($str);
  206. $str = str_replace('?', '', $str);
  207. $str = str_replace(' ', ' ', $str);
  208. $str = str_replace(' ', ' ', $str);
  209. $str = str_replace(' ', ' ', $str);
  210. return $str;
  211. }
  212.  
  213. Sync(131);
  214.  
  215. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement