roykin/erp/erp_add_product.php
2017-06-14 11:41:31 +02:00

313 lines
7.8 KiB
PHP
Executable File

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// 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 de diff pour la récupération produit
$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', array(
array(
array('sale_ok', '=', 'true'),
array('create_date', '>=', $date_since_import->format('Y-m-d H:i:s')),
)
)
);
foreach ($ids_product as $key => $id) {
if (ErpTools::productAlreadyExists($id)) {
continue;
}
// get product's information
$record = $models->execute_kw($db, $uid, $password,
'product.template', 'read', array($id)
);
$variants = $models->execute_kw($db, $uid, $password,
'product.product', 'read', [$record['product_variant_ids']]
);
try {
$d = Db::getInstance();
$current_date = new DateTime();
$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,
// if no one contain any number of article per box,
// 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;
$nb_per_box_empty = true;
foreach ($pakages as $pakage) {
if ($pakage['product_tmpl_id']['1'] == $record['name']) {
$nb_per_box = $pakage['qty'];
$nb_per_box_empty = false;
}
}
if ($nb_per_box_empty && isset($variants[0])) {
$pakages = $models->execute_kw($db, $uid, $password,
'product.packaging', 'read', [$variants[0]['packaging_ids']]
);
foreach ($pakages as $pakage) {
$nb_per_box = $pakage['qty'];
$nb_per_box_empty = false;
break;
}
if ((int)$nb_per_box<1) {
$nb_per_box = 12;
}
}
// end fix
//set in ps_product
// fix 13064 - tax set to default tax rather than zero
$sql = 'INSERT INTO '._DB_PREFIX_.'product (
id_category_default,
id_shop_default,
id_tax_rules_group,
ean13,
quantity,
price,
reference,
weight,
active,
redirect_type,
id_erp,
date_add,
date_upd,
nb_per_box,
unit_price_ratio,
unity,
out_of_stock)
VALUES (
2,
1,
1,
"'.pSQL(trim($record['ean13'])).'",
'.pSQL($record['qty_available']).',
'.pSQL($record['lst_price'] * $nb_per_box).',
"'.pSQL(trim($record['default_code'])).'",
'.pSQL($record['weight']).',
0,
"404",
'.pSQL($id).',
NOW(),
NOW(),
'.pSQL($nb_per_box).',
'.pSQL($nb_per_box).', "bouteille",
2)';
$d->execute($sql);
//set in ps_product_lang
$product_id = $d->Insert_ID();
$sql = 'INSERT INTO '._DB_PREFIX_.'product_lang (
id_product,
id_shop,
id_lang,
description_short,
link_rewrite,
name)
VALUES(
'.pSQL($product_id).',
1,
1,
"<p>'.pSQL(trim($record['description_sale'])).'</p>",
"'.pSQL(Tools::link_rewrite(trim($record['name']))).'", "
'.pSQL(trim($record['name'])).'")';
$d->execute($sql);
// set in ps_category_product
$sql = 'INSERT INTO '._DB_PREFIX_.'category_product(
id_category, id_product, position)
VALUES(18, '.pSQL($product_id).', 0)';
$d->execute($sql);
//set in ps_product_shop
$sql = 'INSERT INTO '._DB_PREFIX_.'product_shop (
id_product,
id_shop,
id_category_default,
id_tax_rules_group,
price,
active,
redirect_type,
date_add,
date_upd,
unit_price_ratio,
unity)
VALUES (
'.pSQL($product_id).',
1,
18,
1,
'.pSQL($record['lst_price'] * $nb_per_box).',
0,
"404",
NOW(),
NOW(),
'.pSQL($nb_per_box).', "bouteille")';
$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 (
'.pSQL($product_id).',
0,
1,
0,
0,
2)';
$d->execute($sql);
// 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);
if (!empty($record['image'])) {
$tmp_path = _PS_TMP_IMG_DIR_.'odoo_tmp_image.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_.'*'));
}
// set variant
foreach ($variants as $key => $variant) {
$attrs = $models->execute_kw($db, $uid, $password,
'product.attribute.value', 'read', [$variant['attribute_value_ids']]
);
// set in ps_product_attribute
$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 ($id_product_attribute && $product_id) {
// set in ps_product_attribute_shop
if ($key == 0) {
$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']).',
1)';
$d->execute($sql);
} else {
$sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute_shop (
id_product,
id_product_attribute,
id_shop,
weight)
VALUES(
'.(int)$product_id.',
'.(int)$id_product_attribute.',
1,
'.pSQL($variant['weight']).')';
$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);
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);
}
}
}
} catch (Exception $e) {
d($e->getMessage());
}
}