/**
  * Create a new product sale element, with or without combination
  *
  * @param  ProductSaleElementCreateEvent $event
  * @throws \Exception
  */
 public function create(ProductSaleElementCreateEvent $event)
 {
     $con = Propel::getWriteConnection(ProductSaleElementsTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         // Check if we have a PSE without combination, this is the "default" PSE. Attach the combination to this PSE
         $salesElement = ProductSaleElementsQuery::create()->filterByProductId($event->getProduct()->getId())->joinAttributeCombination(null, Criteria::LEFT_JOIN)->add(AttributeCombinationTableMap::PRODUCT_SALE_ELEMENTS_ID, null, Criteria::ISNULL)->findOne($con);
         if ($salesElement == null) {
             // Create a new default product sale element
             $salesElement = $event->getProduct()->createProductSaleElement($con, 0, 0, 0, $event->getCurrencyId(), false);
         } else {
             // This (new) one is the default
             $salesElement->setIsDefault(true)->save($con);
         }
         // Attach combination, if defined.
         $combinationAttributes = $event->getAttributeAvList();
         if (count($combinationAttributes) > 0) {
             foreach ($combinationAttributes as $attributeAvId) {
                 $attributeAv = AttributeAvQuery::create()->findPk($attributeAvId);
                 if ($attributeAv !== null) {
                     $attributeCombination = new AttributeCombination();
                     $attributeCombination->setAttributeAvId($attributeAvId)->setAttribute($attributeAv->getAttribute())->setProductSaleElements($salesElement)->save($con);
                 }
             }
         }
         $event->setProductSaleElement($salesElement);
         // Store all the stuff !
         $con->commit();
     } catch (\Exception $ex) {
         $con->rollback();
         throw $ex;
     }
 }
 /**
  * @return JsonResponse
  *
  * Create product sale elements
  */
 public function createAction()
 {
     $this->checkAuth(AdminResources::PRODUCT, [], AccessManager::CREATE);
     $baseForm = $this->createForm("thelia.api.product_sale_elements", "form", [], ["validation_groups" => ["create", "Default"], 'csrf_protection' => false, "cascade_validation" => true]);
     $con = Propel::getConnection(ProductSaleElementsTableMap::DATABASE_NAME);
     $con->beginTransaction();
     $createdIds = array();
     try {
         $form = $this->validateForm($baseForm);
         $entries = $form->getData();
         foreach ($entries["pse"] as $entry) {
             $createEvent = new ProductSaleElementCreateEvent(ProductQuery::create()->findPk($entry["product_id"]), $entry["attribute_av"], $entry["currency_id"]);
             $createEvent->bindForm($form);
             $this->dispatch(TheliaEvents::PRODUCT_ADD_PRODUCT_SALE_ELEMENT, $createEvent);
             $this->processUpdateAction($entry, $pse = $createEvent->getProductSaleElement(), $createEvent->getProduct());
             $createdIds[] = $pse->getId();
         }
         $con->commit();
     } catch (\Exception $e) {
         $con->rollBack();
         return new JsonResponse(["error" => $e->getMessage()], 500);
     }
     return new JsonResponse($this->getProductSaleElements(array_merge($this->getRequest()->query->all(), ["id" => implode(",", $createdIds)])), 201);
 }