-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
330 lines (296 loc) · 7.94 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
//Enable debug
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
//---------------------------------------------------------------------------------------
// odoo data
require_once('ripcord-master/ripcord.php');
$odoo_url = "https://siaptest.odoo.com";
$odoo_db = "siaptest";
$odoo_username = "charlie.yossa@sample.com";
$odoo_password = "1e67784626d33e72c9dbe8ea48e221d56313ee32";
// woocommerce data
require __DIR__ . '/vendor/autoload.php';
//use Automattic\WooCommerce\Client;
//use Automattic\WooCommerce\HttpClient\HttpClientException;
$woo_url = 'https://sample.info';
$woo_consumer_key = 'ck_96734546235a0be1fe0bde48af5bbf128367e8ba';
$woo_consumer_secret = 'cs_f796c3159489387e975a1b8fff0fce8827b3d8d6';
// server data
$connector_location = $_SERVER['DOCUMENT_ROOT'].'/MyOdooConnector';
$connector_link ='https://sample.info/MyOdooConnector';
//--------------------------------------------------------------------------
//My functions
function odoo_Authentication($url, $db, $username, $password)
{
$common = ripcord::client($url.'/xmlrpc/2/common');
$uid = $common->authenticate($db, $username, $password, array());
if (empty($uid))
{
echo "Erreur d'authentification sur Odoo";
exit();
}
return $uid;
}
function odoo_Create_user($name, $login, $password)
{
global $odoo_url, $odoo_db, $odoo_username, $odoo_password;
$admin_uid = odoo_Authentication($odoo_url, $odoo_db, $odoo_username, $odoo_password);
$models = ripcord::client($odoo_url.'/xmlrpc/2/object');
$user_id = $models->execute_kw
(
$odoo_db,
$admin_uid,
$odoo_password,
'res.users',
'create',
array
(
array
(
'name' => $name,
'login' => $login,
'company_ids' => [1],
'company_id' => 1,
'new_password' => $password
)
)
);
if (empty($user_id))
{
echo "La reation de l'utilisateur a echoue";
exit();
}
return $user_id;
}
function odoo_Create_Invoice($woo_products_list)
{
global $odoo_url, $odoo_db, $odoo_username, $odoo_password;
$user_uid = odoo_Authentication($odoo_url, $odoo_db, $odoo_username, $odoo_password);
$models = ripcord::client($odoo_url.'/xmlrpc/2/object');
$invoice_id = $models->execute_kw
(
$odoo_db,
$user_uid,
$odoo_password,
'sale.order',
'create',
array
(
array
(
'partner_id' => 1,
'validity_date' => "2021-08-09",
'state' => 'sale',
'order_line' => [
[
0, 0, ['product_id' => 2, 'product_uom_qty' => 2, 'price_unit' => 10.00]
],
[
0, 0, ['product_id' => 5, 'product_uom_qty' => 4, 'price_unit' => 20.00]
]
]
)
)
);
if (empty($invoice_id))
{
echo "Erreur de creation de la facture sur Odoo";
exit();
}
return $invoice_id;
}
function woo_Authentication($url, $consumer_key, $consumer_secret)
{
$woo_options = [
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true // Force Basic Authentication as query string true and using under HTTPS
];
$woocommerce = new \Automattic\WooCommerce\Client ($url, $consumer_key, $consumer_secret, $woo_options);
if (empty($woocommerce))
{
echo "Erreur d'authentification sur WooCommerce";
exit();
}
return $woocommerce;
}
function clearTmp($folder)
{
$files = glob($folder.'*'); // get all file names
foreach($files as $file)
{
if(is_file($file))
{
unlink($file); // delete file
}
}
}
function convBase64ToImg($img_base64, $path)
{
global $connector_location, $connector_link;
if( !empty($img_base64) )
{
//get extension
switch ($img_base64[0]) //determine extension with first caracter of base64
{
case '/':
$extension = '.jpeg';
break;
case 'i':
$extension = '.png';
break;
case 'R':
$extension = '.gif';
break;
case 'U':
$extension = '.webp';
break;
case 'J':
$extension = '.pdf';
break;
default:
$extension = '.unknown';
}
$image_name = md5(uniqid()).$extension; //Generate name
$store_at = $connector_location.$path.$image_name; //to store image datas
$decoded_string = base64_decode( $img_base64 ); //decoding
file_put_contents( $store_at, $decoded_string ); // store data
return $connector_link.$path.$image_name;
}
return $connector_link.'/default.jpg';
}
function create_product($woocommerce, $p_name, $p_regular_price, $p_qty, $p_imageBase64, $p_sku, $p_short_description ='')
{
$data =
[
'name' => $p_name,
'type' => 'simple',
'regular_price' => sprintf($p_regular_price),
'manage_stock' => True,
'stock_quantity' => intval($p_qty),
'short_description' => $p_short_description,
'images' =>
[
[
'src' => convBase64ToImg($p_imageBase64, '/images_tmp/')
]
],
'sku' => sprintf($p_sku)/*,
'description' => '',
'categories' =>
[
[
'id' => 9
],
[
'id' => 14
]
]*/
];
$woocommerce->post('products', $data);
}
function update_product($woocommerce, $product_id, $p_regular_price, $p_qty, $p_imageBase64)
{
$data =
[
'regular_price' => sprintf($p_regular_price),
'stock_quantity' => intval($p_qty)/*,
'images' =>
[
[
'src' => convBase64ToImg($p_imageBase64, '/images_tmp/')
]
]*/
];
$woocommerce->put('products/'.$product_id, $data);
}
function delete_product($woocommerce, $product_id)
{
$woocommerce->delete('products/'.$product_id, ['force' => true]);
}
//--------------------------------------------------------------------
// Authentication to odoo
$odoo_uid = odoo_Authentication($odoo_url, $odoo_db, $odoo_username, $odoo_password);
// read odoo database
$models = ripcord::client($odoo_url.'/xmlrpc/2/object');
$odoo_products = $models->execute_kw
(
$odoo_db,
$odoo_uid,
$odoo_password,
'product.template',
'search_read',
array(),
array
(
'fields'=>array
(
'id', 'name', 'list_price', 'qty_available', 'categ_id', 'image_1920'/*, 'standard_price', 'qty_available', 'default_code', 'product_tag_ids'*/
)
)
);
// Authentication to woocommerce
$woocommerce = woo_Authentication($woo_url, $woo_consumer_key, $woo_consumer_secret);
// sync odoo - woocommerce
$woo_products = $woocommerce->get('products'); //Get all woocommerce products
$woo_list_products_names = array(); //To store all products names
foreach ($woo_products as $woo_product)
{
array_push($woo_list_products_names, $woo_product->name);
}
echo '<br/>';
$odoo_list_products_names = array(); //To store all odoo products name
foreach ($odoo_products as $odoo_product)
{
$odoo_product = json_decode(json_encode($odoo_product));
array_push($odoo_list_products_names, $odoo_product->name);
if (in_array($odoo_product->name, $woo_list_products_names))
{
foreach ($woo_products as $woo_product)
{
if ($woo_product->name == $odoo_product->name)
{
if ($woo_product->regular_price != sprintf($odoo_product->list_price) || $woo_product->stock_quantity != intval($odoo_product->qty_available))
{
update_product
(
$woocommerce,
$woo_product -> id,
$odoo_product -> list_price,
$odoo_product -> qty_available,
$odoo_product -> image_1920
);
echo $woo_product->name.' mit a jour<br/>';
}
}
}
}
else
{
if(implode($odoo_product->categ_id) == '4ParaPharcie' && $odoo_product->qty_available > 0)
{
create_product
(
$woocommerce,
$odoo_product -> name,
$odoo_product -> list_price,
$odoo_product -> qty_available,
$odoo_product -> image_1920,
$odoo_product -> id
);
echo $odoo_product -> name.' cree<br/>';
}
}
}
$woo_products = $woocommerce->get('products');
foreach ( $woo_products as $woo_product )
{
if ( ! in_array($woo_product->name, $odoo_list_products_names))
{
delete_product($woocommerce, $woo_product->id);
}
}
// clear cache and clean tmp files
clearTmp('images_tmp/');