private function integrateOneLine($fileValues, $timeStart, $beginTest) { try { $em = $this->getDoctrine()->getManager(); $elementTimeStart = microtime(true); $columnScanOffset = 0; $masterValues = []; //seront stockées ici les valeurs master du produit lu dans le fichier $optionnalValues = []; //seront stockées ici les valeurs optionnelles du produits lu dans le fichier //Récupération de la structure master obligatoire attendue $masterStructure = $this->importFile->getStructure()->getItem('master'); //Lecture des colonnes du fichier / tests et stockage des valeurs foreach ($masterStructure->getColumnNames() as $columnName) { $readValue = $fileValues[$columnScanOffset]; $readValue = preg_replace('#^€\\s+(\\d+)[,|\\.](\\d+)$#', '$1.$2', $readValue); $readValue = preg_replace('#^(\\d+)[,|\\.](\\d+)\\s+€$#', '$1.$2', $readValue); $readValue = preg_replace('#^€(\\d+),(\\d+)$#', '$1.$2', $readValue); $readValue = preg_replace('#^(\\d+),(\\d+)€$#', '$1.$2', $readValue); //test si une valeur obligatoire est vide if ($columnName != "description" and (null == $readValue or $readValue == '')) { throw new \Exception("Import annulé. La cellule ligne n°" . $this->currentRow . " colonne n°" . ($columnScanOffset + 1) . " ne peut être nulle ou vide."); } //test si min/max/ht est numeric if (($columnName == "min" or $columnName == "max" or $columnName == "ht") and (!is_numeric($readValue) or $readValue <= 0)) { throw new \Exception("Import annulé. La cellule ligne n°" . $this->currentRow . " colonne n°" . ($columnScanOffset + 1) . "(" . $readValue . ") doit être numérique et plus grand que 0."); } $masterValues[$columnName] = $readValue; $columnScanOffset++; } //Unsetting unused var unset($columnName); unset($readValue); //test de la validité du range (min <= max) if ($masterValues['min'] > $masterValues['max']) { throw new \Exception("Import annulé. La quantité mini ligne n°" . $this->currentRow . " doit être inférieur ou égale à la quantité maxi."); } //Structure mineure (pas de tests car non bloquant) $optionnalStructure = $this->importFile->getFindItems(); $optionnalValues = []; foreach ($optionnalStructure as $keyStructure => $structure) { $optionnalValues[$keyStructure] = []; foreach ($structure->getValues() as $scanOption) { $scanOptionOffset = 0; $option = []; foreach ($this->importFile->getStructure()->getItem($keyStructure)->getColumnNames() as $optionItemName) { $option[$optionItemName] = $fileValues[$scanOption + $scanOptionOffset]; $scanOptionOffset++; } $optionnalValues[$keyStructure][] = $option; //Unsetting unused var unset($optionItemName); } //Unsetting unused var unset($scanOption); unset($scanOptionOffset); unset($option); } //Unsetting unused var unset($keyStructure); unset($structure); //L'objet Product de la ligne CSV est le suivant $product = array('masterStructure' => $masterValues, 'optionnalStructure' => $optionnalValues); //On va le fluscher en base //On commence par Tester l'existence du produit, le creer si besoin // et mettre à jour ses ranges de prix try { $existProduct = $em->getRepository('EcortexProductManagerBundle:Product')->getByRefAndProvider($product['masterStructure']['ref'], $this->providerId); $flag_range_ok = false; foreach ($existProduct->getRangePrices() as $RPrice) { //Si Range min max correspondent au product on met à jour celui-là et on sort if ($product['masterStructure']['min'] == $RPrice->getMin() and $product['masterStructure']['max'] == $RPrice->getMax()) { $RPrice->setPrice(number_format($product['masterStructure']['ht'], 2, '.', '')); $flag_range_ok = true; break; } //Si min et max < Range min on sort if ($product['masterStructure']['min'] < $RPrice->getMin() and $product['masterStructure']['max'] < $RPrice->getMin()) { continue; } //Si min et max > Range max on sort if ($product['masterStructure']['min'] > $RPrice->getMax() and $product['masterStructure']['max'] > $RPrice->getMax()) { continue; } //Dans tous les autres cas throw new \Exception("Les quantités Min/Max de l'article " . $product['masterStructure']['produit'] . " référence: " . $product['masterStructure']['ref'] . " (ligne " . ($this->currentRow + 1) . " ) chevauchent ou incluent une plage déjà existante en base de donnée.\n Merci de modifier le fichier à importer ou l'article en base"); } //Unsetting unused var unset($RPrice); if (!$flag_range_ok) { $newRangePrice = new RangePrice(); $newRangePrice->setMin((int) $product['masterStructure']['min']); $newRangePrice->setMax((int) $product['masterStructure']['max']); $newRangePrice->setPrice(number_format($product['masterStructure']['ht'], 2, '.', '')); $em->persist($newRangePrice); $existProduct->addRangePrice($newRangePrice); } //traitement si tout est ok $existProduct->setName($product['masterStructure']['produit']); $product['masterStructure']['description'] != null ? $existProduct->setDescription($product['masterStructure']['description']) : false; //On flush le produit avec son range $em->flush(); //Unsetting unused var unset($flag_range_ok); unset($newRangePrice); unset($existProduct); } catch (NoResultException $e) { $newProduct = new Product(); $newProduct->setName($product['masterStructure']['produit']); $newProduct->setRef($product['masterStructure']['ref']); $newProduct->setDescription($product['masterStructure']['description']); $newProduct->setProvider($em->getRepository('EcortexProductManagerBundle:Provider')->findOneById($this->providerId)); //le rangePrice est forcement nouveau $newRangePrice = new RangePrice(); $newRangePrice->setMin((int) $product['masterStructure']['min']); $newRangePrice->setMax((int) $product['masterStructure']['max']); $newRangePrice->setPrice(number_format($product['masterStructure']['ht'], 2, '.', '')); $newProduct->addRangePrice($newRangePrice); $em->persist($newProduct); //On flush le produit avec son range $em->flush(); //Unsetting unused var unset($newRangePrice); unset($newProduct); } //Une fois flushé on recupere le Range en cours // pour mettre à jour les options //Quelques règles: le nom indice 0 et la valeur sont systématiquement mb_strtolower UTF-8 et price=0 si null ou '' try { $currentRange = $em->getRepository('EcortexProductManagerBundle:RangePrice')->getByProductRefProviderIdMinMax($product['masterStructure']['ref'], $this->providerId, $product['masterStructure']['min'], $product['masterStructure']['max']); $matchId = []; foreach ($product['optionnalStructure']['option'] as $option) { //On met à jour ou on insere l'option si elle porte un nom et une valeur if (null != $option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[0]] and null != $option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[1]]) { $match_Option_ok = false; //Mise à jour foreach ($currentRange->getProductOptions() as $productOption) { if ($productOption->getName() == mb_strtolower($option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[0]], 'UTF-8') and $productOption->getValue() == mb_strtolower($option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[1]], 'UTF-8')) { if (is_null($option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[2]]) or $option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[2]] == '') { $option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[2]] = 0; } $productOption->setPrice(number_format($option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[2]], 2, '.', '')); $matchId[] = $productOption->getId(); $match_Option_ok = true; } } $em->flush(); //Unsetting unused var unset($productOption); //Creation si inexistante if (!$match_Option_ok) { $newProductOption = new ProductOption(); $newProductOption->setName(mb_strtolower($option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[0]], 'UTF-8')); $newProductOption->setValue(mb_strtolower($option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[1]], 'UTF-8')); $newProductOption->setPrice($option[$this->importFile->getStructure()->getItem('option')->getColumnNames()[2]]); $em->persist($newProductOption); $currentRange->addProductOption($newProductOption); $em->flush(); //si insertion on recup l'ID $matchId[] = $newProductOption->getId(); } //Unsetting unused var unset($newProductOption); } //Unsetting unused var unset($match_Option_ok); } //Unsetting unused var unset($option); //Suppresion des option non matchées foreach ($currentRange->getProductOptions() as $productOption) { if (!in_array($productOption->getId(), $matchId)) { $em->remove($productOption); } } $em->flush(); //Unsetting unused var unset($productOption); unset($matchId); unset($currentRange); } catch (NoResultException $e) { throw new \Exception("Erreur lors de la lecture en Base du produit " . $product['masterStructure']['produit'] . " référence: " . $product['masterStructure']['ref'] . " (ligne " . $this->currentRow . " )"); } //Maintenant on met à jour les Tags //recupération de tous les tags de la ligne try { $currentRange = $em->getRepository('EcortexProductManagerBundle:RangePrice')->getByProductRefProviderIdMinMax($product['masterStructure']['ref'], $this->providerId, $product['masterStructure']['min'], $product['masterStructure']['max']); $tagArray = []; foreach ($product['optionnalStructure']['tag'] as $tags) { if (null != $tags[$this->importFile->getStructure()->getItem('tag')->getColumnNames()[0]] and $tags[$this->importFile->getStructure()->getItem('tag')->getColumnNames()[0]] != '') { $tagArray[] = mb_strtolower($tags[$this->importFile->getStructure()->getItem('tag')->getColumnNames()[0]], 'UTF-8'); } } //Unsetting unused var unset($tags); $tagArray = array_unique($tagArray); //Recuperation de tous les tag du RangePrice $existTags = []; foreach ($currentRange->getTags() as $existTag) { $existTags[] = $existTag->getName(); } //Unsetting unused var unset($existTag); //On test si les tag du fichiers sont associés, sinon on le crée si besoin puis on associe foreach ($tagArray as $tag) { if (!in_array($tag, $existTags)) { if ($findTag = $em->getRepository('EcortexProductManagerBundle:Tag')->findOneByName($tag)) { $currentRange->addTag($findTag); } else { $newTag = new Tag(); $newTag->setName($tag); $em->persist($newTag); $currentRange->addTag($newTag); } $em->flush(); //Unsetting unused var unset($findTag); unset($newTag); } } //Unsetting unused var unset($tag); //A l'envers maintenant: si des tags sont présents dans Range mais pas dans le fichier on remove foreach ($currentRange->getTags() as $existTag) { if (!in_array($existTag->getName(), $tagArray)) { $currentRange->removeTag($existTag); } } $em->flush(); //Unsetting unused var unset($existTag); unset($tagArray); unset($existTags); unset($currentRange); } catch (NoResultException $e) { throw new \Exception("Erreur lors de la lecture en Base du produit " . $product['masterStructure']['produit'] . " référence: " . $product['masterStructure']['ref'] . " (ligne " . $this->currentRow . " )"); } $actualTime = microtime(true); $elapseTime = $actualTime - $timeStart; $averageTime = $elapseTime / ($this->currentRow - ($beginTest - 1)); $remaining = ($this->importFile->getLineCount() - $this->currentRow) * $averageTime; $elementTime = $actualTime - $elementTimeStart; $progress = json_encode(array('percent' => (int) ($this->currentRow / $this->importFile->getLineCount() * 100), 'currentRow' => $this->currentRow, 'totalRow' => $this->importFile->getLineCount(), 'elapseTime' => $elapseTime, 'averageTime' => $averageTime, 'remaining' => $remaining, 'elementTime' => $elementTime)); $varGate = $em->getRepository('EcortexProductManagerBundle:VarGate')->findOneBySessid($this->varGateSessid); $varGate->setValue($progress); $em->flush(); //Unsetting unused var unset($elapseTime); unset($actualTime); unset($averageTime); unset($elementTimeStart); unset($elementTime); unset($remaining); unset($progress); unset($product); unset($masterStructure); unset($columnScanOffset); unset($masterValues); unset($optionnalStructure); unset($optionnalValues); $em->clear(); return $e = false; } catch (\Exception $e) { throw new \Exception($e->getMessage()); } }
/** * Add rangePrices * * @param \Ecortex\ProductManagerBundle\Entity\RangePrice $rangePrices * @return Product */ public function addRangePrice(\Ecortex\ProductManagerBundle\Entity\RangePrice $rangePrices) { $this->rangePrices[] = $rangePrices; $rangePrices->setProduct($this); return $this; }