public function importData(array $data)
 {
     $pse = ProductSaleElementsQuery::create()->findPk($data['id']);
     if ($pse === null) {
         return Translator::getInstance()->trans('The product sale element id %id doesn\'t exist', ['%id' => $data['id']]);
     } else {
         $currency = null;
         if (isset($data['currency'])) {
             $currency = CurrencyQuery::create()->findOneByCode($data['currency']);
         }
         if ($currency === null) {
             $currency = Currency::getDefaultCurrency();
         }
         $price = ProductPriceQuery::create()->filterByProductSaleElementsId($pse->getId())->findOneByCurrencyId($currency->getId());
         if ($price === null) {
             $price = new ProductPrice();
             $price->setProductSaleElements($pse)->setCurrency($currency);
         }
         $price->setPrice($data['price']);
         if (isset($data['promo_price'])) {
             $price->setPromoPrice($data['promo_price']);
         }
         if (isset($data['promo'])) {
             $price->getProductSaleElements()->setPromo((int) $data['promo'])->save();
         }
         $price->save();
         $this->importedRows++;
     }
     return null;
 }
예제 #2
0
 /**
  * @param \Thelia\Core\FileFormat\Formatting\FormatterData
  * @return string|array error messages
  *
  * The method does the import routine from a FormatterData
  */
 public function retrieveFromFormatterData(FormatterData $data)
 {
     $errors = [];
     $translator = Translator::getInstance();
     while (null !== ($row = $data->popRow())) {
         $this->checkMandatoryColumns($row);
         $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
         if ($obj === null) {
             $errorMessage = $translator->trans("The product sale element reference %ref doesn't exist", ["%ref" => $row["ref"]]);
             $errors[] = $errorMessage;
         } else {
             $currency = null;
             if (isset($row["currency"])) {
                 $currency = CurrencyQuery::create()->findOneByCode($row["currency"]);
             }
             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"]);
             }
             if (isset($row["promo"])) {
                 $price->getProductSaleElements()->setPromo((int) $row["promo"])->save();
             }
             $price->save();
             $this->importedRows++;
         }
     }
     return $errors;
 }
예제 #3
0
 /**
  * Update an existing product sale element
  *
  * @param  ProductSaleElementUpdateEvent $event
  * @throws \Exception
  */
 public function update(ProductSaleElementUpdateEvent $event)
 {
     $salesElement = ProductSaleElementsQuery::create()->findPk($event->getProductSaleElementId());
     $con = Propel::getWriteConnection(ProductSaleElementsTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         // Update the product's tax rule
         $event->getProduct()->setTaxRuleId($event->getTaxRuleId())->save($con);
         // If product sale element is not defined, create it.
         if ($salesElement == null) {
             $salesElement = new ProductSaleElements();
             $salesElement->setProduct($event->getProduct());
         }
         // If this PSE is the default one, be sure to have *only one* default for this product
         if ($event->getIsDefault()) {
             ProductSaleElementsQuery::create()->filterByProduct($event->getProduct())->filterByIsDefault(true)->filterById($event->getProductSaleElementId(), Criteria::NOT_EQUAL)->update(['IsDefault' => false], $con);
         }
         // Update sale element
         $salesElement->setRef($event->getReference())->setQuantity($event->getQuantity())->setPromo($event->getOnsale())->setNewness($event->getIsnew())->setWeight($event->getWeight())->setIsDefault($event->getIsDefault())->setEanCode($event->getEanCode())->save();
         // Update/create price for current currency
         $productPrice = ProductPriceQuery::create()->filterByCurrencyId($event->getCurrencyId())->filterByProductSaleElementsId($salesElement->getId())->findOne($con);
         // If price is not defined, create it.
         if ($productPrice == null) {
             $productPrice = new ProductPrice();
             $productPrice->setProductSaleElements($salesElement)->setCurrencyId($event->getCurrencyId());
         }
         // Check if we have to store the price
         $productPrice->setFromDefaultCurrency($event->getFromDefaultCurrency());
         if ($event->getFromDefaultCurrency() == 0) {
             // Store the price
             $productPrice->setPromoPrice($event->getSalePrice())->setPrice($event->getPrice());
         } else {
             // Do not store the price.
             $productPrice->setPromoPrice(0)->setPrice(0);
         }
         $productPrice->save($con);
         // Store all the stuff !
         $con->commit();
     } catch (\Exception $ex) {
         $con->rollback();
         throw $ex;
     }
 }