diff --git a/erp/ErpTools.php b/erp/ErpTools.php index 8d61637..9c8f051 100644 --- a/erp/ErpTools.php +++ b/erp/ErpTools.php @@ -68,4 +68,4 @@ class ErpTools return true; } -} +} \ No newline at end of file diff --git a/erp/erp_add_product.php b/erp/erp_add_product.php index 5d711fe..52794aa 100644 --- a/erp/erp_add_product.php +++ b/erp/erp_add_product.php @@ -18,9 +18,12 @@ $ids_product = $models->execute_kw($db, $uid, $password, ); - 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) @@ -31,34 +34,244 @@ foreach ($ids_product as $key => $id) { 'product.product', 'read', [$record['product_variant_ids']] ); - $db = Db::getInstance(); try { - // set in ps_product - $sql = 'INSERT INTO '._DB_PREFIX_.'product (id_category_default, id_shop_default, id_tax_rules_group, ean13, quantity, price, reference, weight, active, redirect_type, erp_id) VALUES (18, 1, 0, '.pSQL($record['ean13']).', '.pSQL($record['qty_available']).', '.pSQL($record['lst_price']).', "'.pSQL($record['default_code']).'", '.pSQL($record['weight_net']).', 1, "404", '.pSQL($id).')'; - $db->execute($sql); - // set in ps_product_lang - $product_id = $db->Insert_ID(); + $d = Db::getInstance(); + $current_date = new DateTime(); + + $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']; + } + } + + //set in ps_product + $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) + VALUES ( + 2, + 1, + 0, "'.pSQL(trim($record['ean13'])).'", + '.pSQL($record['qty_available']).', + '.pSQL($record['lst_price']).', + "'.pSQL(trim($record['default_code'])).'", + '.pSQL($record['weight']).', + 1, + "404", + '.pSQL($id).', + NOW(), + NOW(), + '.pSQL($nb_per_box).')'; + $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, + "
'.pSQL(trim($record['description_sale'])).'
", + "'.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) + VALUES ( + '.pSQL($product_id).', + 1, + 18, + 1, + '.pSQL($record['lst_price']).', + 1, + "404", + NOW(), + NOW())'; + $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, + '.pSQL($record['qty_available']).', + 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_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.jpg'; + $data = base64_decode($record['image']); + file_put_contents($tmp_path, $data); + $image = new Image($img_id); + ImageManager::resize($tmp_path, $image->getPathForCreation().'.'.$image->image_format); + $imagesTypes = ImageType::getImagesTypes('products'); + foreach ($imagesTypes as $imageType) + ImageManager::resize($tmpName, $new_path.'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format); - $sql = 'INSERT INTO '._DB_PREFIX_.'product_lang (id_product, id_shop, id_lang, description_short, link_rewrite, name) VALUES('.$product_id.', 1, 1, "'.pSQL($record['description_sale']).'
", "'.pSQL(Tools::link_rewrite($record['name'])).'", "'.pSQL($record['name']).'")'; - $db->execute($sql); // set variant foreach ($variants as $key => $variant) { - - $attr = $models->execute_kw($db, $uid, $password, - 'product.attribute.line', 'read', [$variant['attribute_line_ids']] + $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, + price, + quantity, + weight, + reference) + VALUE( + '.pSQL($product_id).', + "'.pSQL(trim($variant['ean13'])).'", + '.pSQL($variant['lst_price']).', + '.pSQL($variant['qty_available']).', + '.pSQL($variant['weight']).', + "'.pSQL(trim($variant['default_code'])).'")'; + $d->execute($sql); - var_dump($attr); die; - $sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute (id_product, ean13, price, quantity, weight, reference) VALUE('.pSQL(product_id).', '.pSQL($variant['ean13']).', '.pSQL($variant['lst_price']).', '.pSQL($variant['qty_available']).', '.pSQL($variant['weight_net']).', '.pSQL($variant['default_code']).')'; - $db->execute($sql); + $id_product_attribute = $d->Insert_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, + price, + weight, + default_on) + VALUES( + '.pSQL($product_id).', + '.pSQL($id_product_attribute).', + 1, + '.pSQL($variant['lst_price']).', + '.pSQL($variant['weight_net']).', + 1)'; + $d->execute($sql); + } else { + $sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute_shop ( + id_product, + id_product_attribute, + id_shop, + price, + weight) + VALUES( + '.pSQL($product_id).', + '.pSQL($id_product_attribute).', + 1, + '.pSQL($variant['lst_price']).', + '.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 ( + '.pSQL($product_id).', + '.pSQL($id_product_attribute).', + 1, + 0, + '.pSQL($variant['qty_available']).', + 2)'; + $d->execute($sql); + + + + foreach ($attrs as $attr) { + + $attr['name'] = str_replace('/ml', '', $attr['name']); + + $sql = 'SELECT id_attribute FROM '._DB_PREFIX_.'attribute_lang WHERE name = "'.pSQL(str_replace("\n", '', trim($attr['name']))).'"'; + $row = $d->getRow($sql); + + $id_attr = 0; + + if ($row['id_attribute']) { + $id_attr = $row['id_attribute']; + } else { + $sql = 'INSERT INTO '._DB_PREFIX_.'attribute(id_attribute_group) VALUES (4)'; + $d->execute($sql); + + $id_attr = $d->Insert_ID(); + + $sql = 'INSERT INTO '._DB_PREFIX_.'attribute_lang(id_attribute, id_lang, name) VALUES('.pSQL($id_attr).', 1, "'.pSQL(trim($attr['name'])).'")'; + $d->execute($sql); + } + + + $sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute_combination (id_attribute, id_product_attribute) VALUES('.pSQL($id_attr).', '.pSQL($id_product_attribute).')'; + $d->execute($sql); + + } - $last_variant_id = $db->Insert_ID(); } @@ -67,6 +280,5 @@ foreach ($ids_product as $key => $id) { d($e->getMessage()); } - die; +} -} \ No newline at end of file diff --git a/erp/erp_update_product.php b/erp/erp_update_product.php new file mode 100644 index 0000000..27cdfab --- /dev/null +++ b/erp/erp_update_product.php @@ -0,0 +1,108 @@ +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']] + ); + + // update product + $sql = 'UPDATE '._DB_PREFIX_.'product p, '._DB_PREFIX_.'product_lang pl, '._DB_PREFIX_.'product_shop ps + SET p.ean13 = "'.$record['ean13'].'", + ps.ean13 = "'.$record['ean13'].'", + quantity = '.(int)$record['qty_available'].', + p.price = '.(float)$record['lst_price'].', + ps.price = '.(float)$record['lst_price'].', + p.reference = "'.$record['default_code'].'", + p.weight = '.(float)$record['weight'].', + name = "'.$record['name'].'", + description_short = "'.pSQL(trim($record['description_sale'])).'
", + 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); + + foreach ($variants as $key => $variant) { + // update product attribute + $sql = 'UPDATE '._DB_PREFIX_.'product_attribute pa, '._DB_PREFIX_.'product p, '._DB_PREFIX_.'product_attribute_shop pas + SET pa.ean13 = "'.$variant['ean13'].'", + pa.quantity = '.(int)$variant['qty_available'].', + pa.price = '.(float)$variant['lst_price'].', + pas.price = '.(float)$variant['lst_price'].', + pas.weight = '.(float)$variant['weight'].' + WHERE id_erp = '.(int)$id.' + AND p.id_product = pa.id_product'; + $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 LEFT JOIN '._DB_PREFIX_.'product p ON pa.id_product = p.id_product WHERE id_erp = '.(int)$id; + $id_product_attribute = $d->getRow($sql); + + $sql = 'DELETE FROM '._DB_PREFIX_.'product_attribute_combination WHERE id_product_attribute = '.(int)$id_product_attribute; + $d->execute($sql); + + foreach ($attrs as $key => $attr) { + $attr['name'] = str_replace('/ml', '', $attr['name']); + + $sql = 'SELECT id_attribute FROM '._DB_PREFIX_.'attribute_lang WHERE name = "'.pSQL(str_replace("\n", '', trim($attr['name']))).'"'; + $row = $d->getRow($sql); + + $id_attr = 0; + + if ($row['id_attribute']) { + $id_attr = $row['id_attribute']; + } else { + $sql = 'INSERT INTO '._DB_PREFIX_.'attribute(id_attribute_group) VALUES (4)'; + $d->execute($sql); + + $id_attr = $d->Insert_ID(); + + $sql = 'INSERT INTO '._DB_PREFIX_.'attribute_lang(id_attribute, id_lang, name) VALUES('.pSQL($id_attr).', 1, "'.pSQL(trim($attr['name'])).'")'; + $d->execute($sql); + } + + + $sql = 'INSERT INTO '._DB_PREFIX_.'product_attribute_combination (id_attribute, id_product_attribute) VALUES('.pSQL($id_attr).', '.pSQL($id_product_attribute).')'; + $d->execute($sql); + } + } + + + //die; + +} \ No newline at end of file