Add mandatory columns for imports

modifié:         core/lib/Thelia/ImportExport/Import/ImportHandler.php
	modifié:         core/lib/Thelia/ImportExport/Import/Type/ProductPricesImport.php
	modifié:         core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php
This commit is contained in:
Benjamin Perche
2014-07-21 16:39:17 +02:00
parent df98ce2de4
commit 2a56864f94
3 changed files with 98 additions and 49 deletions

View File

@@ -11,7 +11,9 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\ImportExport\Import; namespace Thelia\ImportExport\Import;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\FileFormat\Formatting\FormatterData;
use Thelia\Core\Translation\Translator;
use Thelia\ImportExport\AbstractHandler; use Thelia\ImportExport\AbstractHandler;
/** /**
@@ -23,11 +25,41 @@ abstract class ImportHandler extends AbstractHandler
{ {
protected $importedRows = 0; protected $importedRows = 0;
/** @var Translator */
protected $translator;
public function __construct(ContainerInterface $container)
{
$this->translator = Translator::getInstance();
parent::__construct($container);
}
public function getImportedRows() public function getImportedRows()
{ {
return $this->importedRows; return $this->importedRows;
} }
protected function checkMandatoryColumns(array $row)
{
$mandatoryColumns = $this->getMandatoryColumns();
if ($mandatoryColumns != $keys = array_keys($row)) {
throw new \UnexpectedValueException(
$this->translator->trans(
"The following columns are missing: %columns",
[
"%columns" => implode(", ", array_diff($keys, $mandatoryColumns)),
]
)
);
}
}
/**
* @return array The mandatory columns to have for import
*/
abstract protected function getMandatoryColumns();
/** /**
* @param \Thelia\Core\FileFormat\Formatting\FormatterData * @param \Thelia\Core\FileFormat\Formatting\FormatterData
* @return string|array error messages * @return string|array error messages
@@ -35,4 +67,4 @@ abstract class ImportHandler extends AbstractHandler
* The method does the import routine from a FormatterData * The method does the import routine from a FormatterData
*/ */
abstract public function retrieveFromFormatterData(FormatterData $data); abstract public function retrieveFromFormatterData(FormatterData $data);
} }

View File

@@ -63,57 +63,67 @@ class ProductPricesImport extends ImportHandler
$translator = Translator::getInstance(); $translator = Translator::getInstance();
while (null !== $row = $data->popRow()) { while (null !== $row = $data->popRow()) {
if (count($row) > 1) {
$obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
if ($obj === null) { $this->checkMandatoryColumns($row);
$errorMessage = $translator->trans(
"The product sale element reference %ref doesn't exist",
[
"%ref" => $row["ref"]
]
);
$errors[] = $errorMessage ; $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
} else {
$currency = null; if ($obj === null) {
$errorMessage = $translator->trans(
"The product sale element reference %ref doesn't exist",
[
"%ref" => $row["ref"]
]
);
if (isset($row["currency"])) { $errors[] = $errorMessage ;
$currency = CurrencyQuery::create()->findOneByCode($row["currency"]); } else {
}
if ($currency === null) { $currency = null;
$currency = Currency::getDefaultCurrency();
}
$price = ProductPriceQuery::create() if (isset($row["currency"])) {
->filterByProductSaleElementsId($obj->getId()) $currency = CurrencyQuery::create()->findOneByCode($row["currency"]);
->findOneByCurrencyId($currency->getId())
;
if ($price === null) {
$price = new ProductPrice();
$price
->setProductSaleElements($obj)
->setCurrency($currency)
;
}
$price->setPrice($row["price"]);
if (isset($row["promo_price"])) {
$price->setPromoPrice($row["promo_price"]);
}
$price->save();
$this->importedRows++;
} }
if ($currency === null) {
$currency = Currency::getDefaultCurrency();
}
$price = ProductPriceQuery::create()
->filterByProductSaleElementsId($obj->getId())
->findOneByCurrencyId($currency->getId())
;
if ($price === null) {
$price = new ProductPrice();
$price
->setProductSaleElements($obj)
->setCurrency($currency)
;
}
$price->setPrice($row["price"]);
if (isset($row["promo_price"])) {
$price->setPromoPrice($row["promo_price"]);
}
$price->save();
$this->importedRows++;
} }
} }
return $errors; return $errors;
} }
/**
* @return array The mandatory columns to have for import
*/
protected function getMandatoryColumns()
{
return ["ref", "price"];
}
} }

View File

@@ -11,7 +11,6 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\ImportExport\Import\Type; namespace Thelia\ImportExport\Import\Type;
use Propel\Runtime\Collection\ObjectCollection;
use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\FileFormat\Formatting\FormatterData;
use Thelia\Core\Translation\Translator; use Thelia\Core\Translation\Translator;
use Thelia\Core\FileFormat\FormatType; use Thelia\Core\FileFormat\FormatType;
@@ -36,23 +35,28 @@ class ProductStockImport extends ImportHandler
public function retrieveFromFormatterData(FormatterData $data) public function retrieveFromFormatterData(FormatterData $data)
{ {
$errors = []; $errors = [];
$translator = Translator::getInstance();
while (null !== $row = $data->popRow()) { while (null !== $row = $data->popRow()) {
/**
* Check for mandatory columns
*/
$this->checkMandatoryColumns($row);
$obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]); $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
if ($obj === null) { if ($obj === null) {
$errorMessage = $translator->trans( $errors[] = $this->translator->trans(
"The product sale element reference %ref doesn't exist", "The product sale element reference %ref doesn't exist",
[ [
"%ref" => $row["ref"] "%ref" => $row["ref"]
] ]
); );
$errors[] = $errorMessage ;
} else { } else {
$obj->setQuantity($row["stock"])->save(); $obj
->setQuantity($row["stock"])
->setEanCode($row["ean"])
->save()
;
$this->importedRows++; $this->importedRows++;
} }
} }
@@ -60,7 +64,10 @@ class ProductStockImport extends ImportHandler
return $errors; return $errors;
} }
protected function getMandatoryColumns()
{
return ["ref", "stock", "ean"];
}
/** /**
* @return string|array * @return string|array