Skip to content

Commit

Permalink
Added checks to allow more robust import
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Jan 7, 2025
1 parent 6d4dfcc commit 05cf8af
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<descriptive locale="fr_FR">
<title>CSV Importer</title>
</descriptive>
<version>1.0.10</version>
<version>1.0.11</version>
<author>
<name>Thelia</name>
<email>info@thelia.net</email>
Expand Down
28 changes: 27 additions & 1 deletion Service/CsvParserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CsvImporter\Service;

use Symfony\Component\Yaml\Yaml;
use Thelia\Log\Tlog;

class CsvParserService
{
Expand All @@ -27,7 +28,7 @@ public function mapToArray(array $productData): array
}
foreach ($this->mapping as $fieldName => $header) {
$mappedData[$fieldName] = $productData[$header] ?? null;
}
}


return $mappedData;
Expand All @@ -50,4 +51,29 @@ private function getAttributeColumns(array $productData): array
return $this->getColumns($productData, self::ATTRIBUTE_DISCRIMINATOR);
}

/**
* Issue a warning for each missing column in a file header
*
* @param array $headers
* @return void
*/
public function checkHeaders(array $headers): void
{
foreach ($this->mapping as $fieldName => $headerLabel) {
if (! in_array($headerLabel, $headers, true)) {
Tlog::getInstance()->warning("Column \"$headerLabel\" not found, some data could be missing after import");
}
}

foreach ($headers as $headerLabel) {
if (!in_array($headerLabel, $this->mapping, true)
&&
!str_starts_with($headerLabel, self::ATTRIBUTE_DISCRIMINATOR)
&&
!str_starts_with($headerLabel, self::FEATURE_DISCRIMINATOR)
) {
Tlog::getInstance()->warning("Found additional \"$headerLabel\" column. This column will be ignored.");
}
}
}
}
30 changes: 26 additions & 4 deletions Service/CsvProductImporterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace CsvImporter\Service;

use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Exception\PropelException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -35,6 +36,7 @@
use Thelia\Core\Event\Template\TemplateAddFeatureEvent;
use Thelia\Core\Event\Template\TemplateCreateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Exception\TheliaProcessException;
use Thelia\Files\FileManager;
use Thelia\Log\Tlog;
use Thelia\Model\Attribute;
Expand Down Expand Up @@ -169,6 +171,9 @@ public function importProductsFromCsv(string $filePath, string $basedir, Country
$line = 0;

$headers = fgetcsv($handle);

$this->csvParser->checkHeaders($headers);

while (($data = fgetcsv($handle)) !== false) {
$line++;

Expand Down Expand Up @@ -358,6 +363,17 @@ private function findOrCreateProduct(array $productData, Country $country, strin
Tlog::getInstance()->addInfo('Created product ' . $productData[self::TITLE_COLUMN]);
}

// Si un produit avec la même ref existe, refuser la modification
if (ProductQuery::create()
->filterById($product->getId(), Criteria::NOT_EQUAL)
->filterByRef($productData[self::REF_COLUMN])
->count() > 0
) {
throw new TheliaProcessException(
'Failed to import product ref. ' . $productData[self::REF_COLUMN] . ': a product with the same ref. already exists.'
);
}

return $this->dispatchProductEvent(new ProductUpdateEvent($product->getId()), $productData, $locale, $category, $country);
}

Expand All @@ -377,8 +393,8 @@ private function dispatchProductEvent(
->setLocale($locale)
->setTitle($productData[self::TITLE_COLUMN])
->setDefaultCategory($category->getId())
->setBasePrice($productData[self::PRICE_EXCL_TAX_COLUMN])
->setBaseWeight($productData[self::WEIGHT_COLUMN] ?? 0)
->setBasePrice($this->cleanupNumber($productData[self::PRICE_EXCL_TAX_COLUMN]))
->setBaseWeight($this->cleanupNumber($productData[self::WEIGHT_COLUMN] ?? 0))
->setTaxRuleId($this->findOrCreateTax($productData, $country, $locale)->getId())
->setVisible(1)
->setCurrencyId(1);
Expand Down Expand Up @@ -449,9 +465,10 @@ private function createOrUpdateProductSaleElements(Product $product, array $prod
}
}
$event = new ProductSaleElementUpdateEvent($product, $productSaleElement->getId());
$event->setWeight($productData[self::WEIGHT_COLUMN])
$event
->setWeight($this->cleanupNumber($productData[self::WEIGHT_COLUMN]))
->setProductSaleElement($productSaleElement)
->setPrice($productData[self::PRICE_EXCL_TAX_COLUMN])
->setPrice($this->cleanupNumber($productData[self::PRICE_EXCL_TAX_COLUMN]))
->setEanCode($productData[self::EAN_COLUMN])
->setReference($productData[self::REF_COLUMN])
->setCurrencyId(1)
Expand Down Expand Up @@ -724,4 +741,9 @@ private function copyFile(string $filePath): string

return $newFilePath;
}

protected function cleanupNumber(string $number)
{
return (float) preg_replace("/[^0-9.,]/", '', str_replace(',', '.', $number));
}
}

0 comments on commit 05cf8af

Please sign in to comment.