public function buildModelCriteria() { $search = OrderCouponQuery::create(); $order = $this->getOrder(); $search->filterByOrderId($order, Criteria::EQUAL)->orderById(Criteria::ASC); return $search; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see OrderCoupon::setDeleted() * @see OrderCoupon::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(OrderCouponTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildOrderCouponQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
/** * Returns the number of related OrderCoupon objects. * * @param Criteria $criteria * @param boolean $distinct * @param ConnectionInterface $con * @return int Count of related OrderCoupon objects. * @throws PropelException */ public function countOrderCoupons(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) { $partial = $this->collOrderCouponsPartial && !$this->isNew(); if (null === $this->collOrderCoupons || null !== $criteria || $partial) { if ($this->isNew() && null === $this->collOrderCoupons) { return 0; } if ($partial && !$criteria) { return count($this->getOrderCoupons()); } $query = ChildOrderCouponQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByOrder($this)->count($con); } return count($this->collOrderCoupons); }
/** * Get the associated ChildOrderCoupon object * * @param ConnectionInterface $con Optional Connection object. * @return ChildOrderCoupon The associated ChildOrderCoupon object. * @throws PropelException */ public function getOrderCoupon(ConnectionInterface $con = null) { if ($this->aOrderCoupon === null && $this->coupon_id !== null) { $this->aOrderCoupon = ChildOrderCouponQuery::create()->findPk($this->coupon_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aOrderCoupon->addOrderCouponModules($this); */ } return $this->aOrderCoupon; }
/** * Performs an INSERT on the database, given a OrderCoupon or Criteria object. * * @param mixed $criteria Criteria or OrderCoupon object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(OrderCouponTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from OrderCoupon object } if ($criteria->containsKey(OrderCouponTableMap::ID) && $criteria->keyContainsValue(OrderCouponTableMap::ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . OrderCouponTableMap::ID . ')'); } // Set the correct dbName $query = OrderCouponQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * Gets the number of ChildOrderCoupon objects related by a many-to-many relationship * to the current object by way of the order_coupon_module cross-reference table. * * @param Criteria $criteria Optional query object to filter the query * @param boolean $distinct Set to true to force count distinct * @param ConnectionInterface $con Optional connection object * * @return int the number of related ChildOrderCoupon objects */ public function countOrderCoupons($criteria = null, $distinct = false, ConnectionInterface $con = null) { if (null === $this->collOrderCoupons || null !== $criteria) { if ($this->isNew() && null === $this->collOrderCoupons) { return 0; } else { $query = ChildOrderCouponQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByModule($this)->count($con); } } else { return count($this->collOrderCoupons); } }
/** * Cancels order coupons usage when order is canceled or refunded, * or use canceled coupons again if the order is no longer canceled or refunded * * @param OrderEvent $event * @param string $eventName * @param EventDispatcherInterface $dispatcher * @throws \Exception * @throws \Propel\Runtime\Exception\PropelException */ public function orderStatusChange(OrderEvent $event, $eventName, EventDispatcherInterface $dispatcher) { // The order has been canceled or refunded ? if ($event->getOrder()->isCancelled() || $event->getOrder()->isRefunded()) { // Cancel usage of all coupons for this order $usedCoupons = OrderCouponQuery::create()->filterByUsageCanceled(false)->findByOrderId($event->getOrder()->getId()); $customerId = $event->getOrder()->getCustomerId(); /** @var OrderCoupon $usedCoupon */ foreach ($usedCoupons as $usedCoupon) { if (null !== ($couponModel = CouponQuery::create()->findOneByCode($usedCoupon->getCode()))) { // If the coupon still exists, restore one usage to the usage count. $this->couponManager->incrementQuantity($couponModel, $customerId); } // Mark coupon usage as canceled in the OrderCoupon table $usedCoupon->setUsageCanceled(true)->save(); } } else { // Mark canceled coupons for this order as used again $usedCoupons = OrderCouponQuery::create()->filterByUsageCanceled(true)->findByOrderId($event->getOrder()->getId()); $customerId = $event->getOrder()->getCustomerId(); /** @var OrderCoupon $usedCoupon */ foreach ($usedCoupons as $usedCoupon) { if (null !== ($couponModel = CouponQuery::create()->findOneByCode($usedCoupon->getCode()))) { // If the coupon still exists, mark the coupon as used $this->couponManager->decrementQuantity($couponModel, $customerId); } // The coupon is no longer canceled $usedCoupon->setUsageCanceled(false)->save(); } } }
public function testQuery() { $container = new Container(); new Translator($container); $handler = new OrderExport($container); $lang = Lang::getDefaultLanguage(); $locale = $lang->getLocale(); $data = $handler->buildData($lang)->getData(); $ordersProductQuery = OrderProductQuery::create(); $orders = OrderQuery::create()->find(); $count = $ordersProductQuery->count(); $this->assertEquals(count($data), $count); /** * For the rest of the test, 50 orders are much enough */ if ($count > 50) { $count = 50; } $current = 0; for ($i = 0; $i < $count; ++$current) { $row = $data[$i]; /** @var \Thelia\Model\Order $order */ $order = $orders->get($current); $this->assertEquals($ref = $order->getRef(), $row["ref"]); $this->assertEquals($order->getCustomer()->getRef(), $row["customer_ref"]); $coupons = OrderCouponQuery::create()->filterByOrder($order)->select(OrderCouponTableMap::TITLE)->find()->toArray(); $coupons = implode(",", $coupons); $this->assertTrue(empty($coupons) ? empty($row["coupons"]) : $coupons === $row["coupons"]); $this->assertEquals($order->getCreatedAt()->format($lang->getDatetimeFormat()), $row["date"]); $this->assertEquals($order->getCurrency()->getCode(), $row["currency"]); $this->assertEquals($order->getCustomer()->getRef(), $row["customer_ref"]); $this->assertEquals($order->getOrderStatus()->setLocale($locale)->getTitle(), $row["status"]); $this->assertEquals($order->getDeliveryRef(), $row["delivery_ref"]); $this->assertEquals($order->getModuleRelatedByDeliveryModuleId()->getCode(), $row["delivery_module"]); $this->assertEquals($order->getInvoiceRef(), $row["invoice_ref"]); $this->assertEquals($order->getModuleRelatedByPaymentModuleId()->getCode(), $row["payment_module"]); $this->assertEquals($order->getTotalAmount($tax, false, false), $row["total_including_taxes"]); $this->assertEquals($order->getTotalAmount($tax, false, true), $row["total_with_discount"]); $this->assertEquals($order->getTotalAmount($tax, true, true), $row["total_discount_and_postage"]); $invoiceAddress = $order->getOrderAddressRelatedByInvoiceOrderAddressId(); $deliveryAddress = $order->getOrderAddressRelatedByDeliveryOrderAddressId(); $addresses = ["delivery" => $deliveryAddress, "invoice" => $invoiceAddress]; /** @var \Thelia\Model\OrderAddress $address */ foreach ($addresses as $prefix => $address) { $this->assertEquals($address->getCustomerTitle()->setLocale($locale)->getShort(), $row[$prefix . "_title"]); $this->assertEquals($address->getAddress1(), $row[$prefix . "_address1"]); $this->assertEquals($address->getAddress2(), $row[$prefix . "_address2"]); $this->assertEquals($address->getAddress3(), $row[$prefix . "_address3"]); $this->assertEquals($address->getCity(), $row[$prefix . "_city"]); $this->assertEquals($address->getZipcode(), $row[$prefix . "_zip_code"]); $this->assertEquals($address->getCompany(), $row[$prefix . "_company"]); $this->assertEquals($address->getFirstname(), $row[$prefix . "_first_name"]); $this->assertEquals($address->getLastname(), $row[$prefix . "_last_name"]); $this->assertEquals($address->getCountry()->setLocale($locale)->getTitle(), $row[$prefix . "_country"]); $this->assertEquals($address->getPhone(), $row[$prefix . "_phone"]); } while ($data[$i]["ref"] === $ref) { /** @var \Thelia\Model\OrderProduct $product */ $product = OrderProductQuery::create()->filterByTitle($data[$i]["product_title"])->filterByTaxRuleTitle($data[$i]["tax_title"])->filterByWasInPromo($data[$i]["was_in_promo"])->_if((bool) (int) $data[$i]["was_in_promo"])->filterByPromoPrice($data[$i]["price"])->_else()->filterByPrice($data[$i]["price"])->_endif()->filterByQuantity($data[$i]["quantity"])->findOne(); $this->assertNotNull($product); $sum = 0; foreach ($product->getOrderProductTaxes() as $tax) { $sum += $product->getWasInPromo() ? $tax->getPromoAmount() : $tax->getAmount(); } $this->assertEquals($sum, $data[$i++]["tax_amount"]); } } }