bebeboutik/modules/importproduit/_importstock.php
Srv Bebeboutik 6c0978166c add modules
2016-01-04 12:49:26 +01:00

107 lines
3.5 KiB
PHP
Executable File

<?php
require_once(dirname(__FILE__).'/../../config/config.inc.php');
@ini_set('display_errors', 'on');
@ini_set('max_execution_time', 0);
if (!defined('_PS_BASE_URL_'))
define('_PS_BASE_URL_', Tools::getShopDomain(true));
$path = _PS_ROOT_DIR_.'/modules/importproduit/';
$fichier = "stock.csv";
$handle = fopen($path.$fichier, "r");
if($handle){
$row = 0;
while (($data01 = fgetcsv($handle, 100000, ";")) !== FALSE) {
$num = count($data01);
$data = utf8EncodeArray($data01);
if($row > 29693){
$reference = $data[0]; // A
$quantity = (int)$data[1]; // JP
$id_product = existsRefInDatabase($reference);
if($id_product){
$product = new Product($id_product);
$product->quantity = (int)$quantity;
$product->update();
setQuantity($product->id, 0, (int)$quantity);
}
}
$row++;
echo $row."<br/>";
ob_end_flush();
flush();
ob_start();
}
fclose($handle);
echo "Import terminé";
}else{
echo "prob ouverture";
}
function existsRefInDatabase($reference){
$row = Db::getInstance()->getRow('
SELECT `id_product`
FROM `'._DB_PREFIX_.'product` p
WHERE p.reference = "'.$reference.'"');
return $row['id_product'];
}
function utf8EncodeArray($array){
return (is_array($array) ? array_map('utf8_encode', $array) : utf8_encode($array));
}
function setQuantity($id_product, $id_product_attribute, $quantity, $id_shop = null){
if (!Validate::isUnsignedId($id_product))
return false;
$context = Context::getContext();
// if there is no $id_shop, gets the context one
if ($id_shop === null && Shop::getContext() != Shop::CONTEXT_GROUP)
$id_shop = (int)$context->shop->id;
$depends_on_stock = StockAvailable::dependsOnStock($id_product);
//Try to set available quantity if product does not depend on physical stock
if (!$depends_on_stock)
{
$id_stock_available = (int)StockAvailable::getStockAvailableIdByProductId($id_product, $id_product_attribute, $id_shop);
if ($id_stock_available)
{
$stock_available = new StockAvailable($id_stock_available);
$stock_available->quantity = (int)$quantity;
$stock_available->update();
}
else
{
$out_of_stock = StockAvailable::outOfStock($id_product, $id_shop);
$stock_available = new StockAvailable();
$stock_available->out_of_stock = (int)$out_of_stock;
$stock_available->id_product = (int)$id_product;
$stock_available->id_product_attribute = (int)$id_product_attribute;
$stock_available->quantity = (int)$quantity;
if ($id_shop === null)
$shop_group = Shop::getContextShopGroup();
else
$shop_group = new ShopGroup((int)Shop::getGroupFromShop((int)$id_shop));
// if quantities are shared between shops of the group
if ($shop_group->share_stock)
{
$stock_available->id_shop = 0;
$stock_available->id_shop_group = (int)$shop_group->id;
}
else
{
$stock_available->id_shop = (int)$id_shop;
$stock_available->id_shop_group = 0;
}
$stock_available->add();
}
}
}