· 9 years ago · Jan 23, 2017, 05:48 PM
1$conn = new PDO('mysql:host=localhost;dbname=erp_comerciourbano','root', '');
2 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
3}
4catch (PDOException $e)
5{
6 echo 'ERROR: '.$e->getMessage();
7}
8
9$tokenQuery = "
10 SELECT
11 erp_app_id AS app_id,
12 erp_secret_key AS secret_key,
13 erp_access_token AS access_token,
14 erp_refresh_token AS refresh_token,
15 erp_expires_in AS expires_in
16 FROM
17 erp_token
18 WHERE
19 erp_tokenid = 1
20 ";
21
22$stmt = $conn->prepare($tokenQuery);
23$stmt->execute();
24
25$result = $stmt->fetchAll(PDO::FETCH_OBJ);
26
27$app_id = $result[0]->app_id;
28$secret_key = $result[0]->secret_key;
29$access_token = $result[0]->access_token;
30$refresh_token = $result[0]->refresh_token;
31$expired = $result[0]->expires_in;
32
33$meli = new Meli($app_id,$secret_key,$access_token,$refresh_token);
34
35if($expired < time())
36{
37 $refresh = $meli->refreshAccessToken();
38
39 $access_token = $refresh['body']->access_token;
40 $expired = time() + $refresh['body']->expires_in;
41 $refresh_token = $refresh['body']->refresh_token;
42
43 $query = "
44 UPDATE
45 erp_token
46 SET
47 erp_access_token = '$access_token',
48 erp_refresh_token = '$refresh_token',
49 erp_expires_in = $expired
50 WHERE
51 erp_tokenid = 1
52 ";
53 $stmt = $conn->prepare($query);
54 $stmt->execute();
55}
56else
57{
58 $offset = 0;
59
60 $incrementer = "SELECT cron_value FROM cron_incrementer WHERE cron_incrementerid = 2";
61
62 $stmt = $conn->prepare($incrementer);
63 $stmt->execute();
64
65 $result = $stmt->fetchAll(PDO::FETCH_OBJ);
66 $offset += $result[0]->cron_value;
67
68 $k = 0;
69 while($k < 10)
70 {
71 $query = "
72 SELECT
73 ep.erp_productid AS id,
74 epd.erp_name AS title,
75 em.erp_mlbid AS category_id,
76 ep.erp_price AS price,
77 'BRL' AS currency_id,
78 ep.erp_quantity AS available_quantity,
79 'buy_it_now' AS buying_mode,
80 'gold' AS listing_type_id,
81 'new' AS 'condition',
82 epd.erp_description AS description,
83 epi.erp_image AS pictures
84 FROM
85 erp_product AS ep
86 INNER JOIN
87 erp_product_description AS epd ON epd.erp_productid = ep.erp_productid
88 INNER JOIN
89 erp_product_category AS epc ON epc.erp_productid = ep.erp_productid
90 INNER JOIN
91 erp_mlbcategory_erpcategory AS emc ON emc.erp_categoryid = epc.erp_categoryid
92 INNER JOIN
93 erp_mlb_category AS em ON em.erp_mcid = emc.erp_mlbcategoryid
94 INNER JOIN
95 erp_product_image AS epi ON epi.erp_productid = ep.erp_productid
96 WHERE
97 ep.erp_productid NOT IN (
98 SELECT
99 epm.erp_productid
100 FROM
101 erp_product_to_mlb AS epm
102 )
103 AND ep.erp_quantity > 0
104 AND ep.erp_status > 0
105 LIMIT
106 $offset,10
107 ";
108 //if($k == 1) {var_dump($query);die();}
109 $stmt = $conn->prepare($query);
110 $stmt->execute();
111
112 $result = $stmt->fetchAll(PDO::FETCH_OBJ);
113 $j = 0;
114 $tmp = array();
115 for($i = 0; $i < count($result); $i++)
116 {
117 $items[$i] = array(
118 'title' => 'Teste',
119 'category_id' => 'MLB',
120 'price' => '10,00',
121 'currency_id' => 'BRL',
122 'available_quantity' => '20',
123 'buying_mode' => 'buy_it_now',
124 'listing_type_id' => 'gold',
125 'condition' => 'new',
126 'description' => 'Teste',
127 'pictures' => array( array('source' => $result[$i]->pictures) )
128 );
129
130 $retorno[$i] = $meli->GET('/items/validate',$items[$i], array('access_token' => $access_token));
131 if($retorno[$i]['httpCode'] == 204)
132 {
133 $return[$j] = $meli->GET('/items',$items[$i],array('access_token' => $access_token));
134 //var_dump($return);die();
135 //$tmp[$j] = array('erp_productid' => $result[$i]->id,'erp_mlbid' => $return[$j]['body']->id);
136 $mlbId = $return[$j]['body']->id;
137 $productId = $result[$i]->id;
138
139 $productCheck = 'SELECT erp_product_to_mlb_id FROM erp_product_to_mlb WHERE erp_mlbid = "'.$mlbId.'"';
140 $stmtProductCheck = $conn->prepare($productCheck);
141 $stmtProductCheck->execute();
142 $resultProductCheck = $stmtProductCheck->fetchAll(PDO::FETCH_OBJ);
143 //var_dump($resultProductCheck);
144 if (count($resultProductCheck) === 0){
145 $queryInsertProduct = '
146 INSERT INTO
147 erp_product_to_mlb(erp_mlbid, erp_productid)
148 VALUES
149 ("'.$mlbId.'","'.$productId.'")
150 ';
151 $stmt = $conn->prepare($queryInsertProduct);
152 $stmt->execute();
153 echo "Produto Inserido com Sucesso: ".$mlbId."<br>n";
154 }else{
155 echo "Produto já cadastrado: ".$mlbId."<br>n";
156 }
157 $j++;
158 }
159 }
160 //$connection = array();
161 //var_dump(json_encode($tmp));die();
162 //file_put_contents('D:/Utilitarios/Xampp/htdocs/svn/trunk/connection.txt', json_encode($tmp).',n', FILE_APPEND | LOCK_EX);
163 //file_put_contents('D:/Utilitarios/Xampp/htdocs/svn/trunk/return_cron.txt', json_encode($retorno).',n', FILE_APPEND | LOCK_EX);
164 //file_put_contents('C:/inetpub/wwwroot/connection.txt', json_encode($tmp).',n', FILE_APPEND | LOCK_EX);
165 file_put_contents('C:/inetpub/wwwroot/return_cron.txt', json_encode($retorno).',n', FILE_APPEND | LOCK_EX);
166
167
168 $offset = $offset + 10;
169 $k++;
170 }
171
172 $val = $offset + 10;
173
174 $update = "UPDATE cron_incrementer SET cron_value = $val WHERE cron_incrementerid = 2";
175
176 $stmt = $conn->prepare($update);
177 $stmt->execute();
178
179}