283 lines
11 KiB
PHP
283 lines
11 KiB
PHP
<?php
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
set_time_limit(0);
|
|
|
|
// Vendors
|
|
require_once 'ripcord/ripcord.php';
|
|
require '../config/config.inc.php';
|
|
require 'ErpTools.php';
|
|
|
|
// load config Odoo
|
|
require 'config.php';
|
|
|
|
|
|
if (empty($_GET['token']) || $_GET['token'] !== ERP_SCRIPT_TOKEN) {
|
|
die;
|
|
}
|
|
|
|
$models = ripcord::client("$url/xmlrpc/2/object");
|
|
|
|
$date_since_import = new DateTime();
|
|
$date_since_import->modify('-1 day');
|
|
|
|
// get all salable products ids
|
|
$ids_product = $models->execute_kw($db, $uid, $password,
|
|
'product.template', 'search', [[
|
|
['sale_ok', '=', true],
|
|
['__last_update', '>=', $date_since_import->format('Y-m-d H:i:s')]
|
|
]]
|
|
);
|
|
|
|
$d = Db::getInstance();
|
|
|
|
foreach ($ids_product as $key => $id) {
|
|
|
|
// get product's information
|
|
$record = $models->execute_kw($db, $uid, $password,
|
|
'product.template', 'read', array($id)
|
|
);
|
|
|
|
// get translation information
|
|
$translation_search = $models->execute_kw($db, $uid, $password,
|
|
'ir.translation', 'search', array(
|
|
array(
|
|
array('res_id', '=', $id),
|
|
array('name', '=', 'product.template,name'),
|
|
)
|
|
)
|
|
);
|
|
$translation = $models->execute_kw($db, $uid, $password,
|
|
'ir.translation', 'read', array($translation_search)
|
|
);
|
|
$name_product = (isset($translation[0]['value']) ? $translation[0]['value'] : $record['name'] );
|
|
|
|
|
|
$variants = $models->execute_kw($db, $uid, $password,
|
|
'product.product', 'read', [$record['product_variant_ids']]
|
|
);
|
|
|
|
$pakages = $models->execute_kw($db, $uid, $password,
|
|
'product.packaging', 'read', [$record['packaging_ids']]
|
|
);
|
|
|
|
// fix 13075 :
|
|
// as article packaging_ids and thus "pakages" are always empty (since 20/04/2017)
|
|
// and only variant packaging_ids are correctly defined,
|
|
// we define by default at '12' the number of article per box (manual changes will be required for those with inferior quantity)
|
|
$nb_per_box = 12;
|
|
foreach ($pakages as $pakage) {
|
|
if ($pakage['product_tmpl_id']['1'] == $record['name']) {
|
|
$nb_per_box = $pakage['qty'];
|
|
}
|
|
}
|
|
|
|
// update product
|
|
$sql = '
|
|
UPDATE '._DB_PREFIX_.'product p, '._DB_PREFIX_.'product_lang pl, '._DB_PREFIX_.'product_shop ps
|
|
SET
|
|
p.`ean13` = "'.$record['ean13'].'",
|
|
`quantity` = '.(int)$record['qty_available'].',
|
|
p.`price` = '.(float)($record['lst_price'] * $nb_per_box).',
|
|
p.`unit_price_ratio` = '.(float)$nb_per_box.',
|
|
p.`nb_per_box` = '.(float)$nb_per_box.',
|
|
ps.`price` = '.(float)($record['lst_price'] * $nb_per_box).',
|
|
ps.`unit_price_ratio` = '.(float)$nb_per_box.',
|
|
p.`reference` = "'.$record['default_code'].'",
|
|
p.`weight` = '.(float)$record['weight'].',
|
|
p.`nb_per_box` = '.(float)$nb_per_box.',
|
|
p.`out_of_stock` = 2,
|
|
p.`unity` = "bouteille",
|
|
ps.`unity` = "bouteille",
|
|
pl.`name` = "'.pSQL(trim(utf8_encode($name_product))).'"
|
|
WHERE
|
|
`id_erp` = '.(int)$id.'
|
|
AND p.`id_product` = pl.id_product
|
|
AND p.`id_product` = ps.id_product
|
|
AND pl.`id_lang` = 1
|
|
';
|
|
|
|
$d->execute($sql);
|
|
|
|
|
|
$sql = 'SELECT `id_product` FROM `'._DB_PREFIX_.'product` WHERE `id_erp` = '.pSQL($id);
|
|
$product_id = $d->getRow($sql)['id_product'];
|
|
|
|
$sql = 'SELECT `id_image` FROM '._DB_PREFIX_.'image WHERE `id_product` = '.(int)$product_id;
|
|
$id_image = $d->getValue($sql);
|
|
|
|
if ($id_image) {
|
|
$image = new Image($id_image);
|
|
$imagesTypes = ImageType::getImagesTypes('products');
|
|
$existing_path = $image->getExistingImgPath();
|
|
|
|
$image_exists = true;
|
|
foreach ($imagesTypes as $imageType) {
|
|
$image_exists = file_exists($existing_path.'-'.stripslashes($imageType['name']).'.'.$image->image_format);
|
|
if (!$image_exists) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$image_exists) {
|
|
$sql = 'DELETE FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image;
|
|
$d->execute($sql);
|
|
$sql = 'DELETE FROM '._DB_PREFIX_.'image_shop WHERE id_image = '.(int)$id_image;
|
|
$d->execute($sql);
|
|
$sql = 'DELETE FROM '._DB_PREFIX_.'image_lang WHERE id_image = '.(int)$id_image;
|
|
$d->execute($sql);
|
|
$id_image = null;
|
|
}
|
|
}
|
|
|
|
if (!$id_image) {
|
|
// set image
|
|
$sql = 'INSERT INTO '._DB_PREFIX_.'image (id_product, position, cover) VALUES('.(int)$product_id.', 0, 1)';
|
|
$d->execute($sql);
|
|
$img_id = $d->Insert_ID();
|
|
$sql = 'INSERT INTO '._DB_PREFIX_.'image_shop (id_product, id_image, id_shop, cover) VALUES('.(int)$product_id.', '.$img_id.',1, 1)';
|
|
$d->execute($sql);
|
|
$sql = 'INSERT INTO '._DB_PREFIX_.'image_lang (id_image, id_lang, legend) VALUES('.(int)$img_id.', 1, "'.$record['name'].'")';
|
|
$d->execute($sql);
|
|
|
|
$tmp_path = _PS_TMP_IMG_DIR_.'odoo_tmp_image_update.jpg';
|
|
$data = base64_decode($record['image']);
|
|
file_put_contents($tmp_path, $data);
|
|
$image = new Image($img_id);
|
|
$new_path = $image->getPathForCreation();
|
|
ImageManager::resize($tmp_path, $image->getPathForCreation().'.'.$image->image_format);
|
|
$imagesTypes = ImageType::getImagesTypes('products');
|
|
foreach ($imagesTypes as $imageType) {
|
|
ImageManager::resize($tmp_path, $new_path.'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format);
|
|
}
|
|
array_map('unlink', glob(_PS_TMP_IMG_DIR_.'*'));
|
|
}
|
|
|
|
foreach ($variants as $key => $variant) {
|
|
$id_variant = $variant['id'];
|
|
|
|
$id_product_attribute = $d->getValue('
|
|
SELECT `id_product_attribute`
|
|
FROM `'._DB_PREFIX_.'product_attribute`
|
|
WHERE `id_erp`= '.(int) $id_variant
|
|
);
|
|
|
|
// Déclinaison déja existante
|
|
if (isset($id_product_attribute) && !empty($id_product_attribute)) {
|
|
$sql = '
|
|
UPDATE '._DB_PREFIX_.'product_attribute
|
|
SET
|
|
`reference`= "'.pSQL(trim($variant['default_code'])).'",
|
|
`ean13`= "'.pSQL(trim($variant['ean13'])).'",
|
|
`quantity`= '.pSQL($variant['qty_available']).',
|
|
`weight`= '.pSQL($variant['weight']).'
|
|
WHERE
|
|
`id_erp` = '.(int) $id_variant;
|
|
$d->execute($sql);
|
|
|
|
if ($product_id && $id_product_attribute) {
|
|
// set in ps_product_attribute_shop
|
|
$sql = '
|
|
UPDATE `'._DB_PREFIX_.'product_attribute_shop`
|
|
SET
|
|
`weight` = '.pSQL($variant['weight_net']).'
|
|
WHERE
|
|
`id_product_attribute` = '.(int)$id_product_attribute.'
|
|
AND
|
|
`id_product` = '.(int)$product_id.'
|
|
';
|
|
$d->execute($sql);
|
|
|
|
// set in ps_stock_available
|
|
$sql = 'UPDATE `'._DB_PREFIX_.'stock_available`
|
|
SET
|
|
`quantity` = '.pSQL($variant['qty_available']).'
|
|
WHERE
|
|
`id_product_attribute` = '.(int)$id_product_attribute.'
|
|
AND
|
|
`id_product` = '.(int)$product_id.'
|
|
';
|
|
$d->execute($sql);
|
|
}
|
|
} else {
|
|
// Création nouvelle variante
|
|
$sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute (
|
|
id_product,
|
|
ean13,
|
|
quantity,
|
|
weight,
|
|
reference,
|
|
id_erp)
|
|
VALUE(
|
|
'.(int)$product_id.',
|
|
"'.pSQL(trim($variant['ean13'])).'",
|
|
'.pSQL($variant['qty_available']).',
|
|
'.pSQL($variant['weight']).',
|
|
"'.pSQL(trim($variant['default_code'])).'",
|
|
'.pSQL($variant['id']).')';
|
|
$d->execute($sql);
|
|
$id_product_attribute = $d->Insert_ID();
|
|
|
|
if ($product_id && $id_product_attribute) {
|
|
|
|
// check if product has already id_product_attribute default_on
|
|
if( Db::getInstance()->getValue('
|
|
SELECT `id_product_attribute`
|
|
FROM `'._DB_PREFIX_.'product_attribute_shop`
|
|
WHERE `default_on` = 1
|
|
AND `id_product` = '. (int) $product_id)
|
|
) {
|
|
$pa_default = 1;
|
|
} else {
|
|
$pa_default = 0;
|
|
}
|
|
|
|
// set in ps_product_attribute_shop
|
|
$sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute_shop (
|
|
id_product,
|
|
id_product_attribute,
|
|
id_shop,
|
|
weight,
|
|
default_on)
|
|
VALUES(
|
|
'.(int)$product_id.',
|
|
'.(int)$id_product_attribute.',
|
|
1,
|
|
'.pSQL($variant['weight_net']).',
|
|
'.(($pa_default == 1) ? 'null' : '1').')';
|
|
$d->execute($sql);
|
|
|
|
//set in ps_stock_available
|
|
$sql = 'INSERT INTO '._DB_PREFIX_.'stock_available (
|
|
id_product,
|
|
id_product_attribute,
|
|
id_shop,
|
|
id_shop_group,
|
|
quantity,
|
|
out_of_stock)
|
|
VALUES (
|
|
'.(int)$product_id.',
|
|
'.(int)$id_product_attribute.',
|
|
1,
|
|
0,
|
|
'.pSQL($variant['qty_available']).',
|
|
2)';
|
|
$d->execute($sql);
|
|
|
|
$attrs = $models->execute_kw($db, $uid, $password,
|
|
'product.attribute.value', 'read', [$variant['attribute_value_ids']]
|
|
);
|
|
|
|
foreach ($attrs as $attr) {
|
|
if (empty($assoc_attr[$attr['id']])) {
|
|
var_dump($attr); die;
|
|
}
|
|
$sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute_combination (id_attribute, id_product_attribute) VALUES('.(int)$assoc_attr[$attr['id']].', '.(int)$id_product_attribute.')';
|
|
$d->execute($sql);
|
|
}
|
|
}
|
|
}
|
|
// fin de création d'une nouvelle décli
|
|
|
|
}
|
|
} |