Advertisement
Guest User

SageOpenCart

a guest
Jun 24th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.02 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. class SageCart{
  6.  
  7.  
  8.  
  9. function OpenCartConnect($server,$user,$pass,$db){
  10.  
  11. $link = mysqli_connect($server, $user, $pass, $db);
  12.  
  13. if (!$link):
  14.  
  15. echo "Error: Unable to connect to MySQL." . PHP_EOL;
  16.  
  17. echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
  18.  
  19. echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
  20.  
  21. exit;
  22.  
  23. endif;
  24.  
  25. return $link;
  26.  
  27. }
  28.  
  29.  
  30.  
  31. function SageConnect($FBBD,$FBUSER,$FBPASS){
  32.  
  33. if (!($dbh=ibase_connect($FBBD, $FBUSER, $FBPASS))) die('Erro ao conectar: ' . ibase_errmsg());
  34.  
  35. return $dbh;
  36.  
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. function logs($dirlogs){
  44.  
  45. if(!file_exists($dirlogs)):
  46.  
  47. mkdir($dirlogs);
  48.  
  49. endif;
  50.  
  51. $Clog = date("dmY").".log";
  52.  
  53. $flog = $dirlogs."\\".$Clog;
  54.  
  55. $fp = fopen($flog,"a+");
  56.  
  57. }
  58.  
  59.  
  60.  
  61. function ShowHelp(){
  62.  
  63. echo "
  64.  
  65. -----------------------------------------------------------
  66.  
  67.  
  68.  
  69. produtos_erp - Apenas lista produtos ERP
  70.  
  71. produtos_atualiza - Atualiza produtos (ERP -> OpenCart)
  72.  
  73.  
  74.  
  75. categorias_opencart - Lista categorias OpenCart
  76.  
  77. categorias_erp - Lista categorias ERP
  78.  
  79. categorias_compara - Compara mas sem atualizar categorias (ERP para OPENCART)
  80.  
  81. categorias_atualiza - Compara e atualiza lista de categorias (ERP para OPENCART)
  82.  
  83.  
  84.  
  85.  
  86.  
  87. -----------------------------------------------------------
  88.  
  89. \n\n
  90.  
  91. ";
  92.  
  93.  
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. // ----------------------------------------------------------------------------------------- PRODUTOS
  102.  
  103.  
  104.  
  105. function produtos_opencart($mysqli){
  106.  
  107.  
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. function produtos_erp($dbh){
  116.  
  117. $sql = 'SELECT * FROM PRODUTO'; /* PESQUISA CATEGORIAS ERP */
  118.  
  119. $query= ibase_query ($dbh, $sql);
  120.  
  121. $DataR = array();
  122.  
  123. while($produtos = ibase_fetch_object ($query)):
  124.  
  125. $produto = $produtos->DESCRICAO.";-;".$produtos->ID_CATEGORIA.";-;".$produtos->PRECOVENDA3.";-;".$produtos->MARCA;
  126.  
  127. array_push($DataR,$produto);
  128.  
  129. endwhile;
  130.  
  131. return $DataR;
  132.  
  133. ibase_close($dbh);
  134.  
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141. function produtos_atualiza($mysqli,$dbh){
  142.  
  143. $produtos = SageCart::produtos_erp($dbh);
  144.  
  145. $datahora = date('Y-m-d h:m:s');
  146.  
  147. foreach($produtos as $key=>$r):
  148.  
  149. $r = explode(";-;",$r);
  150.  
  151. if($r[0] == ""): $r[0] = "n/a"; endif;
  152.  
  153.  
  154.  
  155. foreach($r as $key=>$tmp):
  156.  
  157. $r[$key] = str_replace("'","",$tmp);
  158.  
  159. $r[$key] = str_replace("´","",$tmp);
  160.  
  161. $r[$key] = str_replace('"','',$tmp);
  162.  
  163. $r[$key] = str_replace(',','',$tmp);
  164.  
  165. $r[$key] = str_replace(';','',$tmp);
  166.  
  167. $r[$key] = str_replace('.','',$tmp);
  168.  
  169.  
  170.  
  171. endforeach;
  172.  
  173.  
  174.  
  175. // Verifica se já existe produto com esse nome
  176.  
  177. $sqlx = "SELECT * FROM `opencart`.`oc_product_description` WHERE name = '".$r[0]."'";
  178.  
  179. $resultt = $mysqli->query($sqlx);
  180.  
  181. $nrow = $resultt->num_rows;
  182.  
  183. if($nrow == 0){
  184.  
  185.  
  186.  
  187.  
  188.  
  189. // Verifica se fabricante existe e pega o ID
  190.  
  191. $sql = "SELECT * FROM oc_manufacturer WHERE name='".$r[3]."'";
  192.  
  193. $result = $mysqli->query($sql)or die(mysqli_error($mysqli));
  194.  
  195. $reg = $result->fetch_array();
  196.  
  197. $row_cnt = $result->num_rows;
  198.  
  199. if($row_cnt == "1"):
  200.  
  201. $manufacturer_id = $reg['manufacturer_id'];
  202.  
  203. else:
  204.  
  205. // Se não existe insere no BD e pega o ID
  206.  
  207. $sql = "INSERT INTO `opencart`.`oc_manufacturer`
  208.  
  209. (`manufacturer_id`, `name`, `image`, `sort_order`)
  210.  
  211. VALUES ('', '".$r[3]."', '', '0');";
  212.  
  213. $rtur = $mysqli->query($sql)or die(mysqli_error($mysqli));
  214.  
  215. $manufacturer_id = $mysqli->insert_id;
  216.  
  217.  
  218.  
  219. $sql2 = "INSERT INTO `opencart`.`oc_manufacturer_to_store`
  220.  
  221. (`manufacturer_id`, `store_id`)
  222.  
  223. VALUES ('".$manufacturer_id."', '0');";
  224.  
  225. //$rtur2 = $mysqli->query($sql2)or die(mysqli_error($mysqli));
  226.  
  227. endif;
  228.  
  229.  
  230.  
  231.  
  232.  
  233. // Cadastra produto
  234.  
  235. $sql = "INSERT INTO `opencart`.`oc_product` (`product_id`, `model`, `sku`, `upc`, `ean`, `jan`, `isbn`, `mpn`, `location`, `quantity`, `stock_status_id`, `image`, `manufacturer_id`, `shipping`, `price`, `points`, `tax_class_id`, `date_available`, `weight`, `weight_class_id`, `length`, `width`, `height`, `length_class_id`, `subtract`, `minimum`, `sort_order`, `status`, `viewed`, `date_added`, `date_modified`) VALUES ('', '', '', '', '', '', '', '', '', '1', '', '', '".$manufacturer_id."', '1', '".$r[2]."', '0', '', '0000-00-00', '0.00000000', '0', '0.00000000', '0.00000000', '0.00000000', '1', '1', '1', '1', '1', '0', '".$datahora."', '".$datahora."')";
  236.  
  237.  
  238.  
  239. if($mysqli->query($sql)): // Se o cadastro da primeira tabela fora Ok cadastra nas outras senão exibe o erro!
  240.  
  241. $oid = $mysqli->insert_id;
  242.  
  243.  
  244.  
  245. // Descrição
  246.  
  247. $sql2 = "INSERT INTO `opencart`.`oc_product_description`
  248.  
  249. (`product_id`, `language_id`, `name`, `description`, `tag`, `meta_title`, `meta_description`, `meta_keyword`)
  250.  
  251. VALUES ('".$oid."', '1', '".$r[0]."', '', '', '', '', '')";
  252.  
  253. if(!$mysqli->query($sql2)): echo mysqli_error($mysqli)."\n";endif;
  254.  
  255.  
  256.  
  257. // Categoria
  258.  
  259. $sql3 = "INSERT INTO `opencart`.`oc_product_to_category`
  260.  
  261. (`product_id`, `category_id`)
  262.  
  263. VALUES ('".$oid."', '".$r[1]."');";
  264.  
  265. if(!$mysqli->query($sql3)): echo mysqli_error($mysqli)."\n";endif;
  266.  
  267.  
  268.  
  269.  
  270.  
  271. // Produto Layout
  272.  
  273. $sql4 = "INSERT INTO `opencart`.`oc_product_to_layout`
  274.  
  275. (`product_id`, `store_id`, `layout_id`)
  276.  
  277. VALUES ('".$oid."', '0', '0')";
  278.  
  279. if(!$mysqli->query($sql4)): echo mysqli_error($mysqli)."\n";endif;
  280.  
  281.  
  282.  
  283. // Produto Loja
  284.  
  285. $sql5 = "INSERT INTO `opencart`.`oc_product_to_store`
  286.  
  287. (`product_id`, `store_id`)
  288.  
  289. VALUES ('".$oid."', '0')";
  290.  
  291. if(!$mysqli->query($sql5)): echo mysqli_error($mysqli)."\n";endif;
  292.  
  293.  
  294.  
  295. echo $r[0]." Ok \n";
  296.  
  297. else:
  298.  
  299. if(@eregi("Duplicate entry",mysqli_error($mysqli))):
  300.  
  301. echo " ".$r[0]." -- ja existe \n";
  302.  
  303. else:
  304.  
  305. echo "Um erro desconhecido: \n\n".mysqli_error($mysqli)."\n";
  306.  
  307. endif;
  308.  
  309. endif;
  310.  
  311.  
  312.  
  313.  
  314.  
  315. }else{
  316.  
  317. echo $r[0]." -- já está cadastrado!\n";
  318.  
  319. }
  320.  
  321. endforeach;
  322.  
  323. ibase_close($dbh);
  324.  
  325. }
  326.  
  327. // ----------------------------------------------------------------------------------------- CATEGORIAS
  328.  
  329.  
  330.  
  331. function categorias_opencart($mysqli){ /* PESQUISA CATEGORIAS OPENCART */
  332.  
  333. $sql = "SELECT * FROM oc_category_description";
  334.  
  335. $result = $mysqli->query($sql)or die(mysqli_error($mysqli));
  336.  
  337. $DataR = array();
  338.  
  339. while($r = $result->fetch_array()):
  340.  
  341. array_push($DataR,$r['name']);
  342.  
  343. endwhile;
  344.  
  345. return $DataR;
  346.  
  347. }
  348.  
  349.  
  350.  
  351. function categorias_erp($dbh){
  352.  
  353. $sql = 'SELECT FIRST 1000 * FROM CATPRODUTO'; /* PESQUISA CATEGORIAS ERP */
  354.  
  355. $query= ibase_query ($dbh, $sql);
  356.  
  357. $DataR = array();
  358.  
  359. while($produtos = ibase_fetch_object ($query)):
  360.  
  361. $DataR[$produtos->ID] = $produtos->DESCRICAO.";-;".$produtos->ID_OWNER;
  362.  
  363. endwhile;
  364.  
  365. return $DataR;
  366.  
  367. ibase_close($dbh);
  368.  
  369. }
  370.  
  371.  
  372.  
  373. function categorias_compara($mysqli,$dbh){ /* PESQUISA CATEGORIAS OPENCART E ERP E COMPARA */
  374.  
  375. $CatOC = SageCart::categorias_opencart($mysqli);
  376.  
  377. $CatERP = SageCart::categorias_erp($dbh);
  378.  
  379.  
  380.  
  381. $DataR = array();
  382.  
  383. foreach($CatERP as $key => $r):
  384.  
  385. $r = explode(';-;',$r);
  386.  
  387. if($r[1] == ""):
  388.  
  389. if(!in_array($r[0], $CatOC)):
  390.  
  391. // array_push($DataR,$r);
  392.  
  393. $DataR[$key] = $r;
  394.  
  395. endif;
  396.  
  397. endif;
  398.  
  399. endforeach;
  400.  
  401. ibase_close($dbh);
  402.  
  403. return $DataR;
  404.  
  405.  
  406.  
  407. }
  408.  
  409.  
  410.  
  411. function categorias_atualiza($mysqli,$dbh){ /* RECUPERA DADOS DE COMPARAÇÃO categcomp() E ATUALIZA OPENCART*/
  412.  
  413. $Comp = SageCart::categorias_compara($mysqli,$dbh);
  414.  
  415. if(count($Comp) > 0):
  416.  
  417. $datahora = date('Y-m-d h:m:s');
  418.  
  419.  
  420.  
  421. foreach($Comp as $key => $r):
  422.  
  423.  
  424.  
  425. $sql_oc_category = "INSERT INTO `opencart`.`oc_category`
  426.  
  427. (`category_id`, `image`, `parent_id`, `top`, `column`, `sort_order`, `status`, `date_added`, `date_modified`)
  428.  
  429. VALUES ('".$key."', '', '0', '1', '1', '0', '1', '".$datahora."', '".$datahora."');";
  430.  
  431. if(!$mysqli->query($sql_oc_category)): echo mysqli_error($mysqli)."\n";endif;
  432.  
  433.  
  434.  
  435. $sql_oc_category_description = "INSERT INTO `opencart`.`oc_category_description`
  436.  
  437. (`category_id`, `language_id`, `name`, `description`, `meta_title`, `meta_description`, `meta_keyword`)
  438.  
  439. VALUES ('".$key."', '1', '".$r[0]."', '', '', '', '')";
  440.  
  441. if(!$mysqli->query($sql_oc_category_description)): echo mysqli_error($mysqli)."\n";endif;
  442.  
  443.  
  444.  
  445. $sql_oc_category_path = "INSERT INTO `opencart`.`oc_category_path` (`category_id`,`path_id`,`level`)
  446.  
  447. VALUES ('".$key."', '".$key."','0')";
  448.  
  449. if(!$mysqli->query($sql_oc_category_path)): echo mysqli_error($mysqli)."\n";endif;
  450.  
  451.  
  452.  
  453. $sql_oc_category_to_layout = "INSERT INTO `opencart`.`oc_category_to_layout`
  454.  
  455. (`category_id`, `store_id`, `layout_id`)
  456.  
  457. VALUES ('".$key."', '0', '0');";
  458.  
  459. if($mysqli->query($sql_oc_category_to_layout)): echo mysqli_error($mysqli)."\n";endif;
  460.  
  461.  
  462.  
  463. $sql_oc_category_to_store = "INSERT INTO `opencart`.`oc_category_to_store` (`category_id`, `store_id`) VALUES ('".$key."', '0');";
  464.  
  465. $result4 = $mysqli->query($sql_oc_category_to_store) or die("erro 4 - ".mysqli_error($mysqli))."\n";
  466.  
  467.  
  468.  
  469. endforeach;
  470.  
  471. ibase_close($dbh);
  472.  
  473. endif;
  474.  
  475.  
  476.  
  477. }
  478.  
  479.  
  480.  
  481.  
  482.  
  483. function subcategorias_atualiza($mysqli,$dbh){
  484.  
  485. $CatERP = SageCart::categorias_erp($dbh);
  486.  
  487.  
  488.  
  489. $datahora = date('Y-m-d h:m:s');
  490.  
  491.  
  492.  
  493. foreach($CatERP as $key=>$r):
  494.  
  495. $r = explode(";-;",$r);
  496.  
  497. if($r[1] != ""):
  498.  
  499. //echo $key."--".$r[0]." - ".$r[1]."\n";
  500.  
  501.  
  502.  
  503.  
  504.  
  505. // ATUALIZA SUBCATEGORIAS
  506.  
  507.  
  508.  
  509.  
  510.  
  511. $sql_oc_category = "INSERT INTO `opencart`.`oc_category`
  512.  
  513. (`category_id`, `image`, `parent_id`, `top`, `column`, `sort_order`, `status`, `date_added`, `date_modified`)
  514.  
  515. VALUES ('".$key."', '', '".$r[1]."', '1', '1', '0', '1', '".$datahora."', '".$datahora."');";
  516.  
  517. if($mysqli->query($sql_oc_category)):
  518.  
  519.  
  520.  
  521. $sql_oc_category_description = "INSERT INTO `opencart`.`oc_category_description`
  522.  
  523. (`category_id`, `language_id`, `name`, `description`, `meta_title`, `meta_description`, `meta_keyword`)
  524.  
  525. VALUES ('".$key."', '1', '".$r[0]."', '1234', '1234', '1234', '1234')";
  526.  
  527. $result1 = $mysqli->query($sql_oc_category_description) or die("erro 1 - ".mysqli_error($mysqli));
  528.  
  529.  
  530.  
  531. $sql_oc_category_path = "INSERT INTO `opencart`.`oc_category_path` (`category_id`,`path_id`,`level`)
  532.  
  533. VALUES ('".$r[1]."', '".$key."','0')";
  534.  
  535. $result2 = $mysqli->query($sql_oc_category_path) or die("erro 2 - ".mysqli_error($mysqli));
  536.  
  537.  
  538.  
  539.  
  540.  
  541. $sql_oc_category_to_layout = "INSERT INTO `opencart`.`oc_category_to_layout`
  542.  
  543. (`category_id`, `store_id`, `layout_id`)
  544.  
  545. VALUES ('".$key."', '0', '0');";
  546.  
  547. $result3 = $mysqli->query($sql_oc_category_to_layout) or die("erro 3 - ".mysqli_error($mysqli));
  548.  
  549.  
  550.  
  551.  
  552.  
  553. $sql_oc_category_to_store = "INSERT INTO `opencart`.`oc_category_to_store` (`category_id`, `store_id`)
  554.  
  555. VALUES ('".$key."', '0');";
  556.  
  557. $result4 = $mysqli->query($sql_oc_category_to_store) or die("erro 4 - ".mysqli_error($mysqli));
  558.  
  559.  
  560.  
  561. echo $r[1]." Ok \n";
  562.  
  563. else:
  564.  
  565. if(@eregi("Duplicate entry",mysqli_error($mysqli))):
  566.  
  567. echo " ".$r[1]." ja existe \n";
  568.  
  569. else:
  570.  
  571. echo mysqli_error($mysqli);
  572.  
  573. endif;
  574.  
  575. endif;
  576.  
  577.  
  578.  
  579. endif;
  580.  
  581.  
  582.  
  583. endforeach;
  584.  
  585.  
  586.  
  587. ibase_close($dbh);
  588.  
  589.  
  590.  
  591.  
  592.  
  593. }
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601. function cliente_atualiza($mysqli,$dbh){
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611. }
  612.  
  613.  
  614.  
  615.  
  616.  
  617. }
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625. echo "
  626.  
  627. Integra 0.1 - Sage Start & OpenCart
  628.  
  629. http://globalestudio.com.br/
  630.  
  631. \n\n";
  632.  
  633.  
  634.  
  635.  
  636.  
  637. $go = new SageCart;
  638.  
  639.  
  640.  
  641.  
  642.  
  643. if(!isset($argv[1])):
  644.  
  645. echo "\n\nNenhum argumento foi passado.\n";
  646.  
  647. $go->ShowHelp();
  648.  
  649. exit;
  650.  
  651. endif;
  652.  
  653. // DIRETÓRIO DE LOGS
  654.  
  655. $dirlogs = "logs";
  656.  
  657.  
  658.  
  659. $server = "hlcsistemas.com.br";
  660.  
  661. $user = "hlcsisro_userloj";
  662.  
  663. $pass = "13581321";
  664.  
  665. $db = "hlcsisro_loja";
  666.  
  667.  
  668.  
  669.  
  670.  
  671. /* $server = "localhost";
  672.  
  673. $user = "root";
  674.  
  675. $pass = "";
  676.  
  677. $db = "opencart";
  678.  
  679. */
  680.  
  681.  
  682.  
  683. // BASE DE DADOS FIREBIRD
  684.  
  685. $FBBD = 'C:\Program Files (x86)\Sage Start\database\startdb.sdb';
  686.  
  687. $FBUSER = 'SYSDBA';
  688.  
  689. $FBPASS = 'masterkey';
  690.  
  691.  
  692.  
  693.  
  694.  
  695. $link = $go->OpenCartConnect($server,$user,$pass,$db); // CONECTAR OPENCART
  696.  
  697. $OCTables = array_column(mysqli_fetch_all($link->query('SHOW TABLES')),0); // ASSOCIAR NOME DE TABELAS A VARIÁVEL $OCTables
  698.  
  699.  
  700.  
  701. $dbh = $go->SageConnect($FBBD,$FBUSER,$FBPASS); // CONECTAR FIREBIRD
  702.  
  703.  
  704.  
  705. $go->logs($dirlogs);
  706.  
  707.  
  708.  
  709. switch($argv[1]):
  710.  
  711. case "produtos_opencart":
  712.  
  713. $go->produtos_opencart($link);
  714.  
  715. break;
  716.  
  717.  
  718.  
  719. case "produtos_erp":
  720.  
  721. foreach($r = $go->produtos_erp($dbh) as $r):
  722.  
  723. echo $r."\n";
  724.  
  725. endforeach;
  726.  
  727. break;
  728.  
  729.  
  730.  
  731. case "produtos_atualiza":
  732.  
  733. $go->produtos_atualiza($link,$dbh);
  734.  
  735. break;
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743. case "categorias_opencart": // LISTAR CATEGORIAS OPENCART
  744.  
  745. foreach($go->categorias_opencart($link) as $r):
  746.  
  747. echo $r."\n";
  748.  
  749. endforeach;
  750.  
  751. break;
  752.  
  753.  
  754.  
  755. case "categorias_erp": // LISTAR CATEGORIAS ERP FIREBIRD
  756.  
  757. foreach($go->categorias_erp($dbh) as $r):
  758.  
  759. $r = explode(";-;",$r);
  760.  
  761. if($r[1] == ""):
  762.  
  763. echo $r[0]."\n";
  764.  
  765. endif;
  766.  
  767. endforeach;
  768.  
  769. break;
  770.  
  771.  
  772.  
  773. case "categorias_compara":
  774.  
  775. echo "Categorias que existem no ERP e nao no OPENCART \n\n";
  776.  
  777. $r = $go->categorias_compara($link,$dbh);
  778.  
  779. foreach($go->categorias_compara($link,$dbh) as $r):
  780.  
  781. echo $r[0]."\n";
  782.  
  783. endforeach;
  784.  
  785. break;
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793. case "categorias_atualiza":
  794.  
  795. echo $go->categorias_atualiza($link,$dbh);
  796.  
  797. break;
  798.  
  799.  
  800.  
  801.  
  802.  
  803. case "subcategorias_atualiza":
  804.  
  805. $go->subcategorias_atualiza($link,$dbh);
  806.  
  807. break;
  808.  
  809.  
  810.  
  811. case "cliente_atualiza":
  812.  
  813. $go->cliente_atualiza($mysqli,$dbh);
  814.  
  815. break;
  816.  
  817.  
  818.  
  819. default:
  820.  
  821. echo "Argumento inexistente.\n";
  822.  
  823. $go->ShowHelp();
  824.  
  825. break;
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835. endswitch;
  836.  
  837.  
  838.  
  839. exit;
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849. /* -----------------------------------------------------------------------------------------------------------------------------------------*/
  850.  
  851.  
  852.  
  853.  
  854.  
  855. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement