roykin/erp/erp_update_product.php
Alexandre Simonet 726e69c5e9 ajout token
2016-04-21 15:30:14 +02:00

166 lines
5.3 KiB
PHP

<?php
if (empty($_GET['token']) || $_GET['token'] !== ERP_SCRIPT_TOKEN) {
die;
}
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';
$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)
);
$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']]
);
$nb_per_box = 1;
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'].',
name = "'.$record['name'].'",
description_short = "<p>'.pSQL(trim($record['description_sale'])).'</p>",
link_rewrite = "'.pSQL(Tools::link_rewrite(trim($record['name']))).'"
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 = 'DELETE FROM '._DB_PREFIX_.'product_attribute WHERE id_product = '.(int)$product_id;
$d->execute($sql);
$sql = 'DELETE FROM '._DB_PREFIX_.'product_attribute_shop WHERE id_product = '.(int)$product_id;
$d->execute($sql);
foreach ($variants as $key => $variant) {
// 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();
// 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.',
'.pSQL($id_product_attribute).',
1,
'.pSQL($variant['weight_net']).',
'.(($key == 0) ? '1' : 'null').')';
$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.',
'.pSQL($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']]
);
$sql = 'SELECT id_product_attribute FROM '._DB_PREFIX_.'product_attribute pa WHERE id_erp = '.(int)$variant['id'];
$id_product_attribute = $d->getRow($sql)['id_product_attribute'];
$sql = 'DELETE FROM '._DB_PREFIX_.'product_attribute_combination WHERE id_product_attribute = '.(int)$id_product_attribute;
$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);
}
}
}