public function testUpdatePseSale() { $sale = $this->getRandomSale(); $date = new \DateTime(); $product = ProductQuery::create()->findOne(); $attrAv = AttributeAvQuery::create()->findOne(); $event = new SaleUpdateEvent($sale->getId()); $event->setStartDate($date->setTimestamp(strtotime("today - 1 month")))->setEndDate($date->setTimestamp(strtotime("today + 1 month")))->setActive(1)->setDisplayInitialPrice(1)->setPriceOffsetType(SaleModel::OFFSET_TYPE_AMOUNT)->setPriceOffsets([CurrencyQuery::create()->findOne()->getId() => 10])->setProducts([$product->getId()])->setProductAttributes([$product->getId() => [$attrAv->getId()]])->setLocale('en_US')->setTitle('test update sale title')->setChapo('test update sale short description')->setDescription('test update sale description')->setPostscriptum('test update sale postscriptum')->setSaleLabel('test create sale label'); $saleAction = new Sale($this->getMockEventDispatcher()); $saleAction->update($event, null, $this->getMockEventDispatcher()); $updatedSale = $event->getSale(); $this->assertInstanceOf('Thelia\\Model\\Sale', $updatedSale); $this->assertEquals(1, $updatedSale->getActive()); $this->assertEquals('test update sale title', $updatedSale->getTitle()); $this->assertEquals('test update sale short description', $updatedSale->getChapo()); $this->assertEquals('test update sale description', $updatedSale->getDescription()); $this->assertEquals('test update sale postscriptum', $updatedSale->getPostscriptum()); $this->assertEquals('test create sale label', $updatedSale->getSaleLabel()); }
/** * Process update sale * * @param SaleUpdateEvent $event * @param $eventName * @param EventDispatcherInterface $dispatcher * @throws PropelException */ public function update(SaleUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher) { if (null !== ($sale = SaleQuery::create()->findPk($event->getSaleId()))) { $sale->setDispatcher($dispatcher); $con = Propel::getWriteConnection(SaleTableMap::DATABASE_NAME); $con->beginTransaction(); try { // Disable all promo flag on sale's currently selected products, // to reset promo status of the products that have been removed from the selection. $sale->setActive(false); $now = new \DateTime(); $startDate = $event->getStartDate(); $endDate = $event->getEndDate(); $update = $startDate <= $now && $now <= $endDate; if ($update) { $dispatcher->dispatch(TheliaEvents::UPDATE_PRODUCT_SALE_STATUS, new ProductSaleStatusUpdateEvent($sale)); } $sale->setActive($event->getActive())->setStartDate($startDate)->setEndDate($endDate)->setPriceOffsetType($event->getPriceOffsetType())->setDisplayInitialPrice($event->getDisplayInitialPrice())->setLocale($event->getLocale())->setSaleLabel($event->getSaleLabel())->setTitle($event->getTitle())->setDescription($event->getDescription())->setChapo($event->getChapo())->setPostscriptum($event->getPostscriptum())->save($con); $event->setSale($sale); // Update price offsets SaleOffsetCurrencyQuery::create()->filterBySaleId($sale->getId())->delete($con); foreach ($event->getPriceOffsets() as $currencyId => $priceOffset) { $saleOffset = new SaleOffsetCurrency(); $saleOffset->setCurrencyId($currencyId)->setSaleId($sale->getId())->setPriceOffsetValue($priceOffset)->save($con); } // Update products SaleProductQuery::create()->filterBySaleId($sale->getId())->delete($con); $productAttributesArray = $event->getProductAttributes(); foreach ($event->getProducts() as $productId) { if (isset($productAttributesArray[$productId])) { foreach ($productAttributesArray[$productId] as $attributeId) { $saleProduct = new SaleProduct(); $saleProduct->setSaleId($sale->getId())->setProductId($productId)->setAttributeAvId($attributeId)->save($con); } } else { $saleProduct = new SaleProduct(); $saleProduct->setSaleId($sale->getId())->setProductId($productId)->setAttributeAvId(null)->save($con); } } if ($update) { // Update related products sale status $dispatcher->dispatch(TheliaEvents::UPDATE_PRODUCT_SALE_STATUS, new ProductSaleStatusUpdateEvent($sale)); } $con->commit(); } catch (PropelException $e) { $con->rollback(); throw $e; } } }
/** * Creates the update event with the provided form data * * @param array $formData * @return SaleUpdateEvent */ protected function getUpdateEvent($formData) { // Build the product attributes array $productAttributes = []; foreach ($formData['product_attributes'] as $productId => $attributeAvIdList) { if (!empty($attributeAvIdList)) { $productAttributes[$productId] = explode(',', $attributeAvIdList); } } $saleUpdateEvent = new SaleUpdateEvent($formData['id']); $saleUpdateEvent->setStartDate($formData['start_date'])->setEndDate($formData['end_date'])->setActive($formData['active'])->setDisplayInitialPrice($formData['display_initial_price'])->setPriceOffsetType($formData['price_offset_type'])->setPriceOffsets($formData['price_offset'])->setProducts($formData['products'])->setProductAttributes($productAttributes)->setLocale($formData['locale'])->setTitle($formData['title'])->setSaleLabel($formData['label'])->setChapo($formData['chapo'])->setDescription($formData['description'])->setPostscriptum($formData['postscriptum']); return $saleUpdateEvent; }